-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Propagators Support #55419
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
Propagators Support #55419
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
190 changes: 190 additions & 0 deletions
190
src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/LegacyPropagator.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,190 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Net; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace System.Diagnostics | ||
| { | ||
| internal class LegacyPropagator : TextMapPropagator | ||
| { | ||
| internal static TextMapPropagator Instance { get; } = new LegacyPropagator(); | ||
|
|
||
| public override IReadOnlyCollection<string> Fields { get; } = new HashSet<string>() { TraceParent, RequestId, TraceState, Baggage, CorrelationContext }; | ||
tarekgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public override void Inject(Activity activity, object carrier, PropagatorSetterCallback setter) | ||
| { | ||
| if (activity is null || setter is null) | ||
tarekgh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return; | ||
| } | ||
|
|
||
| string? id = activity.Id; | ||
| if (id is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (activity.IdFormat == ActivityIdFormat.W3C) | ||
| { | ||
| setter(carrier, TraceParent, id); | ||
| if (activity.TraceStateString is not null) | ||
tarekgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| setter(carrier, TraceState, activity.TraceStateString); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| setter(carrier, RequestId, id); | ||
| } | ||
|
|
||
| InjectBaggage(carrier, activity.Baggage, setter); | ||
| } | ||
|
|
||
| public override void ExtractTraceIdAndState(object carrier, PropagatorGetterCallback getter, out string? traceId, out string? traceState) | ||
| { | ||
| if (getter is null) | ||
tarekgh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| traceId = null; | ||
| traceState = null; | ||
| return; | ||
| } | ||
|
|
||
| getter(carrier, TraceParent, out traceId, out _); | ||
| if (traceId is null) | ||
| { | ||
| getter(carrier, RequestId, out traceId, out _); | ||
| } | ||
|
|
||
| getter(carrier, TraceState, out traceState, out _); | ||
| } | ||
|
|
||
| public override IEnumerable<KeyValuePair<string, string?>>? ExtractBaggage(object carrier, PropagatorGetterCallback getter) | ||
| { | ||
| IEnumerable<KeyValuePair<string, string?>>? baggage = null; | ||
tarekgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (getter is null) | ||
tarekgh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return null; | ||
| } | ||
|
|
||
| getter(carrier, Baggage, out string? theBaggage, out _); | ||
| if (theBaggage is null || !TryExtractBaggage(theBaggage, out baggage)) | ||
| { | ||
| getter(carrier, CorrelationContext, out theBaggage, out _); | ||
| if (theBaggage is not null) | ||
| { | ||
| TryExtractBaggage(theBaggage, out baggage); | ||
| } | ||
| } | ||
|
|
||
| return baggage; | ||
| } | ||
|
|
||
| internal static bool TryExtractBaggage(string baggagestring, out IEnumerable<KeyValuePair<string, string?>>? baggage) | ||
tarekgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| baggage = null; | ||
| List<KeyValuePair<string, string?>>? baggageList = null; | ||
|
|
||
| if (string.IsNullOrEmpty(baggagestring)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| int currentIndex = 0; | ||
|
|
||
| do | ||
| { | ||
| // Skip spaces | ||
| while (currentIndex < baggagestring.Length && (baggagestring[currentIndex] == Space || baggagestring[currentIndex] == Tab)) | ||
| { | ||
| currentIndex++; | ||
| } | ||
|
|
||
| if (currentIndex >= baggagestring.Length) | ||
| { | ||
| break; // No Key exist | ||
| } | ||
|
|
||
| int keyStart = currentIndex; | ||
|
|
||
| // Search end of the key | ||
| while (currentIndex < baggagestring.Length && baggagestring[currentIndex] != Space && baggagestring[currentIndex] != Tab && baggagestring[currentIndex] != '=') | ||
| { | ||
| currentIndex++; | ||
| } | ||
|
|
||
| if (currentIndex >= baggagestring.Length) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| int keyEnd = currentIndex; | ||
|
|
||
| if (baggagestring[currentIndex] != '=') | ||
| { | ||
| // Skip Spaces | ||
| while (currentIndex < baggagestring.Length && (baggagestring[currentIndex] == Space || baggagestring[currentIndex] == Tab)) | ||
| { | ||
| currentIndex++; | ||
| } | ||
|
|
||
| if (currentIndex >= baggagestring.Length) | ||
| { | ||
| break; // Wrong key format | ||
| } | ||
|
|
||
| if (baggagestring[currentIndex] != '=') | ||
| { | ||
| break; // wrong key format. | ||
| } | ||
| } | ||
|
|
||
| currentIndex++; | ||
|
|
||
| // Skip spaces | ||
| while (currentIndex < baggagestring.Length && (baggagestring[currentIndex] == Space || baggagestring[currentIndex] == Tab)) | ||
| { | ||
| currentIndex++; | ||
| } | ||
|
|
||
| if (currentIndex >= baggagestring.Length) | ||
| { | ||
| break; // Wrong value format | ||
| } | ||
|
|
||
| int valueStart = currentIndex; | ||
|
|
||
| // Search end of the value | ||
| while (currentIndex < baggagestring.Length && baggagestring[currentIndex] != Space && baggagestring[currentIndex] != Tab && | ||
| baggagestring[currentIndex] != Comma && baggagestring[currentIndex] != Semicolon) | ||
| { | ||
| currentIndex++; | ||
| } | ||
|
|
||
| if (keyStart < keyEnd && valueStart < currentIndex) | ||
| { | ||
| if (baggageList is null) | ||
| { | ||
| baggageList = new(); | ||
| } | ||
tarekgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Insert in reverse order for asp.net compatability. | ||
| baggageList.Insert(0, new KeyValuePair<string, string?>( | ||
| WebUtility.UrlDecode(baggagestring.Substring(keyStart, keyEnd - keyStart)).Trim(s_trimmingSpaceCharacters), | ||
| WebUtility.UrlDecode(baggagestring.Substring(valueStart, currentIndex - valueStart)).Trim(s_trimmingSpaceCharacters))); | ||
| } | ||
|
|
||
| // Skip to end of values | ||
| while (currentIndex < baggagestring.Length && baggagestring[currentIndex] != Comma) | ||
| { | ||
| currentIndex++; | ||
| } | ||
|
|
||
| currentIndex++; // Move to next key-value entry | ||
| } while (currentIndex < baggagestring.Length); | ||
|
|
||
| baggage = baggageList; | ||
| return baggageList != null; | ||
| } | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
...ibraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/NoOutputPropagator.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,23 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| namespace System.Diagnostics | ||
| { | ||
| internal class NoOutputPropagator : TextMapPropagator | ||
tarekgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| internal static TextMapPropagator Instance { get; } = new NoOutputPropagator(); | ||
|
|
||
| public override IReadOnlyCollection<string> Fields { get; } = new HashSet<string>() { TraceParent, RequestId, TraceState, Baggage, CorrelationContext }; | ||
|
|
||
| public override void Inject(Activity activity, object carrier, PropagatorSetterCallback setter) | ||
| { | ||
| // nothing to do. | ||
| } | ||
|
|
||
| public override void ExtractTraceIdAndState(object carrier, PropagatorGetterCallback getter, out string? traceId, out string? traceState) => LegacyPropagator.Instance.ExtractTraceIdAndState(carrier, getter, out traceId, out traceState); | ||
|
|
||
| public override IEnumerable<KeyValuePair<string, string?>>? ExtractBaggage(object carrier, PropagatorGetterCallback getter) => LegacyPropagator.Instance.ExtractBaggage(carrier, getter); | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
...aries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/PassThroughPropagator.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,62 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| namespace System.Diagnostics | ||
| { | ||
| internal class PassThroughPropagator : TextMapPropagator | ||
tarekgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| internal static TextMapPropagator Instance { get; } = new PassThroughPropagator(); | ||
|
|
||
| public override IReadOnlyCollection<string> Fields { get; } = new HashSet<string>() { TraceParent, RequestId, TraceState, Baggage, CorrelationContext }; | ||
|
|
||
| public override void Inject(Activity activity, object carrier, PropagatorSetterCallback setter) | ||
| { | ||
| GetRootId(out string? parentId, out string? traceState, out bool isW3c, out IEnumerable<KeyValuePair<string, string?>>? baggage); | ||
| if (parentId is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| setter(carrier, isW3c ? TraceParent : RequestId, parentId); | ||
|
|
||
| if (traceState is not null) | ||
| { | ||
| setter(carrier, TraceState, traceState); | ||
| } | ||
|
|
||
| if (baggage is not null) | ||
| { | ||
| InjectBaggage(carrier, baggage, setter); | ||
| } | ||
| } | ||
|
|
||
| public override void ExtractTraceIdAndState(object carrier, PropagatorGetterCallback getter, out string? traceId, out string? traceState) => LegacyPropagator.Instance.ExtractTraceIdAndState(carrier, getter, out traceId, out traceState); | ||
|
|
||
| public override IEnumerable<KeyValuePair<string, string?>>? ExtractBaggage(object carrier, PropagatorGetterCallback getter) => LegacyPropagator.Instance.ExtractBaggage(carrier, getter); | ||
|
|
||
| private static void GetRootId(out string? parentId, out string? traceState, out bool isW3c, out IEnumerable<KeyValuePair<string, string?>>? baggage) | ||
| { | ||
| Activity? activity = Activity.Current; | ||
| if (activity is null) | ||
| { | ||
| parentId = null; | ||
| traceState = null; | ||
| isW3c = false; | ||
| baggage = null; | ||
| return; | ||
| } | ||
|
|
||
| while (activity is not null && activity.Parent is not null) | ||
| { | ||
| activity = activity.Parent; | ||
| } | ||
tarekgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| traceState = activity?.TraceStateString; | ||
| parentId = activity?.ParentId ?? activity?.Id; | ||
| isW3c = activity?.IdFormat == ActivityIdFormat.W3C; | ||
tarekgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| baggage = activity?.Baggage; | ||
| } | ||
| } | ||
| } | ||
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.