Skip to content

Commit

Permalink
[Instrumentation.AspNetCore] .NET6 library works on .NET7 and .NET8 (#…
Browse files Browse the repository at this point in the history
…5252)

Co-authored-by: Reiley Yang <[email protected]>
Co-authored-by: Vishwesh Bankwar <[email protected]>
  • Loading branch information
3 people authored Feb 6, 2024
1 parent ebb3524 commit c057e6a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<ActivitySource>();
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<ActivitySource>();
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
}
}
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c057e6a

Please sign in to comment.