Skip to content

Commit c057e6a

Browse files
Kielekreyangvishweshbankwar
authored
[Instrumentation.AspNetCore] .NET6 library works on .NET7 and .NET8 (#5252)
Co-authored-by: Reiley Yang <[email protected]> Co-authored-by: Vishwesh Bankwar <[email protected]>
1 parent ebb3524 commit c057e6a

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationTracerProviderBuilderExtensions.cs

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#if NET7_0_OR_GREATER
54
using System.Diagnostics;
6-
#endif
75
using Microsoft.Extensions.DependencyInjection;
86
using Microsoft.Extensions.Options;
97
using OpenTelemetry.Instrumentation.AspNetCore;
@@ -101,22 +99,25 @@ private static void AddAspNetCoreInstrumentationSources(
10199
// For .NET7.0 onwards activity will be created using activitySource.
102100
// https://github.com/dotnet/aspnetcore/blob/bf3352f2422bf16fa3ca49021f0e31961ce525eb/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L327
103101
// For .NET6.0 and below, we will continue to use legacy way.
104-
#if NET7_0_OR_GREATER
105-
// TODO: Check with .NET team to see if this can be prevented
106-
// as this allows user to override the ActivitySource.
107-
var activitySourceService = serviceProvider?.GetService<ActivitySource>();
108-
if (activitySourceService != null)
102+
if (HttpInListener.Net7OrGreater)
109103
{
110-
builder.AddSource(activitySourceService.Name);
104+
// TODO: Check with .NET team to see if this can be prevented
105+
// as this allows user to override the ActivitySource.
106+
var activitySourceService = serviceProvider?.GetService<ActivitySource>();
107+
if (activitySourceService != null)
108+
{
109+
builder.AddSource(activitySourceService.Name);
110+
}
111+
else
112+
{
113+
// For users not using hosting package?
114+
builder.AddSource(HttpInListener.AspNetCoreActivitySourceName);
115+
}
111116
}
112117
else
113118
{
114-
// For users not using hosting package?
115-
builder.AddSource(HttpInListener.AspNetCoreActivitySourceName);
119+
builder.AddSource(HttpInListener.ActivitySourceName);
120+
builder.AddLegacySource(HttpInListener.ActivityOperationName); // for the activities created by AspNetCore
116121
}
117-
#else
118-
builder.AddSource(HttpInListener.ActivitySourceName);
119-
builder.AddLegacySource(HttpInListener.ActivityOperationName); // for the activities created by AspNetCore
120-
#endif
121122
}
122123
}

src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[#4466](https://github.com/open-telemetry/opentelemetry-dotnet/issues/4466)
77
where the activity instance returned by `Activity.Current` was different than
88
instance obtained from `IHttpActivityFeature.Activity`.
9-
[#5136](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5136)
9+
([#5136](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5136))
1010

1111
* Fixed an issue where the `http.route` attribute was not set on either the
1212
`Activity` or `http.server.request.duration` metric generated from a

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

+29-19
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ internal class HttpInListener : ListenerHandler
2727
internal const string OnUnhandledHostingExceptionEvent = "Microsoft.AspNetCore.Hosting.UnhandledException";
2828
internal const string OnUnHandledDiagnosticsExceptionEvent = "Microsoft.AspNetCore.Diagnostics.UnhandledException";
2929

30-
#if NET7_0_OR_GREATER
3130
// https://github.com/dotnet/aspnetcore/blob/8d6554e655b64da75b71e0e20d6db54a3ba8d2fb/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs#L85
3231
internal static readonly string AspNetCoreActivitySourceName = "Microsoft.AspNetCore";
33-
#endif
3432

3533
internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName();
3634
internal static readonly string ActivitySourceName = AssemblyName.Name;
3735
internal static readonly Version Version = AssemblyName.Version;
3836
internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version.ToString());
37+
internal static readonly bool Net7OrGreater = Environment.Version.Major >= 7;
3938

4039
private const string DiagnosticSourceName = "Microsoft.AspNetCore";
4140

@@ -121,14 +120,19 @@ public void OnStartActivity(Activity activity, object payload)
121120
// Create a new activity with its parent set from the extracted context.
122121
// This makes the new activity as a "sibling" of the activity created by
123122
// Asp.Net Core.
124-
#if NET7_0_OR_GREATER
125-
// For NET7.0 onwards activity is created using ActivitySource so,
126-
// we will use the source of the activity to create the new one.
127-
Activity newOne = activity.Source.CreateActivity(ActivityOperationName, ActivityKind.Server, ctx.ActivityContext);
128-
#else
129-
Activity newOne = new Activity(ActivityOperationName);
130-
newOne.SetParentId(ctx.ActivityContext.TraceId, ctx.ActivityContext.SpanId, ctx.ActivityContext.TraceFlags);
131-
#endif
123+
Activity newOne;
124+
if (Net7OrGreater)
125+
{
126+
// For NET7.0 onwards activity is created using ActivitySource so,
127+
// we will use the source of the activity to create the new one.
128+
newOne = activity.Source.CreateActivity(ActivityOperationName, ActivityKind.Server, ctx.ActivityContext);
129+
}
130+
else
131+
{
132+
newOne = new Activity(ActivityOperationName);
133+
newOne.SetParentId(ctx.ActivityContext.TraceId, ctx.ActivityContext.SpanId, ctx.ActivityContext.TraceFlags);
134+
}
135+
132136
newOne.TraceStateString = ctx.ActivityContext.TraceState;
133137

134138
newOne.SetTag("IsCreatedByInstrumentation", bool.TrueString);
@@ -166,10 +170,11 @@ public void OnStartActivity(Activity activity, object payload)
166170
return;
167171
}
168172

169-
#if !NET7_0_OR_GREATER
170-
ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
171-
ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server);
172-
#endif
173+
if (!Net7OrGreater)
174+
{
175+
ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
176+
ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server);
177+
}
173178

174179
var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/";
175180
activity.DisplayName = GetDisplayName(request.Method);
@@ -263,12 +268,17 @@ public void OnStopActivity(Activity activity, object payload)
263268
}
264269
}
265270

266-
#if NET7_0_OR_GREATER
267-
var tagValue = activity.GetTagValue("IsCreatedByInstrumentation");
271+
object tagValue;
272+
if (Net7OrGreater)
273+
{
274+
tagValue = activity.GetTagValue("IsCreatedByInstrumentation");
275+
}
276+
else
277+
{
278+
_ = activity.TryCheckFirstTag("IsCreatedByInstrumentation", out tagValue);
279+
}
280+
268281
if (ReferenceEquals(tagValue, bool.TrueString))
269-
#else
270-
if (activity.TryCheckFirstTag("IsCreatedByInstrumentation", out var tagValue) && ReferenceEquals(tagValue, bool.TrueString))
271-
#endif
272282
{
273283
// If instrumentation started a new Activity, it must
274284
// be stopped here.

0 commit comments

Comments
 (0)