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

[HttpClient] Add error.type for traces and metrics #5005

Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
13 changes: 13 additions & 0 deletions src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
`http` or `http/dup`.
([#5003](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5003))

* An additional attribute `error.type` will be added to activity and
`http.client.request.duration` metric in case of failed requests as per the
[specification](https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md#common-attributes).

Users moving to `net8.0` or newer frameworks from lower versions will see
difference in values in case of an exception. `net8.0` or newer frameworks adds
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
the ability to further drilldown the exceptions to a specific type through
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
[HttpRequestError](https://learn.microsoft.com/dotnet/api/system.net.http.httprequesterror?view=net-8.0)
enum. For lower versions the individual types will be rolled in to a single
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
type. This could be a **breaking change** if alerts are set based on the values.

([#5005](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5005))

## 1.6.0-beta.2

Released 2023-Oct-26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ public void OnStopActivity(Activity activity, object payload)

if (TryFetchResponse(payload, out HttpResponseMessage response))
{
if (currentStatusCode == ActivityStatusCode.Unset)
{
activity.SetStatus(SpanHelper.ResolveSpanStatusForHttpStatusCode(activity.Kind, (int)response.StatusCode));
Copy link
Member

Choose a reason for hiding this comment

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

is this change related to "error.type" ?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, this was there already

if (currentStatusCode == ActivityStatusCode.Unset)
{
activity.SetStatus(SpanHelper.ResolveSpanStatusForHttpStatusCode(activity.Kind, (int)response.StatusCode));
}
. I just moved it to the top.

}

if (this.emitOldAttributes)
{
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
Expand All @@ -279,11 +284,10 @@ public void OnStopActivity(Activity activity, object payload)
if (this.emitNewAttributes)
{
activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
}

if (currentStatusCode == ActivityStatusCode.Unset)
{
activity.SetStatus(SpanHelper.ResolveSpanStatusForHttpStatusCode(activity.Kind, (int)response.StatusCode));
if (activity.Status == ActivityStatusCode.Error)
{
activity.SetTag(SemanticConventions.AttributeErrorType, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
}
}

try
Expand Down Expand Up @@ -337,6 +341,18 @@ public void OnException(Activity activity, object payload)
return;
}

if (this.emitNewAttributes)
{
#if NET8_0_OR_GREATER
// For net8.0 and above exception type can be found using HttpRequestError.
// https://learn.microsoft.com/dotnet/api/system.net.http.httprequesterror?view=net-8.0
var errorType = GetErrorType(exc);
#else
var errorType = exc.GetType().FullName;
#endif
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
activity.SetTag(SemanticConventions.AttributeErrorType, errorType);
}

if (this.options.RecordException)
{
activity.RecordException(exc);
Expand Down Expand Up @@ -372,4 +388,35 @@ static bool TryFetchException(object payload, out Exception exc)
return true;
}
}

#if NET8_0_OR_GREATER
private static object GetErrorType(Exception exc)
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
var httpRequestException = exc as HttpRequestException;
if (httpRequestException != null)
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
return httpRequestException.HttpRequestError switch
{
HttpRequestError.NameResolutionError => "name_resolution_error",
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
HttpRequestError.ConnectionError => "connection_error",
HttpRequestError.SecureConnectionError => "secure_connection_error",
HttpRequestError.HttpProtocolError => "http_protocol_error",
HttpRequestError.ExtendedConnectNotSupported => "extended_connect_not_supported",
HttpRequestError.VersionNegotiationError => "version_negotiation_error",
HttpRequestError.UserAuthenticationError => "user_authentication_error",
HttpRequestError.ProxyTunnelError => "proxy_tunnel_error",
HttpRequestError.InvalidResponse => "invalid_response",
HttpRequestError.ResponseEnded => "response_ended",
HttpRequestError.ConfigurationLimitExceeded => "configuration_limit_exceeded",

// Fall back to the exception type name in case of HttpRequestError.Unknown
_ => exc.GetType().FullName,
};
}
else
{
return exc.GetType().FullName;
}
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
}
#endif
}
Loading