From 41cdb6f16d2ec97535686b2516e142af9bbc609e Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Thu, 15 Sep 2022 11:21:35 -0700 Subject: [PATCH] Added support for named options in the HttpWebRequest builder extensions. --- .../.publicApi/net462/PublicAPI.Unshipped.txt | 4 ++- .../CHANGELOG.md | 3 +- .../TracerProviderBuilderExtensions.cs | 31 ++++++++++++++++--- .../HttpWebRequestTests.Basic.netfx.cs | 20 ++++++++++-- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Http/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.Http/.publicApi/net462/PublicAPI.Unshipped.txt index f303cf77aae..2a3aa89a04a 100644 --- a/src/OpenTelemetry.Instrumentation.Http/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.Http/.publicApi/net462/PublicAPI.Unshipped.txt @@ -17,4 +17,6 @@ OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions.RecordEx OpenTelemetry.Metrics.MeterProviderBuilderExtensions OpenTelemetry.Trace.TracerProviderBuilderExtensions static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configureHttpWebRequestInstrumentationOptions = null) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action configureHttpWebRequestInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configureHttpWebRequestInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder diff --git a/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md index 4777e306a67..8dfc7d9e413 100644 --- a/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md @@ -8,7 +8,8 @@ * Added overloads which accept a name to the `TracerProviderBuilder` `AddHttpClientInstrumentation` extension to allow for more fine-grained options management - ([#3664](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3664)) + ([#3664](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3664), + [#XXXX](https://github.com/open-telemetry/opentelemetry-dotnet/pull/XXXX)) ## 1.0.0-rc9.6 diff --git a/src/OpenTelemetry.Instrumentation.Http/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.Http/TracerProviderBuilderExtensions.cs index 619035e82bc..17dc4c29ebf 100644 --- a/src/OpenTelemetry.Instrumentation.Http/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.Http/TracerProviderBuilderExtensions.cs @@ -35,20 +35,43 @@ public static class TracerProviderBuilderExtensions /// Enables HttpClient and HttpWebRequest instrumentation. /// /// being configured. - /// HttpWebRequest configuration options. + /// The instance of to chain the calls. + public static TracerProviderBuilder AddHttpClientInstrumentation(this TracerProviderBuilder builder) + => AddHttpClientInstrumentation(builder, name: null, configureHttpWebRequestInstrumentationOptions: null); + + /// + /// Enables HttpClient and HttpWebRequest instrumentation. + /// + /// being configured. + /// Callback action for configuring . /// The instance of to chain the calls. public static TracerProviderBuilder AddHttpClientInstrumentation( this TracerProviderBuilder builder, - Action configureHttpWebRequestInstrumentationOptions = null) + Action configureHttpWebRequestInstrumentationOptions) + => AddHttpClientInstrumentation(builder, name: null, configureHttpWebRequestInstrumentationOptions); + + /// + /// Enables HttpClient and HttpWebRequest instrumentation. + /// + /// being configured. + /// Name which is used when retrieving options. + /// Callback action for configuring . + /// The instance of to chain the calls. + public static TracerProviderBuilder AddHttpClientInstrumentation( + this TracerProviderBuilder builder, + string name, + Action configureHttpWebRequestInstrumentationOptions) { + name ??= Options.DefaultName; + if (configureHttpWebRequestInstrumentationOptions != null) { - builder.ConfigureServices(services => services.Configure(configureHttpWebRequestInstrumentationOptions)); + builder.ConfigureServices(services => services.Configure(name, configureHttpWebRequestInstrumentationOptions)); } return builder.ConfigureBuilder((sp, builder) => { - var options = sp.GetRequiredService>().Value; + var options = sp.GetRequiredService>().Get(name); HttpWebRequestActivitySource.Options = options; diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs index 620438e262e..382a1016343 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs @@ -20,6 +20,8 @@ using System.Net; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Moq; using OpenTelemetry.Context.Propagation; using OpenTelemetry.Instrumentation.Http.Implementation; @@ -252,17 +254,29 @@ public async Task RequestNotCollectedWhenInstrumentationFilterThrowsException() } } - [Fact] - public void AddHttpClientInstrumentationUsesHttpWebRequestInstrumentationOptions() + [Theory] + [InlineData(null)] + [InlineData("CustomName")] + public void AddHttpClientInstrumentationUsesHttpWebRequestInstrumentationOptions(string name) { + name ??= Options.DefaultName; + + int configurationDelegateInvocations = 0; + var activityProcessor = new Mock>(); using var tracerProvider = Sdk.CreateTracerProviderBuilder() + .ConfigureServices(services => + { + services.Configure(name, o => configurationDelegateInvocations++); + }) .AddProcessor(activityProcessor.Object) - .AddHttpClientInstrumentation(options => + .AddHttpClientInstrumentation(name, options => { Assert.IsType(options); }) .Build(); + + Assert.Equal(1, configurationDelegateInvocations); } [Fact]