From f28654f15f995f7874bc3e08f075ea47e1486dcb Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 12 Dec 2023 14:04:12 -0800 Subject: [PATCH] readd --- .../.publicApi/PublicAPI.Unshipped.txt | 4 +- ...ttpClientMeterProviderBuilderExtensions.cs | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/OpenTelemetry.Instrumentation.Http/HttpClientMeterProviderBuilderExtensions.cs diff --git a/src/OpenTelemetry.Instrumentation.Http/.publicApi/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.Http/.publicApi/PublicAPI.Unshipped.txt index cb63a823bd..42b31fb6f9 100644 --- a/src/OpenTelemetry.Instrumentation.Http/.publicApi/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.Http/.publicApi/PublicAPI.Unshipped.txt @@ -16,9 +16,9 @@ OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions.FilterH OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions.HttpClientTraceInstrumentationOptions() -> void OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions.RecordException.get -> bool OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions.RecordException.set -> void -OpenTelemetry.Metrics.MeterProviderBuilderExtensions +OpenTelemetry.Metrics.HttpClientMeterProviderBuilderExtensions OpenTelemetry.Trace.HttpClientTracerProviderBuilderExtensions -static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder +static OpenTelemetry.Metrics.HttpClientMeterProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Trace.HttpClientTracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.HttpClientTracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action configureHttpClientTraceInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.HttpClientTracerProviderBuilderExtensions.AddHttpClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configureHttpClientTraceInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder diff --git a/src/OpenTelemetry.Instrumentation.Http/HttpClientMeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.Http/HttpClientMeterProviderBuilderExtensions.cs new file mode 100644 index 0000000000..b8630f8e66 --- /dev/null +++ b/src/OpenTelemetry.Instrumentation.Http/HttpClientMeterProviderBuilderExtensions.cs @@ -0,0 +1,49 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#if !NET8_0_OR_GREATER +#if !NETFRAMEWORK +using OpenTelemetry.Instrumentation.Http; +#endif +using OpenTelemetry.Instrumentation.Http.Implementation; +#endif + +using OpenTelemetry.Internal; + +namespace OpenTelemetry.Metrics; + +/// +/// Extension methods to simplify registering of HttpClient instrumentation. +/// +public static class HttpClientMeterProviderBuilderExtensions +{ + /// + /// Enables HttpClient instrumentation. + /// + /// being configured. + /// The instance of to chain the calls. + public static MeterProviderBuilder AddHttpClientInstrumentation( + this MeterProviderBuilder builder) + { + Guard.ThrowIfNull(builder); + +#if NET8_0_OR_GREATER + return builder + .AddMeter("System.Net.Http") + .AddMeter("System.Net.NameResolution"); +#else + // Note: Warm-up the status code and method mapping. + _ = TelemetryHelper.BoxedStatusCodes; + _ = RequestMethodHelper.KnownMethods; + +#if NETFRAMEWORK + builder.AddMeter(HttpWebRequestActivitySource.MeterName); +#else + builder.AddMeter(HttpHandlerMetricsDiagnosticListener.MeterName); + + builder.AddInstrumentation(new HttpClientMetrics()); +#endif + return builder; +#endif + } +}