Skip to content

Commit

Permalink
Add GlobalPropagators API and have instrumentation default to it (#1428)
Browse files Browse the repository at this point in the history
* Add GlobalPropagators API and have instrumentation default to it

* changelog

* fix name

* reset after

* min comment address

* move id format to same place as context propagator

* renam

* renai

* text initialize correctly

* modify microsoervice example to pick global propagator

* make Global propagator get only in api

* Add SetDefaultPropagato to SDK
  • Loading branch information
cijothomas authored Nov 3, 2020
1 parent ddac284 commit 0979259
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Utils.Messaging
public class MessageSender : IDisposable
{
private static readonly ActivitySource ActivitySource = new ActivitySource(nameof(MessageSender));
private static readonly TextMapPropagator Propagator = new TraceContextPropagator();
private static readonly TextMapPropagator Propagator = Propagators.DefaultTextMapPropagator;

private readonly ILogger<MessageSender> logger;
private readonly IConnection connection;
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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))
* Added GlobalPropagators API via Propagators.DefaultTextMapPropagator.
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1428))

## 0.7.0-beta.1

Expand Down
37 changes: 37 additions & 0 deletions src/OpenTelemetry.Api/Context/Propagation/NoopTextMapPropagator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// <copyright file="NoopTextMapPropagator.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;

namespace OpenTelemetry.Context.Propagation
{
internal class NoopTextMapPropagator : TextMapPropagator
{
private static readonly PropagationContext DefaultPropagationContext = default;

public override ISet<string> Fields => null;

public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
{
return DefaultPropagationContext;
}

public override void Inject<T>(PropagationContext context, T carrier, Action<T, string, string> setter)
{
}
}
}
39 changes: 39 additions & 0 deletions src/OpenTelemetry.Api/Context/Propagation/Propagators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// <copyright file="Propagators.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

namespace OpenTelemetry.Context.Propagation
{
/// <summary>
/// Propagators allow setting the global default Propagators.
/// </summary>
public static class Propagators
{
private static readonly TextMapPropagator Noop = new NoopTextMapPropagator();

/// <summary>
/// Gets the Default TextMapPropagator to be used.
/// </summary>
/// <remarks>
/// Setting this can be done only from Sdk.
/// </remarks>
public static TextMapPropagator DefaultTextMapPropagator { get; internal set; } = Noop;

internal static void Reset()
{
DefaultTextMapPropagator = Noop;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ namespace OpenTelemetry.Instrumentation.AspNet
public class AspNetInstrumentationOptions
{
/// <summary>
/// Gets or sets <see cref="TextMapPropagator"/> for context propagation. Default value: <see cref="CompositeTextMapPropagator"/> with <see cref="TraceContextPropagator"/> &amp; <see cref="BaggagePropagator"/>.
/// Gets or sets <see cref="TextMapPropagator"/> for context propagation.
/// By default, <see cref="Propagators.DefaultTextMapPropagator" /> will be used.
/// </summary>
public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
});
public TextMapPropagator Propagator { get; set; }

/// <summary>
/// Gets or sets a Filter function to filter instrumentation for requests on a per request basis.
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +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
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1428))

## 0.7.0-beta.1

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

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

if (!(this.options.Propagator is TraceContextPropagator))
if (!(textMapPropagator is TraceContextPropagator))
{
var ctx = this.options.Propagator.Extract(default, request, HttpRequestHeaderValuesGetter);
var ctx = textMapPropagator.Extract(default, request, HttpRequestHeaderValuesGetter);

if (ctx.ActivityContext.IsValid()
&& ctx.ActivityContext != new ActivityContext(activity.TraceId, activity.ParentSpanId, activity.ActivityTraceFlags, activity.TraceStateString, true))
Expand Down Expand Up @@ -139,7 +140,8 @@ public override void OnStopActivity(Activity activity, object payload)
Activity activityToEnrich = activity;
Activity createdActivity = null;

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

if (isCustomPropagator)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ namespace OpenTelemetry.Instrumentation.AspNetCore
public class AspNetCoreInstrumentationOptions
{
/// <summary>
/// Gets or sets <see cref="TextMapPropagator"/> for context propagation. Default value: <see cref="CompositeTextMapPropagator"/> with <see cref="TraceContextPropagator"/> &amp; <see cref="BaggagePropagator"/>.
/// Gets or sets <see cref="TextMapPropagator"/> for context propagation.
/// By default, <see cref="Propagators.DefaultTextMapPropagator" /> will be used.
/// </summary>
public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
});
public TextMapPropagator Propagator { get; set; } = Propagators.DefaultTextMapPropagator;

/// <summary>
/// Gets or sets a Filter function to filter instrumentation for requests on a per request basis.
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1428))

## 0.7.0-beta.1

Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1428))

## 0.7.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,10 @@ public class HttpClientInstrumentationOptions
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"/>.
/// Gets or sets <see cref="TextMapPropagator"/> for context propagation.
/// By default, <see cref="Propagators.DefaultTextMapPropagator" /> will be used.
/// </summary>
public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
});
public TextMapPropagator Propagator { get; set; } = Propagators.DefaultTextMapPropagator;

/// <summary>
/// Gets or sets a Filter function to filter instrumentation for requests on a per request basis.
Expand Down
7 changes: 7 additions & 0 deletions src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public class OpenTelemetryLoggerProvider : ILoggerProvider, ISupportExternalScop
private bool disposed;
private IExternalScopeProvider scopeProvider;

static OpenTelemetryLoggerProvider()
{
// Accessing Sdk class is just to trigger its static ctor,
// which sets default Propagators and default Activity Id format
_ = Sdk.SuppressInstrumentation;
}

public OpenTelemetryLoggerProvider(IOptionsMonitor<OpenTelemetryLoggerOptions> options)
: this(options?.CurrentValue)
{
Expand Down
23 changes: 23 additions & 0 deletions src/OpenTelemetry/Sdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

using System.Diagnostics;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

Expand All @@ -24,11 +26,32 @@ namespace OpenTelemetry
/// </summary>
public static class Sdk
{
static Sdk()
{
Propagators.DefaultTextMapPropagator = new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
});

Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
}

/// <summary>
/// Gets a value indicating whether instrumentation is suppressed (disabled).
/// </summary>
public static bool SuppressInstrumentation => SuppressInstrumentationScope.IsSuppressed;

/// <summary>
/// Sets the Default TextMapPropagator.
/// </summary>
/// <param name="textMapPropagator">TextMapPropagator to be set as default.</param>
public static void SetDefaultTextMapPropagator(TextMapPropagator textMapPropagator)
{
Propagators.DefaultTextMapPropagator = textMapPropagator;
}

/// <summary>
/// Creates MeterProviderBuilder which should be used to build MeterProvider.
/// </summary>
Expand Down
6 changes: 0 additions & 6 deletions src/OpenTelemetry/Trace/TracerProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ internal class TracerProviderSdk : TracerProvider
private readonly ActivitySourceAdapter adapter;
private BaseProcessor<Activity> processor;

static TracerProviderSdk()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
}

internal TracerProviderSdk(
Resource resource,
IEnumerable<string> sources,
Expand Down
15 changes: 15 additions & 0 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
51 changes: 51 additions & 0 deletions test/OpenTelemetry.Tests/Context/PropagatorsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// <copyright file="PropagatorsTest.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Context.Propagation.Tests;
using Xunit;

namespace OpenTelemetry.Context.Tests
{
public class PropagatorsTest : IDisposable
{
public PropagatorsTest()
{
Propagators.Reset();
}

[Fact]
public void DefaultTextMapPropagatorIsNoop()
{
Assert.IsType<NoopTextMapPropagator>(Propagators.DefaultTextMapPropagator);
Assert.Same(Propagators.DefaultTextMapPropagator, Propagators.DefaultTextMapPropagator);
}

[Fact]
public void CanSetPropagator()
{
var testPropagator = new TestPropagator(string.Empty, string.Empty);
Propagators.DefaultTextMapPropagator = testPropagator;
Assert.Same(testPropagator, Propagators.DefaultTextMapPropagator);
}

public void Dispose()
{
Propagators.Reset();
}
}
}

0 comments on commit 0979259

Please sign in to comment.