Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Traces] Support named options in HttpWebRequest instrumentation #3667

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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