Skip to content

Commit 479b938

Browse files
committed
Revert "[.NET7.0] AspNetCore ActivitySource Migration (#3391)"
This reverts commit e24677b.
1 parent cfb3602 commit 479b938

File tree

3 files changed

+5
-89
lines changed

3 files changed

+5
-89
lines changed

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation
3535
internal class HttpInListener : ListenerHandler
3636
{
3737
internal const string ActivityOperationName = "Microsoft.AspNetCore.Hosting.HttpRequestIn";
38-
#if NET7_0_OR_GREATER
39-
// https://github.com/dotnet/aspnetcore/blob/8d6554e655b64da75b71e0e20d6db54a3ba8d2fb/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs#L85
40-
internal static readonly string AspNetCoreActivitySourceName = "Microsoft.AspNetCore";
41-
#endif
4238
internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName();
4339
internal static readonly string ActivitySourceName = AssemblyName.Name;
4440
internal static readonly Version Version = AssemblyName.Version;
@@ -100,14 +96,8 @@ public override void OnStartActivity(Activity activity, object payload)
10096
// Create a new activity with its parent set from the extracted context.
10197
// This makes the new activity as a "sibling" of the activity created by
10298
// Asp.Net Core.
103-
#if NET7_0_OR_GREATER
104-
// For NET7.0 onwards activity is created using ActivitySource so,
105-
// we will use the source of the activity to create the new one.
106-
Activity newOne = activity.Source.CreateActivity(ActivityOperationName, ActivityKind.Server, ctx.ActivityContext);
107-
#else
10899
Activity newOne = new Activity(ActivityOperationName);
109100
newOne.SetParentId(ctx.ActivityContext.TraceId, ctx.ActivityContext.SpanId, ctx.ActivityContext.TraceFlags);
110-
#endif
111101
newOne.TraceStateString = ctx.ActivityContext.TraceState;
112102

113103
newOne.SetTag("IsCreatedByInstrumentation", bool.TrueString);
@@ -145,10 +135,8 @@ public override void OnStartActivity(Activity activity, object payload)
145135
return;
146136
}
147137

148-
#if !NET7_0_OR_GREATER
149138
ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
150139
ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server);
151-
#endif
152140

153141
var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/";
154142
activity.DisplayName = path;

src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
// </copyright>
1616

1717
using System;
18-
#if NET7_0_OR_GREATER
19-
using System.Diagnostics;
20-
using Microsoft.Extensions.DependencyInjection;
21-
#endif
2218
using OpenTelemetry.Instrumentation.AspNetCore;
2319
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
2420
using OpenTelemetry.Internal;
@@ -46,7 +42,7 @@ public static TracerProviderBuilder AddAspNetCoreInstrumentation(
4642
{
4743
return deferredTracerProviderBuilder.Configure((sp, builder) =>
4844
{
49-
AddAspNetCoreInstrumentation(builder, sp.GetOptions<AspNetCoreInstrumentationOptions>(), configureAspNetCoreInstrumentationOptions, sp);
45+
AddAspNetCoreInstrumentation(builder, sp.GetOptions<AspNetCoreInstrumentationOptions>(), configureAspNetCoreInstrumentationOptions);
5046
});
5147
}
5248

@@ -55,44 +51,22 @@ public static TracerProviderBuilder AddAspNetCoreInstrumentation(
5551

5652
internal static TracerProviderBuilder AddAspNetCoreInstrumentation(
5753
this TracerProviderBuilder builder,
58-
AspNetCoreInstrumentation instrumentation,
59-
IServiceProvider serviceProvider = null)
54+
AspNetCoreInstrumentation instrumentation)
6055
{
61-
// For .NET7.0 onwards activity will be created using activitySource.
62-
// https://github.com/dotnet/aspnetcore/blob/bf3352f2422bf16fa3ca49021f0e31961ce525eb/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L327
63-
// For .NET6.0 and below, we will continue to use legacy way.
64-
#if NET7_0_OR_GREATER
65-
// TODO: Check with .NET team to see if this can be prevented
66-
// as this allows user to override the ActivitySource.
67-
var activitySourceService = serviceProvider?.GetService<ActivitySource>();
68-
if (activitySourceService != null)
69-
{
70-
builder.AddSource(activitySourceService.Name);
71-
}
72-
else
73-
{
74-
// For users not using hosting package?
75-
builder.AddSource(HttpInListener.AspNetCoreActivitySourceName);
76-
}
77-
#else
7856
builder.AddSource(HttpInListener.ActivitySourceName);
7957
builder.AddLegacySource(HttpInListener.ActivityOperationName); // for the activities created by AspNetCore
80-
#endif
81-
8258
return builder.AddInstrumentation(() => instrumentation);
8359
}
8460

8561
private static TracerProviderBuilder AddAspNetCoreInstrumentation(
8662
TracerProviderBuilder builder,
8763
AspNetCoreInstrumentationOptions options,
88-
Action<AspNetCoreInstrumentationOptions> configure = null,
89-
IServiceProvider serviceProvider = null)
64+
Action<AspNetCoreInstrumentationOptions> configure = null)
9065
{
9166
configure?.Invoke(options);
9267
return AddAspNetCoreInstrumentation(
9368
builder,
94-
new AspNetCoreInstrumentation(new HttpInListener(options)),
95-
serviceProvider);
69+
new AspNetCoreInstrumentation(new HttpInListener(options)));
9670
}
9771
}
9872
}

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

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public async Task ExtractContextIrrespectiveOfSamplingDecision(SamplingDecision
348348
var expectedTraceId = ActivityTraceId.CreateRandom();
349349
var expectedParentSpanId = ActivitySpanId.CreateRandom();
350350
var expectedTraceState = "rojo=1,congo=2";
351-
var activityContext = new ActivityContext(expectedTraceId, expectedParentSpanId, ActivityTraceFlags.Recorded, expectedTraceState, true);
351+
var activityContext = new ActivityContext(expectedTraceId, expectedParentSpanId, ActivityTraceFlags.Recorded, expectedTraceState);
352352
var expectedBaggage = Baggage.SetBaggage("key1", "value1").SetBaggage("key2", "value2");
353353
Sdk.SetDefaultTextMapPropagator(new ExtractOnlyPropagator(activityContext, expectedBaggage));
354354

@@ -586,47 +586,6 @@ public async Task ActivitiesStartedInMiddlewareShouldNotBeUpdatedByInstrumentati
586586
Assert.Equal(activityName, middlewareActivity.DisplayName);
587587
}
588588

589-
#if NET7_0_OR_GREATER
590-
[Fact]
591-
public async Task UserRegisteredActivitySourceIsUsedForActivityCreationByAspNetCore()
592-
{
593-
var exportedItems = new List<Activity>();
594-
void ConfigureTestServices(IServiceCollection services)
595-
{
596-
services.AddOpenTelemetryTracing(options =>
597-
{
598-
options.AddAspNetCoreInstrumentation()
599-
.AddInMemoryExporter(exportedItems);
600-
});
601-
602-
// Register ActivitySource here so that it will be used
603-
// by ASP.NET Core to create activities
604-
// https://github.com/dotnet/aspnetcore/blob/0e5cbf447d329a1e7d69932c3decd1c70a00fbba/src/Hosting/Hosting/src/Internal/WebHost.cs#L152
605-
services.AddSingleton(sp => new ActivitySource("UserRegisteredActivitySource"));
606-
}
607-
608-
// Arrange
609-
using (var client = this.factory
610-
.WithWebHostBuilder(builder =>
611-
builder.ConfigureTestServices(ConfigureTestServices))
612-
.CreateClient())
613-
{
614-
// Act
615-
var response = await client.GetAsync("/api/values");
616-
617-
// Assert
618-
response.EnsureSuccessStatusCode(); // Status Code 200-299
619-
620-
WaitForActivityExport(exportedItems, 1);
621-
}
622-
623-
Assert.Single(exportedItems);
624-
var activity = exportedItems[0];
625-
626-
Assert.Equal("UserRegisteredActivitySource", activity.Source.Name);
627-
}
628-
#endif
629-
630589
public void Dispose()
631590
{
632591
this.tracerProvider?.Dispose();
@@ -649,13 +608,8 @@ private static void WaitForActivityExport(List<Activity> exportedItems, int coun
649608
private static void ValidateAspNetCoreActivity(Activity activityToValidate, string expectedHttpPath)
650609
{
651610
Assert.Equal(ActivityKind.Server, activityToValidate.Kind);
652-
#if NET7_0_OR_GREATER
653-
Assert.Equal(HttpInListener.AspNetCoreActivitySourceName, activityToValidate.Source.Name);
654-
Assert.Empty(activityToValidate.Source.Version);
655-
#else
656611
Assert.Equal(HttpInListener.ActivitySourceName, activityToValidate.Source.Name);
657612
Assert.Equal(HttpInListener.Version.ToString(), activityToValidate.Source.Version);
658-
#endif
659613
Assert.Equal(expectedHttpPath, activityToValidate.GetTagValue(SemanticConventions.AttributeHttpTarget) as string);
660614
}
661615

0 commit comments

Comments
 (0)