-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTraceContextPropagation.cs
243 lines (206 loc) · 6.17 KB
/
TraceContextPropagation.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
namespace Zipkin
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using Utils;
/// <summary>
/// Used to propagate and recover trace information
/// (trace id and parent span id) across processes.
/// </summary>
public static class TraceContextPropagation
{
// Todo: make it compatible with
// X-B3-TraceId : 64 lower-hex encoded bits(required)
// X-B3-SpanId : 64 lower-hex encoded bits(required)
// X-B3-ParentSpanId : 64 lower-hex encoded bits(absent on root span)
// X-B3-Sampled : Boolean(either “1” or “0”, can be absent)
// X-B3-Flags : “1” means debug(can be absent)
private const string TraceIdKey = "_zipkin_traceid";
private const string SpanIdKey = "_zipkin_spanid";
// AsyncLocal is unreliable. We get race situations every once in a while
private static readonly AsyncLocal<ConcurrentStack<Span>> LocalSpanStack = new AsyncLocal<ConcurrentStack<Span>>();
/// <summary>
/// For internal use only. Pushes Span onto context stack.
/// </summary>
/// <param name="span"></param>
public static void PushSpan(Span span)
{
EnsureStack();
var stack = LocalSpanStack.Value;
stack.Push(span);
}
/// <summary>
/// For internal use only. Pops Span from context stack.
/// </summary>
public static void PopSpan()
{
EnsureStack();
var stack = LocalSpanStack.Value;
Span dummy;
stack.TryPop(out dummy);
}
public static Span CurrentSpan
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
var stack = LocalSpanStack.Value;
if (stack != null /*&& stack.Count != 0*/)
{
Span dummy;
stack.TryPeek(out dummy);
return dummy;
}
return null;
}
}
/// <summary>
/// Indicates whether there's an active trace in the current context.
/// </summary>
public static bool IsWithinTrace
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return CurrentSpan != null; }
}
/// <summary>
/// If within a trace, writes the required information onto the specified dictionary.
/// The dictionary should carry data from a cross process boundary (for example http headers)
/// </summary>
public static void PropagateTraceIdOnto(IDictionary<string, string> dictionary)
{
var span = CurrentSpan;
if (span != null)
{
dictionary[TraceIdKey] = span.TraceId.ToString();
dictionary[SpanIdKey] = span.Id.ToString();
}
}
/// <summary>
/// If within a trace, writes the required information onto the specified dictionary.
/// The dictionary should carry data from a cross process boundary (for example http headers)
/// </summary>
public static void PropagateTraceIdOnto(IDictionary<string, object> dictionary)
{
var span = CurrentSpan;
if (span != null)
{
dictionary[TraceIdKey] = span.TraceId;
dictionary[SpanIdKey] = span.Id;
}
}
/// <summary>
/// Checks the specified dictionary for required trace information.
/// The dictionary should carry data from a cross process boundary (for example http headers)
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryObtainTraceIdFrom(IDictionary<string, string> dictionary, out TraceInfo? traceInfo)
{
traceInfo = null;
string value;
if (dictionary != null && dictionary.TryGetValue(TraceIdKey, out value))
{
var traceId = Convert.ToInt64(value);
var parentSpanId = 0L;
if (dictionary.TryGetValue(SpanIdKey, out value))
parentSpanId = Convert.ToInt64(value);
traceInfo = new TraceInfo
{
span = new Span(traceId, null, 0L) { ParentId = parentSpanId }
};
}
return traceInfo.HasValue;
}
/// <summary>
/// Checks the specified dictionary for required trace information.
/// The dictionary should carry data from a cross process boundary (for example http headers)
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryObtainTraceIdFrom(IDictionary<string, object> dictionary, out TraceInfo? traceInfo)
{
traceInfo = null;
object value;
if (dictionary != null && dictionary.TryGetValue(TraceIdKey, out value))
{
var traceId = Convert.ToInt64(value);
long parentSpanId = 0;
if (dictionary.TryGetValue(SpanIdKey, out value))
parentSpanId = Convert.ToInt64(value);
traceInfo = new TraceInfo
{
span = new Span(traceId, null, 0L) { ParentId = parentSpanId }
};
}
return traceInfo.HasValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TraceInfo? GetTraceInfoFrom(IDictionary<string, string> dictionary)
{
TraceInfo? traceInfo;
TryObtainTraceIdFrom(dictionary, out traceInfo);
return traceInfo;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TraceInfo? GetTraceInfoFrom(IDictionary<string, object> dictionary)
{
TraceInfo? traceInfo;
TryObtainTraceIdFrom(dictionary, out traceInfo);
return traceInfo;
}
public static TraceInfo CaptureCurrentTrace()
{
var span = CurrentSpan;
if (span != null)
{
return new TraceInfo
{
span = span.Clone()
};
}
return new TraceInfo();
}
public static void ApplyCaptured(TraceInfo capturedContext)
{
if (capturedContext.span != null)
{
capturedContext.span.DurationInMicroseconds = TickClock.Start();
PushSpan(capturedContext.span);
}
}
public static void UndoApply(TraceInfo capturedContext)
{
if (capturedContext.span != null)
{
PopSpan();
capturedContext.span.DurationInMicroseconds = TickClock.GetDuration(capturedContext.span.DurationInMicroseconds.Value);
ZipkinConfig.Record(capturedContext.span);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void EnsureStack()
{
if (LocalSpanStack.Value == null)
LocalSpanStack.Value = new ConcurrentStack<Span>();
}
/// <summary>
/// For unit tests
/// </summary>
internal static void Reset()
{
LocalSpanStack.Value = null;
}
}
public struct TraceInfo
{
internal Span span;
public TraceInfo(long traceId, long parentSpanId) : this()
{
this.span = new Span(traceId, "", RandomHelper.NewId())
{
ParentId = parentSpanId
};
}
}
}