-
Notifications
You must be signed in to change notification settings - Fork 857
Added Jaeger Propagator to Opentelemetry.Extensions.Propagators #3309
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9c19f44
Addjaeger (#7)
notcool11 afd7a88
Update CHANGELOG.md
notcool11 442c6a0
Anotherrefresh (#8)
notcool11 bc9db4d
another merge from otel main
341fb7e
moved jaegerpropagator comment to unreleased
91415d0
Update src/OpenTelemetry.Extensions.Propagators/CHANGELOG.md
notcool11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| OpenTelemetry.Extensions.Propagators.JaegerPropagator | ||
| OpenTelemetry.Extensions.Propagators.JaegerPropagator.JaegerPropagator() -> void | ||
| override OpenTelemetry.Extensions.Propagators.JaegerPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string, System.Collections.Generic.IEnumerable<string>> getter) -> OpenTelemetry.Context.Propagation.PropagationContext | ||
| override OpenTelemetry.Extensions.Propagators.JaegerPropagator.Fields.get -> System.Collections.Generic.ISet<string> | ||
| override OpenTelemetry.Extensions.Propagators.JaegerPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string, string> setter) -> void |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| OpenTelemetry.Extensions.Propagators.JaegerPropagator | ||
| OpenTelemetry.Extensions.Propagators.JaegerPropagator.JaegerPropagator() -> void | ||
| override OpenTelemetry.Extensions.Propagators.JaegerPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string, System.Collections.Generic.IEnumerable<string>> getter) -> OpenTelemetry.Context.Propagation.PropagationContext | ||
| override OpenTelemetry.Extensions.Propagators.JaegerPropagator.Fields.get -> System.Collections.Generic.ISet<string> | ||
| override OpenTelemetry.Extensions.Propagators.JaegerPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string, string> setter) -> void |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
src/OpenTelemetry.Extensions.Propagators/JaegerPropagator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| // <copyright file="JaegerPropagator.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; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using OpenTelemetry.Context.Propagation; | ||
| using OpenTelemetry.Internal; | ||
|
|
||
| namespace OpenTelemetry.Extensions.Propagators; | ||
|
|
||
| /// <summary> | ||
| /// A text map propagator for Jaeger trace context. See https://www.jaegertracing.io/docs/next-release/client-libraries/#propagation-format. | ||
| /// </summary> | ||
| public class JaegerPropagator : TextMapPropagator | ||
| { | ||
| internal const string JaegerHeader = "uber-trace-id"; | ||
| internal const string JaegerDelimiter = ":"; | ||
| internal const string JaegerDelimiterEncoded = "%3A"; // while the spec defines the delimiter as a ':', some clients will url encode headers. | ||
| internal const string SampledValue = "1"; | ||
|
|
||
| internal static readonly string[] JaegerDelimiters = { JaegerDelimiter, JaegerDelimiterEncoded }; | ||
|
|
||
| private static readonly int TraceId128BitLength = "0af7651916cd43dd8448eb211c80319c".Length; | ||
| private static readonly int SpanIdLength = "00f067aa0ba902b7".Length; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override ISet<string> Fields => new HashSet<string> { JaegerHeader }; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> 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(JaegerPropagator), "null carrier"); | ||
cijothomas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return context; | ||
| } | ||
|
|
||
| if (getter == null) | ||
| { | ||
| OpenTelemetryApiEventSource.Log.FailedToExtractActivityContext(nameof(JaegerPropagator), "null getter"); | ||
| return context; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var jaegerHeaderCollection = getter(carrier, JaegerHeader); | ||
| var jaegerHeader = jaegerHeaderCollection?.First(); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(jaegerHeader)) | ||
| { | ||
| return context; | ||
| } | ||
|
|
||
| var jaegerHeaderParsed = TryExtractTraceContext(jaegerHeader, out var traceId, out var spanId, out var traceOptions); | ||
|
|
||
| if (!jaegerHeaderParsed) | ||
| { | ||
| return context; | ||
| } | ||
|
|
||
| return new PropagationContext( | ||
| new ActivityContext(traceId, spanId, traceOptions, isRemote: true), | ||
| context.Baggage); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| OpenTelemetryApiEventSource.Log.ActivityContextExtractException(nameof(JaegerPropagator), ex); | ||
| } | ||
|
|
||
| return context; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void Inject<T>(PropagationContext context, T carrier, Action<T, string, string> setter) | ||
| { | ||
| // from https://www.jaegertracing.io/docs/next-release/client-libraries/#propagation-format | ||
| // parent id is optional and deprecated, will not attempt to set it. | ||
| // 128 bit uber-trace-id=e0ad975be108cd107990683f59cda9e6:e422f3fe664f6342:0:1 | ||
| const string defaultParentSpanId = "0"; | ||
|
|
||
| if (context.ActivityContext.TraceId == default || context.ActivityContext.SpanId == default) | ||
| { | ||
| OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext(nameof(JaegerPropagator), "Invalid context"); | ||
| return; | ||
| } | ||
|
|
||
| if (carrier == null) | ||
| { | ||
| OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext(nameof(JaegerPropagator), "null carrier"); | ||
| return; | ||
| } | ||
|
|
||
| if (setter == null) | ||
| { | ||
| OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext(nameof(JaegerPropagator), "null setter"); | ||
| return; | ||
| } | ||
|
|
||
| var flags = (context.ActivityContext.TraceFlags & ActivityTraceFlags.Recorded) != 0 ? "1" : "0"; | ||
|
|
||
| var jaegerTrace = string.Join( | ||
| JaegerDelimiter, | ||
| context.ActivityContext.TraceId.ToHexString(), | ||
| context.ActivityContext.SpanId.ToHexString(), | ||
| defaultParentSpanId, | ||
| flags); | ||
|
|
||
| setter(carrier, JaegerHeader, jaegerTrace); | ||
| } | ||
|
|
||
| internal static bool TryExtractTraceContext(string jaegerHeader, out ActivityTraceId traceId, out ActivitySpanId spanId, out ActivityTraceFlags traceOptions) | ||
| { | ||
| // from https://www.jaegertracing.io/docs/next-release/client-libraries/#propagation-format | ||
| // parent id is optional and deprecated. will not attempt to store it. | ||
| // 128 bit uber-trace-id=e0ad975be108cd107990683f59cda9e6:e422f3fe664f6342:0:1 | ||
| // 128 bit with encoded delimiter uber-trace-id=e0ad975be108cd107990683f59cda9e6%3Ae422f3fe664f6342%3A0%3A1 | ||
| // 64 bit uber-trace-id=7990683f59cda9e6:e422f3fe664f6342:0:1 | ||
| // 64 bit with encoded delimiter uber-trace-id=7990683f59cda9e6%3Ae422f3fe664f6342%3A0%3A1 | ||
|
|
||
| traceId = default; | ||
| spanId = default; | ||
| traceOptions = ActivityTraceFlags.None; | ||
|
|
||
| if (string.IsNullOrWhiteSpace(jaegerHeader)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var traceComponents = jaegerHeader.Split(JaegerDelimiters, StringSplitOptions.RemoveEmptyEntries); | ||
| if (traceComponents.Length != 4) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var traceIdStr = traceComponents[0]; | ||
| if (traceIdStr.Length < TraceId128BitLength) | ||
| { | ||
| traceIdStr = traceIdStr.PadLeft(TraceId128BitLength, '0'); | ||
| } | ||
|
|
||
| traceId = ActivityTraceId.CreateFromString(traceIdStr.AsSpan()); | ||
|
|
||
| var spanIdStr = traceComponents[1]; | ||
| if (spanIdStr.Length < SpanIdLength) | ||
| { | ||
| spanIdStr = spanIdStr.PadLeft(SpanIdLength, '0'); | ||
| } | ||
|
|
||
| spanId = ActivitySpanId.CreateFromString(spanIdStr.AsSpan()); | ||
|
|
||
| var traceFlagsStr = traceComponents[3]; | ||
| if (SampledValue.Equals(traceFlagsStr)) | ||
| { | ||
| traceOptions |= ActivityTraceFlags.Recorded; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.