-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Initialize hosting trace with OTEL tags for sampling #62090
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 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
272cc6f
Feature for setting activity tags on creation
rkarg-blizz 67c6363
adding unshipped public API entries
rkarg-blizz 2edbad5
moving to TagList
rkarg-blizz 3fcaf85
Less specific property type
rkarg-blizz 3ec3501
Update HostingApplicationDiagnostics.cs
rkargMsft de03968
unconditionally adding host/port tags to Activity creation
rkargMsft 50cf8a9
correcting variable naming
rkargMsft af4426e
reverting HostAndPort property
rkargMsft 8a8dcbb
naming
rkargMsft 9a45fbf
Initialize hosting trace with OTEL tags for sampling
JamesNK 4d29363
Default to off
JamesNK 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
132 changes: 132 additions & 0 deletions
132
src/Hosting/Hosting/src/Internal/HostingTelemetryHelpers.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,132 @@ | ||
// 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.Frozen; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Hosting; | ||
|
||
internal static class HostingTelemetryHelpers | ||
{ | ||
// Semantic Conventions for HTTP. | ||
// Note: Not all telemetry code is using these const attribute names yet. | ||
public const string AttributeHttpRequestMethod = "http.request.method"; | ||
public const string AttributeHttpRequestMethodOriginal = "http.request.method_original"; | ||
public const string AttributeUrlScheme = "url.scheme"; | ||
public const string AttributeUrlPath = "url.path"; | ||
public const string AttributeServerAddress = "server.address"; | ||
public const string AttributeServerPort = "server.port"; | ||
public const string AttributeUserAgentOriginal = "user_agent.original"; | ||
|
||
// The value "_OTHER" is used for non-standard HTTP methods. | ||
// https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md#common-attributes | ||
private const string OtherHttpMethod = "_OTHER"; | ||
|
||
private static readonly object[] BoxedStatusCodes = new object[512]; | ||
|
||
private static readonly FrozenDictionary<string, string> KnownHttpMethods = FrozenDictionary.ToFrozenDictionary([ | ||
KeyValuePair.Create(HttpMethods.Connect, HttpMethods.Connect), | ||
KeyValuePair.Create(HttpMethods.Delete, HttpMethods.Delete), | ||
KeyValuePair.Create(HttpMethods.Get, HttpMethods.Get), | ||
KeyValuePair.Create(HttpMethods.Head, HttpMethods.Head), | ||
KeyValuePair.Create(HttpMethods.Options, HttpMethods.Options), | ||
KeyValuePair.Create(HttpMethods.Patch, HttpMethods.Patch), | ||
KeyValuePair.Create(HttpMethods.Post, HttpMethods.Post), | ||
KeyValuePair.Create(HttpMethods.Put, HttpMethods.Put), | ||
KeyValuePair.Create(HttpMethods.Trace, HttpMethods.Trace) | ||
], StringComparer.OrdinalIgnoreCase); | ||
|
||
// Boxed port values for HTTP and HTTPS. | ||
private static readonly object HttpPort = 80; | ||
private static readonly object HttpsPort = 443; | ||
|
||
public static bool TryGetServerPort(HostString host, string scheme, [NotNullWhen(true)] out object? port) | ||
{ | ||
if (host.Port.HasValue) | ||
{ | ||
port = host.Port.Value; | ||
return true; | ||
} | ||
|
||
// If the port is not specified, use the default port for the scheme. | ||
if (string.Equals(scheme, "http", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
port = HttpPort; | ||
return true; | ||
} | ||
else if (string.Equals(scheme, "https", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
port = HttpsPort; | ||
return true; | ||
} | ||
|
||
// Unknown scheme, no default port. | ||
port = null; | ||
return false; | ||
} | ||
|
||
public static object GetBoxedStatusCode(int statusCode) | ||
{ | ||
object[] boxes = BoxedStatusCodes; | ||
return (uint)statusCode < (uint)boxes.Length | ||
? boxes[statusCode] ??= statusCode | ||
: statusCode; | ||
} | ||
|
||
public static string GetNormalizedHttpMethod(string method) | ||
{ | ||
// TODO: Support configuration for configuring known methods | ||
if (method != null && KnownHttpMethods.TryGetValue(method, out var result)) | ||
{ | ||
// KnownHttpMethods ignores case. Use the value returned by the dictionary to have a consistent case. | ||
return result; | ||
} | ||
return OtherHttpMethod; | ||
} | ||
|
||
public static bool TryGetHttpVersion(string protocol, [NotNullWhen(true)] out string? version) | ||
{ | ||
if (HttpProtocol.IsHttp11(protocol)) | ||
{ | ||
version = "1.1"; | ||
return true; | ||
} | ||
if (HttpProtocol.IsHttp2(protocol)) | ||
{ | ||
// HTTP/2 only has one version. | ||
version = "2"; | ||
return true; | ||
} | ||
if (HttpProtocol.IsHttp3(protocol)) | ||
{ | ||
// HTTP/3 only has one version. | ||
version = "3"; | ||
return true; | ||
} | ||
if (HttpProtocol.IsHttp10(protocol)) | ||
{ | ||
version = "1.0"; | ||
return true; | ||
} | ||
if (HttpProtocol.IsHttp09(protocol)) | ||
{ | ||
version = "0.9"; | ||
return true; | ||
} | ||
version = null; | ||
return false; | ||
} | ||
|
||
public static void SetActivityHttpMethodTags(ref TagList tags, string originalHttpMethod) | ||
{ | ||
var normalizedHttpMethod = GetNormalizedHttpMethod(originalHttpMethod); | ||
tags.Add(AttributeHttpRequestMethod, normalizedHttpMethod); | ||
|
||
if (originalHttpMethod != normalizedHttpMethod) | ||
{ | ||
tags.Add(AttributeHttpRequestMethodOriginal, originalHttpMethod); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
QUERY
given #63260 went in yesterday?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This list is based on the known values list at https://opentelemetry.io/docs/specs/semconv/registry/attributes/http
You're welcome to create an issue with OTEL folks to add QUERY to the standard. Then we'll add it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I just took at quick look at the table at the top and because it didn't have everything, just assumed that it was more open:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
open-telemetry/semantic-conventions#2642