From d0829fff2f229b67c884526cb4943b99f6c75f29 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 14 Feb 2023 15:39:07 -0800 Subject: [PATCH] [ASP.NET Core] Add test validating route information for non-mvc scenario (#4105) --- .../BasicTests.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs index cb51bd437c4..852b7d716e5 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs @@ -990,6 +990,57 @@ static void ThrowException(IApplicationBuilder app) await app.DisposeAsync().ConfigureAwait(false); } + [Fact] + public async Task RouteInformationIsNotAddedToRequestsOutsideOfMVC() + { + var exportedItems = new List(); + + // configure SDK + using var tracerprovider = Sdk.CreateTracerProviderBuilder() + .AddAspNetCoreInstrumentation() + .AddInMemoryExporter(exportedItems) + .Build(); + + var builder = WebApplication.CreateBuilder(); + builder.Logging.ClearProviders(); + var app = builder.Build(); + + app.MapGet("/custom/{name:alpha}", () => "Hello"); + + _ = app.RunAsync(); + + using var client = new HttpClient(); + var res = await client.GetStringAsync("http://localhost:5000/custom/abc").ConfigureAwait(false); + Assert.NotNull(res); + + tracerprovider.ForceFlush(); + for (var i = 0; i < 10; i++) + { + if (exportedItems.Count > 0) + { + break; + } + + // We need to let End callback execute as it is executed AFTER response was returned. + // In unit tests environment there may be a lot of parallel unit tests executed, so + // giving some breezing room for the End callback to complete + await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false); + } + + var activity = exportedItems[0]; + + Assert.NotNull(activity); + + // After fix update to Contains http.route + Assert.DoesNotContain(activity.TagObjects, t => t.Key == SemanticConventions.AttributeHttpRoute); + Assert.Equal("Microsoft.AspNetCore.Hosting.HttpRequestIn", activity.OperationName); + + // After fix this should be /custom/{name:alpha} + Assert.Equal("/custom/abc", activity.DisplayName); + + await app.DisposeAsync().ConfigureAwait(false); + } + public void Dispose() { this.tracerProvider?.Dispose();