Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla committed Dec 12, 2023
1 parent 486ee58 commit d100428
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,15 @@ internal sealed class HttpHandlerMetricsDiagnosticListener : ListenerHandler
private static readonly PropertyFetcher<Exception> StopExceptionFetcher = new("Exception");
private static readonly PropertyFetcher<HttpRequestMessage> RequestFetcher = new("Request");
#if NET6_0_OR_GREATER
private static readonly HttpRequestOptionsKey<string> HttpRequestOptionsErrorKey = new HttpRequestOptionsKey<string>(SemanticConventions.AttributeErrorType);
private static readonly HttpRequestOptionsKey<string> HttpRequestOptionsErrorKey = new(SemanticConventions.AttributeErrorType);
#endif

public HttpHandlerMetricsDiagnosticListener(string name)
: base(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))
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -173,4 +161,16 @@ static bool TryFetchRequest(object payload, out HttpRequestMessage request)
return true;
}
}

public override void OnEventWritten(string name, object payload)
{
if (name == OnUnhandledExceptionEvent)
{
OnExceptionEventWritten(Activity.Current, payload);
}
else if (name == OnStopEvent)
{
OnStopEventWritten(Activity.Current, payload);
}
}
}

0 comments on commit d100428

Please sign in to comment.