Skip to content

Commit

Permalink
[Instrumentation.AspNetCore] Use static Meter and Histogram (#5112)
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla committed Dec 2, 2023
1 parent c5f5dd7 commit 9dbd22d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
// </copyright>

#if !NET8_0_OR_GREATER
using System.Diagnostics.Metrics;
using System.Reflection;
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;

namespace OpenTelemetry.Instrumentation.AspNetCore;
Expand All @@ -26,10 +24,6 @@ namespace OpenTelemetry.Instrumentation.AspNetCore;
/// </summary>
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 HashSet<string> DiagnosticSourceEvents = new()
{
"Microsoft.AspNetCore.Hosting.HttpRequestIn",
Expand All @@ -43,12 +37,10 @@ internal sealed class AspNetCoreMetrics : IDisposable
=> DiagnosticSourceEvents.Contains(eventName);

private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;
private readonly Meter meter;

internal AspNetCoreMetrics()
{
this.meter = new Meter(InstrumentationName, InstrumentationVersion);
var metricsListener = new HttpInMetricsListener("Microsoft.AspNetCore", this.meter);
var metricsListener = new HttpInMetricsListener("Microsoft.AspNetCore");
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(metricsListener, this.isEnabled, AspNetCoreInstrumentationEventSource.Log.UnknownErrorProcessingEvent);
this.diagnosticSourceSubscriber.Subscribe();
}
Expand All @@ -57,7 +49,6 @@ internal AspNetCoreMetrics()
public void Dispose()
{
this.diagnosticSourceSubscriber?.Dispose();
this.meter?.Dispose();
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Reflection;
using Microsoft.AspNetCore.Http;
using OpenTelemetry.Internal;

Expand All @@ -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<Exception> ExceptionPropertyFetcher = new("Exception");
private static readonly PropertyFetcher<HttpContext> HttpContextPropertyFetcher = new("HttpContext");
private static readonly object ErrorTypeHttpContextItemsKey = new();

private readonly Meter meter;
private readonly Histogram<double> httpServerRequestDuration;
private static readonly Histogram<double> HttpServerRequestDuration = Meter.CreateHistogram<double>(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<double>(HttpServerRequestDurationMetricName, "s", "Duration of HTTP server requests.");
}

public override void OnEventWritten(string name, object payload)
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static class MeterProviderBuilderExtensions
_ = TelemetryHelper.BoxedStatusCodes;
_ = RequestMethodHelper.KnownMethods;

builder.AddMeter(AspNetCoreMetrics.InstrumentationName);
builder.AddMeter(HttpInMetricsListener.InstrumentationName);

builder.AddInstrumentation(new AspNetCoreMetrics());

Expand Down

0 comments on commit 9dbd22d

Please sign in to comment.