Skip to content

Commit

Permalink
[Traces] Support named options in HttpClient instrumentation (#3664)
Browse files Browse the repository at this point in the history
* Enabled named options in httpclient instrumentation.

* Tests.

* CHANGELOG update.

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
  • Loading branch information
CodeBlanch and cijothomas authored Sep 15, 2022
1 parent 960908c commit 215f58b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions.RecordExcept
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.HttpClientInstrumentationOptions> configureHttpClientInstrumentationOptions = 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.HttpClientInstrumentationOptions> configureHttpClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.Http.HttpClientInstrumentationOptions> configureHttpClientInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
8 changes: 8 additions & 0 deletions src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased

* Dropped `netstandard2.0` target and added `net6.0`
([#3664](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3664))

* 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))

## 1.0.0-rc9.6

Released 2022-Aug-18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

<PropertyGroup>
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
<TargetFrameworks>net6.0;net462</TargetFrameworks>
<Description>Http instrumentation for OpenTelemetry .NET</Description>
<PackageTags>$(PackageTags);distributed-tracing</PackageTags>
<IncludeDiagnosticSourceInstrumentationHelpers>true</IncludeDiagnosticSourceInstrumentationHelpers>
</PropertyGroup>

<!--Do not run ApiCompat for net6.0 as this is newly added. Remove this property once we have released a stable version.-->
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<RunApiCompat>false</RunApiCompat>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,45 @@ public static TracerProviderBuilder AddHttpClientInstrumentation(
/// Enables HttpClient instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configureHttpClientInstrumentationOptions">HttpClient 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, configureHttpClientInstrumentationOptions: null);

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

/// <summary>
/// Enables HttpClient instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <param name="configureHttpClientInstrumentationOptions">Callback action for configuring <see cref="HttpClientInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddHttpClientInstrumentation(
this TracerProviderBuilder builder,
string name,
Action<HttpClientInstrumentationOptions> configureHttpClientInstrumentationOptions)
{
Guard.ThrowIfNull(builder);

name ??= Options.DefaultName;

if (configureHttpClientInstrumentationOptions != null)
{
builder.ConfigureServices(services => services.Configure(configureHttpClientInstrumentationOptions));
builder.ConfigureServices(services => services.Configure(name, configureHttpClientInstrumentationOptions));
}

return builder.ConfigureBuilder((sp, builder) =>
{
var options = sp.GetRequiredService<IOptions<HttpClientInstrumentationOptions>>().Value;
var options = sp.GetRequiredService<IOptionsMonitor<HttpClientInstrumentationOptions>>().Get(name);
AddHttpClientInstrumentation(builder, new HttpClientInstrumentation(options));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.Http.Implementation;
Expand Down Expand Up @@ -47,6 +48,27 @@ public HttpClientTests()
this.url = $"http://{host}:{port}/";
}

[Fact]
public void AddHttpClientInstrumentation_NamedOptions()
{
int defaultExporterOptionsConfigureOptionsInvocations = 0;
int namedExporterOptionsConfigureOptionsInvocations = 0;

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.ConfigureServices(services =>
{
services.Configure<HttpClientInstrumentationOptions>(o => defaultExporterOptionsConfigureOptionsInvocations++);
services.Configure<HttpClientInstrumentationOptions>("Instrumentation2", o => namedExporterOptionsConfigureOptionsInvocations++);
})
.AddHttpClientInstrumentation()
.AddHttpClientInstrumentation("Instrumentation2", configureHttpClientInstrumentationOptions: null)
.Build();

Assert.Equal(1, defaultExporterOptionsConfigureOptionsInvocations);
Assert.Equal(1, namedExporterOptionsConfigureOptionsInvocations);
}

[Fact]
public void AddHttpClientInstrumentation_BadArgs()
{
Expand Down

0 comments on commit 215f58b

Please sign in to comment.