Skip to content

Commit

Permalink
Use static Histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla committed Dec 2, 2023
1 parent 31032a0 commit c6acb9e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 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,12 +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 Meter InstrumentationMeter = new(InstrumentationName, InstrumentationVersion);

private static readonly HashSet<string> DiagnosticSourceEvents = new()
{
"Microsoft.AspNetCore.Hosting.HttpRequestIn",
Expand All @@ -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();
}
Expand Down
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 MeterProviderBuilder AddAspNetCoreInstrumentation(
_ = TelemetryHelper.BoxedStatusCodes;
_ = RequestMethodHelper.KnownMethods;

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

builder.AddInstrumentation(new AspNetCoreMetrics());

Expand Down

0 comments on commit c6acb9e

Please sign in to comment.