-
Notifications
You must be signed in to change notification settings - Fork 385
[AspNetCore] Avoid adding tags for traces #3993
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
Merged
martincostello
merged 19 commits into
open-telemetry:main
from
martincostello:aspnetcore-avoid-tags-where-possible
Apr 30, 2026
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5d40e60
[AspNetCore] Avoid adding tags for traces
martincostello 4423e89
[AspNetCore] Update CHANGELOG
martincostello 1ea52fd
[AspNetCore] Fix lint warning
martincostello 2409aa4
Merge branch 'main' into aspnetcore-avoid-tags-where-possible
martincostello e83d19c
Merge branch 'main' into aspnetcore-avoid-tags-where-possible
martincostello b5abf77
[AspNetCore] Fix comments
martincostello 22c1885
[AspNetCore] Address review comment
martincostello f5de0fd
Merge branch 'main' into aspnetcore-avoid-tags-where-possible
martincostello bebb82a
[AspNetCore] Fix lint warning
martincostello e9bcd6f
[AspNetCore] Fix spacing
martincostello acfe230
Merge branch 'main' into aspnetcore-avoid-tags-where-possible
martincostello 5090f43
[AspNetCore] Update CHANGELOG
martincostello ef69442
Merge branch 'main' into aspnetcore-avoid-tags-where-possible
Kielek c5ba983
[AspNetCore] Make method .NET 11 aware
martincostello f857002
[AspNetCore] Add tests for feature switch
martincostello 85435cb
[AspNetCore] Apply suggestions from code review
martincostello db71b3b
[AspNetCore] Refactor switch handling
martincostello 44aeb61
[AspNetCore] Update comment
martincostello 42d6789
[AspNetCore] Fix comment
martincostello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
test/OpenTelemetry.Instrumentation.AspNetCore.Tests/EndToEndTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Diagnostics; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Mvc.Testing; | ||
| using Microsoft.AspNetCore.TestHost; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using OpenTelemetry.Instrumentation.AspNetCore.Implementation; | ||
| using OpenTelemetry.Trace; | ||
| using Xunit; | ||
|
|
||
| namespace OpenTelemetry.Instrumentation.AspNetCore.Tests; | ||
|
|
||
| [Collection("AspNetCore")] | ||
| public sealed class EndToEndTests | ||
| : IClassFixture<WebApplicationFactory<Program>>, IDisposable | ||
| { | ||
| private readonly WebApplicationFactory<Program> factory; | ||
| private TracerProvider? tracerProvider; | ||
|
|
||
| public EndToEndTests(WebApplicationFactory<Program> factory) | ||
| { | ||
| this.factory = factory; | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public async Task HttpRequestActivityIsCorrectWithFeatureSwitch(bool isEnabled) | ||
| { | ||
| bool? originalValue = null; | ||
|
|
||
| if (AppContext.TryGetSwitch("Microsoft.AspNetCore.Hosting.SuppressActivityOpenTelemetryData", out var existingValue)) | ||
| { | ||
| originalValue = existingValue; | ||
| } | ||
|
|
||
| AppContext.SetSwitch("Microsoft.AspNetCore.Hosting.SuppressActivityOpenTelemetryData", isEnabled); | ||
|
|
||
| try | ||
| { | ||
| var exportedItems = new List<Activity>(); | ||
|
|
||
| void ConfigureTestServices(IServiceCollection services) | ||
| { | ||
| this.tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
| .AddAspNetCoreInstrumentation() | ||
| .AddInMemoryExporter(exportedItems) | ||
| .Build(); | ||
| } | ||
|
|
||
| // Arrange | ||
| using var client = this.factory | ||
| .WithWebHostBuilder(builder => | ||
| { | ||
| builder.ConfigureTestServices(ConfigureTestServices); | ||
| builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders()); | ||
| }) | ||
| .CreateClient(); | ||
|
|
||
| client.DefaultRequestHeaders.UserAgent.Add(new("OpenTelemetry.Instrumentation.AspNetCore.Tests", "1.0")); | ||
|
|
||
| _ = await client.GetStringAsync(new Uri("/ping", UriKind.Relative)); | ||
|
|
||
| WaitForActivityExport(exportedItems, 1); | ||
|
|
||
| var activity = Assert.Single(exportedItems); | ||
|
|
||
| ValidateAspNetCoreActivity(activity, "/ping"); | ||
|
|
||
| Assert.Equal("GET /ping", activity.DisplayName); | ||
| Assert.Equal("GET", activity.GetTagValue(SemanticConventions.AttributeHttpRequestMethod)); | ||
| Assert.Equal("localhost", activity.GetTagValue(SemanticConventions.AttributeServerAddress)); | ||
| Assert.Equal("OpenTelemetry.Instrumentation.AspNetCore.Tests/1.0", activity.GetTagValue(SemanticConventions.AttributeUserAgentOriginal)); | ||
| Assert.Equal("http", activity.GetTagValue(SemanticConventions.AttributeUrlScheme)); | ||
| Assert.Equal("/ping", activity.GetTagValue(SemanticConventions.AttributeUrlPath)); | ||
| } | ||
| finally | ||
| { | ||
| if (originalValue is { } previousValue) | ||
| { | ||
| AppContext.SetSwitch("Microsoft.AspNetCore.Hosting.SuppressActivityOpenTelemetryData", previousValue); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| => this.tracerProvider?.Dispose(); | ||
|
|
||
| private static void WaitForActivityExport(List<Activity> exportedItems, int count) | ||
| => Assert.True( | ||
| SpinWait.SpinUntil( | ||
| () => | ||
| { | ||
| // 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 breathing room for the End callback to complete | ||
| Thread.Sleep(10); | ||
| return exportedItems.Count >= count; | ||
| }, | ||
| TimeSpan.FromSeconds(5)), | ||
| $"Actual: {exportedItems.Count} Expected: {count}"); | ||
|
|
||
| private static void ValidateAspNetCoreActivity(Activity activityToValidate, string expectedHttpPath) | ||
| { | ||
| Assert.Equal(ActivityKind.Server, activityToValidate.Kind); | ||
| Assert.Equal(HttpInListener.AspNetCoreActivitySourceName, activityToValidate.Source.Name); | ||
| Assert.NotNull(activityToValidate.Source.Version); | ||
| Assert.Empty(activityToValidate.Source.Version); | ||
| Assert.Equal(expectedHttpPath, activityToValidate.GetTagValue(SemanticConventions.AttributeUrlPath) as string); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.