-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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 all commits
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
189 changes: 189 additions & 0 deletions
189
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,189 @@ | ||
// 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; | ||
using System.Collections.ObjectModel; | ||
|
||
namespace System.Diagnostics | ||
{ | ||
internal sealed class LegacyPropagator : TextMapPropagator | ||
{ | ||
internal static TextMapPropagator Instance { get; } = new LegacyPropagator(); | ||
|
||
public override IReadOnlyCollection<string> Fields { get; } = new ReadOnlyCollection<string>(new[] { TraceParent, RequestId, TraceState, Baggage, CorrelationContext }); | ||
|
||
public override void Inject(Activity? activity, object? carrier, PropagatorSetterCallback? setter) | ||
{ | ||
if (activity is null || setter is null) | ||
{ | ||
return; | ||
} | ||
|
||
string? id = activity.Id; | ||
if (id is null) | ||
{ | ||
return; | ||
} | ||
|
||
if (activity.IdFormat == ActivityIdFormat.W3C) | ||
{ | ||
setter(carrier, TraceParent, id); | ||
if (!string.IsNullOrEmpty(activity.TraceStateString)) | ||
{ | ||
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) | ||
{ | ||
if (getter is null) | ||
tarekgh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return null; | ||
} | ||
|
||
getter(carrier, Baggage, out string? theBaggage, out _); | ||
|
||
IEnumerable<KeyValuePair<string, string?>>? baggage = null; | ||
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) | ||
{ | ||
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) | ||
{ | ||
baggageList ??= new(); | ||
|
||
// 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 sealed class NoOutputPropagator : TextMapPropagator | ||
{ | ||
internal static TextMapPropagator Instance { get; } = new NoOutputPropagator(); | ||
|
||
public override IReadOnlyCollection<string> Fields { get; } = LegacyPropagator.Instance.Fields; | ||
|
||
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); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...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,59 @@ | ||
// 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 sealed class PassThroughPropagator : TextMapPropagator | ||
{ | ||
internal static TextMapPropagator Instance { get; } = new PassThroughPropagator(); | ||
|
||
public override IReadOnlyCollection<string> Fields { get; } = LegacyPropagator.Instance.Fields; | ||
|
||
public override void Inject(Activity? activity, object? carrier, PropagatorSetterCallback? setter) | ||
tarekgh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (setter is null) | ||
{ | ||
return; | ||
} | ||
|
||
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 (!string.IsNullOrEmpty(traceState)) | ||
{ | ||
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; | ||
|
||
while (activity?.Parent is Activity parent) | ||
{ | ||
activity = parent; | ||
} | ||
|
||
traceState = activity?.TraceStateString; | ||
parentId = activity?.ParentId ?? activity?.Id; | ||
isW3c = parentId is not null ? Activity.TryConvertIdToContext(parentId, traceState, out _) : false; | ||
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.