16
16
17
17
using System . Diagnostics ;
18
18
using System . Diagnostics . Metrics ;
19
+ using System . Reflection ;
19
20
using Microsoft . AspNetCore . Http ;
20
21
using OpenTelemetry . Internal ;
21
22
@@ -33,21 +34,25 @@ internal sealed class HttpInMetricsListener : ListenerHandler
33
34
34
35
internal const string OnUnhandledHostingExceptionEvent = "Microsoft.AspNetCore.Hosting.UnhandledException" ;
35
36
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
+
36
43
private const string OnStopEvent = "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop" ;
44
+
37
45
private const string EventName = "OnStopActivity" ;
38
46
private const string NetworkProtocolName = "http" ;
39
47
private static readonly PropertyFetcher < Exception > ExceptionPropertyFetcher = new ( "Exception" ) ;
40
48
private static readonly PropertyFetcher < HttpContext > HttpContextPropertyFetcher = new ( "HttpContext" ) ;
41
49
private static readonly object ErrorTypeHttpContextItemsKey = new ( ) ;
42
50
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." ) ;
45
52
46
- internal HttpInMetricsListener ( string name , Meter meter )
53
+ internal HttpInMetricsListener ( string name )
47
54
: base ( name )
48
55
{
49
- this . meter = meter ;
50
- this . httpServerRequestDuration = meter . CreateHistogram < double > ( HttpServerRequestDurationMetricName , "s" , "Duration of HTTP server requests." ) ;
51
56
}
52
57
53
58
public override void OnEventWritten ( string name , object payload )
@@ -130,6 +135,6 @@ public void OnStopEventWritten(string name, object payload)
130
135
// We are relying here on ASP.NET Core to set duration before writing the stop event.
131
136
// https://github.com/dotnet/aspnetcore/blob/d6fa351048617ae1c8b47493ba1abbe94c3a24cf/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L449
132
137
// 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 ) ;
134
139
}
135
140
}
0 commit comments