Skip to content

Commit

Permalink
[OpenTracing.Shim] TracerShim constructor (#4812)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek authored Aug 30, 2023
1 parent 1da0297 commit 759d540
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 40 deletions.
8 changes: 4 additions & 4 deletions examples/Console/TestOpenTracingShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ internal class TestOpenTracingShim
{
internal static object Run(OpenTracingShimOptions options)
{
// Enable OpenTelemetry for the source "MyCompany.MyProduct.MyWebServer"
// Enable OpenTelemetry for the source "opentracing-shim"
// and use Console exporter.
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyCompany.MyProduct.MyWebServer")
.AddSource("opentracing-shim")
.ConfigureResource(r => r.AddService("MyServiceName"))
.AddConsoleExporter()
.Build();

// Instantiate the OpenTracing shim. The underlying OpenTelemetry tracer will create
// spans using the "MyCompany.MyProduct.MyWebServer" source.
// spans using the "opentracing-shim" source.
var openTracingTracerShim = new TracerShim(
TracerProvider.Default.GetTracer("MyCompany.MyProduct.MyWebServer"),
TracerProvider.Default,
Propagators.DefaultTextMapPropagator);

// The OpenTracing Tracer shim instance must be registered prior to any calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ OpenTelemetry.Shims.OpenTracing.TracerShim.BuildSpan(string operationName) -> Op
OpenTelemetry.Shims.OpenTracing.TracerShim.Extract<TCarrier>(OpenTracing.Propagation.IFormat<TCarrier> format, TCarrier carrier) -> OpenTracing.ISpanContext
OpenTelemetry.Shims.OpenTracing.TracerShim.Inject<TCarrier>(OpenTracing.ISpanContext spanContext, OpenTracing.Propagation.IFormat<TCarrier> format, TCarrier carrier) -> void
OpenTelemetry.Shims.OpenTracing.TracerShim.ScopeManager.get -> OpenTracing.IScopeManager
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.Tracer tracer, OpenTelemetry.Context.Propagation.TextMapPropagator textFormat) -> void
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.Tracer tracer, OpenTelemetry.Context.Propagation.TextMapPropagator textFormat) -> void
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.TracerProvider tracerProvider) -> void
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.TracerProvider tracerProvider, OpenTelemetry.Context.Propagation.TextMapPropagator textFormat) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ OpenTelemetry.Shims.OpenTracing.TracerShim.BuildSpan(string operationName) -> Op
OpenTelemetry.Shims.OpenTracing.TracerShim.Extract<TCarrier>(OpenTracing.Propagation.IFormat<TCarrier> format, TCarrier carrier) -> OpenTracing.ISpanContext
OpenTelemetry.Shims.OpenTracing.TracerShim.Inject<TCarrier>(OpenTracing.ISpanContext spanContext, OpenTracing.Propagation.IFormat<TCarrier> format, TCarrier carrier) -> void
OpenTelemetry.Shims.OpenTracing.TracerShim.ScopeManager.get -> OpenTracing.IScopeManager
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.Tracer tracer, OpenTelemetry.Context.Propagation.TextMapPropagator textFormat) -> void
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.Tracer tracer, OpenTelemetry.Context.Propagation.TextMapPropagator textFormat) -> void
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.TracerProvider tracerProvider) -> void
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.TracerProvider tracerProvider, OpenTelemetry.Context.Propagation.TextMapPropagator textFormat) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ OpenTelemetry.Shims.OpenTracing.TracerShim.Extract<TCarrier>(OpenTracing.Propaga
OpenTelemetry.Shims.OpenTracing.TracerShim.Inject<TCarrier>(OpenTracing.ISpanContext spanContext, OpenTracing.Propagation.IFormat<TCarrier> format, TCarrier carrier) -> void
OpenTelemetry.Shims.OpenTracing.TracerShim.ScopeManager.get -> OpenTracing.IScopeManager
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.Tracer tracer, OpenTelemetry.Context.Propagation.TextMapPropagator textFormat) -> void
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.TracerProvider tracerProvider) -> void
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.TracerProvider tracerProvider, OpenTelemetry.Context.Propagation.TextMapPropagator textFormat) -> void
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Shims.OpenTracing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* Fix: Do not raise `ArgumentException` if `Activity` behind the shim span
has an invalid context.
([#2787](https://github.com/open-telemetry/opentelemetry-dotnet/issues/2787))
* Obsolete `TracerShim(Tracer, TextMapPropagator)` constructor.
Provide `TracerShim(TracerProvider)`
and `TracerShim(TracerProvider, TextMapPropagator)` constructors.
([#4812](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4812))

## 1.5.0-beta.1

Expand Down
37 changes: 32 additions & 5 deletions src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,51 @@ namespace OpenTelemetry.Shims.OpenTracing;
public class TracerShim : global::OpenTracing.ITracer
{
private readonly Trace.Tracer tracer;
private readonly TextMapPropagator propagator;
private readonly TextMapPropagator definedPropagator;

public TracerShim(Trace.TracerProvider tracerProvider)
: this(tracerProvider, null)
{
}

public TracerShim(Trace.TracerProvider tracerProvider, TextMapPropagator textFormat)
{
Guard.ThrowIfNull(tracerProvider);

var assemblyName = typeof(TracerShim).Assembly.GetName();
var version = assemblyName.Version;

this.tracer = tracerProvider.GetTracer("opentracing-shim", version?.ToString());
this.definedPropagator = textFormat;
this.ScopeManager = new ScopeManagerShim();
}

[Obsolete("Use TracerShim(TracerProvider, TextMapPropagator) or TracerShim(TracerProvider) constructor.")]

public TracerShim(Trace.Tracer tracer, TextMapPropagator textFormat)
{
Guard.ThrowIfNull(tracer);
Guard.ThrowIfNull(textFormat);

this.tracer = tracer;
this.propagator = textFormat;
this.definedPropagator = textFormat;
this.ScopeManager = new ScopeManagerShim();
}

/// <inheritdoc/>
public global::OpenTracing.IScopeManager ScopeManager { get; private set; }
public global::OpenTracing.IScopeManager ScopeManager { get; }

/// <inheritdoc/>
public global::OpenTracing.ISpan ActiveSpan => this.ScopeManager.Active?.Span;

private TextMapPropagator Propagator
{
get
{
return this.definedPropagator ?? Propagators.DefaultTextMapPropagator;
}
}

/// <inheritdoc/>
public global::OpenTracing.ISpanBuilder BuildSpan(string operationName)
{
Expand Down Expand Up @@ -74,7 +101,7 @@ static IEnumerable<string> GetCarrierKeyValue(Dictionary<string, IEnumerable<str
return value;
}

propagationContext = this.propagator.Extract(propagationContext, carrierMap, GetCarrierKeyValue);
propagationContext = this.Propagator.Extract(propagationContext, carrierMap, GetCarrierKeyValue);
}

// TODO:
Expand All @@ -98,7 +125,7 @@ public void Inject<TCarrier>(

if ((format == BuiltinFormats.TextMap || format == BuiltinFormats.HttpHeaders) && carrier is ITextMap textMapCarrier)
{
this.propagator.Inject(
this.Propagator.Inject(
new PropagationContext(shim.SpanContext, Baggage.Current),
textMapCarrier,
(instrumentation, key, value) => instrumentation.Set(key, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class IntegrationTests
{
private const string ChildActivitySource = "ChildActivitySource";
private const string ParentActivitySource = "ParentActivitySource";
private const string ShimTracerName = "OpenTracing.Shim";

[Theory]
[InlineData(SamplingDecision.Drop, SamplingDecision.Drop, SamplingDecision.Drop)]
Expand Down Expand Up @@ -63,14 +62,14 @@ public void WithActivities(
b => b.AddSource(ParentActivitySource))
.When(
shimSamplingDecision == SamplingDecision.RecordAndSample,
b => b.AddSource(ShimTracerName))
b => b.AddSource("opentracing-shim"))
.When(
childActivitySamplingDecision == SamplingDecision.RecordAndSample,
b => b.AddSource(ChildActivitySource))
.Build();

ITracer otTracer = new TracerShim(
tracerProvider.GetTracer(ShimTracerName),
tracerProvider,
Propagators.DefaultTextMapPropagator);

// Real usage requires a call OpenTracing.Util.GlobalTracer.Register(otTracer),
Expand Down
38 changes: 12 additions & 26 deletions test/OpenTelemetry.Shims.OpenTracing.Tests/TracerShimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,20 @@ namespace OpenTelemetry.Shims.OpenTracing.Tests;

public class TracerShimTests
{
private const string TracerName = "defaultactivitysource";

[Fact]
public void CtorArgumentValidation()
{
// null tracer and text format
Assert.Throws<ArgumentNullException>(() => new TracerShim(null, null));

// null tracer
Assert.Throws<ArgumentNullException>(() => new TracerShim(null, new TraceContextPropagator()));
// null tracer provider and text format
Assert.Throws<ArgumentNullException>(() => new TracerShim((TracerProvider)null, null));

// null context format
var tracerMock = new Mock<Tracer>();
Assert.Throws<ArgumentNullException>(() => new TracerShim(TracerProvider.Default.GetTracer("test"), null));
// null tracer provider
Assert.Throws<ArgumentNullException>(() => new TracerShim((TracerProvider)null, new TraceContextPropagator()));
}

[Fact]
public void ScopeManager_NotNull()
{
var tracer = TracerProvider.Default.GetTracer(TracerName);
var shim = new TracerShim(tracer, new TraceContextPropagator());
var shim = new TracerShim(TracerProvider.Default, new TraceContextPropagator());

// Internals of the ScopeManagerShim tested elsewhere
Assert.NotNull(shim.ScopeManager as ScopeManagerShim);
Expand All @@ -56,8 +49,7 @@ public void ScopeManager_NotNull()
[Fact]
public void BuildSpan_NotNull()
{
var tracer = TracerProvider.Default.GetTracer(TracerName);
var shim = new TracerShim(tracer, new TraceContextPropagator());
var shim = new TracerShim(TracerProvider.Default, new TraceContextPropagator());

// Internals of the SpanBuilderShim tested elsewhere
Assert.NotNull(shim.BuildSpan("foo") as SpanBuilderShim);
Expand All @@ -66,8 +58,7 @@ public void BuildSpan_NotNull()
[Fact]
public void Inject_ArgumentValidation()
{
var tracer = TracerProvider.Default.GetTracer(TracerName);
var shim = new TracerShim(tracer, new TraceContextPropagator());
var shim = new TracerShim(TracerProvider.Default, new TraceContextPropagator());

var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None));
var mockFormat = new Mock<IFormat<ITextMap>>();
Expand All @@ -82,8 +73,7 @@ public void Inject_ArgumentValidation()
[Fact]
public void Inject_UnknownFormatIgnored()
{
var tracer = TracerProvider.Default.GetTracer(TracerName);
var shim = new TracerShim(tracer, new TraceContextPropagator());
var shim = new TracerShim(TracerProvider.Default, new TraceContextPropagator());

var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded));

Expand All @@ -98,8 +88,7 @@ public void Inject_UnknownFormatIgnored()
[Fact]
public void Extract_ArgumentValidation()
{
var tracer = TracerProvider.Default.GetTracer(TracerName);
var shim = new TracerShim(tracer, new TraceContextPropagator());
var shim = new TracerShim(TracerProvider.Default, new TraceContextPropagator());

Assert.Throws<ArgumentNullException>(() => shim.Extract(null, new Mock<ITextMap>().Object));
Assert.Throws<ArgumentNullException>(() => shim.Extract(new Mock<IFormat<ITextMap>>().Object, null));
Expand All @@ -108,8 +97,7 @@ public void Extract_ArgumentValidation()
[Fact]
public void Extract_UnknownFormatIgnored()
{
var tracer = TracerProvider.Default.GetTracer(TracerName);
var shim = new TracerShim(tracer, new TraceContextPropagator());
var shim = new TracerShim(TracerProvider.Default, new TraceContextPropagator());

var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None));

Expand All @@ -124,8 +112,7 @@ public void Extract_UnknownFormatIgnored()
[Fact]
public void Extract_InvalidTraceParent()
{
var tracer = TracerProvider.Default.GetTracer(TracerName);
var shim = new TracerShim(tracer, new TraceContextPropagator());
var shim = new TracerShim(TracerProvider.Default, new TraceContextPropagator());

var mockCarrier = new Mock<ITextMap>();

Expand Down Expand Up @@ -154,8 +141,7 @@ public void InjectExtract_TextMap_Ok()

var format = new TraceContextPropagator();

var tracer = TracerProvider.Default.GetTracer(TracerName);
var shim = new TracerShim(tracer, format);
var shim = new TracerShim(TracerProvider.Default, format);

// first inject
shim.Inject(spanContextShim, BuiltinFormats.TextMap, carrier);
Expand Down

0 comments on commit 759d540

Please sign in to comment.