-
Notifications
You must be signed in to change notification settings - Fork 773
/
JaegerActivityExtensions.cs
388 lines (325 loc) · 15.4 KB
/
JaegerActivityExtensions.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// <copyright file="JaegerActivityExtensions.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.Runtime.CompilerServices;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Exporter.Jaeger.Implementation
{
internal static class JaegerActivityExtensions
{
internal const string JaegerErrorFlagTagName = "error";
private const int DaysPerYear = 365;
// Number of days in 4 years
private const int DaysPer4Years = (DaysPerYear * 4) + 1; // 1461
// Number of days in 100 years
private const int DaysPer100Years = (DaysPer4Years * 25) - 1; // 36524
// Number of days in 400 years
private const int DaysPer400Years = (DaysPer100Years * 4) + 1; // 146097
// Number of days from 1/1/0001 to 12/31/1969
private const int DaysTo1970 = (DaysPer400Years * 4) + (DaysPer100Years * 3) + (DaysPer4Years * 17) + DaysPerYear; // 719,162
private const long UnixEpochTicks = DaysTo1970 * TimeSpan.TicksPerDay;
private const long TicksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;
private const long UnixEpochMicroseconds = UnixEpochTicks / TicksPerMicrosecond; // 62,135,596,800,000,000
public static JaegerSpan ToJaegerSpan(this Activity activity)
{
var jaegerTags = new TagEnumerationState
{
Tags = PooledList<JaegerTag>.Create(),
};
activity.EnumerateTags(ref jaegerTags);
string peerServiceName = null;
if (activity.Kind == ActivityKind.Client || activity.Kind == ActivityKind.Producer)
{
PeerServiceResolver.Resolve(ref jaegerTags, out peerServiceName, out bool addAsTag);
if (peerServiceName != null && addAsTag)
{
PooledList<JaegerTag>.Add(ref jaegerTags.Tags, new JaegerTag(SemanticConventions.AttributePeerService, JaegerTagType.STRING, vStr: peerServiceName));
}
}
// The Span.Kind must translate into a tag.
// See https://opentracing.io/specification/conventions/
if (activity.Kind != ActivityKind.Internal)
{
string spanKind = null;
if (activity.Kind == ActivityKind.Server)
{
spanKind = "server";
}
else if (activity.Kind == ActivityKind.Client)
{
spanKind = "client";
}
else if (activity.Kind == ActivityKind.Consumer)
{
spanKind = "consumer";
}
else if (activity.Kind == ActivityKind.Producer)
{
spanKind = "producer";
}
if (spanKind != null)
{
PooledList<JaegerTag>.Add(ref jaegerTags.Tags, new JaegerTag("span.kind", JaegerTagType.STRING, vStr: spanKind));
}
}
var activitySource = activity.Source;
if (!string.IsNullOrEmpty(activitySource.Name))
{
PooledList<JaegerTag>.Add(ref jaegerTags.Tags, new JaegerTag("otel.library.name", JaegerTagType.STRING, vStr: activitySource.Name));
if (!string.IsNullOrEmpty(activitySource.Version))
{
PooledList<JaegerTag>.Add(ref jaegerTags.Tags, new JaegerTag("otel.library.version", JaegerTagType.STRING, vStr: activitySource.Version));
}
}
var traceId = Int128.Empty;
var spanId = Int128.Empty;
var parentSpanId = Int128.Empty;
if (activity.IdFormat == ActivityIdFormat.W3C)
{
// TODO: The check above should be enforced by the usage of the exporter. Perhaps enforce at higher-level.
traceId = new Int128(activity.TraceId);
spanId = new Int128(activity.SpanId);
if (activity.ParentSpanId != default)
{
parentSpanId = new Int128(activity.ParentSpanId);
}
}
return new JaegerSpan(
peerServiceName: peerServiceName,
traceIdLow: traceId.Low,
traceIdHigh: traceId.High,
spanId: spanId.Low,
parentSpanId: parentSpanId.Low,
operationName: activity.DisplayName,
flags: (activity.Context.TraceFlags & ActivityTraceFlags.Recorded) > 0 ? 0x1 : 0,
startTime: ToEpochMicroseconds(activity.StartTimeUtc),
duration: (long)activity.Duration.TotalMilliseconds * 1000,
references: activity.ToJaegerSpanRefs(),
tags: jaegerTags.Tags,
logs: activity.ToJaegerLogs());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PooledList<JaegerSpanRef> ToJaegerSpanRefs(this Activity activity)
{
LinkEnumerationState references = default;
activity.EnumerateLinks(ref references);
return references.SpanRefs;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PooledList<JaegerLog> ToJaegerLogs(this Activity activity)
{
EventEnumerationState logs = default;
activity.EnumerateEvents(ref logs);
return logs.Logs;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JaegerLog ToJaegerLog(this ActivityEvent timedEvent)
{
var jaegerTags = new EventTagsEnumerationState
{
Tags = PooledList<JaegerTag>.Create(),
};
timedEvent.EnumerateTags(ref jaegerTags);
if (!jaegerTags.HasEvent)
{
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk_exporters/jaeger.md#events
PooledList<JaegerTag>.Add(ref jaegerTags.Tags, new JaegerTag("event", JaegerTagType.STRING, vStr: timedEvent.Name));
}
// TODO: Use the same function as JaegerConversionExtensions or check that the perf here is acceptable.
return new JaegerLog(timedEvent.Timestamp.ToEpochMicroseconds(), jaegerTags.Tags);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JaegerSpanRef ToJaegerSpanRef(this in ActivityLink link)
{
var traceId = new Int128(link.Context.TraceId);
var spanId = new Int128(link.Context.SpanId);
// Assume FOLLOWS_FROM for links, mirrored from Java: https://github.com/open-telemetry/opentelemetry-java/pull/481#discussion_r312577862
var refType = JaegerSpanRefType.FOLLOWS_FROM;
return new JaegerSpanRef(refType, traceId.Low, traceId.High, spanId.Low);
}
public static JaegerTag ToJaegerTag(this KeyValuePair<string, object> attribute)
{
return attribute.Value switch
{
string s => new JaegerTag(attribute.Key, JaegerTagType.STRING, vStr: s),
int i => new JaegerTag(attribute.Key, JaegerTagType.LONG, vLong: Convert.ToInt64(i)),
long l => new JaegerTag(attribute.Key, JaegerTagType.LONG, vLong: l),
float f => new JaegerTag(attribute.Key, JaegerTagType.DOUBLE, vDouble: Convert.ToDouble(f)),
double d => new JaegerTag(attribute.Key, JaegerTagType.DOUBLE, vDouble: d),
bool b => new JaegerTag(attribute.Key, JaegerTagType.BOOL, vBool: b),
_ => new JaegerTag(attribute.Key, JaegerTagType.STRING, vStr: attribute.Value.ToString()),
};
}
public static long ToEpochMicroseconds(this DateTime utcDateTime)
{
// Truncate sub-microsecond precision before offsetting by the Unix Epoch to avoid
// the last digit being off by one for dates that result in negative Unix times
long microseconds = utcDateTime.Ticks / TicksPerMicrosecond;
return microseconds - UnixEpochMicroseconds;
}
public static long ToEpochMicroseconds(this DateTimeOffset timestamp)
{
// Truncate sub-microsecond precision before offsetting by the Unix Epoch to avoid
// the last digit being off by one for dates that result in negative Unix times
long microseconds = timestamp.UtcDateTime.Ticks / TicksPerMicrosecond;
return microseconds - UnixEpochMicroseconds;
}
private static void ProcessJaegerTagArray(ref PooledList<JaegerTag> tags, KeyValuePair<string, object> activityTag)
{
if (activityTag.Value is int[] intArray)
{
foreach (var item in intArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.LONG, vLong: Convert.ToInt64(item));
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
else if (activityTag.Value is string[] stringArray)
{
foreach (var item in stringArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.STRING, vStr: item);
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
else if (activityTag.Value is bool[] boolArray)
{
foreach (var item in boolArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.BOOL, vBool: item);
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
else if (activityTag.Value is double[] doubleArray)
{
foreach (var item in doubleArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.DOUBLE, vDouble: item);
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ProcessJaegerTag(ref TagEnumerationState state, string key, JaegerTag jaegerTag)
{
if (jaegerTag.VStr != null)
{
PeerServiceResolver.InspectTag(ref state, key, jaegerTag.VStr);
if (key == SpanAttributeConstants.StatusCodeKey)
{
StatusCode? statusCode = StatusHelper.GetStatusCodeForTagValue(jaegerTag.VStr);
if (statusCode == StatusCode.Error)
{
// Error flag: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk_exporters/jaeger.md#error-flag
PooledList<JaegerTag>.Add(ref state.Tags, new JaegerTag(JaegerErrorFlagTagName, JaegerTagType.BOOL, vBool: true));
}
else if (!statusCode.HasValue || statusCode == StatusCode.Unset)
{
// Unset Status is not sent: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk_exporters/jaeger.md#status
return;
}
// Normalize status since it is user-driven.
jaegerTag = new JaegerTag(key, JaegerTagType.STRING, vStr: StatusHelper.GetTagValueForStatusCode(statusCode.Value));
}
else if (key == JaegerErrorFlagTagName)
{
// Ignore `error` tag if it exists, it will be added based on StatusCode + StatusDescription.
return;
}
}
else if (jaegerTag.VLong.HasValue)
{
PeerServiceResolver.InspectTag(ref state, key, jaegerTag.VLong.Value);
}
PooledList<JaegerTag>.Add(ref state.Tags, jaegerTag);
}
private struct TagEnumerationState : IActivityEnumerator<KeyValuePair<string, object>>, PeerServiceResolver.IPeerServiceState
{
public PooledList<JaegerTag> Tags;
public string PeerService { get; set; }
public int? PeerServicePriority { get; set; }
public string HostName { get; set; }
public string IpAddress { get; set; }
public long Port { get; set; }
public bool ForEach(KeyValuePair<string, object> activityTag)
{
if (activityTag.Value is Array)
{
ProcessJaegerTagArray(ref this.Tags, activityTag);
}
else if (activityTag.Value != null)
{
ProcessJaegerTag(ref this, activityTag.Key, activityTag.ToJaegerTag());
}
return true;
}
}
private struct LinkEnumerationState : IActivityEnumerator<ActivityLink>
{
public bool Created;
public PooledList<JaegerSpanRef> SpanRefs;
public bool ForEach(ActivityLink activityLink)
{
if (!this.Created)
{
this.SpanRefs = PooledList<JaegerSpanRef>.Create();
this.Created = true;
}
PooledList<JaegerSpanRef>.Add(ref this.SpanRefs, activityLink.ToJaegerSpanRef());
return true;
}
}
private struct EventEnumerationState : IActivityEnumerator<ActivityEvent>
{
public bool Created;
public PooledList<JaegerLog> Logs;
public bool ForEach(ActivityEvent activityEvent)
{
if (!this.Created)
{
this.Logs = PooledList<JaegerLog>.Create();
this.Created = true;
}
PooledList<JaegerLog>.Add(ref this.Logs, activityEvent.ToJaegerLog());
return true;
}
}
private struct EventTagsEnumerationState : IActivityEnumerator<KeyValuePair<string, object>>
{
public PooledList<JaegerTag> Tags;
public bool HasEvent;
public bool ForEach(KeyValuePair<string, object> tag)
{
if (tag.Value is Array)
{
ProcessJaegerTagArray(ref this.Tags, tag);
}
else if (tag.Value != null)
{
PooledList<JaegerTag>.Add(ref this.Tags, tag.ToJaegerTag());
}
if (tag.Key == "event")
{
this.HasEvent = true;
}
return true;
}
}
}
}