From c6acb9e120c00f327fcaa7ea58ebc0f74996b584 Mon Sep 17 00:00:00 2001 From: Utkarsh Umesan Pillai <66651184+utpilla@users.noreply.github.com> Date: Fri, 1 Dec 2023 16:22:09 -0800 Subject: [PATCH] Use static Histogram --- .../AspNetCoreMetrics.cs | 10 +--------- .../Implementation/HttpInMetricsListener.cs | 17 +++++++++++------ .../MeterProviderBuilderExtensions.cs | 2 +- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetrics.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetrics.cs index 14ab42d511b..7d8dce970a2 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreMetrics.cs @@ -15,8 +15,6 @@ // #if !NET8_0_OR_GREATER -using System.Diagnostics.Metrics; -using System.Reflection; using OpenTelemetry.Instrumentation.AspNetCore.Implementation; namespace OpenTelemetry.Instrumentation.AspNetCore; @@ -26,12 +24,6 @@ namespace OpenTelemetry.Instrumentation.AspNetCore; /// internal sealed class AspNetCoreMetrics : IDisposable { - internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName(); - internal static readonly string InstrumentationName = AssemblyName.Name; - internal static readonly string InstrumentationVersion = AssemblyName.Version.ToString(); - - private static readonly Meter InstrumentationMeter = new(InstrumentationName, InstrumentationVersion); - private static readonly HashSet DiagnosticSourceEvents = new() { "Microsoft.AspNetCore.Hosting.HttpRequestIn", @@ -48,7 +40,7 @@ internal sealed class AspNetCoreMetrics : IDisposable internal AspNetCoreMetrics() { - var metricsListener = new HttpInMetricsListener("Microsoft.AspNetCore", InstrumentationMeter); + var metricsListener = new HttpInMetricsListener("Microsoft.AspNetCore"); this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(metricsListener, this.isEnabled, AspNetCoreInstrumentationEventSource.Log.UnknownErrorProcessingEvent); this.diagnosticSourceSubscriber.Subscribe(); } diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs index a47ee19858e..b3631d0ef21 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs @@ -16,6 +16,7 @@ using System.Diagnostics; using System.Diagnostics.Metrics; +using System.Reflection; using Microsoft.AspNetCore.Http; using OpenTelemetry.Internal; @@ -33,21 +34,25 @@ internal sealed class HttpInMetricsListener : ListenerHandler internal const string OnUnhandledHostingExceptionEvent = "Microsoft.AspNetCore.Hosting.UnhandledException"; internal const string OnUnhandledDiagnosticsExceptionEvent = "Microsoft.AspNetCore.Diagnostics.UnhandledException"; + + internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName(); + internal static readonly string InstrumentationName = AssemblyName.Name; + internal static readonly string InstrumentationVersion = AssemblyName.Version.ToString(); + internal static readonly Meter Meter = new(InstrumentationName, InstrumentationVersion); + private const string OnStopEvent = "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop"; + private const string EventName = "OnStopActivity"; private const string NetworkProtocolName = "http"; private static readonly PropertyFetcher ExceptionPropertyFetcher = new("Exception"); private static readonly PropertyFetcher HttpContextPropertyFetcher = new("HttpContext"); private static readonly object ErrorTypeHttpContextItemsKey = new(); - private readonly Meter meter; - private readonly Histogram httpServerRequestDuration; + private static readonly Histogram HttpServerRequestDuration = Meter.CreateHistogram(HttpServerRequestDurationMetricName, "s", "Duration of HTTP server requests."); - internal HttpInMetricsListener(string name, Meter meter) + internal HttpInMetricsListener(string name) : base(name) { - this.meter = meter; - this.httpServerRequestDuration = meter.CreateHistogram(HttpServerRequestDurationMetricName, "s", "Duration of HTTP server requests."); } public override void OnEventWritten(string name, object payload) @@ -130,6 +135,6 @@ public void OnStopEventWritten(string name, object payload) // We are relying here on ASP.NET Core to set duration before writing the stop event. // https://github.com/dotnet/aspnetcore/blob/d6fa351048617ae1c8b47493ba1abbe94c3a24cf/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L449 // TODO: Follow up with .NET team if we can continue to rely on this behavior. - this.httpServerRequestDuration.Record(Activity.Current.Duration.TotalSeconds, tags); + HttpServerRequestDuration.Record(Activity.Current.Duration.TotalSeconds, tags); } } diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/MeterProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/MeterProviderBuilderExtensions.cs index c394a3590ee..06e41eedc15 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/MeterProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/MeterProviderBuilderExtensions.cs @@ -44,7 +44,7 @@ public static MeterProviderBuilder AddAspNetCoreInstrumentation( _ = TelemetryHelper.BoxedStatusCodes; _ = RequestMethodHelper.KnownMethods; - builder.AddMeter(AspNetCoreMetrics.InstrumentationName); + builder.AddMeter(HttpInMetricsListener.InstrumentationName); builder.AddInstrumentation(new AspNetCoreMetrics());