diff --git a/examples/Console/TestOpenTracingShim.cs b/examples/Console/TestOpenTracingShim.cs index c059e18e961..da9921cf914 100644 --- a/examples/Console/TestOpenTracingShim.cs +++ b/examples/Console/TestOpenTracingShim.cs @@ -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 diff --git a/src/OpenTelemetry.Shims.OpenTracing/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Shims.OpenTracing/.publicApi/net462/PublicAPI.Unshipped.txt index a9a1b3717e4..b0ed15509e4 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Shims.OpenTracing/.publicApi/net462/PublicAPI.Unshipped.txt @@ -4,4 +4,6 @@ OpenTelemetry.Shims.OpenTracing.TracerShim.BuildSpan(string operationName) -> Op OpenTelemetry.Shims.OpenTracing.TracerShim.Extract(OpenTracing.Propagation.IFormat format, TCarrier carrier) -> OpenTracing.ISpanContext OpenTelemetry.Shims.OpenTracing.TracerShim.Inject(OpenTracing.ISpanContext spanContext, OpenTracing.Propagation.IFormat 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 \ No newline at end of file +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 diff --git a/src/OpenTelemetry.Shims.OpenTracing/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Shims.OpenTracing/.publicApi/net6.0/PublicAPI.Unshipped.txt index a9a1b3717e4..b0ed15509e4 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Shims.OpenTracing/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -4,4 +4,6 @@ OpenTelemetry.Shims.OpenTracing.TracerShim.BuildSpan(string operationName) -> Op OpenTelemetry.Shims.OpenTracing.TracerShim.Extract(OpenTracing.Propagation.IFormat format, TCarrier carrier) -> OpenTracing.ISpanContext OpenTelemetry.Shims.OpenTracing.TracerShim.Inject(OpenTracing.ISpanContext spanContext, OpenTracing.Propagation.IFormat 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 \ No newline at end of file +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 diff --git a/src/OpenTelemetry.Shims.OpenTracing/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Shims.OpenTracing/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index f6459c7461e..b0ed15509e4 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Shims.OpenTracing/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -5,3 +5,5 @@ OpenTelemetry.Shims.OpenTracing.TracerShim.Extract(OpenTracing.Propaga OpenTelemetry.Shims.OpenTracing.TracerShim.Inject(OpenTracing.ISpanContext spanContext, OpenTracing.Propagation.IFormat 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 diff --git a/src/OpenTelemetry.Shims.OpenTracing/CHANGELOG.md b/src/OpenTelemetry.Shims.OpenTracing/CHANGELOG.md index 3688b1b8771..80b3d544373 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/CHANGELOG.md +++ b/src/OpenTelemetry.Shims.OpenTracing/CHANGELOG.md @@ -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 diff --git a/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs b/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs index ac07d7e8b86..cb1bd5e8957 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs +++ b/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs @@ -23,7 +23,26 @@ 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) { @@ -31,16 +50,24 @@ public TracerShim(Trace.Tracer tracer, TextMapPropagator textFormat) Guard.ThrowIfNull(textFormat); this.tracer = tracer; - this.propagator = textFormat; + this.definedPropagator = textFormat; this.ScopeManager = new ScopeManagerShim(); } /// - public global::OpenTracing.IScopeManager ScopeManager { get; private set; } + public global::OpenTracing.IScopeManager ScopeManager { get; } /// public global::OpenTracing.ISpan ActiveSpan => this.ScopeManager.Active?.Span; + private TextMapPropagator Propagator + { + get + { + return this.definedPropagator ?? Propagators.DefaultTextMapPropagator; + } + } + /// public global::OpenTracing.ISpanBuilder BuildSpan(string operationName) { @@ -74,7 +101,7 @@ static IEnumerable GetCarrierKeyValue(Dictionary( 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)); diff --git a/test/OpenTelemetry.Shims.OpenTracing.Tests/IntegrationTests.cs b/test/OpenTelemetry.Shims.OpenTracing.Tests/IntegrationTests.cs index efef7f7055c..3f0883e701a 100644 --- a/test/OpenTelemetry.Shims.OpenTracing.Tests/IntegrationTests.cs +++ b/test/OpenTelemetry.Shims.OpenTracing.Tests/IntegrationTests.cs @@ -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)] @@ -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), diff --git a/test/OpenTelemetry.Shims.OpenTracing.Tests/TracerShimTests.cs b/test/OpenTelemetry.Shims.OpenTracing.Tests/TracerShimTests.cs index c85c6ae5ad6..08f2147c7cb 100644 --- a/test/OpenTelemetry.Shims.OpenTracing.Tests/TracerShimTests.cs +++ b/test/OpenTelemetry.Shims.OpenTracing.Tests/TracerShimTests.cs @@ -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(() => new TracerShim(null, null)); - - // null tracer - Assert.Throws(() => new TracerShim(null, new TraceContextPropagator())); + // null tracer provider and text format + Assert.Throws(() => new TracerShim((TracerProvider)null, null)); - // null context format - var tracerMock = new Mock(); - Assert.Throws(() => new TracerShim(TracerProvider.Default.GetTracer("test"), null)); + // null tracer provider + Assert.Throws(() => 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); @@ -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); @@ -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>(); @@ -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)); @@ -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(() => shim.Extract(null, new Mock().Object)); Assert.Throws(() => shim.Extract(new Mock>().Object, null)); @@ -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)); @@ -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(); @@ -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);