|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Net; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.ObjectModel; |
| 7 | + |
| 8 | +namespace System.Diagnostics |
| 9 | +{ |
| 10 | + internal sealed class LegacyPropagator : TextMapPropagator |
| 11 | + { |
| 12 | + internal static TextMapPropagator Instance { get; } = new LegacyPropagator(); |
| 13 | + |
| 14 | + public override IReadOnlyCollection<string> Fields { get; } = new ReadOnlyCollection<string>(new[] { TraceParent, RequestId, TraceState, Baggage, CorrelationContext }); |
| 15 | + |
| 16 | + public override void Inject(Activity? activity, object? carrier, PropagatorSetterCallback? setter) |
| 17 | + { |
| 18 | + if (activity is null || setter is null) |
| 19 | + { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + string? id = activity.Id; |
| 24 | + if (id is null) |
| 25 | + { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + if (activity.IdFormat == ActivityIdFormat.W3C) |
| 30 | + { |
| 31 | + setter(carrier, TraceParent, id); |
| 32 | + if (!string.IsNullOrEmpty(activity.TraceStateString)) |
| 33 | + { |
| 34 | + setter(carrier, TraceState, activity.TraceStateString); |
| 35 | + } |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + setter(carrier, RequestId, id); |
| 40 | + } |
| 41 | + |
| 42 | + InjectBaggage(carrier, activity.Baggage, setter); |
| 43 | + } |
| 44 | + |
| 45 | + public override void ExtractTraceIdAndState(object? carrier, PropagatorGetterCallback? getter, out string? traceId, out string? traceState) |
| 46 | + { |
| 47 | + if (getter is null) |
| 48 | + { |
| 49 | + traceId = null; |
| 50 | + traceState = null; |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + getter(carrier, TraceParent, out traceId, out _); |
| 55 | + if (traceId is null) |
| 56 | + { |
| 57 | + getter(carrier, RequestId, out traceId, out _); |
| 58 | + } |
| 59 | + |
| 60 | + getter(carrier, TraceState, out traceState, out _); |
| 61 | + } |
| 62 | + |
| 63 | + public override IEnumerable<KeyValuePair<string, string?>>? ExtractBaggage(object? carrier, PropagatorGetterCallback? getter) |
| 64 | + { |
| 65 | + if (getter is null) |
| 66 | + { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + getter(carrier, Baggage, out string? theBaggage, out _); |
| 71 | + |
| 72 | + IEnumerable<KeyValuePair<string, string?>>? baggage = null; |
| 73 | + if (theBaggage is null || !TryExtractBaggage(theBaggage, out baggage)) |
| 74 | + { |
| 75 | + getter(carrier, CorrelationContext, out theBaggage, out _); |
| 76 | + if (theBaggage is not null) |
| 77 | + { |
| 78 | + TryExtractBaggage(theBaggage, out baggage); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return baggage; |
| 83 | + } |
| 84 | + |
| 85 | + internal static bool TryExtractBaggage(string baggageString, out IEnumerable<KeyValuePair<string, string?>>? baggage) |
| 86 | + { |
| 87 | + baggage = null; |
| 88 | + List<KeyValuePair<string, string?>>? baggageList = null; |
| 89 | + |
| 90 | + if (string.IsNullOrEmpty(baggageString)) |
| 91 | + { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + int currentIndex = 0; |
| 96 | + |
| 97 | + do |
| 98 | + { |
| 99 | + // Skip spaces |
| 100 | + while (currentIndex < baggageString.Length && (baggageString[currentIndex] == Space || baggageString[currentIndex] == Tab)) |
| 101 | + { |
| 102 | + currentIndex++; |
| 103 | + } |
| 104 | + |
| 105 | + if (currentIndex >= baggageString.Length) |
| 106 | + { |
| 107 | + break; // No Key exist |
| 108 | + } |
| 109 | + |
| 110 | + int keyStart = currentIndex; |
| 111 | + |
| 112 | + // Search end of the key |
| 113 | + while (currentIndex < baggageString.Length && baggageString[currentIndex] != Space && baggageString[currentIndex] != Tab && baggageString[currentIndex] != '=') |
| 114 | + { |
| 115 | + currentIndex++; |
| 116 | + } |
| 117 | + |
| 118 | + if (currentIndex >= baggageString.Length) |
| 119 | + { |
| 120 | + break; |
| 121 | + } |
| 122 | + |
| 123 | + int keyEnd = currentIndex; |
| 124 | + |
| 125 | + if (baggageString[currentIndex] != '=') |
| 126 | + { |
| 127 | + // Skip Spaces |
| 128 | + while (currentIndex < baggageString.Length && (baggageString[currentIndex] == Space || baggageString[currentIndex] == Tab)) |
| 129 | + { |
| 130 | + currentIndex++; |
| 131 | + } |
| 132 | + |
| 133 | + if (currentIndex >= baggageString.Length) |
| 134 | + { |
| 135 | + break; // Wrong key format |
| 136 | + } |
| 137 | + |
| 138 | + if (baggageString[currentIndex] != '=') |
| 139 | + { |
| 140 | + break; // wrong key format. |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + currentIndex++; |
| 145 | + |
| 146 | + // Skip spaces |
| 147 | + while (currentIndex < baggageString.Length && (baggageString[currentIndex] == Space || baggageString[currentIndex] == Tab)) |
| 148 | + { |
| 149 | + currentIndex++; |
| 150 | + } |
| 151 | + |
| 152 | + if (currentIndex >= baggageString.Length) |
| 153 | + { |
| 154 | + break; // Wrong value format |
| 155 | + } |
| 156 | + |
| 157 | + int valueStart = currentIndex; |
| 158 | + |
| 159 | + // Search end of the value |
| 160 | + while (currentIndex < baggageString.Length && baggageString[currentIndex] != Space && baggageString[currentIndex] != Tab && |
| 161 | + baggageString[currentIndex] != Comma && baggageString[currentIndex] != Semicolon) |
| 162 | + { |
| 163 | + currentIndex++; |
| 164 | + } |
| 165 | + |
| 166 | + if (keyStart < keyEnd && valueStart < currentIndex) |
| 167 | + { |
| 168 | + baggageList ??= new(); |
| 169 | + |
| 170 | + // Insert in reverse order for asp.net compatability. |
| 171 | + baggageList.Insert(0, new KeyValuePair<string, string?>( |
| 172 | + WebUtility.UrlDecode(baggageString.Substring(keyStart, keyEnd - keyStart)).Trim(s_trimmingSpaceCharacters), |
| 173 | + WebUtility.UrlDecode(baggageString.Substring(valueStart, currentIndex - valueStart)).Trim(s_trimmingSpaceCharacters))); |
| 174 | + } |
| 175 | + |
| 176 | + // Skip to end of values |
| 177 | + while (currentIndex < baggageString.Length && baggageString[currentIndex] != Comma) |
| 178 | + { |
| 179 | + currentIndex++; |
| 180 | + } |
| 181 | + |
| 182 | + currentIndex++; // Move to next key-value entry |
| 183 | + } while (currentIndex < baggageString.Length); |
| 184 | + |
| 185 | + baggage = baggageList; |
| 186 | + return baggageList != null; |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments