From 4975fb062bba8cb66b79cab53571608d0a9c08ec Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Fri, 30 Oct 2020 12:18:07 -0700 Subject: [PATCH] Rename of Propagators (#1427) * Rename IPropagator to ITextMapPropagator, TextMapPropagator to TraceContextPropagator * changelogs * build fix * make ITextMapPropagator an abstract class * Renaming ITextMapPropagator to TextMapPropagator * CompositeTextMapPropagator rename * fix changhelog --- .../TestOpenTracingWithConsoleExporter.cs | 2 +- .../Utils/Messaging/MessageReceiver.cs | 2 +- .../Utils/Messaging/MessageSender.cs | 2 +- src/OpenTelemetry.Api/CHANGELOG.md | 4 + .../Context/Propagation/B3Propagator.cs | 10 +- .../Context/Propagation/BaggagePropagator.cs | 10 +- ...gator.cs => CompositeTextMapPropagator.cs} | 23 +- .../Context/Propagation/IPropagator.cs | 54 ---- .../Context/Propagation/TextMapPropagator.cs | 264 ++--------------- .../Propagation/TraceContextPropagator.cs | 265 ++++++++++++++++++ .../AspNetInstrumentationOptions.cs | 6 +- .../CHANGELOG.md | 5 + .../Implementation/HttpInListener.cs | 4 +- .../AspNetCoreInstrumentationOptions.cs | 6 +- .../CHANGELOG.md | 4 + .../Implementation/HttpInListener.cs | 2 +- .../CHANGELOG.md | 4 + .../HttpClientInstrumentationOptions.cs | 6 +- ...pWebRequestInstrumentationOptions.netfx.cs | 6 +- .../HttpHandlerDiagnosticListener.cs | 2 +- .../CHANGELOG.md | 5 + .../TracerShim.cs | 4 +- .../HttpInListenerTests.cs | 2 +- .../BasicTests.cs | 2 +- .../HttpClientTests.Basic.netcore31.cs | 4 +- .../HttpWebRequestTests.Basic.netfx.cs | 2 +- .../TracerShimTests.cs | 20 +- .../Propagation/CompositePropagatorTest.cs | 10 +- .../Trace/Propagation/TestPropagator.cs | 12 +- .../Propagation/TextMapPropagatorTest.cs | 16 +- 30 files changed, 391 insertions(+), 367 deletions(-) rename src/OpenTelemetry.Api/Context/Propagation/{CompositePropagator.cs => CompositeTextMapPropagator.cs} (58%) delete mode 100644 src/OpenTelemetry.Api/Context/Propagation/IPropagator.cs create mode 100644 src/OpenTelemetry.Api/Context/Propagation/TraceContextPropagator.cs diff --git a/examples/Console/TestOpenTracingWithConsoleExporter.cs b/examples/Console/TestOpenTracingWithConsoleExporter.cs index c7d161a6d..2126c71a4 100644 --- a/examples/Console/TestOpenTracingWithConsoleExporter.cs +++ b/examples/Console/TestOpenTracingWithConsoleExporter.cs @@ -40,7 +40,7 @@ namespace Examples.Console // Following shows how to use the OpenTracing shim - var tracer = new TracerShim(TracerProvider.Default.GetTracer("MyCompany.MyProduct.MyWebServer"), new TextMapPropagator()); + var tracer = new TracerShim(TracerProvider.Default.GetTracer("MyCompany.MyProduct.MyWebServer"), new TraceContextPropagator()); using (IScope parentScope = tracer.BuildSpan("Parent").StartActive(finishSpanOnDispose: true)) { diff --git a/examples/MicroserviceExample/Utils/Messaging/MessageReceiver.cs b/examples/MicroserviceExample/Utils/Messaging/MessageReceiver.cs index 272f6fbe8..71d484cae 100644 --- a/examples/MicroserviceExample/Utils/Messaging/MessageReceiver.cs +++ b/examples/MicroserviceExample/Utils/Messaging/MessageReceiver.cs @@ -30,7 +30,7 @@ namespace Utils.Messaging public class MessageReceiver : IDisposable { private static readonly ActivitySource ActivitySource = new ActivitySource(nameof(MessageReceiver)); - private static readonly IPropagator Propagator = new TextMapPropagator(); + private static readonly TextMapPropagator Propagator = new TraceContextPropagator(); private readonly ILogger logger; private readonly IConnection connection; diff --git a/examples/MicroserviceExample/Utils/Messaging/MessageSender.cs b/examples/MicroserviceExample/Utils/Messaging/MessageSender.cs index 0227962be..e8715ad79 100644 --- a/examples/MicroserviceExample/Utils/Messaging/MessageSender.cs +++ b/examples/MicroserviceExample/Utils/Messaging/MessageSender.cs @@ -28,7 +28,7 @@ namespace Utils.Messaging public class MessageSender : IDisposable { private static readonly ActivitySource ActivitySource = new ActivitySource(nameof(MessageSender)); - private static readonly IPropagator Propagator = new TextMapPropagator(); + private static readonly TextMapPropagator Propagator = new TraceContextPropagator(); private readonly ILogger logger; private readonly IConnection connection; diff --git a/src/OpenTelemetry.Api/CHANGELOG.md b/src/OpenTelemetry.Api/CHANGELOG.md index 40731931f..4b0d99981 100644 --- a/src/OpenTelemetry.Api/CHANGELOG.md +++ b/src/OpenTelemetry.Api/CHANGELOG.md @@ -9,6 +9,10 @@ * `B3Propagator` now supports the value `true` to be passed in for the header `X-B3-Sampled`. ([#1413](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1413)) +* Renamed TextMapPropagator to TraceContextPropagator, CompositePropapagor + to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator + and changed from interface to abstract class. + ([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427)) ## 0.7.0-beta.1 diff --git a/src/OpenTelemetry.Api/Context/Propagation/B3Propagator.cs b/src/OpenTelemetry.Api/Context/Propagation/B3Propagator.cs index 73f7677f1..fa53ad95b 100644 --- a/src/OpenTelemetry.Api/Context/Propagation/B3Propagator.cs +++ b/src/OpenTelemetry.Api/Context/Propagation/B3Propagator.cs @@ -24,9 +24,9 @@ using OpenTelemetry.Internal; namespace OpenTelemetry.Context.Propagation { /// - /// B3 text propagator. See https://github.com/openzipkin/b3-propagation for the specification. + /// A text map propagator for B3. See https://github.com/openzipkin/b3-propagation. /// - public sealed class B3Propagator : IPropagator + public sealed class B3Propagator : TextMapPropagator { internal const string XB3TraceId = "X-B3-TraceId"; internal const string XB3SpanId = "X-B3-SpanId"; @@ -73,10 +73,10 @@ namespace OpenTelemetry.Context.Propagation } /// - public ISet Fields => AllFields; + public override ISet Fields => AllFields; /// - public PropagationContext Extract(PropagationContext context, T carrier, Func> getter) + public override PropagationContext Extract(PropagationContext context, T carrier, Func> getter) { if (context.ActivityContext.IsValid()) { @@ -107,7 +107,7 @@ namespace OpenTelemetry.Context.Propagation } /// - public void Inject(PropagationContext context, T carrier, Action setter) + public override void Inject(PropagationContext context, T carrier, Action setter) { if (context.ActivityContext.TraceId == default || context.ActivityContext.SpanId == default) { diff --git a/src/OpenTelemetry.Api/Context/Propagation/BaggagePropagator.cs b/src/OpenTelemetry.Api/Context/Propagation/BaggagePropagator.cs index 0e22aeaf0..30808f642 100644 --- a/src/OpenTelemetry.Api/Context/Propagation/BaggagePropagator.cs +++ b/src/OpenTelemetry.Api/Context/Propagation/BaggagePropagator.cs @@ -24,9 +24,9 @@ using OpenTelemetry.Internal; namespace OpenTelemetry.Context.Propagation { /// - /// W3C baggage: https://github.com/w3c/baggage/blob/master/baggage/HTTP_HEADER_FORMAT.md. + /// A text map propagator for W3C Baggage. See https://w3c.github.io/baggage/. /// - public class BaggagePropagator : IPropagator + public class BaggagePropagator : TextMapPropagator { internal const string BaggageHeaderName = "Baggage"; @@ -34,10 +34,10 @@ namespace OpenTelemetry.Context.Propagation private const int MaxBaggageItems = 180; /// - public ISet Fields => new HashSet { BaggageHeaderName }; + public override ISet Fields => new HashSet { BaggageHeaderName }; /// - public PropagationContext Extract(PropagationContext context, T carrier, Func> getter) + public override PropagationContext Extract(PropagationContext context, T carrier, Func> getter) { if (context.Baggage != default) { @@ -79,7 +79,7 @@ namespace OpenTelemetry.Context.Propagation } /// - public void Inject(PropagationContext context, T carrier, Action setter) + public override void Inject(PropagationContext context, T carrier, Action setter) { if (carrier == null) { diff --git a/src/OpenTelemetry.Api/Context/Propagation/CompositePropagator.cs b/src/OpenTelemetry.Api/Context/Propagation/CompositeTextMapPropagator.cs similarity index 58% rename from src/OpenTelemetry.Api/Context/Propagation/CompositePropagator.cs rename to src/OpenTelemetry.Api/Context/Propagation/CompositeTextMapPropagator.cs index f8636efac..35e7d9721 100644 --- a/src/OpenTelemetry.Api/Context/Propagation/CompositePropagator.cs +++ b/src/OpenTelemetry.Api/Context/Propagation/CompositeTextMapPropagator.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,27 +20,28 @@ using System.Collections.Generic; namespace OpenTelemetry.Context.Propagation { /// - /// CompositePropagator provides a mechanism for combining multiple propagators into a single one. + /// CompositeTextMapPropagator provides a mechanism for combining multiple + /// textmap propagators into a single one. /// - public class CompositePropagator : IPropagator + public class CompositeTextMapPropagator : TextMapPropagator { private static readonly ISet EmptyFields = new HashSet(); - private readonly List propagators; + private readonly List propagators; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - /// List of wire context propagator. - public CompositePropagator(IEnumerable propagators) + /// List of wire context propagator. + public CompositeTextMapPropagator(IEnumerable propagators) { - this.propagators = new List(propagators ?? throw new ArgumentNullException(nameof(propagators))); + this.propagators = new List(propagators ?? throw new ArgumentNullException(nameof(propagators))); } /// - public ISet Fields => EmptyFields; + public override ISet Fields => EmptyFields; /// - public PropagationContext Extract(PropagationContext context, T carrier, Func> getter) + public override PropagationContext Extract(PropagationContext context, T carrier, Func> getter) { foreach (var propagator in this.propagators) { @@ -51,7 +52,7 @@ namespace OpenTelemetry.Context.Propagation } /// - public void Inject(PropagationContext context, T carrier, Action setter) + public override void Inject(PropagationContext context, T carrier, Action setter) { foreach (var propagator in this.propagators) { diff --git a/src/OpenTelemetry.Api/Context/Propagation/IPropagator.cs b/src/OpenTelemetry.Api/Context/Propagation/IPropagator.cs deleted file mode 100644 index 432c2c279..000000000 --- a/src/OpenTelemetry.Api/Context/Propagation/IPropagator.cs +++ /dev/null @@ -1,54 +0,0 @@ -// -// 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. -// - -using System; -using System.Collections.Generic; - -namespace OpenTelemetry.Context.Propagation -{ - /// - /// Defines an interface for Propagator, which is used to read and write - /// context data from and to message exchanges by the applications. - /// - public interface IPropagator - { - /// - /// Gets the list of headers used by propagator. The use cases of this are: - /// * allow pre-allocation of fields, especially in systems like gRPC Metadata - /// * allow a single-pass over an iterator (ex OpenTracing has no getter in TextMap). - /// - ISet Fields { get; } - - /// - /// Injects the context into a carrier. - /// - /// Type of an object to set context on. Typically HttpRequest or similar. - /// The default context to transmit over the wire. - /// Object to set context on. Instance of this object will be passed to setter. - /// Action that will set name and value pair on the object. - void Inject(PropagationContext context, T carrier, Action setter); - - /// - /// Extracts the context from a carrier. - /// - /// Type of object to extract context from. Typically HttpRequest or similar. - /// The default context to be used if Extract fails. - /// Object to extract context from. Instance of this object will be passed to the getter. - /// Function that will return string value of a key with the specified name. - /// Context from it's text representation. - PropagationContext Extract(PropagationContext context, T carrier, Func> getter); - } -} diff --git a/src/OpenTelemetry.Api/Context/Propagation/TextMapPropagator.cs b/src/OpenTelemetry.Api/Context/Propagation/TextMapPropagator.cs index 2db70a0c2..2ac95bc71 100644 --- a/src/OpenTelemetry.Api/Context/Propagation/TextMapPropagator.cs +++ b/src/OpenTelemetry.Api/Context/Propagation/TextMapPropagator.cs @@ -16,250 +16,40 @@ using System; using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using OpenTelemetry.Internal; namespace OpenTelemetry.Context.Propagation { /// - /// W3C trace context text wire protocol formatter. See https://github.com/w3c/distributed-tracing/. + /// Defines an interface for a Propagator of TextMap type, + /// which uses string key/value pairs to inject and extract + /// propagation data. /// - public class TextMapPropagator : IPropagator + public abstract class TextMapPropagator { - private const string TraceParent = "traceparent"; - private const string TraceState = "tracestate"; + /// + /// Gets the list of headers used by propagator. The use cases of this are: + /// * allow pre-allocation of fields, especially in systems like gRPC Metadata + /// * allow a single-pass over an iterator (ex OpenTracing has no getter in TextMap). + /// + public abstract ISet Fields { get; } - private static readonly int VersionPrefixIdLength = "00-".Length; - private static readonly int TraceIdLength = "0af7651916cd43dd8448eb211c80319c".Length; - private static readonly int VersionAndTraceIdLength = "00-0af7651916cd43dd8448eb211c80319c-".Length; - private static readonly int SpanIdLength = "00f067aa0ba902b7".Length; - private static readonly int VersionAndTraceIdAndSpanIdLength = "00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-".Length; - private static readonly int OptionsLength = "00".Length; - private static readonly int TraceparentLengthV0 = "00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-00".Length; + /// + /// Injects the context into a carrier. + /// + /// Type of an object to set context on. Typically HttpRequest or similar. + /// The default context to transmit over the wire. + /// Object to set context on. Instance of this object will be passed to setter. + /// Action that will set name and value pair on the object. + public abstract void Inject(PropagationContext context, T carrier, Action setter); - /// - public ISet Fields => new HashSet { TraceState, TraceParent }; - - /// - public PropagationContext Extract(PropagationContext context, T carrier, Func> getter) - { - if (context.ActivityContext.IsValid()) - { - // If a valid context has already been extracted, perform a noop. - return context; - } - - if (carrier == null) - { - OpenTelemetryApiEventSource.Log.FailedToExtractActivityContext(nameof(TextMapPropagator), "null carrier"); - return context; - } - - if (getter == null) - { - OpenTelemetryApiEventSource.Log.FailedToExtractActivityContext(nameof(TextMapPropagator), "null getter"); - return context; - } - - try - { - var traceparentCollection = getter(carrier, TraceParent); - - // There must be a single traceparent - if (traceparentCollection == null || traceparentCollection.Count() != 1) - { - return context; - } - - var traceparent = traceparentCollection.First(); - var traceparentParsed = TryExtractTraceparent(traceparent, out var traceId, out var spanId, out var traceoptions); - - if (!traceparentParsed) - { - return context; - } - - string tracestate = null; - var tracestateCollection = getter(carrier, TraceState); - if (tracestateCollection?.Any() ?? false) - { - TryExtractTracestate(tracestateCollection.ToArray(), out tracestate); - } - - return new PropagationContext( - new ActivityContext(traceId, spanId, traceoptions, tracestate, isRemote: true), - context.Baggage); - } - catch (Exception ex) - { - OpenTelemetryApiEventSource.Log.ActivityContextExtractException(nameof(TextMapPropagator), ex); - } - - // in case of exception indicate to upstream that there is no parseable context from the top - return context; - } - - /// - public void Inject(PropagationContext context, T carrier, Action setter) - { - if (context.ActivityContext.TraceId == default || context.ActivityContext.SpanId == default) - { - OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext(nameof(TextMapPropagator), "Invalid context"); - return; - } - - if (carrier == null) - { - OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext(nameof(TextMapPropagator), "null carrier"); - return; - } - - if (setter == null) - { - OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext(nameof(TextMapPropagator), "null setter"); - return; - } - - var traceparent = string.Concat("00-", context.ActivityContext.TraceId.ToHexString(), "-", context.ActivityContext.SpanId.ToHexString()); - traceparent = string.Concat(traceparent, (context.ActivityContext.TraceFlags & ActivityTraceFlags.Recorded) != 0 ? "-01" : "-00"); - - setter(carrier, TraceParent, traceparent); - - string tracestateStr = context.ActivityContext.TraceState; - if (tracestateStr?.Length > 0) - { - setter(carrier, TraceState, tracestateStr); - } - } - - internal static bool TryExtractTraceparent(string traceparent, out ActivityTraceId traceId, out ActivitySpanId spanId, out ActivityTraceFlags traceOptions) - { - // from https://github.com/w3c/distributed-tracing/blob/master/trace_context/HTTP_HEADER_FORMAT.md - // traceparent: 00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01 - - traceId = default; - spanId = default; - traceOptions = default; - var bestAttempt = false; - - if (string.IsNullOrWhiteSpace(traceparent) || traceparent.Length < TraceparentLengthV0) - { - return false; - } - - // if version does not end with delimiter - if (traceparent[VersionPrefixIdLength - 1] != '-') - { - return false; - } - - // or version is not a hex (will throw) - var version0 = HexCharToByte(traceparent[0]); - var version1 = HexCharToByte(traceparent[1]); - - if (version0 == 0xf && version1 == 0xf) - { - return false; - } - - if (version0 > 0) - { - // expected version is 00 - // for higher versions - best attempt parsing of trace id, span id, etc. - bestAttempt = true; - } - - if (traceparent[VersionAndTraceIdLength - 1] != '-') - { - return false; - } - - try - { - traceId = ActivityTraceId.CreateFromString(traceparent.AsSpan().Slice(VersionPrefixIdLength, TraceIdLength)); - } - catch (ArgumentOutOfRangeException) - { - // it's ok to still parse tracestate - return false; - } - - if (traceparent[VersionAndTraceIdAndSpanIdLength - 1] != '-') - { - return false; - } - - byte options1; - try - { - spanId = ActivitySpanId.CreateFromString(traceparent.AsSpan().Slice(VersionAndTraceIdLength, SpanIdLength)); - options1 = HexCharToByte(traceparent[VersionAndTraceIdAndSpanIdLength + 1]); - } - catch (ArgumentOutOfRangeException) - { - // it's ok to still parse tracestate - return false; - } - - if ((options1 & 1) == 1) - { - traceOptions |= ActivityTraceFlags.Recorded; - } - - if ((!bestAttempt) && (traceparent.Length != VersionAndTraceIdAndSpanIdLength + OptionsLength)) - { - return false; - } - - if (bestAttempt) - { - if ((traceparent.Length > TraceparentLengthV0) && (traceparent[TraceparentLengthV0] != '-')) - { - return false; - } - } - - return true; - } - - internal static bool TryExtractTracestate(string[] tracestateCollection, out string tracestateResult) - { - tracestateResult = string.Empty; - - if (tracestateCollection != null) - { - var result = new StringBuilder(); - - // Iterate in reverse order because when call builder set the elements is added in the - // front of the list. - for (int i = tracestateCollection.Length - 1; i >= 0; i--) - { - if (string.IsNullOrEmpty(tracestateCollection[i])) - { - return false; - } - - result.Append(tracestateCollection[i]); - } - - tracestateResult = result.ToString(); - } - - return true; - } - - private static byte HexCharToByte(char c) - { - if (((c >= '0') && (c <= '9')) - || ((c >= 'a') && (c <= 'f')) - || ((c >= 'A') && (c <= 'F'))) - { - return Convert.ToByte(c); - } - - throw new ArgumentOutOfRangeException(nameof(c), $"Invalid character: {c}."); - } + /// + /// Extracts the context from a carrier. + /// + /// Type of object to extract context from. Typically HttpRequest or similar. + /// The default context to be used if Extract fails. + /// Object to extract context from. Instance of this object will be passed to the getter. + /// Function that will return string value of a key with the specified name. + /// Context from it's text representation. + public abstract PropagationContext Extract(PropagationContext context, T carrier, Func> getter); } } diff --git a/src/OpenTelemetry.Api/Context/Propagation/TraceContextPropagator.cs b/src/OpenTelemetry.Api/Context/Propagation/TraceContextPropagator.cs new file mode 100644 index 000000000..76a27ad05 --- /dev/null +++ b/src/OpenTelemetry.Api/Context/Propagation/TraceContextPropagator.cs @@ -0,0 +1,265 @@ +// +// 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. +// + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using OpenTelemetry.Internal; + +namespace OpenTelemetry.Context.Propagation +{ + /// + /// A text map propagator for W3C trace context. See https://w3c.github.io/trace-context/. + /// + public class TraceContextPropagator : TextMapPropagator + { + private const string TraceParent = "traceparent"; + private const string TraceState = "tracestate"; + + private static readonly int VersionPrefixIdLength = "00-".Length; + private static readonly int TraceIdLength = "0af7651916cd43dd8448eb211c80319c".Length; + private static readonly int VersionAndTraceIdLength = "00-0af7651916cd43dd8448eb211c80319c-".Length; + private static readonly int SpanIdLength = "00f067aa0ba902b7".Length; + private static readonly int VersionAndTraceIdAndSpanIdLength = "00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-".Length; + private static readonly int OptionsLength = "00".Length; + private static readonly int TraceparentLengthV0 = "00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-00".Length; + + /// + public override ISet Fields => new HashSet { TraceState, TraceParent }; + + /// + public override PropagationContext Extract(PropagationContext context, T carrier, Func> getter) + { + if (context.ActivityContext.IsValid()) + { + // If a valid context has already been extracted, perform a noop. + return context; + } + + if (carrier == null) + { + OpenTelemetryApiEventSource.Log.FailedToExtractActivityContext(nameof(TraceContextPropagator), "null carrier"); + return context; + } + + if (getter == null) + { + OpenTelemetryApiEventSource.Log.FailedToExtractActivityContext(nameof(TraceContextPropagator), "null getter"); + return context; + } + + try + { + var traceparentCollection = getter(carrier, TraceParent); + + // There must be a single traceparent + if (traceparentCollection == null || traceparentCollection.Count() != 1) + { + return context; + } + + var traceparent = traceparentCollection.First(); + var traceparentParsed = TryExtractTraceparent(traceparent, out var traceId, out var spanId, out var traceoptions); + + if (!traceparentParsed) + { + return context; + } + + string tracestate = null; + var tracestateCollection = getter(carrier, TraceState); + if (tracestateCollection?.Any() ?? false) + { + TryExtractTracestate(tracestateCollection.ToArray(), out tracestate); + } + + return new PropagationContext( + new ActivityContext(traceId, spanId, traceoptions, tracestate, isRemote: true), + context.Baggage); + } + catch (Exception ex) + { + OpenTelemetryApiEventSource.Log.ActivityContextExtractException(nameof(TraceContextPropagator), ex); + } + + // in case of exception indicate to upstream that there is no parseable context from the top + return context; + } + + /// + public override void Inject(PropagationContext context, T carrier, Action setter) + { + if (context.ActivityContext.TraceId == default || context.ActivityContext.SpanId == default) + { + OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext(nameof(TraceContextPropagator), "Invalid context"); + return; + } + + if (carrier == null) + { + OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext(nameof(TraceContextPropagator), "null carrier"); + return; + } + + if (setter == null) + { + OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext(nameof(TraceContextPropagator), "null setter"); + return; + } + + var traceparent = string.Concat("00-", context.ActivityContext.TraceId.ToHexString(), "-", context.ActivityContext.SpanId.ToHexString()); + traceparent = string.Concat(traceparent, (context.ActivityContext.TraceFlags & ActivityTraceFlags.Recorded) != 0 ? "-01" : "-00"); + + setter(carrier, TraceParent, traceparent); + + string tracestateStr = context.ActivityContext.TraceState; + if (tracestateStr?.Length > 0) + { + setter(carrier, TraceState, tracestateStr); + } + } + + internal static bool TryExtractTraceparent(string traceparent, out ActivityTraceId traceId, out ActivitySpanId spanId, out ActivityTraceFlags traceOptions) + { + // from https://github.com/w3c/distributed-tracing/blob/master/trace_context/HTTP_HEADER_FORMAT.md + // traceparent: 00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01 + + traceId = default; + spanId = default; + traceOptions = default; + var bestAttempt = false; + + if (string.IsNullOrWhiteSpace(traceparent) || traceparent.Length < TraceparentLengthV0) + { + return false; + } + + // if version does not end with delimiter + if (traceparent[VersionPrefixIdLength - 1] != '-') + { + return false; + } + + // or version is not a hex (will throw) + var version0 = HexCharToByte(traceparent[0]); + var version1 = HexCharToByte(traceparent[1]); + + if (version0 == 0xf && version1 == 0xf) + { + return false; + } + + if (version0 > 0) + { + // expected version is 00 + // for higher versions - best attempt parsing of trace id, span id, etc. + bestAttempt = true; + } + + if (traceparent[VersionAndTraceIdLength - 1] != '-') + { + return false; + } + + try + { + traceId = ActivityTraceId.CreateFromString(traceparent.AsSpan().Slice(VersionPrefixIdLength, TraceIdLength)); + } + catch (ArgumentOutOfRangeException) + { + // it's ok to still parse tracestate + return false; + } + + if (traceparent[VersionAndTraceIdAndSpanIdLength - 1] != '-') + { + return false; + } + + byte options1; + try + { + spanId = ActivitySpanId.CreateFromString(traceparent.AsSpan().Slice(VersionAndTraceIdLength, SpanIdLength)); + options1 = HexCharToByte(traceparent[VersionAndTraceIdAndSpanIdLength + 1]); + } + catch (ArgumentOutOfRangeException) + { + // it's ok to still parse tracestate + return false; + } + + if ((options1 & 1) == 1) + { + traceOptions |= ActivityTraceFlags.Recorded; + } + + if ((!bestAttempt) && (traceparent.Length != VersionAndTraceIdAndSpanIdLength + OptionsLength)) + { + return false; + } + + if (bestAttempt) + { + if ((traceparent.Length > TraceparentLengthV0) && (traceparent[TraceparentLengthV0] != '-')) + { + return false; + } + } + + return true; + } + + internal static bool TryExtractTracestate(string[] tracestateCollection, out string tracestateResult) + { + tracestateResult = string.Empty; + + if (tracestateCollection != null) + { + var result = new StringBuilder(); + + // Iterate in reverse order because when call builder set the elements is added in the + // front of the list. + for (int i = tracestateCollection.Length - 1; i >= 0; i--) + { + if (string.IsNullOrEmpty(tracestateCollection[i])) + { + return false; + } + + result.Append(tracestateCollection[i]); + } + + tracestateResult = result.ToString(); + } + + return true; + } + + private static byte HexCharToByte(char c) + { + if (((c >= '0') && (c <= '9')) + || ((c >= 'a') && (c <= 'f')) + || ((c >= 'A') && (c <= 'F'))) + { + return Convert.ToByte(c); + } + + throw new ArgumentOutOfRangeException(nameof(c), $"Invalid character: {c}."); + } + } +} diff --git a/src/OpenTelemetry.Instrumentation.AspNet/AspNetInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.AspNet/AspNetInstrumentationOptions.cs index 075573b8b..e53b92d63 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/AspNetInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNet/AspNetInstrumentationOptions.cs @@ -27,11 +27,11 @@ namespace OpenTelemetry.Instrumentation.AspNet public class AspNetInstrumentationOptions { /// - /// Gets or sets for context propagation. Default value: with & . + /// Gets or sets for context propagation. Default value: with & . /// - public IPropagator Propagator { get; set; } = new CompositePropagator(new IPropagator[] + public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[] { - new TextMapPropagator(), + new TraceContextPropagator(), new BaggagePropagator(), }); diff --git a/src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md index 1cd73a5f3..5bf0cc5b2 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +* Renamed TextMapPropagator to TraceContextPropagator, CompositePropapagor + to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator + and changed from interface to abstract class. + ([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427)) + ## 0.7.0-beta.1 Released 2020-Oct-16 diff --git a/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs index 8d672979e..d1e5c99f6 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInListener.cs @@ -70,7 +70,7 @@ namespace OpenTelemetry.Instrumentation.AspNet.Implementation var request = context.Request; var requestValues = request.Unvalidated; - if (!(this.options.Propagator is TextMapPropagator)) + if (!(this.options.Propagator is TraceContextPropagator)) { var ctx = this.options.Propagator.Extract(default, request, HttpRequestHeaderValuesGetter); @@ -139,7 +139,7 @@ namespace OpenTelemetry.Instrumentation.AspNet.Implementation Activity activityToEnrich = activity; Activity createdActivity = null; - bool isCustomPropagator = !(this.options.Propagator is TextMapPropagator); + bool isCustomPropagator = !(this.options.Propagator is TraceContextPropagator); if (isCustomPropagator) { diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationOptions.cs index d38eb04ce..e07372fd6 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreInstrumentationOptions.cs @@ -27,11 +27,11 @@ namespace OpenTelemetry.Instrumentation.AspNetCore public class AspNetCoreInstrumentationOptions { /// - /// Gets or sets for context propagation. Default value: with & . + /// Gets or sets for context propagation. Default value: with & . /// - public IPropagator Propagator { get; set; } = new CompositePropagator(new IPropagator[] + public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[] { - new TextMapPropagator(), + new TraceContextPropagator(), new BaggagePropagator(), }); diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md index 563d362f3..973f95d0f 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md @@ -10,6 +10,10 @@ [Grpc.AspNetCore](https://www.nuget.org/packages/Grpc.AspNetCore/). This option is enabled by default. ([#1423](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1423)) +* Renamed TextMapPropagator to TraceContextPropagator, CompositePropapagor + to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator + and changed from interface to abstract class. + ([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427)) ## 0.7.0-beta.1 diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index e1b44a1dc..9015af938 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -78,7 +78,7 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation } var request = context.Request; - if (!this.hostingSupportsW3C || !(this.options.Propagator is TextMapPropagator)) + if (!this.hostingSupportsW3C || !(this.options.Propagator is TraceContextPropagator)) { var ctx = this.options.Propagator.Extract(default, request, HttpRequestHeaderValuesGetter); diff --git a/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md index a015ead66..e42f76fb8 100644 --- a/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md @@ -6,6 +6,10 @@ `HttpWebRequest` in Activity.CustomProperty. To enrich activity, use the Enrich action on the instrumentation. ([#1407](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1407)) +* Renamed TextMapPropagator to TraceContextPropagator, CompositePropapagor + to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator + and changed from interface to abstract class. + ([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427)) ## 0.7.0-beta.1 diff --git a/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentationOptions.cs index 2fe270f22..da4293c3b 100644 --- a/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.Http/HttpClientInstrumentationOptions.cs @@ -35,11 +35,11 @@ namespace OpenTelemetry.Instrumentation.Http public bool SetHttpFlavor { get; set; } /// - /// Gets or sets for context propagation. Default value: with & . + /// Gets or sets for context propagation. Default value: with & . /// - public IPropagator Propagator { get; set; } = new CompositePropagator(new IPropagator[] + public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[] { - new TextMapPropagator(), + new TraceContextPropagator(), new BaggagePropagator(), }); diff --git a/src/OpenTelemetry.Instrumentation.Http/HttpWebRequestInstrumentationOptions.netfx.cs b/src/OpenTelemetry.Instrumentation.Http/HttpWebRequestInstrumentationOptions.netfx.cs index bde54ec8c..c96a901be 100644 --- a/src/OpenTelemetry.Instrumentation.Http/HttpWebRequestInstrumentationOptions.netfx.cs +++ b/src/OpenTelemetry.Instrumentation.Http/HttpWebRequestInstrumentationOptions.netfx.cs @@ -35,11 +35,11 @@ namespace OpenTelemetry.Instrumentation.Http public bool SetHttpFlavor { get; set; } /// - /// Gets or sets for context propagation. Default value: with & . + /// Gets or sets for context propagation. Default value: with & . /// - public IPropagator Propagator { get; set; } = new CompositePropagator(new IPropagator[] + public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[] { - new TextMapPropagator(), + new TraceContextPropagator(), new BaggagePropagator(), }); diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs index b4e7e9fbb..ec810f3a6 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs @@ -115,7 +115,7 @@ namespace OpenTelemetry.Instrumentation.Http.Implementation } } - if (!(this.httpClientSupportsW3C && this.options.Propagator is TextMapPropagator)) + if (!(this.httpClientSupportsW3C && this.options.Propagator is TraceContextPropagator)) { this.options.Propagator.Inject(new PropagationContext(activity.Context, Baggage.Current), request, HttpRequestMessageHeaderValueSetter); } diff --git a/src/OpenTelemetry.Shims.OpenTracing/CHANGELOG.md b/src/OpenTelemetry.Shims.OpenTracing/CHANGELOG.md index 7e45f4944..eeaae2f89 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/CHANGELOG.md +++ b/src/OpenTelemetry.Shims.OpenTracing/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +* Renamed TextMapPropagator to TraceContextPropagator, CompositePropapagor + to CompositeTextMapPropagator. IPropagator is renamed to TextMapPropagator + and changed from interface to abstract class. + ([#1427](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1427)) + ## 0.7.0-beta.1 Released 2020-Oct-16 diff --git a/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs b/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs index 5432edfd1..459d6dfb6 100644 --- a/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs +++ b/src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs @@ -24,9 +24,9 @@ namespace OpenTelemetry.Shims.OpenTracing public class TracerShim : global::OpenTracing.ITracer { private readonly Trace.Tracer tracer; - private readonly IPropagator propagator; + private readonly TextMapPropagator propagator; - public TracerShim(Trace.Tracer tracer, IPropagator textFormat) + public TracerShim(Trace.Tracer tracer, TextMapPropagator textFormat) { this.tracer = tracer ?? throw new ArgumentNullException(nameof(tracer)); this.propagator = textFormat ?? throw new ArgumentNullException(nameof(textFormat)); diff --git a/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInListenerTests.cs b/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInListenerTests.cs index 9b247034d..40326f39f 100644 --- a/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInListenerTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNet.Tests/HttpInListenerTests.cs @@ -128,7 +128,7 @@ namespace OpenTelemetry.Instrumentation.AspNet.Tests var expectedTraceId = ActivityTraceId.CreateRandom(); var expectedSpanId = ActivitySpanId.CreateRandom(); - var propagator = new Mock(); + var propagator = new Mock(); propagator.Setup(m => m.Extract(It.IsAny(), It.IsAny(), It.IsAny>>())).Returns(new PropagationContext( new ActivityContext( expectedTraceId, diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs index f684b8062..66572aac7 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs @@ -196,7 +196,7 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Tests var expectedTraceId = ActivityTraceId.CreateRandom(); var expectedSpanId = ActivitySpanId.CreateRandom(); - var propagator = new Mock(); + var propagator = new Mock(); propagator.Setup(m => m.Extract(It.IsAny(), It.IsAny(), It.IsAny>>())).Returns( new PropagationContext( new ActivityContext( diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.netcore31.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.netcore31.cs index 19eda57e7..de76619af 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.netcore31.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.Basic.netcore31.cs @@ -75,7 +75,7 @@ namespace OpenTelemetry.Instrumentation.Http.Tests parent.ActivityTraceFlags = ActivityTraceFlags.Recorded; // Ensure that the header value func does not throw if the header key can't be found - var mockPropagator = new Mock(); + var mockPropagator = new Mock(); // var isInjectedHeaderValueGetterThrows = false; // mockTextFormat @@ -133,7 +133,7 @@ namespace OpenTelemetry.Instrumentation.Http.Tests [InlineData(false)] public async Task HttpClientInstrumentationInjectsHeadersAsync_CustomFormat(bool shouldEnrich) { - var propagator = new Mock(); + var propagator = new Mock(); propagator.Setup(m => m.Inject(It.IsAny(), It.IsAny(), It.IsAny>())) .Callback>((context, message, action) => { diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs index 2d63347eb..df5f86796 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.Basic.netfx.cs @@ -98,7 +98,7 @@ namespace OpenTelemetry.Instrumentation.Http.Tests [Fact] public async Task HttpWebRequestInstrumentationInjectsHeadersAsync_CustomFormat() { - var propagator = new Mock(); + var propagator = new Mock(); propagator.Setup(m => m.Inject(It.IsAny(), It.IsAny(), It.IsAny>())) .Callback>((context, message, action) => { diff --git a/test/OpenTelemetry.Shims.OpenTracing.Tests/TracerShimTests.cs b/test/OpenTelemetry.Shims.OpenTracing.Tests/TracerShimTests.cs index bb72a319c..f24ccd922 100644 --- a/test/OpenTelemetry.Shims.OpenTracing.Tests/TracerShimTests.cs +++ b/test/OpenTelemetry.Shims.OpenTracing.Tests/TracerShimTests.cs @@ -39,7 +39,7 @@ namespace OpenTelemetry.Shims.OpenTracing.Tests Assert.Throws(() => new TracerShim(null, null)); // null tracer - Assert.Throws(() => new TracerShim(null, new TextMapPropagator())); + Assert.Throws(() => new TracerShim(null, new TraceContextPropagator())); // null context format var tracerMock = new Mock(); @@ -50,7 +50,7 @@ namespace OpenTelemetry.Shims.OpenTracing.Tests public void ScopeManager_NotNull() { var tracer = TracerProvider.Default.GetTracer(TracerName); - var shim = new TracerShim(tracer, new TextMapPropagator()); + var shim = new TracerShim(tracer, new TraceContextPropagator()); // Internals of the ScopeManagerShim tested elsewhere Assert.NotNull(shim.ScopeManager as ScopeManagerShim); @@ -60,7 +60,7 @@ namespace OpenTelemetry.Shims.OpenTracing.Tests public void BuildSpan_NotNull() { var tracer = TracerProvider.Default.GetTracer(TracerName); - var shim = new TracerShim(tracer, new TextMapPropagator()); + var shim = new TracerShim(tracer, new TraceContextPropagator()); // Internals of the SpanBuilderShim tested elsewhere Assert.NotNull(shim.BuildSpan("foo") as SpanBuilderShim); @@ -70,7 +70,7 @@ namespace OpenTelemetry.Shims.OpenTracing.Tests public void Inject_ArgumentValidation() { var tracer = TracerProvider.Default.GetTracer(TracerName); - var shim = new TracerShim(tracer, new TextMapPropagator()); + var shim = new TracerShim(tracer, new TraceContextPropagator()); var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None)); var mockFormat = new Mock>(); @@ -86,7 +86,7 @@ namespace OpenTelemetry.Shims.OpenTracing.Tests public void Inject_UnknownFormatIgnored() { var tracer = TracerProvider.Default.GetTracer(TracerName); - var shim = new TracerShim(tracer, new TextMapPropagator()); + var shim = new TracerShim(tracer, new TraceContextPropagator()); var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded)); @@ -102,7 +102,7 @@ namespace OpenTelemetry.Shims.OpenTracing.Tests public void Extract_ArgumentValidation() { var tracer = TracerProvider.Default.GetTracer(TracerName); - var shim = new TracerShim(tracer, new TextMapPropagator()); + var shim = new TracerShim(tracer, new TraceContextPropagator()); Assert.Throws(() => shim.Extract(null, new Mock().Object)); Assert.Throws(() => shim.Extract(new Mock>().Object, null)); @@ -112,7 +112,7 @@ namespace OpenTelemetry.Shims.OpenTracing.Tests public void Extract_UnknownFormatIgnored() { var tracer = TracerProvider.Default.GetTracer(TracerName); - var shim = new TracerShim(tracer, new TextMapPropagator()); + var shim = new TracerShim(tracer, new TraceContextPropagator()); var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None)); @@ -128,11 +128,11 @@ namespace OpenTelemetry.Shims.OpenTracing.Tests public void Extract_InvalidTraceParent() { var tracer = TracerProvider.Default.GetTracer(TracerName); - var shim = new TracerShim(tracer, new TextMapPropagator()); + var shim = new TracerShim(tracer, new TraceContextPropagator()); var mockCarrier = new Mock(); - // The ProxyTracer uses OpenTelemetry.Context.Propagation.TextMapPropagator, so we need to satisfy the traceparent key at the least + // The ProxyTracer uses OpenTelemetry.Context.Propagation.TraceContextPropagator, so we need to satisfy the traceparent key at the least var carrierMap = new Dictionary { // This is an invalid traceparent value @@ -155,7 +155,7 @@ namespace OpenTelemetry.Shims.OpenTracing.Tests var spanContextShim = new SpanContextShim(new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None)); - var format = new TextMapPropagator(); + var format = new TraceContextPropagator(); var tracer = TracerProvider.Default.GetTracer(TracerName); var shim = new TracerShim(tracer, format); diff --git a/test/OpenTelemetry.Tests/Trace/Propagation/CompositePropagatorTest.cs b/test/OpenTelemetry.Tests/Trace/Propagation/CompositePropagatorTest.cs index 78b74c91f..6f3bd077c 100644 --- a/test/OpenTelemetry.Tests/Trace/Propagation/CompositePropagatorTest.cs +++ b/test/OpenTelemetry.Tests/Trace/Propagation/CompositePropagatorTest.cs @@ -49,13 +49,13 @@ namespace OpenTelemetry.Context.Propagation.Tests [Fact] public void CompositePropagator_NullTextFormatList() { - Assert.Throws(() => new CompositePropagator(null)); + Assert.Throws(() => new CompositeTextMapPropagator(null)); } [Fact] public void CompositePropagator_TestPropagator() { - var compositePropagator = new CompositePropagator(new List + var compositePropagator = new CompositeTextMapPropagator(new List { new TestPropagator("custom-traceparent-1", "custom-tracestate-1"), new TestPropagator("custom-traceparent-2", "custom-tracestate-2"), @@ -77,7 +77,7 @@ namespace OpenTelemetry.Context.Propagation.Tests const string header01 = "custom-tracestate-01"; const string header02 = "custom-tracestate-02"; - var compositePropagator = new CompositePropagator(new List + var compositePropagator = new CompositeTextMapPropagator(new List { new TestPropagator("custom-traceparent", header01, true), new TestPropagator("custom-traceparent", header02), @@ -106,9 +106,9 @@ namespace OpenTelemetry.Context.Propagation.Tests [Fact] public void CompositePropagator_ActivityContext_Baggage() { - var compositePropagator = new CompositePropagator(new List + var compositePropagator = new CompositeTextMapPropagator(new List { - new TextMapPropagator(), + new TraceContextPropagator(), new BaggagePropagator(), }); diff --git a/test/OpenTelemetry.Tests/Trace/Propagation/TestPropagator.cs b/test/OpenTelemetry.Tests/Trace/Propagation/TestPropagator.cs index 5a7baf026..1c5fb5726 100644 --- a/test/OpenTelemetry.Tests/Trace/Propagation/TestPropagator.cs +++ b/test/OpenTelemetry.Tests/Trace/Propagation/TestPropagator.cs @@ -21,7 +21,7 @@ using System.Linq; namespace OpenTelemetry.Context.Propagation.Tests { - public class TestPropagator : IPropagator + public class TestPropagator : TextMapPropagator { private readonly string idHeaderName; private readonly string stateHeaderName; @@ -34,9 +34,9 @@ namespace OpenTelemetry.Context.Propagation.Tests this.defaultContext = defaultContext; } - public ISet Fields => new HashSet() { this.idHeaderName, this.stateHeaderName }; + public override ISet Fields => new HashSet() { this.idHeaderName, this.stateHeaderName }; - public PropagationContext Extract(PropagationContext context, T carrier, Func> getter) + public override PropagationContext Extract(PropagationContext context, T carrier, Func> getter) { if (this.defaultContext) { @@ -49,7 +49,7 @@ namespace OpenTelemetry.Context.Propagation.Tests return context; } - var traceparentParsed = TextMapPropagator.TryExtractTraceparent(id.First(), out var traceId, out var spanId, out var traceoptions); + var traceparentParsed = TraceContextPropagator.TryExtractTraceparent(id.First(), out var traceId, out var spanId, out var traceoptions); if (!traceparentParsed) { return context; @@ -59,7 +59,7 @@ namespace OpenTelemetry.Context.Propagation.Tests IEnumerable tracestateCollection = getter(carrier, this.stateHeaderName); if (tracestateCollection?.Any() ?? false) { - TextMapPropagator.TryExtractTracestate(tracestateCollection.ToArray(), out tracestate); + TraceContextPropagator.TryExtractTracestate(tracestateCollection.ToArray(), out tracestate); } return new PropagationContext( @@ -67,7 +67,7 @@ namespace OpenTelemetry.Context.Propagation.Tests context.Baggage); } - public void Inject(PropagationContext context, T carrier, Action setter) + public override void Inject(PropagationContext context, T carrier, Action setter) { string headerNumber = this.stateHeaderName.Split('-').Last(); diff --git a/test/OpenTelemetry.Tests/Trace/Propagation/TextMapPropagatorTest.cs b/test/OpenTelemetry.Tests/Trace/Propagation/TextMapPropagatorTest.cs index 2247cac80..c60558050 100644 --- a/test/OpenTelemetry.Tests/Trace/Propagation/TextMapPropagatorTest.cs +++ b/test/OpenTelemetry.Tests/Trace/Propagation/TextMapPropagatorTest.cs @@ -52,7 +52,7 @@ namespace OpenTelemetry.Context.Propagation.Tests { TraceState, $"congo=lZWRzIHRoNhcm5hbCBwbGVhc3VyZS4,rojo=00-{TraceId}-00f067aa0ba902b7-01" }, }; - var f = new TextMapPropagator(); + var f = new TraceContextPropagator(); var ctx = f.Extract(default, headers, Getter); Assert.Equal(ActivityTraceId.CreateFromString(TraceId.AsSpan()), ctx.ActivityContext.TraceId); @@ -73,7 +73,7 @@ namespace OpenTelemetry.Context.Propagation.Tests { TraceParent, $"00-{TraceId}-{SpanId}-00" }, }; - var f = new TextMapPropagator(); + var f = new TraceContextPropagator(); var ctx = f.Extract(default, headers, Getter); Assert.Equal(ActivityTraceId.CreateFromString(TraceId.AsSpan()), ctx.ActivityContext.TraceId); @@ -89,7 +89,7 @@ namespace OpenTelemetry.Context.Propagation.Tests { var headers = new Dictionary(); - var f = new TextMapPropagator(); + var f = new TraceContextPropagator(); var ctx = f.Extract(default, headers, Getter); Assert.False(ctx.ActivityContext.IsValid()); @@ -103,7 +103,7 @@ namespace OpenTelemetry.Context.Propagation.Tests { TraceParent, $"00-xyz7651916cd43dd8448eb211c80319c-{SpanId}-01" }, }; - var f = new TextMapPropagator(); + var f = new TraceContextPropagator(); var ctx = f.Extract(default, headers, Getter); Assert.False(ctx.ActivityContext.IsValid()); @@ -117,7 +117,7 @@ namespace OpenTelemetry.Context.Propagation.Tests { TraceParent, $"00-{TraceId}-{SpanId}-01" }, }; - var f = new TextMapPropagator(); + var f = new TraceContextPropagator(); var ctx = f.Extract(default, headers, Getter); Assert.Null(ctx.ActivityContext.TraceState); @@ -132,7 +132,7 @@ namespace OpenTelemetry.Context.Propagation.Tests { TraceState, "k1=v1,k2=v2,k3=v3" }, }; - var f = new TextMapPropagator(); + var f = new TraceContextPropagator(); var ctx = f.Extract(default, headers, Getter); Assert.Equal("k1=v1,k2=v2,k3=v3", ctx.ActivityContext.TraceState); @@ -151,7 +151,7 @@ namespace OpenTelemetry.Context.Propagation.Tests var activityContext = new ActivityContext(traceId, spanId, ActivityTraceFlags.Recorded, traceState: null); PropagationContext propagationContext = new PropagationContext(activityContext, default); var carrier = new Dictionary(); - var f = new TextMapPropagator(); + var f = new TraceContextPropagator(); f.Inject(propagationContext, carrier, Setter); Assert.Equal(expectedHeaders, carrier); @@ -171,7 +171,7 @@ namespace OpenTelemetry.Context.Propagation.Tests var activityContext = new ActivityContext(traceId, spanId, ActivityTraceFlags.Recorded, expectedHeaders[TraceState]); PropagationContext propagationContext = new PropagationContext(activityContext, default); var carrier = new Dictionary(); - var f = new TextMapPropagator(); + var f = new TraceContextPropagator(); f.Inject(propagationContext, carrier, Setter); Assert.Equal(expectedHeaders, carrier);