Add OTEL tracing support for Wiremock + automatic OTEL for Aspire integration (#1418)

* Update aspire to 13.1 (examples + code)

Allows usage of aspire CLI which is very useful for dev in codespaces (for my next PR).

* Add OTEL support

* Initial PR feedback

* PR feedback

* PR feedback

* PR feedback

* Cleanup.

* Cleanup

* Fix

* Fix

* Rename stuff around to be more accurate

* PR feedback

* Update WireMock.Net.OpenTelemetry.csproj

Update <Authors>

* PR feedback parser

* PR feedback package versions

* Status code feedback.

* Update preprocessor directives to to Activity Tracing instead of OpenTelemetry. Is more descriptive.

* Add tests

* Improve tests

---------

Co-authored-by: Stef Heyenrath <Stef.Heyenrath@gmail.com>
This commit is contained in:
Petr Houška
2026-01-18 17:22:36 +01:00
committed by GitHub
parent abe996671e
commit 4525c61847
38 changed files with 2057 additions and 9 deletions

View File

@@ -79,6 +79,22 @@ public class WireMockServerArguments
/// </summary>
public Dictionary<string, string[]> ProtoDefinitions { get; set; } = [];
/// <summary>
/// Gets or sets a value indicating whether OpenTelemetry tracing is enabled.
/// When enabled, WireMock.Net will emit distributed traces for request processing.
/// Default value is <c>false</c>.
/// </summary>
public bool OpenTelemetryEnabled { get; set; }
/// <summary>
/// Gets or sets the OTLP exporter endpoint URL.
/// When set, traces will be exported to this endpoint using the OTLP protocol.
/// Example: "http://localhost:4317" for gRPC or "http://localhost:4318" for HTTP.
/// If not set, the OTLP exporter will use the <c>OTEL_EXPORTER_OTLP_ENDPOINT</c> environment variable,
/// or fall back to the default endpoint (<c>http://localhost:4317</c> for gRPC).
/// </summary>
public string? OpenTelemetryOtlpExporterEndpoint { get; set; }
/// <summary>
/// Add an additional Urls on which WireMock should listen.
/// </summary>
@@ -138,6 +154,20 @@ public class WireMockServerArguments
Add(args, "--WatchStaticMappingsInSubdirectories", "true");
}
if (OpenTelemetryEnabled)
{
// Enable activity tracing (creates System.Diagnostics.Activity objects)
Add(args, "--ActivityTracingEnabled", "true");
// Enable OpenTelemetry exporter
Add(args, "--OpenTelemetryEnabled", "true");
if (!string.IsNullOrEmpty(OpenTelemetryOtlpExporterEndpoint))
{
Add(args, "--OpenTelemetryOtlpExporterEndpoint", OpenTelemetryOtlpExporterEndpoint);
}
}
if (AdditionalUrls.Count > 0)
{
Add(args, "--Urls", $"http://*:{HttpContainerPort} {string.Join(' ', AdditionalUrls)}");

View File

@@ -287,6 +287,40 @@ public static class WireMockServerBuilderExtensions
return wiremock;
}
/// <summary>
/// Configures OpenTelemetry distributed tracing for the WireMock.Net server.
/// This enables automatic trace export to the Aspire dashboard.
/// </summary>
/// <param name="wiremock">The <see cref="IResourceBuilder{WireMockServerResource}"/>.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{WireMockServerResource}"/>.</returns>
/// <remarks>
/// When enabled, WireMock.Net will emit distributed traces for each request processed,
/// including information about:
/// <list type="bullet">
/// <item>HTTP method, URL, and status code</item>
/// <item>Mapping match results and scores</item>
/// <item>Request processing duration</item>
/// </list>
/// The traces will automatically appear in the Aspire dashboard.
/// </remarks>
public static IResourceBuilder<WireMockServerResource> WithOpenTelemetry(this IResourceBuilder<WireMockServerResource> wiremock)
{
Guard.NotNull(wiremock);
// Enable OpenTelemetry in WireMock server arguments
wiremock.Resource.Arguments.OpenTelemetryEnabled = true;
// Use Aspire's standard WithOtlpExporter to configure OTEL environment variables for the container
// This sets OTEL_EXPORTER_OTLP_ENDPOINT which the OTLP exporter reads automatically
var containerBuilder = wiremock as IResourceBuilder<ContainerResource>;
if (containerBuilder != null)
{
containerBuilder.WithOtlpExporter();
}
return wiremock;
}
private static Task<ExecuteCommandResult> OnRunOpenInspectorCommandAsync(IResourceBuilder<WireMockServerResource> builder)
{
WireMockInspector.Inspect(builder.Resource.GetEndpoint().Url);

View File

@@ -0,0 +1,38 @@
// Copyright © WireMock.Net
#if ACTIVITY_TRACING_SUPPORTED
namespace WireMock.Owin.ActivityTracing;
/// <summary>
/// Options for controlling activity tracing in WireMock.Net middleware.
/// These options control the creation of System.Diagnostics.Activity objects
/// but do not require any OpenTelemetry exporter dependencies.
/// </summary>
public class ActivityTracingOptions
{
/// <summary>
/// Gets or sets a value indicating whether to exclude admin interface requests from tracing.
/// Default is <c>true</c>.
/// </summary>
public bool ExcludeAdminRequests { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether to record request body in trace attributes.
/// Default is <c>false</c> due to potential PII concerns.
/// </summary>
public bool RecordRequestBody { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to record response body in trace attributes.
/// Default is <c>false</c> due to potential PII concerns.
/// </summary>
public bool RecordResponseBody { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to record mapping match details in trace attributes.
/// Default is <c>true</c>.
/// </summary>
public bool RecordMatchDetails { get; set; } = true;
}
#endif

View File

@@ -0,0 +1,34 @@
// Copyright © WireMock.Net
#if !ACTIVITY_TRACING_SUPPORTED
using System;
#endif
using WireMock.Settings;
namespace WireMock.Owin.ActivityTracing;
/// <summary>
/// Validator for Activity Tracing configuration.
/// </summary>
internal static class ActivityTracingValidator
{
/// <summary>
/// Validates that Activity Tracing is supported on the current framework.
/// Throws an exception if ActivityTracingOptions is configured on an unsupported framework.
/// </summary>
/// <param name="settings">The WireMock server settings to validate.</param>
/// <exception cref="System.InvalidOperationException">
/// Thrown when ActivityTracingOptions is configured but the current framework does not support System.Diagnostics.Activity.
/// </exception>
public static void ValidateActivityApiPresence(WireMockServerSettings settings)
{
#if !ACTIVITY_TRACING_SUPPORTED
if (settings.ActivityTracingOptions is not null)
{
throw new InvalidOperationException(
"Activity Tracing is not supported on this target framework. " +
"It requires .NET 5.0 or higher which includes System.Diagnostics.Activity support.");
}
#endif
}
}

View File

@@ -0,0 +1,201 @@
// Copyright © WireMock.Net
#if ACTIVITY_TRACING_SUPPORTED
using System;
using System.Collections.Generic;
using System.Diagnostics;
using WireMock.Logging;
namespace WireMock.Owin.ActivityTracing;
/// <summary>
/// Provides an ActivitySource for WireMock.Net distributed tracing.
/// </summary>
public static class WireMockActivitySource
{
/// <summary>
/// The name of the ActivitySource used by WireMock.Net.
/// </summary>
internal const string SourceName = "WireMock.Net";
/// <summary>
/// The ActivitySource instance used for creating tracing activities.
/// </summary>
public static readonly ActivitySource Source = new(SourceName, GetVersion());
private static string GetVersion()
{
return typeof(WireMockActivitySource).Assembly.GetName().Version?.ToString() ?? "1.0.0";
}
/// <summary>
/// Starts a new activity for a WireMock request.
/// </summary>
/// <param name="requestMethod">The HTTP method of the request.</param>
/// <param name="requestPath">The path of the request.</param>
/// <returns>The started activity, or null if tracing is not enabled.</returns>
internal static Activity? StartRequestActivity(string requestMethod, string requestPath)
{
if (!Source.HasListeners())
{
return null;
}
var activity = Source.StartActivity(
$"WireMock {requestMethod} {requestPath}",
ActivityKind.Server
);
return activity;
}
/// <summary>
/// Enriches an activity with request information.
/// </summary>
internal static void EnrichWithRequest(Activity? activity, IRequestMessage request, ActivityTracingOptions? options = null)
{
if (activity == null)
{
return;
}
activity.SetTag(WireMockSemanticConventions.HttpMethod, request.Method);
activity.SetTag(WireMockSemanticConventions.HttpUrl, request.Url);
activity.SetTag(WireMockSemanticConventions.HttpPath, request.Path);
activity.SetTag(WireMockSemanticConventions.HttpHost, request.Host);
if (request.ClientIP != null)
{
activity.SetTag(WireMockSemanticConventions.ClientAddress, request.ClientIP);
}
// Record request body if enabled
if (options?.RecordRequestBody == true && request.Body != null)
{
activity.SetTag(WireMockSemanticConventions.RequestBody, request.Body);
}
}
/// <summary>
/// Enriches an activity with response information.
/// </summary>
internal static void EnrichWithResponse(Activity? activity, IResponseMessage? response, ActivityTracingOptions? options = null)
{
if (activity == null || response == null)
{
return;
}
// StatusCode can be int, HttpStatusCode, or string
var statusCode = response.StatusCode;
int? statusCodeInt = statusCode switch
{
int i => i,
System.Net.HttpStatusCode hsc => (int)hsc,
string s when int.TryParse(s, out var parsed) => parsed,
_ => null
};
if (statusCodeInt.HasValue)
{
activity.SetTag(WireMockSemanticConventions.HttpStatusCode, statusCodeInt.Value);
activity.SetTag("otel.status_description", $"HTTP {statusCodeInt.Value}");
// Set status based on HTTP status code (using standard otel.status_code tag)
if (statusCodeInt.Value >= 400)
{
activity.SetTag("otel.status_code", "ERROR");
}
else
{
activity.SetTag("otel.status_code", "OK");
}
}
// Record response body if enabled
if (options?.RecordResponseBody == true && response.BodyData?.BodyAsString != null)
{
activity.SetTag(WireMockSemanticConventions.ResponseBody, response.BodyData.BodyAsString);
}
}
/// <summary>
/// Enriches an activity with mapping match information.
/// </summary>
internal static void EnrichWithMappingMatch(
Activity? activity,
Guid? mappingGuid,
string? mappingTitle,
bool isPerfectMatch,
double? matchScore)
{
if (activity == null)
{
return;
}
activity.SetTag(WireMockSemanticConventions.MappingMatched, isPerfectMatch);
if (mappingGuid.HasValue)
{
activity.SetTag(WireMockSemanticConventions.MappingGuid, mappingGuid.Value.ToString());
}
if (!string.IsNullOrEmpty(mappingTitle))
{
activity.SetTag(WireMockSemanticConventions.MappingTitle, mappingTitle);
}
if (matchScore.HasValue)
{
activity.SetTag(WireMockSemanticConventions.MatchScore, matchScore.Value);
}
}
/// <summary>
/// Enriches an activity with log entry information (includes response and mapping match info).
/// </summary>
internal static void EnrichWithLogEntry(Activity? activity, ILogEntry logEntry, ActivityTracingOptions? options = null)
{
if (activity == null)
{
return;
}
// Enrich with response
EnrichWithResponse(activity, logEntry.ResponseMessage, options);
// Enrich with mapping match (if enabled)
if (options?.RecordMatchDetails != false)
{
EnrichWithMappingMatch(
activity,
logEntry.MappingGuid,
logEntry.MappingTitle,
logEntry.RequestMatchResult?.IsPerfectMatch ?? false,
logEntry.RequestMatchResult?.TotalScore);
}
// Set request GUID
activity.SetTag(WireMockSemanticConventions.RequestGuid, logEntry.Guid.ToString());
}
/// <summary>
/// Records an exception on the activity.
/// </summary>
internal static void RecordException(Activity? activity, Exception exception)
{
if (activity == null)
{
return;
}
// Use standard OpenTelemetry exception semantic conventions
activity.SetTag("otel.status_code", "ERROR");
activity.SetTag("otel.status_description", exception.Message);
activity.SetTag("exception.type", exception.GetType().FullName);
activity.SetTag("exception.message", exception.Message);
activity.SetTag("exception.stacktrace", exception.ToString());
}
}
#endif

View File

@@ -0,0 +1,34 @@
// Copyright © WireMock.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WireMock.Owin.ActivityTracing;
/// <summary>
/// Semantic convention constants for WireMock.Net tracing attributes.
/// </summary>
internal static class WireMockSemanticConventions
{
// Standard HTTP semantic conventions (OpenTelemetry)
public const string HttpMethod = "http.request.method";
public const string HttpUrl = "url.full";
public const string HttpPath = "url.path";
public const string HttpHost = "server.address";
public const string HttpStatusCode = "http.response.status_code";
public const string ClientAddress = "client.address";
// WireMock-specific attributes
public const string MappingMatched = "wiremock.mapping.matched";
public const string MappingGuid = "wiremock.mapping.guid";
public const string MappingTitle = "wiremock.mapping.title";
public const string MatchScore = "wiremock.match.score";
public const string PartialMappingGuid = "wiremock.partial_mapping.guid";
public const string PartialMappingTitle = "wiremock.partial_mapping.title";
public const string RequestGuid = "wiremock.request.guid";
public const string RequestBody = "wiremock.request.body";
public const string ResponseBody = "wiremock.response.body";
}

View File

@@ -5,6 +5,7 @@ using System.Collections.Concurrent;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Owin.ActivityTracing;
using WireMock.Types;
using WireMock.Util;
using System.Security.Cryptography.X509Certificates;
@@ -90,4 +91,12 @@ internal interface IWireMockMiddlewareOptions
QueryParameterMultipleValueSupport? QueryParameterMultipleValueSupport { get; set; }
public bool ProxyAll { get; set; }
#if ACTIVITY_TRACING_SUPPORTED
/// <summary>
/// Gets or sets the activity tracing options.
/// When set, System.Diagnostics.Activity objects are created for request tracing.
/// </summary>
ActivityTracingOptions? ActivityTracingOptions { get; set; }
#endif
}

View File

@@ -16,6 +16,10 @@ using System.Collections.Generic;
using WireMock.Constants;
using WireMock.Exceptions;
using WireMock.Util;
#if ACTIVITY_TRACING_SUPPORTED
using System.Diagnostics;
using WireMock.Owin.ActivityTracing;
#endif
#if !USE_ASPNETCORE
using IContext = Microsoft.Owin.IOwinContext;
using OwinMiddleware = Microsoft.Owin.OwinMiddleware;
@@ -97,6 +101,40 @@ namespace WireMock.Owin
{
var request = await _requestMapper.MapAsync(ctx.Request, _options).ConfigureAwait(false);
#if ACTIVITY_TRACING_SUPPORTED
// Start activity if ActivityTracingOptions is configured
var tracingEnabled = _options.ActivityTracingOptions is not null;
var excludeAdmin = _options.ActivityTracingOptions?.ExcludeAdminRequests ?? true;
Activity? activity = null;
// Check if we should trace this request (optionally exclude admin requests)
var shouldTrace = tracingEnabled && !(excludeAdmin && request.Path.StartsWith("/__admin/"));
if (shouldTrace)
{
activity = WireMockActivitySource.StartRequestActivity(request.Method, request.Path);
WireMockActivitySource.EnrichWithRequest(activity, request, _options.ActivityTracingOptions);
}
try
{
await InvokeInternalCoreAsync(ctx, request, activity).ConfigureAwait(false);
}
finally
{
activity?.Dispose();
}
#else
await InvokeInternalCoreAsync(ctx, request).ConfigureAwait(false);
#endif
}
#if ACTIVITY_TRACING_SUPPORTED
private async Task InvokeInternalCoreAsync(IContext ctx, RequestMessage request, Activity? activity)
#else
private async Task InvokeInternalCoreAsync(IContext ctx, RequestMessage request)
#endif
{
var logRequest = false;
IResponseMessage? response = null;
(MappingMatcherResult? Match, MappingMatcherResult? Partial) result = (null, null);
@@ -193,6 +231,10 @@ namespace WireMock.Owin
{
_options.Logger.Error($"Providing a Response for Mapping '{result.Match?.Mapping.Guid}' failed. HttpStatusCode set to 500. Exception: {ex}");
response = ResponseMessageBuilder.Create(500, ex.Message);
#if ACTIVITY_TRACING_SUPPORTED
WireMockActivitySource.RecordException(activity, ex);
#endif
}
finally
{
@@ -211,6 +253,11 @@ namespace WireMock.Owin
PartialMatchResult = result.Partial?.RequestMatchResult
};
#if ACTIVITY_TRACING_SUPPORTED
// Enrich activity with response and mapping info
WireMockActivitySource.EnrichWithLogEntry(activity, log, _options.ActivityTracingOptions);
#endif
LogRequest(log, logRequest);
try

View File

@@ -5,6 +5,7 @@ using System.Collections.Concurrent;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Owin.ActivityTracing;
using WireMock.Types;
using WireMock.Util;
using System.Security.Cryptography.X509Certificates;
@@ -106,4 +107,9 @@ internal class WireMockMiddlewareOptions : IWireMockMiddlewareOptions
/// <inheritdoc />
public bool ProxyAll { get; set; }
#if ACTIVITY_TRACING_SUPPORTED
/// <inheritdoc />
public ActivityTracingOptions? ActivityTracingOptions { get; set; }
#endif
}

View File

@@ -2,6 +2,7 @@
using System;
using Stef.Validation;
using WireMock.Owin.ActivityTracing;
using WireMock.Settings;
namespace WireMock.Owin;
@@ -34,6 +35,27 @@ internal static class WireMockMiddlewareOptionsHelper
options.RequestLogExpirationDuration = settings.RequestLogExpirationDuration;
options.SaveUnmatchedRequests = settings.SaveUnmatchedRequests;
// Validate and configure activity tracing
ActivityTracingValidator.ValidateActivityApiPresence(settings);
#if ACTIVITY_TRACING_SUPPORTED
if (settings.ActivityTracingOptions is not null)
{
options.ActivityTracingOptions = new Owin.ActivityTracing.ActivityTracingOptions
{
ExcludeAdminRequests = settings.ActivityTracingOptions.ExcludeAdminRequests,
RecordRequestBody = settings.ActivityTracingOptions.RecordRequestBody,
RecordResponseBody = settings.ActivityTracingOptions.RecordResponseBody,
RecordMatchDetails = settings.ActivityTracingOptions.RecordMatchDetails
};
}
#endif
#if USE_ASPNETCORE
options.AdditionalServiceRegistration = settings.AdditionalServiceRegistration;
options.CorsPolicyOptions = settings.CorsPolicyOptions;
options.ClientCertificateMode = settings.ClientCertificateMode;
options.AcceptAnyClientCertificate = settings.AcceptAnyClientCertificate;
#endif
if (settings.CustomCertificateDefined)
{
options.X509StoreName = settings.CertificateSettings!.X509StoreName;

View File

@@ -412,11 +412,6 @@ public partial class WireMockServer : IWireMockServer
);
#if USE_ASPNETCORE
_options.AdditionalServiceRegistration = _settings.AdditionalServiceRegistration;
_options.CorsPolicyOptions = _settings.CorsPolicyOptions;
_options.ClientCertificateMode = _settings.ClientCertificateMode;
_options.AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate;
_httpServer = new AspNetCoreSelfHost(_options, urlOptions);
#else
_httpServer = new OwinSelfHost(_options, urlOptions);

View File

@@ -85,6 +85,7 @@ public static class WireMockServerSettingsParser
ParseProxyAndRecordSettings(settings, parser);
ParseCertificateSettings(settings, parser);
ParseHandlebarsSettings(settings, parser);
ParseActivityTracingSettings(settings, parser);
return true;
}
@@ -226,4 +227,19 @@ public static class WireMockServerSettingsParser
};
}
}
private static void ParseActivityTracingSettings(WireMockServerSettings settings, SimpleSettingsParser parser)
{
// Only create ActivityTracingOptions if tracing is enabled
if (parser.GetBoolValue("ActivityTracingEnabled") || parser.GetBoolValue("ActivityTracingOptions__Enabled"))
{
settings.ActivityTracingOptions = new ActivityTracingOptions
{
ExcludeAdminRequests = parser.GetBoolWithDefault("ActivityTracingExcludeAdminRequests", "ActivityTracingOptions__ExcludeAdminRequests", defaultValue: true),
RecordRequestBody = parser.GetBoolValue("ActivityTracingRecordRequestBody") || parser.GetBoolValue("ActivityTracingOptions__RecordRequestBody"),
RecordResponseBody = parser.GetBoolValue("ActivityTracingRecordResponseBody") || parser.GetBoolValue("ActivityTracingOptions__RecordResponseBody"),
RecordMatchDetails = parser.GetBoolWithDefault("ActivityTracingRecordMatchDetails", "ActivityTracingOptions__RecordMatchDetails", defaultValue: true)
};
}
}
}

View File

@@ -52,6 +52,11 @@
<DefineConstants>$(DefineConstants);TRAILINGHEADERS</DefineConstants>
</PropertyGroup>
<!-- Enable Activity tracing support for .NET 5+ where ActivitySource is available -->
<PropertyGroup Condition="'$(TargetFramework)' == 'net5.0' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0'">
<DefineConstants>$(DefineConstants);ACTIVITY_TRACING_SUPPORTED</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Matchers\LinqMatcher.cs" />
</ItemGroup>

View File

@@ -0,0 +1,35 @@
// Copyright © WireMock.Net
using JetBrains.Annotations;
namespace WireMock.OpenTelemetry;
/// <summary>
/// OpenTelemetry exporter configuration options for WireMock.Net.
/// These options control the export of traces to an OTLP endpoint.
/// For controlling what data is recorded in traces, configure ActivityTracingOptions in WireMockServerSettings.
/// </summary>
public class OpenTelemetryOptions
{
/// <summary>
/// Gets or sets a value indicating whether to exclude admin interface requests from ASP.NET Core instrumentation.
/// Default is <c>true</c>.
/// </summary>
/// <remarks>
/// This controls the ASP.NET Core HTTP server instrumentation filter.
/// To also exclude admin requests from WireMock's own activity tracing,
/// set <c>ActivityTracingOptions.ExcludeAdminRequests</c> in WireMockServerSettings.
/// </remarks>
[PublicAPI]
public bool ExcludeAdminRequests { get; set; } = true;
/// <summary>
/// Gets or sets the OTLP exporter endpoint URL.
/// When set, traces will be exported to this endpoint using the OTLP protocol.
/// Example: "http://localhost:4317" for gRPC or "http://localhost:4318" for HTTP.
/// If not set, the OTLP exporter will use the <c>OTEL_EXPORTER_OTLP_ENDPOINT</c> environment variable,
/// or fall back to the default endpoint (<c>http://localhost:4317</c> for gRPC).
/// </summary>
[PublicAPI]
public string? OtlpExporterEndpoint { get; set; }
}

View File

@@ -0,0 +1,44 @@
// Copyright © WireMock.Net
using System.Collections;
using Stef.Validation;
using WireMock.Settings;
namespace WireMock.OpenTelemetry;
/// <summary>
/// A static helper class to parse commandline arguments into OpenTelemetryOptions.
/// </summary>
public static class OpenTelemetryOptionsParser
{
private const string Prefix = "OpenTelemetry";
/// <summary>
/// Parse commandline arguments into OpenTelemetryOptions.
/// </summary>
/// <param name="args">The commandline arguments</param>
/// <param name="environment">The environment settings (optional)</param>
/// <param name="options">The parsed options, or null if OpenTelemetry is not enabled</param>
/// <returns>Always returns true.</returns>
public static bool TryParseArguments(string[] args, IDictionary? environment, out OpenTelemetryOptions? options)
{
Guard.HasNoNulls(args);
var parser = new SimpleSettingsParser();
parser.Parse(args, environment);
if (!parser.GetBoolValue($"{Prefix}Enabled"))
{
options = null;
return true;
}
options = new OpenTelemetryOptions
{
ExcludeAdminRequests = parser.GetBoolValue($"{Prefix}ExcludeAdminRequests", defaultValue: true),
OtlpExporterEndpoint = parser.GetStringValue($"{Prefix}OtlpExporterEndpoint")
};
return true;
}
}

View File

@@ -0,0 +1,160 @@
# WireMock.Net.OpenTelemetry
OpenTelemetry tracing support for WireMock.Net. This package provides instrumentation and OTLP (OpenTelemetry Protocol) exporting capabilities.
## Overview
WireMock.Net automatically creates `System.Diagnostics.Activity` objects for request tracing when `ActivityTracingOptions` is configured (not null). These activities use the built-in .NET distributed tracing infrastructure and are available without any additional dependencies.
This package provides:
- **WireMock.Net instrumentation** - Adds the WireMock.Net ActivitySource to the tracing pipeline
- **ASP.NET Core instrumentation** - Standard HTTP server tracing with request filtering
- **OTLP exporter** - Sends traces to an OpenTelemetry collector
## Installation
```bash
dotnet add package WireMock.Net.OpenTelemetry
```
## Usage
### Option 1: Using AdditionalServiceRegistration (Recommended)
```csharp
using WireMock.OpenTelemetry;
using WireMock.Server;
using WireMock.Settings;
var openTelemetryOptions = new OpenTelemetryOptions
{
ExcludeAdminRequests = true,
OtlpExporterEndpoint = "http://localhost:4317" // Your OTEL collector
};
var settings = new WireMockServerSettings
{
// Setting ActivityTracingOptions (not null) enables activity creation in middleware
ActivityTracingOptions = new ActivityTracingOptions
{
ExcludeAdminRequests = true,
RecordRequestBody = false, // PII concern
RecordResponseBody = false, // PII concern
RecordMatchDetails = true
},
AdditionalServiceRegistration = services =>
{
services.AddWireMockOpenTelemetry(openTelemetryOptions);
}
};
var server = WireMockServer.Start(settings);
```
### Option 2: Custom TracerProvider Configuration
For more control over the tracing configuration:
```csharp
using OpenTelemetry;
using OpenTelemetry.Trace;
using WireMock.OpenTelemetry;
var openTelemetryOptions = new OpenTelemetryOptions();
// Configure your own TracerProvider
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddWireMockInstrumentation(openTelemetryOptions) // Adds WireMock.Net source
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri("http://localhost:4317");
})
.Build();
```
## Extension Methods
### `AddWireMockOpenTelemetry`
Adds full OpenTelemetry tracing to the service collection with instrumentation and OTLP exporter:
```csharp
services.AddWireMockOpenTelemetry(openTelemetryOptions);
```
This configures:
- The WireMock.Net ActivitySource
- ASP.NET Core instrumentation
- OTLP exporter (using the endpoint from `OpenTelemetryOptions.OtlpExporterEndpoint` or the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable)
### `AddWireMockInstrumentation`
Adds WireMock instrumentation to an existing TracerProviderBuilder:
```csharp
tracerProvider.AddWireMockInstrumentation(openTelemetryOptions);
```
## Configuration
### OpenTelemetryOptions (Exporter configuration)
| Property | Description | Default |
|----------|-------------|---------|
| `ExcludeAdminRequests` | Exclude `/__admin/*` from ASP.NET Core instrumentation | `true` |
| `OtlpExporterEndpoint` | OTLP collector endpoint URL | Uses `OTEL_EXPORTER_OTLP_ENDPOINT` env var |
### ActivityTracingOptions (Trace content configuration)
Configured in `WireMockServerSettings.ActivityTracingOptions`:
| Property | Description | Default |
|----------|-------------|---------|
| `ExcludeAdminRequests` | Exclude `/__admin/*` from WireMock activity creation | `true` |
| `RecordRequestBody` | Include request body in trace attributes | `false` |
| `RecordResponseBody` | Include response body in trace attributes | `false` |
| `RecordMatchDetails` | Include mapping match details in trace attributes | `true` |
## Trace Attributes
WireMock.Net traces include these semantic conventions:
**Standard HTTP attributes:**
- `http.request.method`
- `url.full`
- `url.path`
- `server.address`
- `http.response.status_code`
- `client.address`
**WireMock-specific attributes:**
- `wiremock.mapping.matched` - Whether a mapping was found
- `wiremock.mapping.guid` - GUID of the matched mapping
- `wiremock.mapping.title` - Title of the matched mapping
- `wiremock.match.score` - Match score
- `wiremock.request.guid` - GUID of the request
## CLI Arguments
When using WireMock.Net.StandAlone or Docker images, activity tracing and OpenTelemetry can be configured via command-line arguments:
**Activity Tracing (what gets recorded):**
```bash
--ActivityTracingEnabled true
--ActivityTracingExcludeAdminRequests true
--ActivityTracingRecordRequestBody false
--ActivityTracingRecordResponseBody false
--ActivityTracingRecordMatchDetails true
```
**OpenTelemetry Export (where traces are sent):**
```bash
--OpenTelemetryEnabled true
--OpenTelemetryOtlpExporterEndpoint http://localhost:4317
--OpenTelemetryExcludeAdminRequests true
```
## Requirements
- .NET 6.0 or later
- WireMock.Net 1.6.0 or later

View File

@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>OpenTelemetry exporter support for WireMock.Net</Description>
<AssemblyTitle>WireMock.Net.OpenTelemetry</AssemblyTitle>
<Authors>Petr Houška</Authors>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>wiremock;opentelemetry;otel;tracing;telemetry</PackageTags>
<RootNamespace>WireMock</RootNamespace>
<ProjectGuid>{C8F4E6D2-9A3B-4F1C-8D5E-7A2B3C4D5E6F}</ProjectGuid>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<CodeAnalysisRuleSet>../WireMock.Net/WireMock.Net.ruleset</CodeAnalysisRuleSet>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../WireMock.Net/WireMock.Net.snk</AssemblyOriginatorKeyFile>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<!-- OpenTelemetry packages -->
<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.14.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.14.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.14.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.14.0" />
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WireMock.Net.Shared\WireMock.Net.Shared.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,115 @@
// Copyright © WireMock.Net
using System;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
namespace WireMock.OpenTelemetry;
/// <summary>
/// Extension methods for configuring OpenTelemetry tracing for WireMock.Net.
/// </summary>
public static class WireMockOpenTelemetryExtensions
{
private const string ServiceName = "WireMock.Net";
private const string WireMockActivitySourceName = "WireMock.Net";
/// <summary>
/// Adds OpenTelemetry tracing to the WireMock server with instrumentation and OTLP exporter.
/// This configures:
/// - WireMock.Net ActivitySource instrumentation (custom WireMock traces with mapping details)
/// - ASP.NET Core instrumentation (standard HTTP server traces)
/// - OTLP exporter to send traces to a collector
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="options">The OpenTelemetry options containing exporter configuration.</param>
/// <returns>The service collection for chaining.</returns>
public static IServiceCollection AddWireMockOpenTelemetry(
this IServiceCollection services,
OpenTelemetryOptions? options)
{
if (options is null)
{
return services;
}
services.AddOpenTelemetry()
.ConfigureResource(resource =>
{
resource.AddService(
serviceName: ServiceName,
serviceVersion: typeof(WireMockOpenTelemetryExtensions).Assembly.GetName().Version?.ToString() ?? "unknown"
);
})
.WithTracing(tracing =>
{
// Add WireMock-specific traces
tracing.AddSource(WireMockActivitySourceName);
// Add ASP.NET Core instrumentation for standard HTTP server traces
tracing.AddAspNetCoreInstrumentation(aspNetOptions =>
{
// Filter out admin requests if configured
if (options.ExcludeAdminRequests)
{
aspNetOptions.Filter = context =>
{
var path = context.Request.Path.Value ?? string.Empty;
return !path.StartsWith("/__admin", StringComparison.OrdinalIgnoreCase);
};
}
});
// Add OTLP exporter - automatically reads OTEL_EXPORTER_OTLP_ENDPOINT from environment
// If explicit endpoint is specified in options, use that instead
var otlpEndpoint = options.OtlpExporterEndpoint;
if (!string.IsNullOrEmpty(otlpEndpoint))
{
tracing.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(otlpEndpoint);
});
}
else
{
// Use default - reads from OTEL_EXPORTER_OTLP_ENDPOINT env var
tracing.AddOtlpExporter();
}
});
return services;
}
/// <summary>
/// Configures OpenTelemetry tracing builder with WireMock.Net ActivitySource and ASP.NET Core instrumentation.
/// Use this method when you want more control over the TracerProvider configuration.
/// </summary>
/// <param name="tracing">The TracerProviderBuilder to configure.</param>
/// <param name="options">The OpenTelemetry options (optional).</param>
/// <returns>The TracerProviderBuilder for chaining.</returns>
public static TracerProviderBuilder AddWireMockInstrumentation(
this TracerProviderBuilder tracing,
OpenTelemetryOptions? options = null)
{
// Add WireMock-specific traces
tracing.AddSource(WireMockActivitySourceName);
// Add ASP.NET Core instrumentation for standard HTTP server traces
tracing.AddAspNetCoreInstrumentation(aspNetOptions =>
{
// Filter out admin requests if configured
if (options?.ExcludeAdminRequests == true)
{
aspNetOptions.Filter = context =>
{
var path = context.Request.Path.Value ?? string.Empty;
return !path.StartsWith("/__admin", StringComparison.OrdinalIgnoreCase);
};
}
});
return tracing;
}
}

View File

@@ -28,4 +28,4 @@ internal static class DictionaryExtensions
value = default;
return false;
}
}
}

View File

@@ -7,6 +7,7 @@ using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("WireMock.Net.GraphQL, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
[assembly: InternalsVisibleTo("WireMock.Net.ProtoBuf, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
[assembly: InternalsVisibleTo("WireMock.Net.Matchers.CSharpCode, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
[assembly: InternalsVisibleTo("WireMock.Net.OpenTelemetry, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
// [assembly: InternalsVisibleTo("WireMock.Net.StandAlone, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
[assembly: InternalsVisibleTo("WireMock.Net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]

View File

@@ -0,0 +1,42 @@
// Copyright © WireMock.Net
using JetBrains.Annotations;
namespace WireMock.Settings;
/// <summary>
/// Options for controlling activity tracing in WireMock.Net.
/// These options control the creation of System.Diagnostics.Activity objects
/// but do not require any OpenTelemetry exporter dependencies.
/// </summary>
/// <remarks>
/// To export traces to an OpenTelemetry collector, install the WireMock.Net.OpenTelemetry package
/// and configure the exporter using the provided extension methods.
/// </remarks>
[PublicAPI]
public class ActivityTracingOptions
{
/// <summary>
/// Gets or sets a value indicating whether to exclude admin interface requests from activity tracing.
/// Default is <c>true</c>.
/// </summary>
public bool ExcludeAdminRequests { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether to record request body in trace attributes.
/// Default is <c>false</c> due to potential PII concerns.
/// </summary>
public bool RecordRequestBody { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to record response body in trace attributes.
/// Default is <c>false</c> due to potential PII concerns.
/// </summary>
public bool RecordResponseBody { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to record mapping match details in trace attributes.
/// Default is <c>true</c>.
/// </summary>
public bool RecordMatchDetails { get; set; } = true;
}

View File

@@ -104,6 +104,21 @@ internal class SimpleSettingsParser
}, defaultValue);
}
public bool GetBoolWithDefault(string key1, string key2, bool defaultValue)
{
if (Contains(key1))
{
return GetBoolValue(key1);
}
if (Contains(key2))
{
return GetBoolValue(key2);
}
return defaultValue;
}
public bool GetBoolSwitchValue(string name)
{
return Contains(name);
@@ -184,4 +199,4 @@ internal class SimpleSettingsParser
var value = GetValue(name, values => values.FirstOrDefault());
return string.IsNullOrWhiteSpace(value) ? default : JsonUtils.DeserializeObject<T>(value);
}
}
}

View File

@@ -338,4 +338,15 @@ public class WireMockServerSettings
/// </summary>
[PublicAPI]
public HandlebarsSettings? HandlebarsSettings { get; set; }
/// <summary>
/// Gets or sets the activity tracing options.
/// When set (not null), WireMock.Net will create System.Diagnostics.Activity objects for request processing.
/// </summary>
/// <remarks>
/// To export traces to an OpenTelemetry collector, install the WireMock.Net.OpenTelemetry package
/// and configure the exporter using the provided extension methods.
/// </remarks>
[PublicAPI]
public ActivityTracingOptions? ActivityTracingOptions { get; set; }
}

View File

@@ -10,6 +10,9 @@ using WireMock.Exceptions;
using WireMock.Logging;
using WireMock.Server;
using WireMock.Settings;
#if OPENTELEMETRY_SUPPORTED
using WireMock.OpenTelemetry;
#endif
namespace WireMock.Net.StandAlone;
@@ -37,6 +40,39 @@ public static class StandAloneApp
return server;
}
#if OPENTELEMETRY_SUPPORTED
/// <summary>
/// Start WireMock.Net standalone Server based on the WireMockServerSettings with OpenTelemetry tracing.
/// </summary>
/// <param name="settings">The WireMockServerSettings</param>
/// <param name="openTelemetryOptions">The OpenTelemetry options for exporting traces.</param>
[PublicAPI]
public static WireMockServer Start(WireMockServerSettings settings, OpenTelemetryOptions? openTelemetryOptions)
{
Guard.NotNull(settings);
// Wire up OpenTelemetry OTLP exporter if options are provided
if (openTelemetryOptions is not null)
{
// Enable activity tracing in settings so middleware creates activities
// Only set ExcludeAdminRequests if not already configured
settings.ActivityTracingOptions ??= new ActivityTracingOptions
{
ExcludeAdminRequests = openTelemetryOptions.ExcludeAdminRequests
};
var existingRegistration = settings.AdditionalServiceRegistration;
settings.AdditionalServiceRegistration = services =>
{
existingRegistration?.Invoke(services);
services.AddWireMockOpenTelemetry(openTelemetryOptions);
};
}
return Start(settings);
}
#endif
/// <summary>
/// Start WireMock.Net standalone Server based on the commandline arguments.
/// </summary>
@@ -71,7 +107,13 @@ public static class StandAloneApp
settings.Logger?.Info("Version [{0}]", Version);
settings.Logger?.Debug("Server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'")));
#if OPENTELEMETRY_SUPPORTED
// Parse OpenTelemetry options separately using the OTEL project parser
OpenTelemetryOptionsParser.TryParseArguments(args, Environment.GetEnvironmentVariables(), out var openTelemetryOptions);
server = Start(settings, openTelemetryOptions);
#else
server = Start(settings);
#endif
return true;
}

View File

@@ -35,6 +35,11 @@
<DefineConstants>USE_ASPNETCORE;NET46</DefineConstants>
</PropertyGroup>
<!-- Enable OpenTelemetry exporter support for .NET 6+ -->
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0'">
<DefineConstants>$(DefineConstants);OPENTELEMETRY_SUPPORTED</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PolySharp" Version="1.15.0">
<PrivateAssets>all</PrivateAssets>
@@ -42,4 +47,9 @@
</PackageReference>
<ProjectReference Include="..\WireMock.Net\WireMock.Net.csproj" />
</ItemGroup>
<!-- OpenTelemetry exporter for .NET 6+ -->
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0'">
<ProjectReference Include="..\WireMock.Net.OpenTelemetry\WireMock.Net.OpenTelemetry.csproj" />
</ItemGroup>
</Project>

View File

@@ -37,4 +37,9 @@
<ProjectReference Include="../WireMock.Net.GraphQL/WireMock.Net.GraphQL.csproj" />
<ProjectReference Include="../WireMock.Net.ProtoBuf/WireMock.Net.ProtoBuf.csproj" />
</ItemGroup>
<!-- OpenTelemetry exporter for .NET 6+ -->
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0'">
<ProjectReference Include="..\WireMock.Net.OpenTelemetry\WireMock.Net.OpenTelemetry.csproj" />
</ItemGroup>
</Project>