Skip to content

Commit

Permalink
Add enrichment & filter for http client metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
hayhay27 committed Sep 27, 2023
1 parent 7ced3b4 commit eb8a532
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@

namespace OpenTelemetry.Instrumentation.Http;

#if NETFRAMEWORK
internal sealed
#else
/// <summary>
/// Options for HttpClient instrumentation.
/// </summary>
public class HttpClientMetricInstrumentationOptions
public
#endif
class HttpClientMetricInstrumentationOptions
{
internal readonly HttpSemanticConvention HttpSemanticConvention;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,38 @@ namespace OpenTelemetry.Metrics;
/// </summary>
public static class MeterProviderBuilderExtensions
{
#if NETFRAMEWORK
/// <summary>
/// Enables HttpClient instrumentation.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> being configured.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddHttpClientInstrumentation(this MeterProviderBuilder builder)
{
return builder.AddHttpClientInstrumentationCore(configure: null);
}
#else
/// <summary>
/// Enables HttpClient instrumentation.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> being configured.</param>
/// <param name="configure">Callback action for configuring <see cref="HttpClientMetricInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddHttpClientInstrumentation(
this MeterProviderBuilder builder, Action<HttpClientMetricInstrumentationOptions> configure = null)
this MeterProviderBuilder builder, Action<HttpClientMetricInstrumentationOptions> configure)
{
return builder.AddHttpClientInstrumentationCore(configure);
}
#endif

/// <summary>
/// Enables HttpClient instrumentation.
/// </summary>
/// <param name="builder"><see cref="MeterProviderBuilder"/> being configured.</param>
/// <param name="configure">Callback action for configuring <see cref="HttpClientMetricInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
private static MeterProviderBuilder AddHttpClientInstrumentationCore(
this MeterProviderBuilder builder, Action<HttpClientMetricInstrumentationOptions> configure)
{
Guard.ThrowIfNull(builder);

Expand All @@ -44,7 +68,6 @@ public static MeterProviderBuilder AddHttpClientInstrumentation(
builder.ConfigureServices(services =>
{
services.RegisterOptionsFactory(configuration => new HttpClientMetricInstrumentationOptions(configuration));

if (configure != null)
{
services.Configure(configure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public async Task HttpOutCallsAreCollectedSuccessfullyAsync(HttpTestData.HttpOut
bool enrichWithHttpResponseMessageCalled = false;
bool enrichWithExceptionCalled = false;

#if !NETFRAMEWORK
bool clientFilterCalled = false;
#endif

using var serverLifeTime = TestHttpServer.RunServer(
(ctx) =>
Expand All @@ -59,11 +61,15 @@ public async Task HttpOutCallsAreCollectedSuccessfullyAsync(HttpTestData.HttpOut
var metrics = new List<Metric>();

var meterProvider = Sdk.CreateMeterProviderBuilder()
#if NETFRAMEWORK
.AddHttpClientInstrumentation()
#else
.AddHttpClientInstrumentation(o =>
{
o.Filter = (_, _) => { return clientFilterCalled = true; };
o.Enrich = Enrich;
})
#endif
.AddInMemoryExporter(metrics)
.Build();

Expand Down

0 comments on commit eb8a532

Please sign in to comment.