Add GlobalPropagators API and have instrumentation default to it (#1428)

* 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
This commit is contained in:
Cijo Thomas 2020-11-02 20:40:31 -08:00 committed by GitHub
parent ddac284e53
commit 097925980d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 195 additions and 28 deletions

View File

@ -28,7 +28,7 @@ namespace Utils.Messaging
public class MessageSender : IDisposable public class MessageSender : IDisposable
{ {
private static readonly ActivitySource ActivitySource = new ActivitySource(nameof(MessageSender)); 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 ILogger<MessageSender> logger;
private readonly IConnection connection; private readonly IConnection connection;

View File

@ -15,6 +15,8 @@
to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator
and changed from interface to abstract class. and changed from interface to abstract class.
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427)) ([#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 ## 0.7.0-beta.1

View File

@ -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)
{
}
}
}

View File

@ -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;
}
}
}

View File

@ -27,13 +27,10 @@ namespace OpenTelemetry.Instrumentation.AspNet
public class AspNetInstrumentationOptions public class AspNetInstrumentationOptions
{ {
/// <summary> /// <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> /// </summary>
public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[] public TextMapPropagator Propagator { get; set; }
{
new TraceContextPropagator(),
new BaggagePropagator(),
});
/// <summary> /// <summary>
/// Gets or sets a Filter function to filter instrumentation for requests on a per request basis. /// Gets or sets a Filter function to filter instrumentation for requests on a per request basis.

View File

@ -6,6 +6,8 @@
to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator
and changed from interface to abstract class. and changed from interface to abstract class.
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427)) ([#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 ## 0.7.0-beta.1

View File

@ -69,10 +69,11 @@ namespace OpenTelemetry.Instrumentation.AspNet.Implementation
var request = context.Request; var request = context.Request;
var requestValues = request.Unvalidated; 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() if (ctx.ActivityContext.IsValid()
&& ctx.ActivityContext != new ActivityContext(activity.TraceId, activity.ParentSpanId, activity.ActivityTraceFlags, activity.TraceStateString, true)) && ctx.ActivityContext != new ActivityContext(activity.TraceId, activity.ParentSpanId, activity.ActivityTraceFlags, activity.TraceStateString, true))
@ -139,7 +140,8 @@ namespace OpenTelemetry.Instrumentation.AspNet.Implementation
Activity activityToEnrich = activity; Activity activityToEnrich = activity;
Activity createdActivity = null; Activity createdActivity = null;
bool isCustomPropagator = !(this.options.Propagator is TraceContextPropagator); var textMapPropagator = this.options.Propagator ?? Propagators.DefaultTextMapPropagator;
bool isCustomPropagator = !(textMapPropagator is TraceContextPropagator);
if (isCustomPropagator) if (isCustomPropagator)
{ {

View File

@ -27,13 +27,10 @@ namespace OpenTelemetry.Instrumentation.AspNetCore
public class AspNetCoreInstrumentationOptions public class AspNetCoreInstrumentationOptions
{ {
/// <summary> /// <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> /// </summary>
public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[] public TextMapPropagator Propagator { get; set; } = Propagators.DefaultTextMapPropagator;
{
new TraceContextPropagator(),
new BaggagePropagator(),
});
/// <summary> /// <summary>
/// Gets or sets a Filter function to filter instrumentation for requests on a per request basis. /// Gets or sets a Filter function to filter instrumentation for requests on a per request basis.

View File

@ -14,6 +14,8 @@
to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator
and changed from interface to abstract class. and changed from interface to abstract class.
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427)) ([#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 ## 0.7.0-beta.1

View File

@ -10,6 +10,8 @@
to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator
and changed from interface to abstract class. and changed from interface to abstract class.
([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427)) ([#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 ## 0.7.0-beta.1

View File

@ -35,13 +35,10 @@ namespace OpenTelemetry.Instrumentation.Http
public bool SetHttpFlavor { get; set; } public bool SetHttpFlavor { get; set; }
/// <summary> /// <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> /// </summary>
public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[] public TextMapPropagator Propagator { get; set; } = Propagators.DefaultTextMapPropagator;
{
new TraceContextPropagator(),
new BaggagePropagator(),
});
/// <summary> /// <summary>
/// Gets or sets a Filter function to filter instrumentation for requests on a per request basis. /// Gets or sets a Filter function to filter instrumentation for requests on a per request basis.

View File

@ -31,6 +31,13 @@ namespace OpenTelemetry.Logs
private bool disposed; private bool disposed;
private IExternalScopeProvider scopeProvider; 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) public OpenTelemetryLoggerProvider(IOptionsMonitor<OpenTelemetryLoggerOptions> options)
: this(options?.CurrentValue) : this(options?.CurrentValue)
{ {

View File

@ -14,6 +14,8 @@
// limitations under the License. // limitations under the License.
// </copyright> // </copyright>
using System.Diagnostics;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Metrics; using OpenTelemetry.Metrics;
using OpenTelemetry.Trace; using OpenTelemetry.Trace;
@ -24,11 +26,32 @@ namespace OpenTelemetry
/// </summary> /// </summary>
public static class Sdk public static class Sdk
{ {
static Sdk()
{
Propagators.DefaultTextMapPropagator = new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
});
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
}
/// <summary> /// <summary>
/// Gets a value indicating whether instrumentation is suppressed (disabled). /// Gets a value indicating whether instrumentation is suppressed (disabled).
/// </summary> /// </summary>
public static bool SuppressInstrumentation => SuppressInstrumentationScope.IsSuppressed; 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> /// <summary>
/// Creates MeterProviderBuilder which should be used to build MeterProvider. /// Creates MeterProviderBuilder which should be used to build MeterProvider.
/// </summary> /// </summary>

View File

@ -34,12 +34,6 @@ namespace OpenTelemetry.Trace
private readonly ActivitySourceAdapter adapter; private readonly ActivitySourceAdapter adapter;
private BaseProcessor<Activity> processor; private BaseProcessor<Activity> processor;
static TracerProviderSdk()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
}
internal TracerProviderSdk( internal TracerProviderSdk(
Resource resource, Resource resource,
IEnumerable<string> sources, IEnumerable<string> sources,

View File

@ -58,6 +58,21 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Tests
Assert.Throws<ArgumentNullException>(() => builder.AddAspNetCoreInstrumentation()); 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] [Fact]
public async Task StatusIsUnsetOn200Response() public async Task StatusIsUnsetOn200Response()
{ {

View File

@ -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();
}
}
}