-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathAWSXRayPropagator.cs
314 lines (260 loc) · 9.98 KB
/
AWSXRayPropagator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// <copyright file="AWSXRayPropagator.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.Globalization;
using System.Linq;
using System.Numerics;
using System.Text;
using OpenTelemetry.Context.Propagation;
namespace OpenTelemetry.Contrib.Extensions.AWSXRay.Trace;
/// <summary>
/// Propagator for AWS X-Ray. See https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader.
/// </summary>
public class AWSXRayPropagator : TextMapPropagator
{
private const string AWSXRayTraceHeaderKey = "X-Amzn-Trace-Id";
private const char KeyValueDelimiter = '=';
private const char TraceHeaderDelimiter = ';';
private const string RootKey = "Root";
private const string Version = "1";
private const int RandomNumberHexDigits = 24;
private const int EpochHexDigits = 8;
private const int TotalLength = 35;
private const char TraceIdDelimiter = '-';
private const int TraceIdDelimiterFirstIndex = 1;
private const int TraceIdDelimiterSecondIndex = 10;
private const string ParentKey = "Parent";
private const int ParentIdHexDigits = 16;
private const string SampledKey = "Sampled";
private const char SampledValue = '1';
private const char NotSampledValue = '0';
/// <inheritdoc/>
public override ISet<string> Fields => new HashSet<string>() { AWSXRayTraceHeaderKey };
/// <inheritdoc/>
public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
{
if (context.ActivityContext.IsValid())
{
return context;
}
if (carrier == null)
{
AWSXRayEventSource.Log.FailedToExtractActivityContext(nameof(AWSXRayPropagator), "null carrier");
return context;
}
if (getter == null)
{
AWSXRayEventSource.Log.FailedToExtractActivityContext(nameof(AWSXRayPropagator), "null getter");
return context;
}
try
{
var parentTraceHeader = getter(carrier, AWSXRayTraceHeaderKey);
if (parentTraceHeader == null || parentTraceHeader.Count() != 1)
{
return context;
}
var parentHeader = parentTraceHeader.First();
if (!TryParseXRayTraceHeader(parentHeader, out var newActivityContext))
{
return context;
}
return new PropagationContext(newActivityContext, context.Baggage);
}
catch (Exception ex)
{
AWSXRayEventSource.Log.ActivityContextExtractException(nameof(AWSXRayPropagator), ex);
}
return context;
}
/// <inheritdoc/>
public override void Inject<T>(PropagationContext context, T carrier, Action<T, string, string> setter)
{
if (context.ActivityContext.TraceId == default || context.ActivityContext.SpanId == default)
{
AWSXRayEventSource.Log.FailedToInjectActivityContext(nameof(AWSXRayPropagator), "Invalid context");
return;
}
if (carrier == null)
{
AWSXRayEventSource.Log.FailedToInjectActivityContext(nameof(AWSXRayPropagator), "null carrier");
return;
}
if (setter == null)
{
AWSXRayEventSource.Log.FailedToInjectActivityContext(nameof(AWSXRayPropagator), "null setter");
return;
}
var sb = new StringBuilder();
sb.Append(RootKey);
sb.Append(KeyValueDelimiter);
sb.Append(ToXRayTraceIdFormat(context.ActivityContext.TraceId.ToHexString()));
sb.Append(TraceHeaderDelimiter);
sb.Append(ParentKey);
sb.Append(KeyValueDelimiter);
sb.Append(context.ActivityContext.SpanId.ToHexString());
sb.Append(TraceHeaderDelimiter);
sb.Append(SampledKey);
sb.Append(KeyValueDelimiter);
sb.Append((context.ActivityContext.TraceFlags & ActivityTraceFlags.Recorded) != 0 ? SampledValue : NotSampledValue);
setter(carrier, AWSXRayTraceHeaderKey, sb.ToString());
}
internal static bool TryParseXRayTraceHeader(string rawHeader, out ActivityContext activityContext)
{
// from https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader
// rawHeader format: Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1
activityContext = default;
ReadOnlySpan<char> traceId = default;
ReadOnlySpan<char> parentId = default;
char traceOptions = default;
if (string.IsNullOrEmpty(rawHeader))
{
return false;
}
ReadOnlySpan<char> header = rawHeader.AsSpan();
while (header.Length > 0)
{
int delimiterIndex = header.IndexOf(TraceHeaderDelimiter);
ReadOnlySpan<char> part;
if (delimiterIndex >= 0)
{
part = header.Slice(0, delimiterIndex);
header = header.Slice(delimiterIndex + 1);
}
else
{
part = header.Slice(0);
header = header.Slice(header.Length);
}
ReadOnlySpan<char> trimmedPart = part.Trim();
int equalsIndex = trimmedPart.IndexOf(KeyValueDelimiter);
if (equalsIndex < 0)
{
return false;
}
ReadOnlySpan<char> value = trimmedPart.Slice(equalsIndex + 1);
if (trimmedPart.StartsWith(RootKey.AsSpan()))
{
if (!TryParseOTFormatTraceId(value, out var otFormatTraceId))
{
return false;
}
traceId = otFormatTraceId;
}
else if (trimmedPart.StartsWith(ParentKey.AsSpan()))
{
if (!IsParentIdValid(value))
{
return false;
}
parentId = value;
}
else if (trimmedPart.StartsWith(SampledKey.AsSpan()))
{
if (!TryParseSampleDecision(value, out var sampleDecision))
{
return false;
}
traceOptions = sampleDecision;
}
}
if (traceId == default || parentId == default || traceOptions == default)
{
return false;
}
var activityTraceId = ActivityTraceId.CreateFromString(traceId);
var activityParentId = ActivitySpanId.CreateFromString(parentId);
var activityTraceOptions = traceOptions == SampledValue ? ActivityTraceFlags.Recorded : ActivityTraceFlags.None;
activityContext = new ActivityContext(activityTraceId, activityParentId, activityTraceOptions, isRemote: true);
return true;
}
internal static bool TryParseOTFormatTraceId(ReadOnlySpan<char> traceId, out ReadOnlySpan<char> otFormatTraceId)
{
otFormatTraceId = default;
if (traceId.IsEmpty || traceId.IsWhiteSpace())
{
return false;
}
if (traceId.Length != TotalLength)
{
return false;
}
if (!traceId.StartsWith(Version.AsSpan()))
{
return false;
}
if (traceId[TraceIdDelimiterFirstIndex] != TraceIdDelimiter || traceId[TraceIdDelimiterSecondIndex] != TraceIdDelimiter)
{
return false;
}
var timestamp = traceId.Slice(TraceIdDelimiterFirstIndex + 1, EpochHexDigits);
var randomNumber = traceId.Slice(TraceIdDelimiterSecondIndex + 1);
if (timestamp.Length != EpochHexDigits || randomNumber.Length != RandomNumberHexDigits)
{
return false;
}
var timestampString = timestamp.ToString();
var randomNumberString = randomNumber.ToString();
if (!int.TryParse(timestampString, NumberStyles.HexNumber, null, out _))
{
return false;
}
if (!BigInteger.TryParse(randomNumberString, NumberStyles.HexNumber, null, out _))
{
return false;
}
otFormatTraceId = (timestampString + randomNumberString).AsSpan();
return true;
}
internal static bool IsParentIdValid(ReadOnlySpan<char> parentId)
{
if (parentId.IsEmpty || parentId.IsWhiteSpace())
{
return false;
}
return parentId.Length == ParentIdHexDigits && long.TryParse(parentId.ToString(), NumberStyles.HexNumber, null, out _);
}
internal static bool TryParseSampleDecision(ReadOnlySpan<char> sampleDecision, out char result)
{
result = default;
if (sampleDecision.IsEmpty || sampleDecision.IsWhiteSpace())
{
return false;
}
if (!char.TryParse(sampleDecision.ToString(), out var tempChar))
{
return false;
}
if (tempChar != SampledValue && tempChar != NotSampledValue)
{
return false;
}
result = tempChar;
return true;
}
internal static string ToXRayTraceIdFormat(string traceId)
{
var sb = new StringBuilder();
sb.Append(Version);
sb.Append(TraceIdDelimiter);
sb.Append(traceId.Substring(0, EpochHexDigits));
sb.Append(TraceIdDelimiter);
sb.Append(traceId.Substring(EpochHexDigits));
return sb.ToString();
}
}