Compare commits

..
2 Commits
Author SHA1 Message Date
Stef Heyenrath b2eb7c4a18 2.33.0 2026-11-24 19:58:35 +01:00
66204c8297 Configure OpenTelemetry activity source and service names (#1550)
* Initial plan

* Configure OpenTelemetry activity source and service name

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

* tests

* auth

---------

Co-authored-by: StefH <249938+StefH@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-11-24 19:55:36 +01:00
11 changed files with 122 additions and 22 deletions

No files matched your search

+4
View File
@@ -1,3 +1,7 @@
# 2.33.0 (24 November 2026)
- [#1550](https://github.com/wiremock/WireMock.Net/pull/1550) - Configure OpenTelemetry activity source and service names [feature] contributed by [StefH](https://github.com/StefH)
- [#1549](https://github.com/wiremock/WireMock.Net/issues/1549) - Support for passing ENV variable for Otel Activity Name [feature]
# 2.32.0 (20 November 2026)
- [#1544](https://github.com/wiremock/WireMock.Net/pull/1544) - Depend on Microsoft.AspNetCore.App for .NET 8 and .NET 10 [dependencies] contributed by [StefH](https://github.com/StefH)
- [#1545](https://github.com/wiremock/WireMock.Net/pull/1545) - Upgrade ProtoBufJsonConverter to latest [dependencies] contributed by [StefH](https://github.com/StefH)
+1 -1
View File
@@ -4,7 +4,7 @@
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>2.32.0</VersionPrefix>
<VersionPrefix>2.33.0</VersionPrefix>
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
<PackageProjectUrl>https://github.com/wiremock/WireMock.Net</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
+3 -6
View File
@@ -1,8 +1,5 @@
# 2.32.0 (20 November 2026)
- #1544 Depend on Microsoft.AspNetCore.App for .NET 8 and .NET 10 [dependencies]
- #1545 Upgrade ProtoBufJsonConverter to latest [dependencies]
- #1547 Test Testcontainers ConsoleApp on Linux [bug]
- #1548 Upgrade ProtoBufJsonConverter to latest [dependencies]
- #1546 WireMock.Net.Testcontainers isn't compatible with Testcontainers 4.17.0 [bug]
# 2.33.0 (24 November 2026)
- #1550 Configure OpenTelemetry activity source and service names [feature]
- #1549 Support for passing ENV variable for Otel Activity Name [feature]
The full release notes can be found here: https://github.com/wiremock/WireMock.Net/blob/master/CHANGELOG.md
+1 -1
View File
@@ -21,7 +21,7 @@ A C# .NET version based on [mock4net](https://github.com/alexvictoor/mock4net) w
* GraphQL support
* gRPC / ProtoBuf support
* OpenAPI support
* OpenTelemetry support
* [OpenTelemetry support](src/WireMock.Net.OpenTelemetry/README.md), including configurable service names for standalone and Docker instances
* WebSockets support
## :memo: Blogs
@@ -11,6 +11,15 @@ namespace WireMock.OpenTelemetry;
/// </summary>
public class OpenTelemetryOptions
{
internal const string DefaultActivitySourceName = "WireMock.Net";
/// <summary>
/// Gets or sets the activity source name to subscribe to and the exported service name.
/// Default is <c>WireMock.Net</c>.
/// </summary>
[PublicAPI]
public string ActivitySourceName { get; set; } = DefaultActivitySourceName;
/// <summary>
/// Gets or sets a value indicating whether to exclude admin interface requests from ASP.NET Core instrumentation.
/// Default is <c>true</c>.
@@ -1,6 +1,7 @@
// Copyright © WireMock.Net
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using Stef.Validation;
using WireMock.Settings;
@@ -20,7 +21,7 @@ public static class OpenTelemetryOptionsParser
/// <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)
public static bool TryParseArguments(string[] args, IDictionary? environment, [NotNullWhen(true)] out OpenTelemetryOptions? options)
{
Guard.HasNoNulls(args);
@@ -30,11 +31,13 @@ public static class OpenTelemetryOptionsParser
if (!parser.GetBoolValue($"{Prefix}Enabled"))
{
options = null;
return true;
return false;
}
var activitySourceName = parser.GetStringValue($"{Prefix}ActivitySourceName");
options = new OpenTelemetryOptions
{
ActivitySourceName = string.IsNullOrWhiteSpace(activitySourceName) ? OpenTelemetryOptions.DefaultActivitySourceName : activitySourceName!,
ExcludeAdminRequests = parser.GetBoolValue($"{Prefix}ExcludeAdminRequests", defaultValue: true),
OtlpExporterEndpoint = parser.GetStringValue($"{Prefix}OtlpExporterEndpoint")
};
+11 -3
View File
@@ -28,6 +28,7 @@ using WireMock.Settings;
var openTelemetryOptions = new OpenTelemetryOptions
{
ActivitySourceName = "MyService",
ExcludeAdminRequests = true,
OtlpExporterEndpoint = "http://localhost:4317" // Your OTEL collector
};
@@ -64,7 +65,7 @@ var openTelemetryOptions = new OpenTelemetryOptions();
// Configure your own TracerProvider
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddWireMockInstrumentation(openTelemetryOptions) // Adds WireMock.Net source
.AddWireMockInstrumentation(openTelemetryOptions) // Adds WireMock.Net and the configured source
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri("http://localhost:4317");
@@ -83,7 +84,8 @@ services.AddWireMockOpenTelemetry(openTelemetryOptions);
```
This configures:
- The WireMock.Net ActivitySource
- The WireMock.Net ActivitySource and the configured ActivitySource
- A resource with the configured service name
- ASP.NET Core instrumentation
- OTLP exporter (using the endpoint from `OpenTelemetryOptions.OtlpExporterEndpoint` or the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable)
@@ -101,9 +103,12 @@ tracerProvider.AddWireMockInstrumentation(openTelemetryOptions);
| Property | Description | Default |
|----------|-------------|---------|
| `ActivitySourceName` | Additional ActivitySource to subscribe to and service name for `AddWireMockOpenTelemetry` | `WireMock.Net` |
| `ExcludeAdminRequests` | Exclude `/__admin/*` from ASP.NET Core instrumentation | `true` |
| `OtlpExporterEndpoint` | OTLP collector endpoint URL | Uses `OTEL_EXPORTER_OTLP_ENDPOINT` env var |
WireMock's own activities always use the `WireMock.Net` source, which remains subscribed even when a different source is configured. `AddWireMockInstrumentation` only configures instrumentation; configure its resource separately if you need a custom service name.
### ActivityTracingOptions (Trace content configuration)
Configured in `WireMockServerSettings.ActivityTracingOptions`:
@@ -150,11 +155,14 @@ When using WireMock.Net.StandAlone or Docker images, activity tracing and OpenTe
**OpenTelemetry Export (where traces are sent):**
```bash
--OpenTelemetryEnabled true
--OpenTelemetryActivitySourceName MyService
--OpenTelemetryOtlpExporterEndpoint http://localhost:4317
--OpenTelemetryExcludeAdminRequests true
```
For Docker or standalone environment configuration, set `WireMockServerSettings__OpenTelemetryEnabled=true` and `WireMockServerSettings__OpenTelemetryActivitySourceName=MyService`. The activity source name defaults to `WireMock.Net` when omitted or empty.
## Requirements
- .NET 6.0 or later
- .NET 8.0 or later
- WireMock.Net 1.6.0 or later
@@ -3,7 +3,7 @@
<PropertyGroup>
<Description>OpenTelemetry exporter support for WireMock.Net</Description>
<AssemblyTitle>WireMock.Net.OpenTelemetry</AssemblyTitle>
<Authors>Petr Houška</Authors>
<Authors>Petr Houška, Stef Heyenrath</Authors>
<TargetFrameworks>netstandard2.0;net8.0;net10.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RootNamespace>WireMock.OpenTelemetry</RootNamespace>
@@ -12,9 +12,6 @@ namespace WireMock.OpenTelemetry;
/// </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:
@@ -38,14 +35,14 @@ public static class WireMockOpenTelemetryExtensions
.ConfigureResource(resource =>
{
resource.AddService(
serviceName: ServiceName,
serviceName: options.ActivitySourceName,
serviceVersion: typeof(WireMockOpenTelemetryExtensions).Assembly.GetName().Version?.ToString() ?? "unknown"
);
})
.WithTracing(tracing =>
{
// Add WireMock-specific traces
tracing.AddSource(WireMockActivitySourceName);
tracing.AddSource(OpenTelemetryOptions.DefaultActivitySourceName, options.ActivitySourceName);
// Add ASP.NET Core instrumentation for standard HTTP server traces
tracing.AddAspNetCoreInstrumentation(aspNetOptions =>
@@ -93,7 +90,7 @@ public static class WireMockOpenTelemetryExtensions
OpenTelemetryOptions? options = null)
{
// Add WireMock-specific traces
tracing.AddSource(WireMockActivitySourceName);
tracing.AddSource(OpenTelemetryOptions.DefaultActivitySourceName, options?.ActivitySourceName ?? OpenTelemetryOptions.DefaultActivitySourceName);
// Add ASP.NET Core instrumentation for standard HTTP server traces
tracing.AddAspNetCoreInstrumentation(aspNetOptions =>
@@ -1,5 +1,6 @@
// Copyright © WireMock.Net
using System.Collections;
using WireMock.OpenTelemetry;
namespace WireMock.Net.Tests.OpenTelemetry;
@@ -13,6 +14,7 @@ public class OpenTelemetryOptionsParserTests
var result = OpenTelemetryOptionsParser.TryParseArguments(
[
"--OpenTelemetryEnabled", "true",
"--OpenTelemetryActivitySourceName", "MyService",
"--OpenTelemetryExcludeAdminRequests", "false",
"--OpenTelemetryOtlpExporterEndpoint", "http://localhost:4317"
], null, out var options);
@@ -20,10 +22,40 @@ public class OpenTelemetryOptionsParserTests
// Assert
result.Should().BeTrue();
options.Should().NotBeNull();
options!.ExcludeAdminRequests.Should().BeFalse();
options.ActivitySourceName.Should().Be("MyService");
options.ExcludeAdminRequests.Should().BeFalse();
options.OtlpExporterEndpoint.Should().Be("http://localhost:4317");
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
[InlineData("MyService")]
public void TryParseArguments_Environment_UsesConfiguredOrDefaultActivitySourceName(string? name)
{
var environment = new Hashtable
{
["WireMockServerSettings__OpenTelemetryEnabled"] = "true"
};
if (name is not null)
{
environment["WireMockServerSettings__OpenTelemetryActivitySourceName"] = name;
}
var result = OpenTelemetryOptionsParser.TryParseArguments([], environment, out var options);
result.Should().BeTrue();
options.Should().NotBeNull();
options.ActivitySourceName.Should().Be(string.IsNullOrWhiteSpace(name) ? "WireMock.Net" : name);
}
[Fact]
public void OpenTelemetryOptions_DefaultActivitySourceName_IsWireMockNet()
{
new OpenTelemetryOptions().ActivitySourceName.Should().Be("WireMock.Net");
}
[Fact]
public void TryParseArguments_NotEnabled_ShouldReturnNull()
{
@@ -31,7 +63,7 @@ public class OpenTelemetryOptionsParserTests
var result = OpenTelemetryOptionsParser.TryParseArguments(Array.Empty<string>(), null, out var options);
// Assert
result.Should().BeTrue();
result.Should().BeFalse();
options.Should().BeNull();
}
}
@@ -2,7 +2,10 @@
#if NET6_0_OR_GREATER
using AwesomeAssertions;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Trace;
using WireMock.OpenTelemetry;
namespace WireMock.Net.Tests.OpenTelemetry;
@@ -38,5 +41,52 @@ public class WireMockOpenTelemetryExtensionsTests
result.Should().BeSameAs(services);
services.Count.Should().BeGreaterThan(initialCount);
}
[Theory]
[InlineData("WireMock.Net")]
[InlineData("MyService")]
public void AddWireMockOpenTelemetry_UsesConfiguredServiceAndListensToSources(string name)
{
var services = new ServiceCollection();
var processor = new RecordingProcessor();
services.AddWireMockOpenTelemetry(new OpenTelemetryOptions { ActivitySourceName = name });
services.AddOpenTelemetry().WithTracing(builder => builder.AddProcessor(processor));
using var provider = services.BuildServiceProvider();
var tracerProvider = provider.GetRequiredService<TracerProvider>();
using var wireMockSource = new ActivitySource("WireMock.Net");
using var configuredSource = new ActivitySource(name);
wireMockSource.StartActivity("wiremock-test")?.Dispose();
configuredSource.StartActivity("configured-test")?.Dispose();
processor.Names.Should().Contain(["wiremock-test", "configured-test"]);
tracerProvider.GetResource().Attributes.Should().Contain(attribute => attribute.Key == "service.name" && (string)attribute.Value == name);
}
[Theory]
[InlineData(null, "WireMock.Net")]
[InlineData("MyInstrumentation", "MyInstrumentation")]
public void AddWireMockInstrumentation_ListensToConfiguredAndWireMockSources(string? name, string expectedName)
{
var processor = new RecordingProcessor();
using var provider = Sdk.CreateTracerProviderBuilder()
.AddWireMockInstrumentation(name is null ? null : new OpenTelemetryOptions { ActivitySourceName = name })
.AddProcessor(processor)
.Build();
using var wireMockSource = new ActivitySource("WireMock.Net");
using var configuredSource = new ActivitySource(expectedName);
wireMockSource.StartActivity("wiremock-test")?.Dispose();
configuredSource.StartActivity("configured-test")?.Dispose();
processor.Names.Should().Contain(["wiremock-test", "configured-test"]);
}
private sealed class RecordingProcessor : BaseProcessor<Activity>
{
public List<string> Names { get; } = [];
public override void OnEnd(Activity data) => Names.Add(data.DisplayName);
}
}
#endif