-
Notifications
You must be signed in to change notification settings - Fork 283
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
[Instrumentation.AspNet] pass http.request.method to sampler #2023
base: main
Are you sure you want to change the base?
Changes from all commits
961e5be
993d7bc
29fbce7
9078fe8
353d9d6
d6d0942
dce0aac
d2e511e
8855fd1
f62e71c
4c3c59a
114cabb
8494e65
36496b0
606b118
34e7fa3
128ac4a
ae37251
4ea758b
b56b68f
b1cdc34
06d8c26
7498fde
1c59dd0
1241783
4c0f465
7cafdcf
0010779
0e07997
81830ed
7b7c123
511702f
76fb410
2529b1b
946aca3
49b45d8
5c1b72e
a3f94ea
ccb159a
b694696
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -7,6 +7,7 @@ | |||
using OpenTelemetry.Context; | ||||
using OpenTelemetry.Context.Propagation; | ||||
using OpenTelemetry.Internal; | ||||
using OpenTelemetry.Trace; | ||||
|
||||
namespace OpenTelemetry.Instrumentation.AspNet; | ||||
|
||||
|
@@ -20,6 +21,7 @@ internal static class ActivityHelper | |||
/// </summary> | ||||
internal const string ContextKey = "__AspnetInstrumentationContext__"; | ||||
internal static readonly object StartedButNotSampledObj = new(); | ||||
internal static readonly RequestDataHelper RequestDataHelper = new(configureByHttpKnownMethodsEnvironmentalVariable: true); | ||||
|
||||
private const string BaggageSlotName = "otel.baggage"; | ||||
private static readonly Func<HttpRequest, string, IEnumerable<string>> HttpRequestHeaderValuesGetter = (request, name) => request.Headers.GetValues(name); | ||||
|
@@ -28,7 +30,7 @@ internal static class ActivityHelper | |||
typeof(ActivityHelper).Assembly.GetPackageVersion()); | ||||
|
||||
[ThreadStatic] | ||||
private static KeyValuePair<string, object?>[]? cachedTagsStorage; | ||||
private static List<KeyValuePair<string, object?>>? cachedTagsStorage; | ||||
|
||||
/// <summary> | ||||
/// Try to get the started <see cref="Activity"/> for the running <see | ||||
|
@@ -63,25 +65,29 @@ public static bool HasStarted(HttpContext context, out Activity? aspNetActivity) | |||
{ | ||||
PropagationContext propagationContext = textMapPropagator.Extract(default, context.Request, HttpRequestHeaderValuesGetter); | ||||
|
||||
KeyValuePair<string, object?>[]? tags; | ||||
if (context.Request?.Unvalidated?.Path is string path) | ||||
{ | ||||
tags = cachedTagsStorage ??= new KeyValuePair<string, object?>[1]; | ||||
string? path = context.Request?.Unvalidated?.Path; | ||||
string? originalHttpMethod = context.Request?.HttpMethod; | ||||
|
||||
tags[0] = new KeyValuePair<string, object?>("url.path", path); | ||||
} | ||||
else | ||||
string normalizedHttpMethod = RequestDataHelper.GetNormalizedHttpMethod(originalHttpMethod ?? string.Empty); | ||||
|
||||
List<KeyValuePair<string, object?>> tags = cachedTagsStorage ??= new List<KeyValuePair<string, object?>>(3); // use max capacity | ||||
|
||||
if (path is not null) | ||||
{ | ||||
tags = null; | ||||
tags.Add(new KeyValuePair<string, object?>(SemanticConventions.AttributeUrlPath, path)); | ||||
} | ||||
|
||||
Activity? activity = AspNetSource.StartActivity(TelemetryHttpModule.AspNetActivityName, ActivityKind.Server, propagationContext.ActivityContext, tags); | ||||
tags.Add(new KeyValuePair<string, object?>(SemanticConventions.AttributeHttpRequestMethod, normalizedHttpMethod)); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
RequestDataHelper.SetHttpMethodTag(tags, originalHttpMethod: context.Request.HttpMethod);
class RequestDataHelper
{
public void SetHttpMethodTag(List<KeyValuePair<string, object?>> tags, string originalHttpMethod)
{
var normalizedHttpMethod = this.GetNormalizedHttpMethod(originalHttpMethod);
tags.Add(new(SemanticConventions.AttributeHttpRequestMethod, normalizedHttpMethod));
if (originalHttpMethod != normalizedHttpMethod)
{
tags.Add(new(SemanticConventions.AttributeHttpRequestMethodOriginal, originalHttpMethod));
}
}
} Reason being it seems like all of this logic is already encapsulated inside |
||||
|
||||
if (tags is not null) | ||||
if (originalHttpMethod != null && originalHttpMethod != normalizedHttpMethod) | ||||
{ | ||||
tags[0] = default; | ||||
tags.Add(new KeyValuePair<string, object?>(SemanticConventions.AttributeHttpRequestMethodOriginal, originalHttpMethod)); | ||||
} | ||||
|
||||
Activity? activity = AspNetSource.StartActivity(TelemetryHttpModule.AspNetActivityName, ActivityKind.Server, propagationContext.ActivityContext, tags); | ||||
|
||||
tags.Clear(); | ||||
|
||||
if (activity != null) | ||||
{ | ||||
if (textMapPropagator is not TraceContextPropagator) | ||||
|
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.
Is it possible for
context
to benull
here?