From f7070ee910c91d42816744c051b4bb4e2b2a6dc7 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Fri, 26 Jan 2024 14:10:57 -0800 Subject: [PATCH 1/7] fix operation name --- .../src/Internals/TraceHelper.cs | 15 +++++------ .../TraceHelperNewTests.cs | 27 ++++++++++--------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs index 024ca61c62335..ae6b3d0c96d7a 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs @@ -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 @@ -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}"; } } diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TraceHelperNewTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TraceHelperNewTests.cs index f75e7b3310200..ec69959be7486 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TraceHelperNewTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TraceHelperNewTests.cs @@ -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; @@ -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(SemanticConventions.AttributeHttpRequestMethod, httpMethod)); + AzMonList.Add(ref tagObjects, new KeyValuePair(SemanticConventions.AttributeHttpRequestMethod, httpRequestMethod)); AzMonList.Add(ref tagObjects, new KeyValuePair(SemanticConventions.AttributeHttpRoute, httpRoute)); - AzMonList.Add(ref tagObjects, new KeyValuePair(SemanticConventions.AttributeUrlPath, path)); + AzMonList.Add(ref tagObjects, new KeyValuePair(SemanticConventions.AttributeUrlPath, urlPath)); // Act var result = TraceHelper.GetOperationNameV2(activity!, ref tagObjects); From 6b38c1a4c49e3401f76afb705ae03c2047f890ed Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Fri, 26 Jan 2024 14:39:48 -0800 Subject: [PATCH 2/7] update demo with specific examples --- .../Controllers/CustomerController.cs | 17 +++++++++++++++++ .../Controllers/HomeController.cs | 17 +++++++++++++++++ .../Program.cs | 6 ++++++ 3 files changed, 40 insertions(+) create mode 100644 sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/CustomerController.cs create mode 100644 sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/HomeController.cs diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/CustomerController.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/CustomerController.cs new file mode 100644 index 0000000000000..710a58fbec971 --- /dev/null +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/CustomerController.cs @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#if !NETFRAMEWORK + +using Microsoft.AspNetCore.Mvc; + +namespace Azure.Monitor.OpenTelemetry.AspNetCore.Demo.Controllers +{ + public class CustomerController : Controller + { + public IActionResult Index() => this.Ok("CustomerController Index"); + + public IActionResult Get(int id) => this.Ok($"CustomerController Get '{id}'"); + } +} +#endif diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/HomeController.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/HomeController.cs new file mode 100644 index 0000000000000..2ffe8351038d4 --- /dev/null +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/HomeController.cs @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#if !NETFRAMEWORK + +using Microsoft.AspNetCore.Mvc; + +namespace Azure.Monitor.OpenTelemetry.AspNetCore.Demo.Controllers +{ + public class HomeController : Controller + { + public IActionResult Index() => this.Ok("HomeController Index"); + + public IActionResult Get(int id) => this.Ok($"HomeController Get '{id}'"); + } +} +#endif diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Program.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Program.cs index 949139bc44f3f..ce3a1ff68f8f1 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Program.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Program.cs @@ -11,6 +11,8 @@ var builder = WebApplication.CreateBuilder(args); +builder.Services.AddControllers(); + builder.Services.AddOpenTelemetry().UseAzureMonitor(); /* @@ -33,5 +35,9 @@ return $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}"; }); +app.MapControllerRoute( + name: "default", + pattern: "{controller=Home}/{action=Index}/{id?}"); + app.Run(); #endif From 7a0f5e3468a626c33376ae50b6f18727a40e4111 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Fri, 26 Jan 2024 14:42:09 -0800 Subject: [PATCH 3/7] changelog --- sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md index 518fcc4752b00..67e3a05f8714f 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md @@ -8,6 +8,9 @@ ### Bugs Fixed +* Fix issue that caused Request OperationName to display routing pattern instead of url path. + ([41623](https://github.com/Azure/azure-sdk-for-net/pull/41623)) + ### Other Changes ## 1.2.0 (2024-01-24) From 6eca7a9ac780532bc6607af2d3c1fc315a329724 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Fri, 26 Jan 2024 15:09:42 -0800 Subject: [PATCH 4/7] fix for OperationName v1 --- .../src/Internals/TraceHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs index ae6b3d0c96d7a..a9086c5544ba4 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs @@ -161,7 +161,7 @@ internal static string GetOperationName(Activity activity, ref AzMonList MappedT // 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 httpUrl. - if (!string.IsNullOrWhiteSpace(httpRoute) && !httpRoute!.Contains("{controller}")) + if (!string.IsNullOrWhiteSpace(httpRoute) && !httpRoute!.Contains("{controller")) { return $"{httpMethod} {httpRoute}"; } From 7e03a311cc736d41f6db597e3d2420e7603e96e5 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Fri, 26 Jan 2024 16:52:18 -0800 Subject: [PATCH 5/7] revert change to V1 method --- .../src/Internals/TraceHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs index a9086c5544ba4..ae6b3d0c96d7a 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs @@ -161,7 +161,7 @@ internal static string GetOperationName(Activity activity, ref AzMonList MappedT // 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 httpUrl. - if (!string.IsNullOrWhiteSpace(httpRoute) && !httpRoute!.Contains("{controller")) + if (!string.IsNullOrWhiteSpace(httpRoute) && !httpRoute!.Contains("{controller}")) { return $"{httpMethod} {httpRoute}"; } From 22bfae2b94107d22bbf1285083e40e21d8779d3a Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Mon, 29 Jan 2024 10:01:11 -0800 Subject: [PATCH 6/7] revert changes to Demo --- .../Controllers/CustomerController.cs | 17 ----------------- .../Controllers/HomeController.cs | 17 ----------------- .../Program.cs | 6 ------ 3 files changed, 40 deletions(-) delete mode 100644 sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/CustomerController.cs delete mode 100644 sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/HomeController.cs diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/CustomerController.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/CustomerController.cs deleted file mode 100644 index 710a58fbec971..0000000000000 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/CustomerController.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -#if !NETFRAMEWORK - -using Microsoft.AspNetCore.Mvc; - -namespace Azure.Monitor.OpenTelemetry.AspNetCore.Demo.Controllers -{ - public class CustomerController : Controller - { - public IActionResult Index() => this.Ok("CustomerController Index"); - - public IActionResult Get(int id) => this.Ok($"CustomerController Get '{id}'"); - } -} -#endif diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/HomeController.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/HomeController.cs deleted file mode 100644 index 2ffe8351038d4..0000000000000 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Controllers/HomeController.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -#if !NETFRAMEWORK - -using Microsoft.AspNetCore.Mvc; - -namespace Azure.Monitor.OpenTelemetry.AspNetCore.Demo.Controllers -{ - public class HomeController : Controller - { - public IActionResult Index() => this.Ok("HomeController Index"); - - public IActionResult Get(int id) => this.Ok($"HomeController Get '{id}'"); - } -} -#endif diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Program.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Program.cs index ce3a1ff68f8f1..949139bc44f3f 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Program.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Demo/Program.cs @@ -11,8 +11,6 @@ var builder = WebApplication.CreateBuilder(args); -builder.Services.AddControllers(); - builder.Services.AddOpenTelemetry().UseAzureMonitor(); /* @@ -35,9 +33,5 @@ return $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}"; }); -app.MapControllerRoute( - name: "default", - pattern: "{controller=Home}/{action=Index}/{id?}"); - app.Run(); #endif From b8b75926afcb66fa36859b4502a509080c266922 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Date: Mon, 29 Jan 2024 10:03:28 -0800 Subject: [PATCH 7/7] Update sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md Co-authored-by: Vishwesh Bankwar --- sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md index 67e3a05f8714f..f2553e7cdda9b 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md @@ -8,7 +8,7 @@ ### Bugs Fixed -* Fix issue that caused Request OperationName to display routing pattern instead of url path. +* Fix issue that caused Request OperationName to display identical routing pattern for different endpoints. ([41623](https://github.com/Azure/azure-sdk-for-net/pull/41623)) ### Other Changes