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

[ASP.NET Core] Add error.type attribute for tracing and metrics #4986

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
15fd6ff
draft
vishweshbankwar Oct 18, 2023
80ac64f
Merge branch 'main' into vibankwa/add-error-type-aspnetcore
vishweshbankwar Oct 23, 2023
fbe3eb5
set error.type for asp.net core
vishweshbankwar Oct 24, 2023
219519f
Merge branch 'main' into vibankwa/add-error-type-aspnetcore
vishweshbankwar Oct 24, 2023
1d03f34
rmv using
vishweshbankwar Oct 24, 2023
a7e39de
Merge branch 'main' into vibankwa/add-error-type-aspnetcore
vishweshbankwar Nov 1, 2023
e240c6b
refactor
vishweshbankwar Nov 1, 2023
f6a8e42
changelog
vishweshbankwar Nov 1, 2023
34419b5
align
vishweshbankwar Nov 1, 2023
6a173ab
property name
vishweshbankwar Nov 1, 2023
2ad4a4a
Merge branch 'main' into vibankwa/add-error-type-aspnetcore
vishweshbankwar Nov 1, 2023
386573d
rmv unused code
vishweshbankwar Nov 1, 2023
a84e7a9
address comment
vishweshbankwar Nov 1, 2023
2350dc7
fix description
vishweshbankwar Nov 1, 2023
2f44398
switch to activity tag from context.items
vishweshbankwar Nov 2, 2023
d9a2b19
use activity custom property
vishweshbankwar Nov 2, 2023
ef2bdd3
rmv default param
vishweshbankwar Nov 2, 2023
ea5e5dc
Merge branch 'main' into vibankwa/add-error-type-aspnetcore
vishweshbankwar Nov 2, 2023
cd73339
refactor test
vishweshbankwar Nov 2, 2023
20be1e3
Merge branch 'vibankwa/add-error-type-aspnetcore' of https://github.c…
vishweshbankwar Nov 2, 2023
6bf11e0
Merge branch 'main' into vibankwa/add-error-type-aspnetcore
utpilla Nov 2, 2023
9777eac
assert
vishweshbankwar Nov 2, 2023
7927e10
Merge branch 'vibankwa/add-error-type-aspnetcore' of https://github.c…
vishweshbankwar Nov 2, 2023
a36104c
switch back to ctx.items
vishweshbankwar Nov 3, 2023
b59075c
Merge branch 'main' into vibankwa/add-error-type-aspnetcore
vishweshbankwar Nov 3, 2023
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 @@ -36,6 +36,8 @@ internal sealed class AspNetCoreMetrics : IDisposable
"Microsoft.AspNetCore.Hosting.HttpRequestIn",
"Microsoft.AspNetCore.Hosting.HttpRequestIn.Start",
"Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop",
"Microsoft.AspNetCore.Diagnostics.UnhandledException",
"Microsoft.AspNetCore.Hosting.UnhandledException",
};

private readonly Func<string, object, object, bool> isEnabled = (eventName, _, _)
Expand Down
8 changes: 8 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
`http` or `http/dup`.
([#5001](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5001))

* An additional attribute `error.type` will be added to activity and
`http.server.request.duration` metric when the request results in unhandled
exception. The attribute value will be set to full name of exception type.

The attribute will only be added when `OTEL_SEMCONV_STABILITY_OPT_IN`
environment variable is set to `http` or `http/dup`.
([#4986](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4986))

## 1.6.0-beta.2

Released 2023-Oct-26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ public void OnException(Activity activity, object payload)
return;
}

if (this.emitNewAttributes)
{
activity.SetTag(SemanticConventions.AttributeErrorType, exc.GetType().FullName);
}

if (this.options.RecordException)
{
activity.RecordException(exc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using OpenTelemetry.Internal;

#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Routing;
#endif
using OpenTelemetry.Trace;
Expand All @@ -32,9 +33,14 @@ internal sealed class HttpInMetricsListener : ListenerHandler
internal const string HttpServerDurationMetricName = "http.server.duration";
internal const string HttpServerRequestDurationMetricName = "http.server.request.duration";

internal const string OnUnhandledHostingExceptionEvent = "Microsoft.AspNetCore.Hosting.UnhandledException";
internal const string OnUnhandledDiagnosticsExceptionEvent = "Microsoft.AspNetCore.Diagnostics.UnhandledException";
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
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");
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
private static readonly PropertyFetcher<HttpContext> HttpContextPropertyFetcher = new("httpContext");
private static readonly object ErrorTypeKey = "error.type";

private readonly Meter meter;
private readonly AspNetCoreMetricsInstrumentationOptions options;
Expand Down Expand Up @@ -66,23 +72,66 @@ internal HttpInMetricsListener(string name, Meter meter, AspNetCoreMetricsInstru

public override void OnEventWritten(string name, object payload)
{
if (name == OnStopEvent)
switch (name)
{
if (this.emitOldAttributes)
{
this.OnEventWritten_Old(name, payload);
}
case OnUnhandledDiagnosticsExceptionEvent:
case OnUnhandledHostingExceptionEvent:
{
if (this.emitNewAttributes)
{
this.OnExceptionEventWritten(name, payload);
}
}

break;
case OnStopEvent:
{
if (this.emitOldAttributes)
{
this.OnEventWritten_Old(name, payload);
}

if (this.emitNewAttributes)
{
this.OnEventWritten_New(name, payload);
}
}

break;
}
}

if (this.emitNewAttributes)
{
this.OnEventWritten_New(name, payload);
}
public 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(HttpInListener), nameof(this.OnExceptionEventWritten), HttpServerDurationMetricName);
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
return;
}

ctx.Items.Add(ErrorTypeKey, exc.GetType().FullName);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would the framework create an activity even if only add AspNetCore metrics instrumentation? If yes, then I think we could add this information to Activity.Current. Do you know of any obvious pros and cons to adding this information to HttpContext vs Activity.Current?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, the activity will always be there. Perf was the main reason to choose HttpContext.Items. However, we could use custom property as well on activity. Here are the results from benchmarking

Results when trace is also enabled and activity will have additional tags.

Method Mean Error StdDev Median Gen0 Allocated
UseActivityTag 82.36 ns 1.691 ns 1.736 ns 83.06 ns 0.0085 40 B
UseActivityCustomProperty 81.98 ns 1.133 ns 1.004 ns 81.54 ns - -
UseContextItems 81.11 ns 1.656 ns 3.420 ns 79.67 ns - -

Results when only metrics will be enabled and activity will only have one tag

Method Mean Error StdDev Gen0 Allocated
UseActivityTag 55.56 ns 0.572 ns 0.535 ns 0.0085 40 B
UseActivityCustomProperty 80.94 ns 0.538 ns 0.477 ns - -
UseContextItems 80.50 ns 0.901 ns 0.843 ns - -

Based on above, I have updated the implementation to use custom property. We could follow the same thing on HttpClient side where we need to add error.type.

Copy link
Member

Choose a reason for hiding this comment

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

IIRC the first time you add a custom property to activity you pay for an allocation. Are we already adding one somewhere else? Seems a bit odd that the allocation isn't showing in the benchmark.

Copy link
Member Author

Choose a reason for hiding this comment

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

@CodeBlanch Good catch. The alloc is not showing up in the benchmark as it is getting ignored due to multiple runs. I have switched back to using context.items and it improved the look up as well using the minor tweak on the context key suggested by you. here are the results.

Method Mean Error StdDev Gen0 Allocated
UseActivityTag 56.93 ns 0.572 ns 0.446 ns 0.0085 40 B
UseActivityCustomProperty 83.61 ns 0.546 ns 0.511 ns - -
UseContextItemsObjectKey 68.49 ns 0.429 ns 0.401 ns - -
UseContextItemsStringKey 84.91 ns 1.191 ns 0.994 ns - -


// See https://github.com/dotnet/aspnetcore/blob/690d78279e940d267669f825aa6627b0d731f64c/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L252
// and https://github.com/dotnet/aspnetcore/blob/690d78279e940d267669f825aa6627b0d731f64c/src/Middleware/Diagnostics/src/DeveloperExceptionPage/DeveloperExceptionPageMiddlewareImpl.cs#L174
// this makes sure that top-level properties on the payload object are always preserved.
#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The ASP.NET Core framework guarantees that top level properties are preserved")]
#endif
static bool TryFetchException(object payload, out Exception exc)
=> ExceptionPropertyFetcher.TryFetch(payload, out exc) && exc != null;

#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The ASP.NET Core framework guarantees that top level properties are preserved")]
#endif
static bool TryFetchHttpContext(object payload, out HttpContext ctx)
=> HttpContextPropertyFetcher.TryFetch(payload, out ctx) && ctx != null;
}

public void OnEventWritten_Old(string name, object payload)
{
var context = payload as HttpContext;

if (context == null)
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), EventName, HttpServerDurationMetricName);
Expand Down Expand Up @@ -171,6 +220,11 @@ public void OnEventWritten_New(string name, object payload)
}
#endif

if (context.Items.TryGetValue(ErrorTypeKey, out var errorType))
{
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeErrorType, errorType));
}

// 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.
Expand Down
1 change: 1 addition & 0 deletions src/Shared/SemanticConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ internal static class SemanticConventions
public const string AttributeExceptionType = "exception.type";
public const string AttributeExceptionMessage = "exception.message";
public const string AttributeExceptionStacktrace = "exception.stacktrace";
public const string AttributeErrorType = "error.type";

// v1.21.0
// https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/docs/http/http-spans.md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public IncomingRequestsCollectionsIsAccordingToTheSpecTests_New(WebApplicationFa
}

[Theory]
[InlineData("/api/values", null, "user-agent", 503, "503")]
[InlineData("/api/values", "?query=1", null, 503, null)]
[InlineData("/api/values", null, "user-agent", 200, null)]
[InlineData("/api/values", "?query=1", null, 200, null)]
[InlineData("/api/exception", null, null, 503, null)]
[InlineData("/api/exception", null, null, 503, null, true)]
public async Task SuccessfulTemplateControllerCallGeneratesASpan_New(
Expand Down Expand Up @@ -123,6 +123,7 @@ public async Task SuccessfulTemplateControllerCallGeneratesASpan_New(
if (statusCode == 503)
{
Assert.Equal(ActivityStatusCode.Error, activity.Status);
Assert.Equal("System.Exception", activity.GetTagValue(SemanticConventions.AttributeErrorType));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ public async Task ValidateNet8RateLimitingMetricsAsync()
}
#endif

[Fact]
public async Task RequestMetricIsCaptured_New()
[Theory]
[InlineData("/api/values/2", "api/Values/{id}", null, 200)]
[InlineData("/api/Error", "api/Error", "System.Exception", 500)]
public async Task RequestMetricIsCaptured_New(string api, string expectedRoute, string expectedErrorType, int expectedStatusCode)
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string> { [SemanticConventionOptInKeyName] = "http" })
Expand All @@ -207,11 +209,15 @@ public async Task RequestMetricIsCaptured_New()
})
.CreateClient())
{
using var response1 = await client.GetAsync("/api/values").ConfigureAwait(false);
using var response2 = await client.GetAsync("/api/values/2").ConfigureAwait(false);

response1.EnsureSuccessStatusCode();
response2.EnsureSuccessStatusCode();
try
{
using var response = await client.GetAsync(api).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
}
catch
{
// ignore error.
}
}

// We need to let End callback execute as it is executed AFTER response was returned.
Expand All @@ -229,12 +235,14 @@ public async Task RequestMetricIsCaptured_New()

Assert.Equal("s", metric.Unit);
var metricPoints = GetMetricPoints(metric);
Assert.Equal(2, metricPoints.Count);
Assert.Single(metricPoints);

AssertMetricPoints_New(
metricPoints: metricPoints,
expectedRoutes: new List<string> { "api/Values", "api/Values/{id}" },
expectedTagsCount: 6);
expectedRoutes: new List<string> { expectedRoute },
expectedErrorType,
expectedStatusCode,
expectedTagsCount: expectedErrorType == null ? 6 : 7);
}

[Theory]
Expand Down Expand Up @@ -430,6 +438,8 @@ public async Task RequestMetricIsCaptured_Dup()
AssertMetricPoints_New(
metricPoints: metricPoints,
expectedRoutes: new List<string> { "api/Values", "api/Values/{id}" },
null,
200,
expectedTagsCount: 6);
}
#endif
Expand All @@ -456,6 +466,8 @@ private static List<MetricPoint> GetMetricPoints(Metric metric)
private static void AssertMetricPoints_New(
List<MetricPoint> metricPoints,
List<string> expectedRoutes,
string expectedErrorType,
int expectedStatusCode,
int expectedTagsCount)
{
// Assert that one MetricPoint exists for each ExpectedRoute
Expand All @@ -476,7 +488,7 @@ private static void AssertMetricPoints_New(

if (metricPoint.HasValue)
{
AssertMetricPoint_New(metricPoint.Value, expectedRoute, expectedTagsCount);
AssertMetricPoint_New(metricPoint.Value, expectedRoute, expectedStatusCode, expectedErrorType, expectedTagsCount);
}
else
{
Expand Down Expand Up @@ -520,6 +532,8 @@ private static void AssertMetricPoints_Old(
private static KeyValuePair<string, object>[] AssertMetricPoint_New(
MetricPoint metricPoint,
string expectedRoute = "api/Values",
int expectedStatusCode = 200,
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
string expectedErrorType = null,
int expectedTagsCount = StandardTagsCount)
{
var count = metricPoint.GetHistogramCount();
Expand All @@ -540,7 +554,7 @@ private static KeyValuePair<string, object>[] AssertMetricPoint_New(

var method = new KeyValuePair<string, object>(SemanticConventions.AttributeHttpRequestMethod, "GET");
var scheme = new KeyValuePair<string, object>(SemanticConventions.AttributeUrlScheme, "http");
var statusCode = new KeyValuePair<string, object>(SemanticConventions.AttributeHttpResponseStatusCode, 200);
var statusCode = new KeyValuePair<string, object>(SemanticConventions.AttributeHttpResponseStatusCode, expectedStatusCode);
var flavor = new KeyValuePair<string, object>(SemanticConventions.AttributeNetworkProtocolVersion, "1.1");
var route = new KeyValuePair<string, object>(SemanticConventions.AttributeHttpRoute, expectedRoute);
Assert.Contains(method, attributes);
Expand All @@ -549,6 +563,17 @@ private static KeyValuePair<string, object>[] AssertMetricPoint_New(
Assert.Contains(flavor, attributes);
Assert.Contains(route, attributes);

if (expectedErrorType != null)
{
#if NET8_0_OR_GREATER
// Expected to change in next release
// https://github.com/dotnet/aspnetcore/issues/51029
var errorType = new KeyValuePair<string, object>("exception.type", expectedErrorType);
#else
var errorType = new KeyValuePair<string, object>(SemanticConventions.AttributeErrorType, expectedErrorType);
#endif
}

// Inspect Histogram Bounds
var histogramBuckets = metricPoint.GetHistogramBuckets();
var histogramBounds = new List<double>();
Expand Down