Skip to content

OpenTelemetry Tracing

WireMock.Net supports distributed tracing via System.Diagnostics.Activity and can export traces using OpenTelemetry.

WireMock.Net creates System.Diagnostics.Activity objects for request tracing when ActivityTracingOptions is configured. This uses the built-in .NET distributed tracing infrastructure.

var settings = new WireMockServerSettings
{
ActivityTracingOptions = new ActivityTracingOptions
{
ExcludeAdminRequests = true,
RecordRequestBody = false,
RecordResponseBody = false,
RecordMatchDetails = true
}
};
var server = WireMockServer.Start(settings);
PropertyDescriptionDefault
ExcludeAdminRequestsExclude /__admin/* from tracingtrue
RecordRequestBodyInclude request body in trace attributesfalse
RecordResponseBodyInclude response body in trace attributesfalse
RecordMatchDetailsInclude mapping match details in trace attributestrue

To export traces to an OpenTelemetry collector, install the WireMock.Net.OpenTelemetry package:

Terminal window
dotnet add package WireMock.Net.OpenTelemetry
using WireMock.OpenTelemetry;
using WireMock.Server;
using WireMock.Settings;
var settings = new WireMockServerSettings
{
ActivityTracingOptions = new ActivityTracingOptions
{
ExcludeAdminRequests = true,
RecordMatchDetails = true
},
AdditionalServiceRegistration = services =>
{
services.AddWireMockOpenTelemetry(new OpenTelemetryOptions
{
ExcludeAdminRequests = true,
OtlpExporterEndpoint = "http://localhost:4317"
});
}
};
var server = WireMockServer.Start(settings);
using OpenTelemetry;
using OpenTelemetry.Trace;
using WireMock.OpenTelemetry;
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddWireMockInstrumentation(new OpenTelemetryOptions())
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri("http://localhost:4317");
})
.Build();
PropertyDescriptionDefault
ExcludeAdminRequestsExclude /__admin/* from ASP.NET Core instrumentationtrue
OtlpExporterEndpointOTLP collector endpoint URLUses OTEL_EXPORTER_OTLP_ENDPOINT env var

WireMock traces include standard HTTP attributes and WireMock-specific attributes:

HTTP attributes:

  • http.request.method
  • url.full
  • url.path
  • server.address
  • http.response.status_code
  • client.address

WireMock 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

When using WireMock.Net.StandAlone or Docker, configure tracing via command-line arguments:

Activity Tracing:

Terminal window
--ActivityTracingEnabled true
--ActivityTracingExcludeAdminRequests true
--ActivityTracingRecordRequestBody false
--ActivityTracingRecordResponseBody false
--ActivityTracingRecordMatchDetails true

OpenTelemetry Export:

Terminal window
--OpenTelemetryEnabled true
--OpenTelemetryOtlpExporterEndpoint http://localhost:4317
--OpenTelemetryExcludeAdminRequests true