Skip to content
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

[AzureMonitorexporter] fix operation name #41623

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

### Bugs Fixed

* Fix issue that caused Request OperationName to display identical routing pattern for different endpoints.
([41623](https:/Azure/azure-sdk-for-net/pull/41623))

### Other Changes

## 1.2.0 (2024-01-24)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Text;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Diagnostics;
using Azure.Monitor.OpenTelemetry.Exporter.Models;

using OpenTelemetry;

namespace Azure.Monitor.OpenTelemetry.Exporter.Internals
Expand Down Expand Up @@ -179,22 +178,22 @@ internal static string GetOperationName(Activity activity, ref AzMonList MappedT

internal static string GetOperationNameV2(Activity activity, ref AzMonList MappedTags)
{
var httpMethod = AzMonList.GetTagValue(ref MappedTags, SemanticConventions.AttributeHttpRequestMethod)?.ToString();
if (!string.IsNullOrWhiteSpace(httpMethod))
var httpRequestMethod = AzMonList.GetTagValue(ref MappedTags, SemanticConventions.AttributeHttpRequestMethod)?.ToString();
if (!string.IsNullOrWhiteSpace(httpRequestMethod))
{
var httpRoute = AzMonList.GetTagValue(ref MappedTags, SemanticConventions.AttributeHttpRoute)?.ToString();

// ASP.NET instrumentation assigns route as {controller}/{action}/{id} which would result in the same name for different operations.
// To work around that we will use path from url.path.
if (!string.IsNullOrWhiteSpace(httpRoute) && !httpRoute!.Contains("{controller}"))
if (!string.IsNullOrWhiteSpace(httpRoute) && !httpRoute!.Contains("{controller"))
{
return $"{httpMethod} {httpRoute}";
return $"{httpRequestMethod} {httpRoute}";
}

var httpPath = AzMonList.GetTagValue(ref MappedTags, SemanticConventions.AttributeUrlPath)?.ToString();
if (!string.IsNullOrWhiteSpace(httpPath))
var urlPath = AzMonList.GetTagValue(ref MappedTags, SemanticConventions.AttributeUrlPath)?.ToString();
if (!string.IsNullOrWhiteSpace(urlPath))
{
return $"{httpMethod} {httpPath}";
return $"{httpRequestMethod} {urlPath}";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

using Azure.Monitor.OpenTelemetry.Exporter.Internals;
using OpenTelemetry;
using OpenTelemetry.Trace;
Expand All @@ -18,24 +15,28 @@ public class TraceHelperNewTests
private const string ActivityName = "AzureMonitorTraceHelperTestsActivity";

[Theory]
[InlineData("GET", "/api/{controller}/{action}", "GET /api/urltest")]
[InlineData("GET", "/api/routetest", "GET /api/routetest")]
[InlineData("POST", "/api/routetest", "POST /api/routetest")]
public void GetNewOperationName_ValidateHttpMethodAndHttpRoute(string httpMethod, string httpRoute, string expected)
[InlineData("GET", "/api/{controller}/{action}", "/api/urltest", "GET /api/urltest")]
[InlineData("GET", "/api/routetest", "/api/urltest", "GET /api/routetest")]
[InlineData("POST", "/api/routetest", "/api/urltest", "POST /api/routetest")]
// CONVENTIONAL ROUTING
[InlineData("GET", "{controller=Home}/{action=Index}/{id?}", "/", "GET /")]
[InlineData("GET", "{controller=Home}/{action=Index}/{id?}", "/Home/Index", "GET /Home/Index")]
[InlineData("GET", "{controller=Home}/{action=Index}/{id?}", "/Customer/Index", "GET /Customer/Index")]
// MINIMAL API
[InlineData("GET", "/MinimalApi", "/minimalapi", "GET /MinimalApi")]
[InlineData("GET", "/MinimalApi/{id}", "/minimalapi/44", "GET /MinimalApi/{id}")]
public void GetNewOperationName_ValidateHttpMethodAndHttpRoute(string httpRequestMethod, string httpRoute, string urlPath, string expected)
{
// Arrange
using var tracerProvider = Sdk.CreateTracerProviderBuilder().AddSource(nameof(GetNewOperationName_ValidateHttpMethodAndHttpRoute)).Build();
using var activitySource = new ActivitySource(nameof(GetNewOperationName_ValidateHttpMethodAndHttpRoute));
using var activity = activitySource.StartActivity(
ActivityName,
ActivityKind.Server);
using var activity = activitySource.StartActivity(name: ActivityName, kind: ActivityKind.Server);
activity?.Stop();

var path = "/api/urltest";
var tagObjects = AzMonList.Initialize();
AzMonList.Add(ref tagObjects, new KeyValuePair<string, object?>(SemanticConventions.AttributeHttpRequestMethod, httpMethod));
AzMonList.Add(ref tagObjects, new KeyValuePair<string, object?>(SemanticConventions.AttributeHttpRequestMethod, httpRequestMethod));
AzMonList.Add(ref tagObjects, new KeyValuePair<string, object?>(SemanticConventions.AttributeHttpRoute, httpRoute));
AzMonList.Add(ref tagObjects, new KeyValuePair<string, object?>(SemanticConventions.AttributeUrlPath, path));
AzMonList.Add(ref tagObjects, new KeyValuePair<string, object?>(SemanticConventions.AttributeUrlPath, urlPath));

// Act
var result = TraceHelper.GetOperationNameV2(activity!, ref tagObjects);
Expand Down
Loading