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 11 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";
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved

private readonly Meter meter;
private readonly AspNetCoreMetricsInstrumentationOptions options;
Expand Down Expand Up @@ -66,23 +72,67 @@ 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);

// 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 event source guarantees that top level properties are preserved")]
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
#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 event source guarantees that top level properties are preserved")]
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
#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;
context.Items.TryGetValue(name, out var item);
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved

if (context == null)
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), EventName, HttpServerDurationMetricName);
Expand Down Expand Up @@ -171,6 +221,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