-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,095 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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) | ||
{ | ||
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) | ||
{ | ||
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 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 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) | ||
{ | ||
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.