-
Notifications
You must be signed in to change notification settings - Fork 5k
Add standard tags to HttpClient native trace instrumentation #104251
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1ee8597
DiagnosticsHandler: emit common tags
antonfirsov b0cefd9
Merge branch 'main' into DiagnosticsHandler-sandbox-01
antonfirsov f4ac8ce
Merge branch 'main' into DiagnosticsHandler-sandbox-01
antonfirsov 719e532
Merge branch 'main' into DiagnosticsHandler-sandbox-01
antonfirsov 295c20c
DiagnosticsHelper.cs
antonfirsov 7db6551
emit url.full
antonfirsov 7cf47e1
only emit attributes when IsAllDataRequested is set
antonfirsov 88855cf
pass request tags at activity creation time
antonfirsov a008d02
consolidate Uri redaction
antonfirsov ea311f3
implement http.request.method_original
antonfirsov 4c78567
set ActivityStatusCode.Error on errors
antonfirsov a40530b
Merge branch 'main' into request-attributes-01
antonfirsov d33c2fb
undo passing tags at Activity creation
antonfirsov 7f0ad44
Make test conditional
antonfirsov c5a4582
change file order in csproj
antonfirsov a9eae60
Merge branch 'main' into request-attributes-01
antonfirsov c76b5a2
set the span name to {method}
antonfirsov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/libraries/System.Net.Http/src/System/Net/Http/DiagnosticsHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
|
||
namespace System.Net.Http | ||
{ | ||
internal static class DiagnosticsHelper | ||
{ | ||
internal static string GetRedactedUriString(Uri uri) | ||
{ | ||
Debug.Assert(uri.IsAbsoluteUri); | ||
|
||
if (GlobalHttpSettings.DiagnosticsHandler.DisableUriRedaction) | ||
noahfalk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return uri.AbsoluteUri; | ||
} | ||
|
||
string pathAndQuery = uri.PathAndQuery; | ||
int queryIndex = pathAndQuery.IndexOf('?'); | ||
|
||
bool redactQuery = queryIndex >= 0 && // Query is present. | ||
queryIndex < pathAndQuery.Length - 1; // Query is not empty. | ||
antonfirsov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return (redactQuery, uri.IsDefaultPort) switch | ||
{ | ||
(true, true) => $"{uri.Scheme}://{uri.Host}{pathAndQuery.AsSpan(0, queryIndex + 1)}*", | ||
(true, false) => $"{uri.Scheme}://{uri.Host}:{uri.Port}{pathAndQuery.AsSpan(0, queryIndex + 1)}*", | ||
(false, true) => $"{uri.Scheme}://{uri.Host}{pathAndQuery}", | ||
(false, false) => $"{uri.Scheme}://{uri.Host}:{uri.Port}{pathAndQuery}" | ||
}; | ||
} | ||
|
||
internal static KeyValuePair<string, object?> GetMethodTag(HttpMethod method, out bool isUnknownMethod) | ||
{ | ||
// Return canonical names for known methods and "_OTHER" for unknown ones. | ||
antonfirsov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
HttpMethod? known = HttpMethod.GetKnownMethod(method.Method); | ||
isUnknownMethod = known is null; | ||
return new KeyValuePair<string, object?>("http.request.method", isUnknownMethod ? "_OTHER" : known!.Method); | ||
} | ||
|
||
internal static string GetProtocolVersionString(Version httpVersion) => (httpVersion.Major, httpVersion.Minor) switch | ||
{ | ||
(1, 0) => "1.0", | ||
(1, 1) => "1.1", | ||
(2, 0) => "2", | ||
(3, 0) => "3", | ||
_ => httpVersion.ToString() | ||
}; | ||
|
||
public static bool TryGetErrorType(HttpResponseMessage? response, Exception? exception, out string? errorType) | ||
{ | ||
if (response is not null) | ||
{ | ||
int statusCode = (int)response.StatusCode; | ||
|
||
// In case the status code indicates a client or a server error, return the string representation of the status code. | ||
// See the paragraph Status and the definition of 'error.type' in | ||
// https://github.com/open-telemetry/semantic-conventions/blob/2bad9afad58fbd6b33cc683d1ad1f006e35e4a5d/docs/http/http-spans.md | ||
if (statusCode >= 400 && statusCode <= 599) | ||
{ | ||
errorType = GetErrorStatusCodeString(statusCode); | ||
return true; | ||
} | ||
} | ||
|
||
if (exception is null) | ||
{ | ||
errorType = null; | ||
return false; | ||
} | ||
|
||
Debug.Assert(Enum.GetValues<HttpRequestError>().Length == 12, "We need to extend the mapping in case new values are added to HttpRequestError."); | ||
errorType = (exception as HttpRequestException)?.HttpRequestError switch | ||
{ | ||
HttpRequestError.NameResolutionError => "name_resolution_error", | ||
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 or when exception is not an HttpRequestException. | ||
_ => exception.GetType().FullName! | ||
}; | ||
return true; | ||
} | ||
|
||
private static object[]? s_boxedStatusCodes; | ||
private static string[]? s_statusCodeStrings; | ||
|
||
#pragma warning disable CA1859 // we explictly box here | ||
public static object GetBoxedStatusCode(int statusCode) | ||
{ | ||
object[] boxes = LazyInitializer.EnsureInitialized(ref s_boxedStatusCodes, static () => new object[512]); | ||
antonfirsov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return (uint)statusCode < (uint)boxes.Length | ||
? boxes[statusCode] ??= statusCode | ||
: statusCode; | ||
} | ||
#pragma warning restore | ||
|
||
private static string GetErrorStatusCodeString(int statusCode) | ||
{ | ||
Debug.Assert(statusCode >= 400 && statusCode <= 599); | ||
|
||
string[] strings = LazyInitializer.EnsureInitialized(ref s_statusCodeStrings, static () => new string[200]); | ||
int index = statusCode - 400; | ||
return (uint)index < (uint)strings.Length | ||
? strings[index] ??= statusCode.ToString() | ||
: statusCode.ToString(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.