Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GlobalPropagators API and have instrumentation default to it #1428

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
{
}
}
}
36 changes: 36 additions & 0 deletions src/OpenTelemetry.Api/Context/Propagation/Propagators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// <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 or sets the Default TextMapPropagator to be used.
/// </summary>
public static TextMapPropagator DefaultTextMapPropagator { get; set; } = Noop;

internal static void Reset()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for tests only.

{
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; } = Propagators.DefaultTextMapPropagator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a timing thing here. When options are created they'll copy the ref for the current default propagator. If user sets through SDK the global propagator after that, it won't be reflected. Could leave it null here and then when it is used in instrumentation do options.Propagator ?? Propagators.DefaultTextMapPropagator. It would be a slight perf hit but it makes it hot-swappable.


/// <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 @@ -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
14 changes: 14 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,6 +26,18 @@ namespace OpenTelemetry
/// </summary>
public static class Sdk
{
static Sdk()
{
Propagators.DefaultTextMapPropagator = new CompositeTextMapPropagator(new TextMapPropagator[]
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
{
new TraceContextPropagator(),
new BaggagePropagator(),
});

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

/// <summary>
/// Gets a value indicating whether instrumentation is suppressed (disabled).
/// </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();
}
}
}