Skip to content

Commit 0581ce2

Browse files
authored
Remove ActivitySourceAdapter (#1836)
Removed ActivitySourceAdapater and made TracerProviderSdk natively support legacy activites
1 parent 39841e5 commit 0581ce2

File tree

32 files changed

+969
-732
lines changed

32 files changed

+969
-732
lines changed

docs/trace/getting-started/getting-started.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
33
<!---
44
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="$(OpenTelemetryExporterConsolePkgVer)" />

src/OpenTelemetry.Instrumentation.AspNet/AspNetInstrumentation.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ internal class AspNetInstrumentation : IDisposable
3131
/// <summary>
3232
/// Initializes a new instance of the <see cref="AspNetInstrumentation"/> class.
3333
/// </summary>
34-
/// <param name="activitySource">ActivitySource adapter instance.</param>
3534
/// <param name="options">Configuration options for ASP.NET instrumentation.</param>
36-
public AspNetInstrumentation(ActivitySourceAdapter activitySource, AspNetInstrumentationOptions options)
35+
public AspNetInstrumentation(AspNetInstrumentationOptions options)
3736
{
3837
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(
39-
name => new HttpInListener(name, options, activitySource),
38+
name => new HttpInListener(name, options),
4039
listener => listener.Name == AspNetDiagnosticListenerName,
4140
null);
4241
this.diagnosticSourceSubscriber.Subscribe();

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ internal class HttpInListener : ListenerHandler
3737
private readonly PropertyFetcher<object> routeFetcher = new PropertyFetcher<object>("Route");
3838
private readonly PropertyFetcher<string> routeTemplateFetcher = new PropertyFetcher<string>("RouteTemplate");
3939
private readonly AspNetInstrumentationOptions options;
40-
private readonly ActivitySourceAdapter activitySource;
4140

42-
public HttpInListener(string name, AspNetInstrumentationOptions options, ActivitySourceAdapter activitySource)
41+
public HttpInListener(string name, AspNetInstrumentationOptions options)
4342
: base(name)
4443
{
4544
this.options = options ?? throw new ArgumentNullException(nameof(options));
46-
this.activitySource = activitySource;
4745
}
4846

4947
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "Activity is retrieved from Activity.Current later and disposed.")]
@@ -98,6 +96,9 @@ public override void OnStartActivity(Activity activity, object payload)
9896
// correctly stop and restore Activity.Current.
9997
newOne.SetCustomProperty("OTel.ActivityByAspNet", activity);
10098
activity.SetCustomProperty("OTel.ActivityByHttpInListener", newOne);
99+
100+
// Set IsAllDataRequested to false for the activity created by the framework to only export the sibling activity and not the framework activity
101+
activity.IsAllDataRequested = false;
101102
activity = newOne;
102103
}
103104

@@ -111,7 +112,8 @@ public override void OnStartActivity(Activity activity, object payload)
111112
var path = requestValues.Path;
112113
activity.DisplayName = path;
113114

114-
this.activitySource.Start(activity, ActivityKind.Server, ActivitySource);
115+
ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
116+
ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server);
115117

116118
if (activity.IsAllDataRequested)
117119
{
@@ -244,8 +246,6 @@ public override void OnStopActivity(Activity activity, object payload)
244246
Activity.Current = activity;
245247
}
246248
}
247-
248-
this.activitySource.Stop(activityToEnrich);
249249
}
250250
}
251251
}

src/OpenTelemetry.Instrumentation.AspNet/TracerProviderBuilderExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System;
1818
using OpenTelemetry.Instrumentation.AspNet;
19+
using OpenTelemetry.Instrumentation.AspNet.Implementation;
1920

2021
namespace OpenTelemetry.Trace
2122
{
@@ -42,7 +43,10 @@ public static TracerProviderBuilder AddAspNetInstrumentation(
4243
var aspnetOptions = new AspNetInstrumentationOptions();
4344
configureAspNetInstrumentationOptions?.Invoke(aspnetOptions);
4445

45-
builder.AddDiagnosticSourceInstrumentation((activitySource) => new AspNetInstrumentation(activitySource, aspnetOptions));
46+
builder.AddInstrumentation(() => new AspNetInstrumentation(aspnetOptions));
47+
builder.AddSource(HttpInListener.ActivitySourceName);
48+
builder.AddLegacyActivity("Microsoft.AspNet.HttpReqIn"); // for the activities created by AspNetCore
49+
builder.AddLegacyActivity("ActivityCreatedByHttpInListener"); // for the sibling activities created by the instrumentation library
4650

4751
return builder;
4852
}

src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentation.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ internal class AspNetCoreInstrumentation : IDisposable
2929
/// <summary>
3030
/// Initializes a new instance of the <see cref="AspNetCoreInstrumentation"/> class.
3131
/// </summary>
32-
/// <param name="activitySource">ActivitySource adapter instance.</param>
3332
/// <param name="options">Configuration options for ASP.NET Core instrumentation.</param>
34-
public AspNetCoreInstrumentation(ActivitySourceAdapter activitySource, AspNetCoreInstrumentationOptions options)
33+
public AspNetCoreInstrumentation(AspNetCoreInstrumentationOptions options)
3534
{
36-
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpInListener("Microsoft.AspNetCore", options, activitySource), null);
35+
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpInListener("Microsoft.AspNetCore", options), null);
3736
this.diagnosticSourceSubscriber.Subscribe();
3837
}
3938

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation
3030
{
3131
internal class HttpInListener : ListenerHandler
3232
{
33+
internal const string ActivityOperationName = "Microsoft.AspNetCore.Hosting.HttpRequestIn";
34+
internal const string ActivityNameByHttpInListener = "ActivityCreatedByHttpInListener";
3335
internal static readonly AssemblyName AssemblyName = typeof(HttpInListener).Assembly.GetName();
3436
internal static readonly string ActivitySourceName = AssemblyName.Name;
3537
internal static readonly Version Version = AssemblyName.Version;
3638
internal static readonly ActivitySource ActivitySource = new ActivitySource(ActivitySourceName, Version.ToString());
3739
private const string UnknownHostName = "UNKNOWN-HOST";
38-
private const string ActivityNameByHttpInListener = "ActivityCreatedByHttpInListener";
3940
private static readonly Func<HttpRequest, string, IEnumerable<string>> HttpRequestHeaderValuesGetter = (request, name) => request.Headers[name];
4041
private readonly PropertyFetcher<HttpContext> startContextFetcher = new PropertyFetcher<HttpContext>("HttpContext");
4142
private readonly PropertyFetcher<HttpContext> stopContextFetcher = new PropertyFetcher<HttpContext>("HttpContext");
@@ -45,14 +46,12 @@ internal class HttpInListener : ListenerHandler
4546
private readonly PropertyFetcher<string> beforeActionTemplateFetcher = new PropertyFetcher<string>("Template");
4647
private readonly bool hostingSupportsW3C;
4748
private readonly AspNetCoreInstrumentationOptions options;
48-
private readonly ActivitySourceAdapter activitySource;
4949

50-
public HttpInListener(string name, AspNetCoreInstrumentationOptions options, ActivitySourceAdapter activitySource)
50+
public HttpInListener(string name, AspNetCoreInstrumentationOptions options)
5151
: base(name)
5252
{
5353
this.hostingSupportsW3C = typeof(HttpRequest).Assembly.GetName().Version.Major >= 3;
5454
this.options = options ?? throw new ArgumentNullException(nameof(options));
55-
this.activitySource = activitySource;
5655
}
5756

5857
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The objects should not be disposed.")]
@@ -99,6 +98,9 @@ public override void OnStartActivity(Activity activity, object payload)
9998

10099
// Starting the new activity make it the Activity.Current one.
101100
newOne.Start();
101+
102+
// Set IsAllDataRequested to false for the activity created by the framework to only export the sibling activity and not the framework activity
103+
activity.IsAllDataRequested = false;
102104
activity = newOne;
103105
}
104106

@@ -108,7 +110,8 @@ public override void OnStartActivity(Activity activity, object payload)
108110
}
109111
}
110112

111-
this.activitySource.Start(activity, ActivityKind.Server, ActivitySource);
113+
ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
114+
ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Server);
112115

113116
if (activity.IsAllDataRequested)
114117
{
@@ -208,8 +211,6 @@ public override void OnStopActivity(Activity activity, object payload)
208211
// the one created by the instrumentation.
209212
// And retrieve it here, and set it to Current.
210213
}
211-
212-
this.activitySource.Stop(activity);
213214
}
214215

215216
public override void OnCustom(string name, Activity activity, object payload)

src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System;
1818
using OpenTelemetry.Instrumentation.AspNetCore;
19+
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
1920

2021
namespace OpenTelemetry.Trace
2122
{
@@ -41,7 +42,10 @@ public static TracerProviderBuilder AddAspNetCoreInstrumentation(
4142

4243
var aspnetCoreOptions = new AspNetCoreInstrumentationOptions();
4344
configureAspNetCoreInstrumentationOptions?.Invoke(aspnetCoreOptions);
44-
builder.AddDiagnosticSourceInstrumentation((activitySource) => new AspNetCoreInstrumentation(activitySource, aspnetCoreOptions));
45+
builder.AddInstrumentation(() => new AspNetCoreInstrumentation(aspnetCoreOptions));
46+
builder.AddSource(HttpInListener.ActivitySourceName);
47+
builder.AddLegacyActivity(HttpInListener.ActivityOperationName); // for the activities created by AspNetCore
48+
builder.AddLegacyActivity(HttpInListener.ActivityNameByHttpInListener); // for the sibling activities created by the instrumentation library
4549

4650
return builder;
4751
}

src/OpenTelemetry.Instrumentation.GrpcNetClient/GrpcClientInstrumentation.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,10 @@ internal class GrpcClientInstrumentation : IDisposable
2929
/// <summary>
3030
/// Initializes a new instance of the <see cref="GrpcClientInstrumentation"/> class.
3131
/// </summary>
32-
/// <param name="activitySource">ActivitySource adapter instance.</param>
3332
/// <param name="options">Configuration options for Grpc client instrumentation.</param>
34-
public GrpcClientInstrumentation(ActivitySourceAdapter activitySource, GrpcClientInstrumentationOptions options = null)
33+
public GrpcClientInstrumentation(GrpcClientInstrumentationOptions options = null)
3534
{
36-
if (activitySource == null)
37-
{
38-
throw new ArgumentNullException(nameof(activitySource));
39-
}
40-
41-
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new GrpcClientDiagnosticListener(activitySource, options), null);
35+
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new GrpcClientDiagnosticListener(options), null);
4236
this.diagnosticSourceSubscriber.Subscribe();
4337
}
4438

src/OpenTelemetry.Instrumentation.GrpcNetClient/Implementation/GrpcClientDiagnosticListener.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,13 @@ internal class GrpcClientDiagnosticListener : ListenerHandler
3232
internal static readonly ActivitySource ActivitySource = new ActivitySource(ActivitySourceName, Version.ToString());
3333

3434
private readonly GrpcClientInstrumentationOptions options;
35-
private readonly ActivitySourceAdapter activitySource;
3635
private readonly PropertyFetcher<HttpRequestMessage> startRequestFetcher = new PropertyFetcher<HttpRequestMessage>("Request");
3736
private readonly PropertyFetcher<HttpResponseMessage> stopRequestFetcher = new PropertyFetcher<HttpResponseMessage>("Response");
3837

39-
public GrpcClientDiagnosticListener(ActivitySourceAdapter activitySource, GrpcClientInstrumentationOptions options)
38+
public GrpcClientDiagnosticListener(GrpcClientInstrumentationOptions options)
4039
: base("Grpc.Net.Client")
4140
{
42-
if (activitySource == null)
43-
{
44-
throw new ArgumentNullException(nameof(activitySource));
45-
}
46-
4741
this.options = options;
48-
this.activitySource = activitySource;
4942
}
5043

5144
public override void OnStartActivity(Activity activity, object payload)
@@ -89,7 +82,8 @@ public override void OnStartActivity(Activity activity, object payload)
8982

9083
activity.DisplayName = grpcMethod?.Trim('/');
9184

92-
this.activitySource.Start(activity, ActivityKind.Client, ActivitySource);
85+
ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
86+
ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Client);
9387

9488
if (activity.IsAllDataRequested)
9589
{
@@ -158,8 +152,6 @@ public override void OnStopActivity(Activity activity, object payload)
158152
}
159153
}
160154
}
161-
162-
this.activitySource.Stop(activity);
163155
}
164156
}
165157
}

src/OpenTelemetry.Instrumentation.GrpcNetClient/TracerProviderBuilderExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System;
1818
using OpenTelemetry.Instrumentation.GrpcNetClient;
19+
using OpenTelemetry.Instrumentation.GrpcNetClient.Implementation;
1920

2021
namespace OpenTelemetry.Trace
2122
{
@@ -43,7 +44,10 @@ public static TracerProviderBuilder AddGrpcClientInstrumentation(
4344
var grpcOptions = new GrpcClientInstrumentationOptions();
4445
configure?.Invoke(grpcOptions);
4546

46-
builder.AddDiagnosticSourceInstrumentation((activitySource) => new GrpcClientInstrumentation(activitySource, grpcOptions));
47+
builder.AddInstrumentation(() => new GrpcClientInstrumentation(grpcOptions));
48+
builder.AddSource(GrpcClientDiagnosticListener.ActivitySourceName);
49+
builder.AddLegacyActivity("Grpc.Net.Client.GrpcOut");
50+
4751
return builder;
4852
}
4953
}

0 commit comments

Comments
 (0)