diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs index 4fe276ae2cc..c2afcd02a81 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs @@ -42,32 +42,12 @@ internal HttpInMetricsListener(string name) { } - public override void OnEventWritten(string name, object payload) - { - switch (name) - { - case OnUnhandledDiagnosticsExceptionEvent: - case OnUnhandledHostingExceptionEvent: - { - this.OnExceptionEventWritten(name, payload); - } - - break; - case OnStopEvent: - { - this.OnStopEventWritten(name, payload); - } - - break; - } - } - - public void OnExceptionEventWritten(string name, object payload) + public static void OnExceptionEventWritten(string name, object payload) { // We need to use reflection here as the payload type is not a defined public type. if (!TryFetchException(payload, out Exception exc) || !TryFetchHttpContext(payload, out HttpContext ctx)) { - AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), nameof(this.OnExceptionEventWritten), HttpServerRequestDurationMetricName); + AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), nameof(OnExceptionEventWritten), HttpServerRequestDurationMetricName); return; } @@ -88,12 +68,12 @@ static bool TryFetchHttpContext(object payload, out HttpContext ctx) => HttpContextPropertyFetcher.TryFetch(payload, out ctx) && ctx != null; } - public void OnStopEventWritten(string name, object payload) + public static void OnStopEventWritten(string name, object payload) { var context = payload as HttpContext; if (context == null) { - AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), EventName, HttpServerRequestDurationMetricName); + AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), nameof(OnStopEventWritten), HttpServerRequestDurationMetricName); return; } @@ -124,4 +104,24 @@ public void OnStopEventWritten(string name, object payload) // TODO: Follow up with .NET team if we can continue to rely on this behavior. HttpServerRequestDuration.Record(Activity.Current.Duration.TotalSeconds, tags); } + + public override void OnEventWritten(string name, object payload) + { + switch (name) + { + case OnUnhandledDiagnosticsExceptionEvent: + case OnUnhandledHostingExceptionEvent: + { + OnExceptionEventWritten(name, payload); + } + + break; + case OnStopEvent: + { + OnStopEventWritten(name, payload); + } + + break; + } + } } diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs index 496b739f573..eb85c8fe23c 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs @@ -31,7 +31,7 @@ internal sealed class HttpHandlerMetricsDiagnosticListener : ListenerHandler private static readonly PropertyFetcher StopExceptionFetcher = new("Exception"); private static readonly PropertyFetcher RequestFetcher = new("Request"); #if NET6_0_OR_GREATER - private static readonly HttpRequestOptionsKey HttpRequestOptionsErrorKey = new HttpRequestOptionsKey(SemanticConventions.AttributeErrorType); + private static readonly HttpRequestOptionsKey HttpRequestOptionsErrorKey = new(SemanticConventions.AttributeErrorType); #endif public HttpHandlerMetricsDiagnosticListener(string name) @@ -39,19 +39,7 @@ public HttpHandlerMetricsDiagnosticListener(string name) { } - public override void OnEventWritten(string name, object payload) - { - if (name == OnUnhandledExceptionEvent) - { - this.OnExceptionEventWritten(Activity.Current, payload); - } - else if (name == OnStopEvent) - { - this.OnStopEventWritten(Activity.Current, payload); - } - } - - public void OnStopEventWritten(Activity activity, object payload) + public static void OnStopEventWritten(Activity activity, object payload) { if (TryFetchRequest(payload, out HttpRequestMessage request)) { @@ -129,11 +117,11 @@ static bool TryFetchResponse(object payload, out HttpResponseMessage response) = StopResponseFetcher.TryFetch(payload, out response) && response != null; } - public void OnExceptionEventWritten(Activity activity, object payload) + public static void OnExceptionEventWritten(Activity activity, object payload) { if (!TryFetchException(payload, out Exception exc) || !TryFetchRequest(payload, out HttpRequestMessage request)) { - HttpInstrumentationEventSource.Log.NullPayload(nameof(HttpHandlerMetricsDiagnosticListener), nameof(this.OnExceptionEventWritten)); + HttpInstrumentationEventSource.Log.NullPayload(nameof(HttpHandlerMetricsDiagnosticListener), nameof(OnExceptionEventWritten)); return; } @@ -173,4 +161,16 @@ static bool TryFetchRequest(object payload, out HttpRequestMessage request) return true; } } + + public override void OnEventWritten(string name, object payload) + { + if (name == OnStopEvent) + { + OnStopEventWritten(Activity.Current, payload); + } + else if (name == OnUnhandledExceptionEvent) + { + OnExceptionEventWritten(Activity.Current, payload); + } + } }