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 2 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
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="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>

using System;
using System.Collections.Generic;

namespace OpenTelemetry.Context.Propagation
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
{
internal class NoOpTextMapPropagator : TextMapPropagator
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
{
private static PropagationContext defaultPropagationContext = default;

public override ISet<string> Fields => null;
Copy link
Member Author

Choose a reason for hiding this comment

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

anything better than 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)
{
}
}
}
34 changes: 34 additions & 0 deletions src/OpenTelemetry.Api/Context/Propagation/Propagators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <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
{
/// <summary>
/// Gets or sets the Default TextMapPropagator to be used.
/// </summary>
public static TextMapPropagator DefaultTextMapPropagator { get; set; } = new NoOpTextMapPropagator();

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 = new NoOpTextMapPropagator();
}
}
}
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
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
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.
_ = Sdk.SuppressInstrumentation;
}

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

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

Expand All @@ -24,6 +25,15 @@ 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(),
});
}

/// <summary>
/// Gets a value indicating whether instrumentation is suppressed (disabled).
/// </summary>
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
46 changes: 46 additions & 0 deletions test/OpenTelemetry.Tests/Context/PropagatorsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// <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
{
[Fact]
public static void DefaultTextMapPropagatorIsNoop()
{
Assert.IsType<NoOpTextMapPropagator>(Propagators.DefaultTextMapPropagator);
Assert.Same(Propagators.DefaultTextMapPropagator, Propagators.DefaultTextMapPropagator);
}

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

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