@@ -271,6 +271,11 @@ public void OnStopActivity(Activity activity, object payload)
271271
272272 if ( TryFetchResponse ( payload , out HttpResponseMessage response ) )
273273 {
274+ if ( currentStatusCode == ActivityStatusCode . Unset )
275+ {
276+ activity . SetStatus ( SpanHelper . ResolveSpanStatusForHttpStatusCode ( activity . Kind , ( int ) response . StatusCode ) ) ;
277+ }
278+
274279 if ( this . emitOldAttributes )
275280 {
276281 activity . SetTag ( SemanticConventions . AttributeHttpStatusCode , TelemetryHelper . GetBoxedStatusCode ( response . StatusCode ) ) ;
@@ -279,11 +284,10 @@ public void OnStopActivity(Activity activity, object payload)
279284 if ( this . emitNewAttributes )
280285 {
281286 activity . SetTag ( SemanticConventions . AttributeHttpResponseStatusCode , TelemetryHelper . GetBoxedStatusCode ( response . StatusCode ) ) ;
282- }
283-
284- if ( currentStatusCode == ActivityStatusCode . Unset )
285- {
286- activity . SetStatus ( SpanHelper . ResolveSpanStatusForHttpStatusCode ( activity . Kind , ( int ) response . StatusCode ) ) ;
287+ if ( activity . Status == ActivityStatusCode . Error )
288+ {
289+ activity . SetTag ( SemanticConventions . AttributeErrorType , TelemetryHelper . GetBoxedStatusCode ( response . StatusCode ) ) ;
290+ }
287291 }
288292
289293 try
@@ -337,14 +341,19 @@ public void OnException(Activity activity, object payload)
337341 return ;
338342 }
339343
344+ if ( this . emitNewAttributes )
345+ {
346+ activity . SetTag ( SemanticConventions . AttributeErrorType , GetErrorType ( exc ) ) ;
347+ }
348+
340349 if ( this . options . RecordException )
341350 {
342351 activity . RecordException ( exc ) ;
343352 }
344353
345354 if ( exc is HttpRequestException )
346355 {
347- activity . SetStatus ( ActivityStatusCode . Error , exc . Message ) ;
356+ activity . SetStatus ( ActivityStatusCode . Error ) ;
348357 }
349358
350359 try
@@ -372,4 +381,33 @@ static bool TryFetchException(object payload, out Exception exc)
372381 return true ;
373382 }
374383 }
384+
385+ private static string GetErrorType ( Exception exc )
386+ {
387+ #if NET8_0_OR_GREATER
388+ // For net8.0 and above exception type can be found using HttpRequestError.
389+ // https://learn.microsoft.com/dotnet/api/system.net.http.httprequesterror?view=net-8.0
390+ if ( exc is HttpRequestException httpRequestException )
391+ {
392+ return httpRequestException . HttpRequestError switch
393+ {
394+ HttpRequestError . NameResolutionError => "name_resolution_error" ,
395+ HttpRequestError . ConnectionError => "connection_error" ,
396+ HttpRequestError . SecureConnectionError => "secure_connection_error" ,
397+ HttpRequestError . HttpProtocolError => "http_protocol_error" ,
398+ HttpRequestError . ExtendedConnectNotSupported => "extended_connect_not_supported" ,
399+ HttpRequestError . VersionNegotiationError => "version_negotiation_error" ,
400+ HttpRequestError . UserAuthenticationError => "user_authentication_error" ,
401+ HttpRequestError . ProxyTunnelError => "proxy_tunnel_error" ,
402+ HttpRequestError . InvalidResponse => "invalid_response" ,
403+ HttpRequestError . ResponseEnded => "response_ended" ,
404+ HttpRequestError . ConfigurationLimitExceeded => "configuration_limit_exceeded" ,
405+
406+ // Fall back to the exception type name in case of HttpRequestError.Unknown
407+ _ => exc . GetType ( ) . FullName ,
408+ } ;
409+ }
410+ #endif
411+ return exc . GetType ( ) . FullName ;
412+ }
375413}
0 commit comments