[perf] Minor Httpclient improvement (#4572)

This commit is contained in:
Vishwesh Bankwar 2023-06-20 15:08:58 -07:00 committed by GitHub
parent 5d321ce621
commit 4712c3a9b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 12 deletions

View File

@ -22,10 +22,26 @@ namespace OpenTelemetry.Instrumentation.Http
/// </summary> /// </summary>
internal sealed class HttpClientInstrumentation : IDisposable internal sealed class HttpClientInstrumentation : IDisposable
{ {
private static readonly HashSet<string> ExcludedDiagnosticSourceEventsNet7OrGreater = new()
{
"System.Net.Http.Request",
"System.Net.Http.Response",
"System.Net.Http.HttpRequestOut",
};
private static readonly HashSet<string> ExcludedDiagnosticSourceEvents = new()
{
"System.Net.Http.Request",
"System.Net.Http.Response",
};
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber; private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;
private readonly Func<string, object, object, bool> isEnabled = (activityName, obj1, obj2) private readonly Func<string, object, object, bool> isEnabled = (eventName, _, _)
=> !activityName.Equals("System.Net.Http.HttpRequestOut"); => !ExcludedDiagnosticSourceEvents.Contains(eventName);
private readonly Func<string, object, object, bool> isEnabledNet7OrGreater = (eventName, _, _)
=> !ExcludedDiagnosticSourceEventsNet7OrGreater.Contains(eventName);
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="HttpClientInstrumentation"/> class. /// Initializes a new instance of the <see cref="HttpClientInstrumentation"/> class.
@ -41,11 +57,11 @@ namespace OpenTelemetry.Instrumentation.Http
// so that the sampler's decision is respected. // so that the sampler's decision is respected.
if (HttpHandlerDiagnosticListener.IsNet7OrGreater) if (HttpHandlerDiagnosticListener.IsNet7OrGreater)
{ {
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerDiagnosticListener(options), this.isEnabled); this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerDiagnosticListener(options), this.isEnabledNet7OrGreater);
} }
else else
{ {
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerDiagnosticListener(options), null); this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerDiagnosticListener(options), this.isEnabled);
} }
this.diagnosticSourceSubscriber.Subscribe(); this.diagnosticSourceSubscriber.Subscribe();

View File

@ -29,16 +29,25 @@ namespace OpenTelemetry.Instrumentation.Http
internal static readonly string InstrumentationName = AssemblyName.Name; internal static readonly string InstrumentationName = AssemblyName.Name;
internal static readonly string InstrumentationVersion = AssemblyName.Version.ToString(); internal static readonly string InstrumentationVersion = AssemblyName.Version.ToString();
private static readonly HashSet<string> ExcludedDiagnosticSourceEvents = new()
{
"System.Net.Http.Request",
"System.Net.Http.Response",
};
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber; private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;
private readonly Meter meter; private readonly Meter meter;
private readonly Func<string, object, object, bool> isEnabled = (activityName, obj1, obj2)
=> !ExcludedDiagnosticSourceEvents.Contains(activityName);
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="HttpClientMetrics"/> class. /// Initializes a new instance of the <see cref="HttpClientMetrics"/> class.
/// </summary> /// </summary>
public HttpClientMetrics() public HttpClientMetrics()
{ {
this.meter = new Meter(InstrumentationName, InstrumentationVersion); 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(); this.diagnosticSourceSubscriber.Subscribe();
} }

View File

@ -24,19 +24,19 @@ using OpenTelemetry.Metrics;
using OpenTelemetry.Trace; using OpenTelemetry.Trace;
/* /*
BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.23424.1000) BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.22621.1702/22H2/2022Update/SunValley2)
Intel Core i7-9700 CPU 3.00GHz, 1 CPU, 8 logical and 8 physical cores Intel Core i7-8850H CPU 2.60GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET SDK=7.0.203 .NET SDK=7.0.302
[Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2 [Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
DefaultJob : .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 | | Method | EnableInstrumentation | Mean | Error | StdDev | Gen0 | Allocated |
|------------------ |---------------------- |---------:|--------:|--------:|-------:|----------:| |------------------ |---------------------- |---------:|--------:|--------:|-------:|----------:|
| HttpClientRequest | None | 222.7 us | 4.28 us | 4.75 us | - | 2.45 KB | | HttpClientRequest | None | 161.0 us | 1.27 us | 0.99 us | 0.4883 | 2.45 KB |
| HttpClientRequest | Traces | 233.0 us | 4.08 us | 3.81 us | 0.4883 | 4.36 KB | | HttpClientRequest | Traces | 173.1 us | 1.65 us | 1.54 us | 0.4883 | 4.26 KB |
| HttpClientRequest | Metrics | 208.9 us | 4.17 us | 7.53 us | 0.4883 | 3.76 KB | | HttpClientRequest | Metrics | 165.8 us | 1.33 us | 1.18 us | 0.7324 | 3.66 KB |
| HttpClientRequest | Traces, Metrics | 230.9 us | 3.29 us | 2.92 us | 0.4883 | 4.38 KB | | HttpClientRequest | Traces, Metrics | 175.6 us | 1.96 us | 1.83 us | 0.4883 | 4.28 KB |
*/ */
namespace Benchmarks.Instrumentation namespace Benchmarks.Instrumentation