Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactoring for instrumentation libraries #5162

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this to log the name of the method instead.

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 == OnStopEvent)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed the order of the if conditions. For the common case, we don't want to waste cycles doing a string match for the exception event first.

{
OnStopEventWritten(Activity.Current, payload);
}
else if (name == OnUnhandledExceptionEvent)
{
OnExceptionEventWritten(Activity.Current, payload);
}
}
}