Skip to content

Commit

Permalink
Revert "Removing Propagator from Options (#1448)"
Browse files Browse the repository at this point in the history
This reverts commit 7c9bee3.
  • Loading branch information
cijothomas authored Nov 4, 2020
1 parent 7c9bee3 commit ccd9ae0
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 55 deletions.
6 changes: 4 additions & 2 deletions examples/AspNet/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ public class WebApiApplication : HttpApplication
protected void Application_Start()
{
var builder = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation()
.AddHttpClientInstrumentation();
.AddAspNetInstrumentation(options => options.Propagator = new B3Propagator())
.AddHttpClientInstrumentation(
httpClientOptions => httpClientOptions.Propagator = new B3Propagator(),
httpWebRequestOptions => httpWebRequestOptions.Propagator = new B3Propagator());

switch (ConfigurationManager.AppSettings["UseExporter"].ToLowerInvariant())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Diagnostics;
using System.Web;
using OpenTelemetry.Context.Propagation;

namespace OpenTelemetry.Instrumentation.AspNet
{
Expand All @@ -25,6 +26,12 @@ namespace OpenTelemetry.Instrumentation.AspNet
/// </summary>
public class AspNetInstrumentationOptions
{
/// <summary>
/// Gets or sets <see cref="TextMapPropagator"/> for context propagation.
/// By default, <see cref="Propagators.DefaultTextMapPropagator" /> will be used.
/// </summary>
public TextMapPropagator Propagator { get; set; }

/// <summary>
/// Gets or sets a Filter function to filter instrumentation for requests on a per request basis.
/// The Filter gets the HttpContext, and should return a boolean.
Expand Down
5 changes: 1 addition & 4 deletions src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator
and changed from interface to abstract class.
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427))
* Propagators.DefaultTextMapPropagator will be used as the default Propagator.
* Propagators.DefaultTextMapPropagator will be used as the default Propagator
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1428))
* Removed Propagator from Instrumentation Options. Instrumentation now always
respect the Propagator.DefaultTextMapPropagator.
([#1448](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1448))

## 0.7.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public override void OnStartActivity(Activity activity, object payload)

var request = context.Request;
var requestValues = request.Unvalidated;
var textMapPropagator = Propagators.DefaultTextMapPropagator;
var textMapPropagator = this.options.Propagator ?? Propagators.DefaultTextMapPropagator;

if (!(textMapPropagator is TraceContextPropagator))
{
Expand Down Expand Up @@ -140,7 +140,7 @@ public override void OnStopActivity(Activity activity, object payload)
Activity activityToEnrich = activity;
Activity createdActivity = null;

var textMapPropagator = Propagators.DefaultTextMapPropagator;
var textMapPropagator = this.options.Propagator ?? Propagators.DefaultTextMapPropagator;
bool isCustomPropagator = !(textMapPropagator is TraceContextPropagator);

if (isCustomPropagator)
Expand Down
7 changes: 6 additions & 1 deletion src/OpenTelemetry.Instrumentation.AspNet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public class WebApiApplication : HttpApplication
## Advanced configuration

This instrumentation can be configured to change the default behavior by using
`AspNetInstrumentationOptions`, which allows configuring `Filter` as explained below.
`AspNetInstrumentationOptions`, which allows configuring `Propagator` and
`Filter` as explained below.

### Propagator

TODO

### Filter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using OpenTelemetry.Context.Propagation;

namespace OpenTelemetry.Instrumentation.AspNetCore
{
Expand All @@ -25,6 +26,12 @@ namespace OpenTelemetry.Instrumentation.AspNetCore
/// </summary>
public class AspNetCoreInstrumentationOptions
{
/// <summary>
/// Gets or sets <see cref="TextMapPropagator"/> for context propagation.
/// By default, <see cref="Propagators.DefaultTextMapPropagator" /> will be used.
/// </summary>
public TextMapPropagator Propagator { get; set; } = Propagators.DefaultTextMapPropagator;

/// <summary>
/// Gets or sets a Filter function to filter instrumentation for requests on a per request basis.
/// The Filter gets the HttpContext, and should return a boolean.
Expand Down
3 changes: 0 additions & 3 deletions src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427))
* Propagators.DefaultTextMapPropagator will be used as the default Propagator
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1428))
* Removed Propagator from Instrumentation Options. Instrumentation now always
respect the Propagator.DefaultTextMapPropagator.
([#1448](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1448))

## 0.7.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ public override void OnStartActivity(Activity activity, object payload)
}

var request = context.Request;
var textMapPropagator = Propagators.DefaultTextMapPropagator;
if (!this.hostingSupportsW3C || !(textMapPropagator is TraceContextPropagator))
if (!this.hostingSupportsW3C || !(this.options.Propagator is TraceContextPropagator))
{
var ctx = textMapPropagator.Extract(default, request, HttpRequestHeaderValuesGetter);
var ctx = this.options.Propagator.Extract(default, request, HttpRequestHeaderValuesGetter);

if (ctx.ActivityContext.IsValid()
&& ctx.ActivityContext != new ActivityContext(activity.TraceId, activity.ParentSpanId, activity.ActivityTraceFlags, activity.TraceStateString, true))
Expand Down
7 changes: 6 additions & 1 deletion src/OpenTelemetry.Instrumentation.AspNetCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ public void ConfigureServices(IServiceCollection services)
## Advanced configuration

This instrumentation can be configured to change the default behavior by using
`AspNetCoreInstrumentationOptions`, which allows adding [`Filter`](#filter),
`AspNetCoreInstrumentationOptions`, which allows configuring
[`Propagator`](#propagator) and adding [`Filter`](#filter),
[`Enrich`](#enrich) as explained below.

### Propagator

TODO

### Filter

This instrumentation by default collects all the incoming http requests. It
Expand Down
3 changes: 0 additions & 3 deletions src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427))
* Propagators.DefaultTextMapPropagator will be used as the default Propagator
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1428))
* Removed Propagator from Instrumentation Options. Instrumentation now always
respect the Propagator.DefaultTextMapPropagator.
([#1448](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1448))

## 0.7.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Diagnostics;
using System.Net.Http;
using System.Runtime.CompilerServices;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.Http.Implementation;
using OpenTelemetry.Trace;

Expand All @@ -33,6 +34,12 @@ public class HttpClientInstrumentationOptions
/// </summary>
public bool SetHttpFlavor { get; set; }

/// <summary>
/// Gets or sets <see cref="TextMapPropagator"/> for context propagation.
/// By default, <see cref="Propagators.DefaultTextMapPropagator" /> will be used.
/// </summary>
public TextMapPropagator Propagator { get; set; } = Propagators.DefaultTextMapPropagator;

/// <summary>
/// Gets or sets a Filter function to filter instrumentation for requests on a per request basis.
/// The Filter gets the HttpRequestMessage, and should return a boolean.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Diagnostics;
using System.Net;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.Http.Implementation;
using OpenTelemetry.Trace;

Expand All @@ -33,6 +34,15 @@ public class HttpWebRequestInstrumentationOptions
/// </summary>
public bool SetHttpFlavor { get; set; }

/// <summary>
/// Gets or sets <see cref="TextMapPropagator"/> for context propagation. Default value: <see cref="CompositeTextMapPropagator"/> with <see cref="TraceContextPropagator"/> &amp; <see cref="BaggagePropagator"/>.
/// </summary>
public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
});

/// <summary>
/// Gets or sets a Filter function to filter instrumentation for requests on a per request basis.
/// The Filter gets the HttpWebRequest, and should return a boolean.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public override void OnStartActivity(Activity activity, object payload)
return;
}

if (Propagators.DefaultTextMapPropagator.Extract(default, request, HttpRequestMessageHeaderValuesGetter) != default)
if (this.options.Propagator.Extract(default, request, HttpRequestMessageHeaderValuesGetter) != default)
{
// this request is already instrumented, we should back off
activity.IsAllDataRequested = false;
Expand Down Expand Up @@ -115,11 +115,9 @@ public override void OnStartActivity(Activity activity, object payload)
}
}

var textMapPropagator = Propagators.DefaultTextMapPropagator;

if (!(this.httpClientSupportsW3C && textMapPropagator is TraceContextPropagator))
if (!(this.httpClientSupportsW3C && this.options.Propagator is TraceContextPropagator))
{
textMapPropagator.Inject(new PropagationContext(activity.Context, Baggage.Current), request, HttpRequestMessageHeaderValueSetter);
this.options.Propagator.Inject(new PropagationContext(activity.Context, Baggage.Current), request, HttpRequestMessageHeaderValueSetter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ private static void AddExceptionTags(Exception exception, Activity activity)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void InstrumentRequest(HttpWebRequest request, ActivityContext activityContext)
=> Propagators.DefaultTextMapPropagator.Inject(new PropagationContext(activityContext, Baggage.Current), request, HttpWebRequestHeaderValuesSetter);
=> Options.Propagator.Inject(new PropagationContext(activityContext, Baggage.Current), request, HttpWebRequestHeaderValuesSetter);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsRequestInstrumented(HttpWebRequest request)
=> Propagators.DefaultTextMapPropagator.Extract(default, request, HttpWebRequestHeaderValuesGetter) != default;
=> Options.Propagator.Extract(default, request, HttpWebRequestHeaderValuesGetter) != default;

private static void ProcessRequest(HttpWebRequest request)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ public void AspNetRequestsAreCollectedSuccessfully(
}

var activityProcessor = new Mock<BaseProcessor<Activity>>();
Sdk.SetDefaultTextMapPropagator(propagator.Object);
using (openTelemetry = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation(
(options) =>
Expand All @@ -164,6 +163,11 @@ public void AspNetRequestsAreCollectedSuccessfully(
return httpContext.Request.Path != filter;
};
if (!carrierFormat.Equals("TraceContext"))
{
options.Propagator = propagator.Object;
}
options.Enrich = ActivityEnrichment;
})
.SetResource(expectedResource)
Expand Down
23 changes: 16 additions & 7 deletions test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ public void AddAspNetCoreInstrumentation_BadArgs()
Assert.Throws<ArgumentNullException>(() => builder.AddAspNetCoreInstrumentation());
}

[Fact]
public void DefaultPropagatorIsFromPropagators()
{
var options = new AspNetCoreInstrumentationOptions();
Assert.Same(Propagators.DefaultTextMapPropagator, options.Propagator);
}

[Fact]
public void PropagatorSetDoesNotAffectGlobalPropagators()
{
var options = new AspNetCoreInstrumentationOptions();
options.Propagator = new TraceContextPropagator();
Assert.NotSame(Propagators.DefaultTextMapPropagator, options.Propagator);
}

[Fact]
public async Task StatusIsUnsetOn200Response()
{
Expand Down Expand Up @@ -210,9 +225,8 @@ public async Task CustomPropagator()
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(services =>
{
Sdk.SetDefaultTextMapPropagator(propagator.Object);
this.openTelemetrySdk = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation()
.AddAspNetCoreInstrumentation((opt) => opt.Propagator = propagator.Object)
.AddProcessor(activityProcessor.Object)
.Build();
})))
Expand All @@ -236,11 +250,6 @@ public async Task CustomPropagator()

Assert.Equal(expectedTraceId, activity.Context.TraceId);
Assert.Equal(expectedSpanId, activity.ParentSpanId);
Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
}));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public async Task HttpClientInstrumentationInjectsHeadersAsync(bool shouldEnrich
parent.TraceStateString = "k1=v1,k2=v2";
parent.ActivityTraceFlags = ActivityTraceFlags.Recorded;

// Ensure that the header value func does not throw if the header key can't be found
var mockPropagator = new Mock<TextMapPropagator>();

// var isInjectedHeaderValueGetterThrows = false;
// mockTextFormat
// .Setup(x => x.IsInjected(It.IsAny<HttpRequestMessage>(), It.IsAny<Func<HttpRequestMessage, string, IEnumerable<string>>>()))
Expand All @@ -94,6 +97,7 @@ public async Task HttpClientInstrumentationInjectsHeadersAsync(bool shouldEnrich
using (Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation(o =>
{
o.Propagator = mockPropagator.Object;
if (shouldEnrich)
{
o.Enrich = ActivityEnrichment;
Expand Down Expand Up @@ -151,11 +155,10 @@ public async Task HttpClientInstrumentationInjectsHeadersAsync_CustomFormat(bool
parent.TraceStateString = "k1=v1,k2=v2";
parent.ActivityTraceFlags = ActivityTraceFlags.Recorded;

Sdk.SetDefaultTextMapPropagator(propagator.Object);

using (Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation((opt) =>
{
opt.Propagator = propagator.Object;
if (shouldEnrich)
{
opt.Enrich = ActivityEnrichment;
Expand Down Expand Up @@ -184,11 +187,6 @@ public async Task HttpClientInstrumentationInjectsHeadersAsync_CustomFormat(bool

Assert.Equal($"00/{activity.Context.TraceId}/{activity.Context.SpanId}/01", traceparents.Single());
Assert.Equal("k1=v1,k2=v2", tracestates.Single());
Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
}));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ public async Task HttpWebRequestInstrumentationInjectsHeadersAsyncWhenActivityIs
contentFromPropagator = context.ActivityContext;
});

Sdk.SetDefaultTextMapPropagator(propagator.Object);
// Sdk.SetDefaultTextMapPropagator(propagator.Object);
using var shutdownSignal = Sdk.CreateTracerProviderBuilder()
.AddProcessor(activityProcessor.Object)
.AddHttpWebRequestInstrumentation()
.AddHttpWebRequestInstrumentation(options => options.Propagator = propagator.Object)
.Build();

var request = (HttpWebRequest)WebRequest.Create(this.url);
Expand All @@ -135,11 +135,6 @@ public async Task HttpWebRequestInstrumentationInjectsHeadersAsyncWhenActivityIs
Assert.NotEqual(default, contentFromPropagator.SpanId);

parent.Stop();
Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
}));
}

[Fact]
Expand All @@ -154,10 +149,9 @@ public async Task HttpWebRequestInstrumentationInjectsHeadersAsync_CustomFormat(
});

var activityProcessor = new Mock<BaseProcessor<Activity>>();
Sdk.SetDefaultTextMapPropagator(propagator.Object);
using var shutdownSignal = Sdk.CreateTracerProviderBuilder()
.AddProcessor(activityProcessor.Object)
.AddHttpWebRequestInstrumentation()
.AddHttpWebRequestInstrumentation(options => options.Propagator = propagator.Object)
.Build();

var request = (HttpWebRequest)WebRequest.Create(this.url);
Expand Down Expand Up @@ -188,11 +182,6 @@ public async Task HttpWebRequestInstrumentationInjectsHeadersAsync_CustomFormat(
Assert.Equal("k1=v1,k2=v2", tracestate);

parent.Stop();
Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
}));
}

[Fact]
Expand Down

0 comments on commit ccd9ae0

Please sign in to comment.