Skip to content

Commit 9dbd22d

Browse files
authored
[Instrumentation.AspNetCore] Use static Meter and Histogram (#5112)
1 parent c5f5dd7 commit 9dbd22d

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetrics.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
// </copyright>
1616

1717
#if !NET8_0_OR_GREATER
18-
using System.Diagnostics.Metrics;
19-
using System.Reflection;
2018
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
2119

2220
namespace OpenTelemetry.Instrumentation.AspNetCore;
@@ -26,10 +24,6 @@ namespace OpenTelemetry.Instrumentation.AspNetCore;
2624
/// </summary>
2725
internal sealed class AspNetCoreMetrics : IDisposable
2826
{
29-
internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName();
30-
internal static readonly string InstrumentationName = AssemblyName.Name;
31-
internal static readonly string InstrumentationVersion = AssemblyName.Version.ToString();
32-
3327
private static readonly HashSet<string> DiagnosticSourceEvents = new()
3428
{
3529
"Microsoft.AspNetCore.Hosting.HttpRequestIn",
@@ -43,12 +37,10 @@ internal sealed class AspNetCoreMetrics : IDisposable
4337
=> DiagnosticSourceEvents.Contains(eventName);
4438

4539
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;
46-
private readonly Meter meter;
4740

4841
internal AspNetCoreMetrics()
4942
{
50-
this.meter = new Meter(InstrumentationName, InstrumentationVersion);
51-
var metricsListener = new HttpInMetricsListener("Microsoft.AspNetCore", this.meter);
43+
var metricsListener = new HttpInMetricsListener("Microsoft.AspNetCore");
5244
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(metricsListener, this.isEnabled, AspNetCoreInstrumentationEventSource.Log.UnknownErrorProcessingEvent);
5345
this.diagnosticSourceSubscriber.Subscribe();
5446
}
@@ -57,7 +49,6 @@ internal AspNetCoreMetrics()
5749
public void Dispose()
5850
{
5951
this.diagnosticSourceSubscriber?.Dispose();
60-
this.meter?.Dispose();
6152
}
6253
}
6354
#endif

src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System.Diagnostics;
1818
using System.Diagnostics.Metrics;
19+
using System.Reflection;
1920
using Microsoft.AspNetCore.Http;
2021
using OpenTelemetry.Internal;
2122

@@ -33,21 +34,25 @@ internal sealed class HttpInMetricsListener : ListenerHandler
3334

3435
internal const string OnUnhandledHostingExceptionEvent = "Microsoft.AspNetCore.Hosting.UnhandledException";
3536
internal const string OnUnhandledDiagnosticsExceptionEvent = "Microsoft.AspNetCore.Diagnostics.UnhandledException";
37+
38+
internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName();
39+
internal static readonly string InstrumentationName = AssemblyName.Name;
40+
internal static readonly string InstrumentationVersion = AssemblyName.Version.ToString();
41+
internal static readonly Meter Meter = new(InstrumentationName, InstrumentationVersion);
42+
3643
private const string OnStopEvent = "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop";
44+
3745
private const string EventName = "OnStopActivity";
3846
private const string NetworkProtocolName = "http";
3947
private static readonly PropertyFetcher<Exception> ExceptionPropertyFetcher = new("Exception");
4048
private static readonly PropertyFetcher<HttpContext> HttpContextPropertyFetcher = new("HttpContext");
4149
private static readonly object ErrorTypeHttpContextItemsKey = new();
4250

43-
private readonly Meter meter;
44-
private readonly Histogram<double> httpServerRequestDuration;
51+
private static readonly Histogram<double> HttpServerRequestDuration = Meter.CreateHistogram<double>(HttpServerRequestDurationMetricName, "s", "Duration of HTTP server requests.");
4552

46-
internal HttpInMetricsListener(string name, Meter meter)
53+
internal HttpInMetricsListener(string name)
4754
: base(name)
4855
{
49-
this.meter = meter;
50-
this.httpServerRequestDuration = meter.CreateHistogram<double>(HttpServerRequestDurationMetricName, "s", "Duration of HTTP server requests.");
5156
}
5257

5358
public override void OnEventWritten(string name, object payload)
@@ -130,6 +135,6 @@ public void OnStopEventWritten(string name, object payload)
130135
// We are relying here on ASP.NET Core to set duration before writing the stop event.
131136
// https://github.com/dotnet/aspnetcore/blob/d6fa351048617ae1c8b47493ba1abbe94c3a24cf/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L449
132137
// TODO: Follow up with .NET team if we can continue to rely on this behavior.
133-
this.httpServerRequestDuration.Record(Activity.Current.Duration.TotalSeconds, tags);
138+
HttpServerRequestDuration.Record(Activity.Current.Duration.TotalSeconds, tags);
134139
}
135140
}

src/OpenTelemetry.Instrumentation.AspNetCore/MeterProviderBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static MeterProviderBuilder AddAspNetCoreInstrumentation(
4444
_ = TelemetryHelper.BoxedStatusCodes;
4545
_ = RequestMethodHelper.KnownMethods;
4646

47-
builder.AddMeter(AspNetCoreMetrics.InstrumentationName);
47+
builder.AddMeter(HttpInMetricsListener.InstrumentationName);
4848

4949
builder.AddInstrumentation(new AspNetCoreMetrics());
5050

0 commit comments

Comments
 (0)