From 4712c3a9b0f9cedcb27297d46b08001fc13282cf Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 20 Jun 2023 15:08:58 -0700 Subject: [PATCH] [perf] Minor Httpclient improvement (#4572) --- .../HttpClientInstrumentation.cs | 24 +++++++++++++++---- .../HttpClientMetrics.cs | 11 ++++++++- .../HttpClientInstrumentationBenchmarks.cs | 14 +++++------ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentation.cs b/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentation.cs index c7ffe545f..842cd0b9a 100644 --- a/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentation.cs +++ b/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentation.cs @@ -22,10 +22,26 @@ namespace OpenTelemetry.Instrumentation.Http /// internal sealed class HttpClientInstrumentation : IDisposable { + private static readonly HashSet ExcludedDiagnosticSourceEventsNet7OrGreater = new() + { + "System.Net.Http.Request", + "System.Net.Http.Response", + "System.Net.Http.HttpRequestOut", + }; + + private static readonly HashSet ExcludedDiagnosticSourceEvents = new() + { + "System.Net.Http.Request", + "System.Net.Http.Response", + }; + private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber; - private readonly Func isEnabled = (activityName, obj1, obj2) - => !activityName.Equals("System.Net.Http.HttpRequestOut"); + private readonly Func isEnabled = (eventName, _, _) + => !ExcludedDiagnosticSourceEvents.Contains(eventName); + + private readonly Func isEnabledNet7OrGreater = (eventName, _, _) + => !ExcludedDiagnosticSourceEventsNet7OrGreater.Contains(eventName); /// /// Initializes a new instance of the class. @@ -41,11 +57,11 @@ namespace OpenTelemetry.Instrumentation.Http // so that the sampler's decision is respected. if (HttpHandlerDiagnosticListener.IsNet7OrGreater) { - this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerDiagnosticListener(options), this.isEnabled); + this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerDiagnosticListener(options), this.isEnabledNet7OrGreater); } else { - this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerDiagnosticListener(options), null); + this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerDiagnosticListener(options), this.isEnabled); } this.diagnosticSourceSubscriber.Subscribe(); diff --git a/src/OpenTelemetry.Instrumentation.Http/HttpClientMetrics.cs b/src/OpenTelemetry.Instrumentation.Http/HttpClientMetrics.cs index e48af04a6..3bf57029d 100644 --- a/src/OpenTelemetry.Instrumentation.Http/HttpClientMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.Http/HttpClientMetrics.cs @@ -29,16 +29,25 @@ namespace OpenTelemetry.Instrumentation.Http internal static readonly string InstrumentationName = AssemblyName.Name; internal static readonly string InstrumentationVersion = AssemblyName.Version.ToString(); + private static readonly HashSet ExcludedDiagnosticSourceEvents = new() + { + "System.Net.Http.Request", + "System.Net.Http.Response", + }; + private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber; private readonly Meter meter; + private readonly Func isEnabled = (activityName, obj1, obj2) + => !ExcludedDiagnosticSourceEvents.Contains(activityName); + /// /// Initializes a new instance of the class. /// public HttpClientMetrics() { this.meter = new Meter(InstrumentationName, InstrumentationVersion); - this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerMetricsDiagnosticListener("HttpHandlerDiagnosticListener", this.meter), null); + this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerMetricsDiagnosticListener("HttpHandlerDiagnosticListener", this.meter), this.isEnabled); this.diagnosticSourceSubscriber.Subscribe(); } diff --git a/test/Benchmarks/Instrumentation/HttpClientInstrumentationBenchmarks.cs b/test/Benchmarks/Instrumentation/HttpClientInstrumentationBenchmarks.cs index a25570b90..2583b32cf 100644 --- a/test/Benchmarks/Instrumentation/HttpClientInstrumentationBenchmarks.cs +++ b/test/Benchmarks/Instrumentation/HttpClientInstrumentationBenchmarks.cs @@ -24,19 +24,19 @@ using OpenTelemetry.Metrics; using OpenTelemetry.Trace; /* -BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.23424.1000) -Intel Core i7-9700 CPU 3.00GHz, 1 CPU, 8 logical and 8 physical cores -.NET SDK=7.0.203 +BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.22621.1702/22H2/2022Update/SunValley2) +Intel Core i7-8850H CPU 2.60GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores +.NET SDK=7.0.302 [Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2 DefaultJob : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2 | Method | EnableInstrumentation | Mean | Error | StdDev | Gen0 | Allocated | |------------------ |---------------------- |---------:|--------:|--------:|-------:|----------:| -| HttpClientRequest | None | 222.7 us | 4.28 us | 4.75 us | - | 2.45 KB | -| HttpClientRequest | Traces | 233.0 us | 4.08 us | 3.81 us | 0.4883 | 4.36 KB | -| HttpClientRequest | Metrics | 208.9 us | 4.17 us | 7.53 us | 0.4883 | 3.76 KB | -| HttpClientRequest | Traces, Metrics | 230.9 us | 3.29 us | 2.92 us | 0.4883 | 4.38 KB | +| HttpClientRequest | None | 161.0 us | 1.27 us | 0.99 us | 0.4883 | 2.45 KB | +| HttpClientRequest | Traces | 173.1 us | 1.65 us | 1.54 us | 0.4883 | 4.26 KB | +| HttpClientRequest | Metrics | 165.8 us | 1.33 us | 1.18 us | 0.7324 | 3.66 KB | +| HttpClientRequest | Traces, Metrics | 175.6 us | 1.96 us | 1.83 us | 0.4883 | 4.28 KB | */ namespace Benchmarks.Instrumentation