Skip to content

Commit

Permalink
[Traces] Support named options in HttpWebRequest instrumentation (#3667)
Browse files Browse the repository at this point in the history
* Added support for named options in the HttpWebRequest builder extensions.

* CHANGELOG update.
  • Loading branch information
CodeBlanch authored Sep 15, 2022
1 parent 215f58b commit d72a94f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions> 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<OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions> configureHttpWebRequestInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Http.HttpWebRequestInstrumentationOptions> configureHttpWebRequestInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
3 changes: 2 additions & 1 deletion src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),
[#3667](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3667))

## 1.0.0-rc9.6

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,43 @@ public static class TracerProviderBuilderExtensions
/// Enables HttpClient and HttpWebRequest instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configureHttpWebRequestInstrumentationOptions">HttpWebRequest configuration options.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddHttpClientInstrumentation(this TracerProviderBuilder builder)
=> AddHttpClientInstrumentation(builder, name: null, configureHttpWebRequestInstrumentationOptions: null);

/// <summary>
/// Enables HttpClient and HttpWebRequest instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configureHttpWebRequestInstrumentationOptions">Callback action for configuring <see cref="HttpWebRequestInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddHttpClientInstrumentation(
this TracerProviderBuilder builder,
Action<HttpWebRequestInstrumentationOptions> configureHttpWebRequestInstrumentationOptions = null)
Action<HttpWebRequestInstrumentationOptions> configureHttpWebRequestInstrumentationOptions)
=> AddHttpClientInstrumentation(builder, name: null, configureHttpWebRequestInstrumentationOptions);

/// <summary>
/// Enables HttpClient and HttpWebRequest instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <param name="configureHttpWebRequestInstrumentationOptions">Callback action for configuring <see cref="HttpWebRequestInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddHttpClientInstrumentation(
this TracerProviderBuilder builder,
string name,
Action<HttpWebRequestInstrumentationOptions> 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<IOptions<HttpWebRequestInstrumentationOptions>>().Value;
var options = sp.GetRequiredService<IOptionsMonitor<HttpWebRequestInstrumentationOptions>>().Get(name);

HttpWebRequestActivitySource.Options = options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BaseProcessor<Activity>>();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.ConfigureServices(services =>
{
services.Configure<HttpWebRequestInstrumentationOptions>(name, o => configurationDelegateInvocations++);
})
.AddProcessor(activityProcessor.Object)
.AddHttpClientInstrumentation(options =>
.AddHttpClientInstrumentation(name, options =>
{
Assert.IsType<HttpWebRequestInstrumentationOptions>(options);
})
.Build();

Assert.Equal(1, configurationDelegateInvocations);
}

[Fact]
Expand Down

0 comments on commit d72a94f

Please sign in to comment.