Skip to content

Commit 1374a7d

Browse files
KielekpellaredvishweshbankwarCodeBlanch
authored
[Instrumentation.AspNetCore] Fix span names when http.request.method is reporter as _OTHER (#5484)
Co-authored-by: Robert Pająk <[email protected]> Co-authored-by: Vishwesh Bankwar <[email protected]> Co-authored-by: Mikel Blanchard <[email protected]>
1 parent d64318f commit 1374a7d

File tree

7 files changed

+28
-30
lines changed

7 files changed

+28
-30
lines changed

src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
the original value `Get` will be stored in `http.request.method_original`.
1414
([#5471](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5471))
1515

16+
* Fixed the name of spans that have `http.request.method` attribute set to `_OTHER`.
17+
The span name will be set as `HTTP {http.route}` as per the [specification](https://github.com/open-telemetry/semantic-conventions/blob/v1.24.0/docs/http/http-spans.md#name).
18+
([#5484](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5484))
19+
1620
## 1.7.1
1721

1822
Released 2024-Feb-09

src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void OnStartActivity(Activity activity, object payload)
177177
}
178178

179179
var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/";
180-
activity.DisplayName = GetDisplayName(request.Method);
180+
RequestMethodHelper.SetActivityDisplayName(activity, request.Method);
181181

182182
// see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md
183183

@@ -241,7 +241,7 @@ public void OnStopActivity(Activity activity, object payload)
241241
context.GetEndpoint() as RouteEndpoint)?.RoutePattern.RawText;
242242
if (!string.IsNullOrEmpty(routePattern))
243243
{
244-
activity.DisplayName = GetDisplayName(context.Request.Method, routePattern);
244+
RequestMethodHelper.SetActivityDisplayName(activity, context.Request.Method, routePattern);
245245
activity.SetTag(SemanticConventions.AttributeHttpRoute, routePattern);
246246
}
247247
#endif
@@ -388,13 +388,4 @@ private static void AddGrpcAttributes(Activity activity, string grpcMethod, Http
388388
}
389389
}
390390
}
391-
392-
private static string GetDisplayName(string httpMethod, string httpRoute = null)
393-
{
394-
var normalizedMethod = RequestMethodHelper.GetNormalizedHttpMethod(httpMethod);
395-
396-
return string.IsNullOrEmpty(httpRoute)
397-
? normalizedMethod
398-
: $"{normalizedMethod} {httpRoute}";
399-
}
400391
}

src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void OnStartActivity(Activity activity, object payload)
135135
return;
136136
}
137137

138-
RequestMethodHelper.SetHttpClientActivityDisplayName(activity, request.Method.Method);
138+
RequestMethodHelper.SetActivityDisplayName(activity, request.Method.Method);
139139

140140
if (!IsNet7OrGreater)
141141
{

src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ internal static HttpClientTraceInstrumentationOptions TracingOptions
9595
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9696
private static void AddRequestTagsAndInstrumentRequest(HttpWebRequest request, Activity activity)
9797
{
98-
RequestMethodHelper.SetHttpClientActivityDisplayName(activity, request.Method);
98+
RequestMethodHelper.SetActivityDisplayName(activity, request.Method);
9999

100100
if (activity.IsAllDataRequested)
101101
{

src/Shared/RequestMethodHelper.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
#if NET8_0_OR_GREATER
57
using System.Collections.Frozen;
68
#endif
@@ -62,9 +64,11 @@ public static void SetHttpMethodTag(Activity activity, string originalHttpMethod
6264
}
6365
}
6466

65-
public static void SetHttpClientActivityDisplayName(Activity activity, string method)
67+
public static void SetActivityDisplayName(Activity activity, string method, string? httpRoute = null)
6668
{
67-
// https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md#name
68-
activity.DisplayName = KnownMethods.TryGetValue(method, out var httpMethod) ? httpMethod : "HTTP";
69+
// https://github.com/open-telemetry/semantic-conventions/blob/v1.24.0/docs/http/http-spans.md#name
70+
71+
var namePrefix = KnownMethods.TryGetValue(method, out var httpMethod) ? httpMethod : "HTTP";
72+
activity.DisplayName = string.IsNullOrEmpty(httpRoute) ? namePrefix : $"{namePrefix} {httpRoute}";
6973
}
7074
}

test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -629,18 +629,18 @@ void ConfigureTestServices(IServiceCollection services)
629629
}
630630

631631
[Theory]
632-
[InlineData("CONNECT", "CONNECT", null)]
633-
[InlineData("DELETE", "DELETE", null)]
634-
[InlineData("GET", "GET", null)]
635-
[InlineData("PUT", "PUT", null)]
636-
[InlineData("HEAD", "HEAD", null)]
637-
[InlineData("OPTIONS", "OPTIONS", null)]
638-
[InlineData("PATCH", "PATCH", null)]
639-
[InlineData("Get", "GET", "Get")]
640-
[InlineData("POST", "POST", null)]
641-
[InlineData("TRACE", "TRACE", null)]
642-
[InlineData("CUSTOM", "_OTHER", "CUSTOM")]
643-
public async Task HttpRequestMethodIsSetAsPerSpec(string originalMethod, string expectedMethod, string expectedOriginalMethod)
632+
[InlineData("CONNECT", "CONNECT", null, "CONNECT")]
633+
[InlineData("DELETE", "DELETE", null, "DELETE")]
634+
[InlineData("GET", "GET", null, "GET")]
635+
[InlineData("PUT", "PUT", null, "PUT")]
636+
[InlineData("HEAD", "HEAD", null, "HEAD")]
637+
[InlineData("OPTIONS", "OPTIONS", null, "OPTIONS")]
638+
[InlineData("PATCH", "PATCH", null, "PATCH")]
639+
[InlineData("Get", "GET", "Get", "GET")]
640+
[InlineData("POST", "POST", null, "POST")]
641+
[InlineData("TRACE", "TRACE", null, "TRACE")]
642+
[InlineData("CUSTOM", "_OTHER", "CUSTOM", "HTTP")]
643+
public async Task HttpRequestMethodAndActivityDisplayIsSetAsPerSpec(string originalMethod, string expectedMethod, string expectedOriginalMethod, string expectedDisplayName)
644644
{
645645
var exportedItems = new List<Activity>();
646646

@@ -683,6 +683,7 @@ void ConfigureTestServices(IServiceCollection services)
683683

684684
Assert.Equal(expectedMethod, activity.GetTagValue(SemanticConventions.AttributeHttpRequestMethod));
685685
Assert.Equal(expectedOriginalMethod, activity.GetTagValue(SemanticConventions.AttributeHttpRequestMethodOriginal));
686+
Assert.Equal(expectedDisplayName, activity.DisplayName);
686687
}
687688

688689
[Fact]

test/OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/RoutingTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ namespace RouteTests;
1414

1515
public class RoutingTests : IClassFixture<RoutingTestFixture>
1616
{
17-
private const string OldHttpStatusCode = "http.status_code";
18-
private const string OldHttpMethod = "http.method";
1917
private const string HttpStatusCode = "http.response.status_code";
2018
private const string HttpMethod = "http.request.method";
2119
private const string HttpRoute = "http.route";

0 commit comments

Comments
 (0)