Skip to content

Commit

Permalink
[perf] Minor Httpclient improvement (#4572)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishweshbankwar authored Jun 20, 2023
1 parent 5d321ce commit 4712c3a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,26 @@ namespace OpenTelemetry.Instrumentation.Http
/// </summary>
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 Func<string, object, object, bool> isEnabled = (activityName, obj1, obj2)
=> !activityName.Equals("System.Net.Http.HttpRequestOut");
private readonly Func<string, object, object, bool> isEnabled = (eventName, _, _)
=> !ExcludedDiagnosticSourceEvents.Contains(eventName);

private readonly Func<string, object, object, bool> isEnabledNet7OrGreater = (eventName, _, _)
=> !ExcludedDiagnosticSourceEventsNet7OrGreater.Contains(eventName);

/// <summary>
/// Initializes a new instance of the <see cref="HttpClientInstrumentation"/> class.
Expand All @@ -41,11 +57,11 @@ public HttpClientInstrumentation(HttpClientInstrumentationOptions options)
// 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();
Expand Down
11 changes: 10 additions & 1 deletion src/OpenTelemetry.Instrumentation.Http/HttpClientMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,25 @@ internal sealed class HttpClientMetrics : IDisposable
internal static readonly string InstrumentationName = AssemblyName.Name;
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 Meter meter;

private readonly Func<string, object, object, bool> isEnabled = (activityName, obj1, obj2)
=> !ExcludedDiagnosticSourceEvents.Contains(activityName);

/// <summary>
/// Initializes a new instance of the <see cref="HttpClientMetrics"/> class.
/// </summary>
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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@
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
Expand Down

0 comments on commit 4712c3a

Please sign in to comment.