diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationTracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationTracerProviderBuilderExtensions.cs index d37d77ae3fa..8ec35718ccd 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationTracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationTracerProviderBuilderExtensions.cs @@ -1,9 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#if NET7_0_OR_GREATER using System.Diagnostics; -#endif using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using OpenTelemetry.Instrumentation.AspNetCore; @@ -101,22 +99,25 @@ private static void AddAspNetCoreInstrumentationSources( // For .NET7.0 onwards activity will be created using activitySource. // https://github.com/dotnet/aspnetcore/blob/bf3352f2422bf16fa3ca49021f0e31961ce525eb/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L327 // For .NET6.0 and below, we will continue to use legacy way. -#if NET7_0_OR_GREATER - // TODO: Check with .NET team to see if this can be prevented - // as this allows user to override the ActivitySource. - var activitySourceService = serviceProvider?.GetService(); - if (activitySourceService != null) + if (HttpInListener.Net7OrGreater) { - builder.AddSource(activitySourceService.Name); + // TODO: Check with .NET team to see if this can be prevented + // as this allows user to override the ActivitySource. + var activitySourceService = serviceProvider?.GetService(); + if (activitySourceService != null) + { + builder.AddSource(activitySourceService.Name); + } + else + { + // For users not using hosting package? + builder.AddSource(HttpInListener.AspNetCoreActivitySourceName); + } } else { - // For users not using hosting package? - builder.AddSource(HttpInListener.AspNetCoreActivitySourceName); + builder.AddSource(HttpInListener.ActivitySourceName); + builder.AddLegacySource(HttpInListener.ActivityOperationName); // for the activities created by AspNetCore } -#else - builder.AddSource(HttpInListener.ActivitySourceName); - builder.AddLegacySource(HttpInListener.ActivityOperationName); // for the activities created by AspNetCore -#endif } } diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md index 884b86e122e..15564885cca 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md @@ -6,7 +6,7 @@ [#4466](https://github.com/open-telemetry/opentelemetry-dotnet/issues/4466) where the activity instance returned by `Activity.Current` was different than instance obtained from `IHttpActivityFeature.Activity`. - [#5136](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5136) + ([#5136](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5136)) * Fixed an issue where the `http.route` attribute was not set on either the `Activity` or `http.server.request.duration` metric generated from a diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index 9e724c74afb..97807d41512 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -27,15 +27,14 @@ internal class HttpInListener : ListenerHandler internal const string OnUnhandledHostingExceptionEvent = "Microsoft.AspNetCore.Hosting.UnhandledException"; internal const string OnUnHandledDiagnosticsExceptionEvent = "Microsoft.AspNetCore.Diagnostics.UnhandledException"; -#if NET7_0_OR_GREATER // https://github.com/dotnet/aspnetcore/blob/8d6554e655b64da75b71e0e20d6db54a3ba8d2fb/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs#L85 internal static readonly string AspNetCoreActivitySourceName = "Microsoft.AspNetCore"; -#endif internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName(); internal static readonly string ActivitySourceName = AssemblyName.Name; internal static readonly Version Version = AssemblyName.Version; internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version.ToString()); + internal static readonly bool Net7OrGreater = Environment.Version.Major >= 7; private const string DiagnosticSourceName = "Microsoft.AspNetCore"; @@ -121,14 +120,19 @@ public void OnStartActivity(Activity activity, object payload) // Create a new activity with its parent set from the extracted context. // This makes the new activity as a "sibling" of the activity created by // Asp.Net Core. -#if NET7_0_OR_GREATER - // For NET7.0 onwards activity is created using ActivitySource so, - // we will use the source of the activity to create the new one. - Activity newOne = activity.Source.CreateActivity(ActivityOperationName, ActivityKind.Server, ctx.ActivityContext); -#else - Activity newOne = new Activity(ActivityOperationName); - newOne.SetParentId(ctx.ActivityContext.TraceId, ctx.ActivityContext.SpanId, ctx.ActivityContext.TraceFlags); -#endif + Activity newOne; + if (Net7OrGreater) + { + // For NET7.0 onwards activity is created using ActivitySource so, + // we will use the source of the activity to create the new one. + newOne = activity.Source.CreateActivity(ActivityOperationName, ActivityKind.Server, ctx.ActivityContext); + } + else + { + newOne = new Activity(ActivityOperationName); + newOne.SetParentId(ctx.ActivityContext.TraceId, ctx.ActivityContext.SpanId, ctx.ActivityContext.TraceFlags); + } + newOne.TraceStateString = ctx.ActivityContext.TraceState; newOne.SetTag("IsCreatedByInstrumentation", bool.TrueString); @@ -166,10 +170,11 @@ public void OnStartActivity(Activity activity, object payload) return; } -#if !NET7_0_OR_GREATER - ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource); - ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server); -#endif + if (!Net7OrGreater) + { + ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource); + ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server); + } var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/"; activity.DisplayName = GetDisplayName(request.Method); @@ -263,12 +268,17 @@ public void OnStopActivity(Activity activity, object payload) } } -#if NET7_0_OR_GREATER - var tagValue = activity.GetTagValue("IsCreatedByInstrumentation"); + object tagValue; + if (Net7OrGreater) + { + tagValue = activity.GetTagValue("IsCreatedByInstrumentation"); + } + else + { + _ = activity.TryCheckFirstTag("IsCreatedByInstrumentation", out tagValue); + } + if (ReferenceEquals(tagValue, bool.TrueString)) -#else - if (activity.TryCheckFirstTag("IsCreatedByInstrumentation", out var tagValue) && ReferenceEquals(tagValue, bool.TrueString)) -#endif { // If instrumentation started a new Activity, it must // be stopped here.