-
Notifications
You must be signed in to change notification settings - Fork 769
/
SpanShim.cs
277 lines (230 loc) · 7.42 KB
/
SpanShim.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;
using OpenTracing;
namespace OpenTelemetry.Shims.OpenTracing;
internal sealed class SpanShim : ISpan
{
/// <summary>
/// The default event name if not specified.
/// </summary>
public const string DefaultEventName = "log";
private static readonly IReadOnlyCollection<Type> OpenTelemetrySupportedAttributeValueTypes = new List<Type>
{
typeof(string),
typeof(bool),
typeof(byte),
typeof(short),
typeof(int),
typeof(long),
typeof(float),
typeof(double),
};
private readonly SpanContextShim spanContextShim;
public SpanShim(TelemetrySpan span)
{
Guard.ThrowIfNull(span);
this.Span = span;
this.spanContextShim = new SpanContextShim(this.Span.Context);
}
/// <inheritdoc/>
public ISpanContext Context => this.spanContextShim;
public TelemetrySpan Span { get; private set; }
/// <inheritdoc/>
public void Finish()
{
this.Span.End();
}
/// <inheritdoc/>
public void Finish(DateTimeOffset finishTimestamp)
{
this.Span.End(finishTimestamp);
}
/// <inheritdoc/>
public string GetBaggageItem(string key)
=> Baggage.GetBaggage(key);
/// <inheritdoc/>
public ISpan Log(DateTimeOffset timestamp, IEnumerable<KeyValuePair<string, object>> fields)
{
Guard.ThrowIfNull(fields);
var payload = ConvertToEventPayload(fields);
var eventName = payload.Item1;
var spanAttributes = new SpanAttributes();
foreach (var field in payload.Item2)
{
switch (field.Value)
{
case long value:
spanAttributes.Add(field.Key, value);
break;
case long[] value:
spanAttributes.Add(field.Key, value);
break;
case bool value:
spanAttributes.Add(field.Key, value);
break;
case bool[] value:
spanAttributes.Add(field.Key, value);
break;
case double value:
spanAttributes.Add(field.Key, value);
break;
case double[] value:
spanAttributes.Add(field.Key, value);
break;
case string value:
spanAttributes.Add(field.Key, value);
break;
case string[] value:
spanAttributes.Add(field.Key, value);
break;
default:
break;
}
}
if (timestamp == DateTimeOffset.MinValue)
{
this.Span.AddEvent(eventName, spanAttributes);
}
else
{
this.Span.AddEvent(eventName, timestamp, spanAttributes);
}
return this;
}
/// <inheritdoc/>
public ISpan Log(IEnumerable<KeyValuePair<string, object>> fields)
{
return this.Log(DateTimeOffset.MinValue, fields);
}
/// <inheritdoc/>
public ISpan Log(string @event)
{
Guard.ThrowIfNull(@event);
this.Span.AddEvent(@event);
return this;
}
/// <inheritdoc/>
public ISpan Log(DateTimeOffset timestamp, string @event)
{
Guard.ThrowIfNull(@event);
this.Span.AddEvent(@event, timestamp);
return this;
}
/// <inheritdoc/>
public ISpan SetBaggageItem(string key, string value)
{
Baggage.SetBaggage(key, value);
return this;
}
/// <inheritdoc/>
public ISpan SetOperationName(string operationName)
{
Guard.ThrowIfNull(operationName);
this.Span.UpdateName(operationName);
return this;
}
/// <inheritdoc/>
public ISpan SetTag(string key, string value)
{
Guard.ThrowIfNull(key);
this.Span.SetAttribute(key, value);
return this;
}
/// <inheritdoc/>
public ISpan SetTag(string key, bool value)
{
Guard.ThrowIfNull(key);
// Special case the OpenTracing Error Tag
// see https://opentracing.io/specification/conventions/
if (global::OpenTracing.Tag.Tags.Error.Key.Equals(key, StringComparison.Ordinal))
{
this.Span.SetStatus(value ? Status.Error : Status.Ok);
}
else
{
this.Span.SetAttribute(key, value);
}
return this;
}
/// <inheritdoc/>
public ISpan SetTag(string key, int value)
{
Guard.ThrowIfNull(key);
this.Span.SetAttribute(key, value);
return this;
}
/// <inheritdoc/>
public ISpan SetTag(string key, double value)
{
Guard.ThrowIfNull(key);
this.Span.SetAttribute(key, value);
return this;
}
/// <inheritdoc/>
public ISpan SetTag(global::OpenTracing.Tag.BooleanTag tag, bool value)
{
return this.SetTag(tag?.Key, value);
}
/// <inheritdoc/>
public ISpan SetTag(global::OpenTracing.Tag.IntOrStringTag tag, string value)
{
if (int.TryParse(value, out var result))
{
return this.SetTag(tag?.Key, result);
}
return this.SetTag(tag?.Key, value);
}
/// <inheritdoc/>
public ISpan SetTag(global::OpenTracing.Tag.IntTag tag, int value)
{
return this.SetTag(tag?.Key, value);
}
/// <inheritdoc/>
public ISpan SetTag(global::OpenTracing.Tag.StringTag tag, string value)
{
return this.SetTag(tag?.Key, value);
}
/// <summary>
/// Constructs an OpenTelemetry event payload from an OpenTracing Log key/value map.
/// </summary>
/// <param name="fields">The fields.</param>
/// <returns>A 2-Tuple containing the event name and payload information.</returns>
private static Tuple<string, IDictionary<string, object>> ConvertToEventPayload(IEnumerable<KeyValuePair<string, object>> fields)
{
string eventName = null;
var attributes = new Dictionary<string, object>();
foreach (var field in fields)
{
// TODO verify null values are NOT allowed.
if (field.Value == null)
{
continue;
}
// Duplicate keys must be ignored even though they appear to be allowed in OpenTracing.
if (attributes.ContainsKey(field.Key))
{
continue;
}
if (eventName == null && field.Key.Equals(LogFields.Event, StringComparison.Ordinal) && field.Value is string value)
{
// This is meant to be the event name
eventName = value;
// We don't want to add the event name as a separate attribute
continue;
}
// Supported types are added directly, all other types are converted to strings.
if (OpenTelemetrySupportedAttributeValueTypes.Contains(field.Value.GetType()))
{
attributes.Add(field.Key, field.Value);
}
else
{
// TODO should we completely ignore unsupported types?
attributes.Add(field.Key, field.Value.ToString());
}
}
return new Tuple<string, IDictionary<string, object>>(eventName ?? DefaultEventName, attributes);
}
}