-
Notifications
You must be signed in to change notification settings - Fork 775
/
SpanShim.java
291 lines (247 loc) · 8.36 KB
/
SpanShim.java
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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.opentracingshim;
import static io.opentelemetry.api.common.AttributeKey.booleanKey;
import static io.opentelemetry.api.common.AttributeKey.doubleKey;
import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.ImplicitContextKeyed;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.log.Fields;
import io.opentracing.tag.Tag;
import io.opentracing.tag.Tags;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
/*
* SpanContextShim is directly stored in the SpanShim for simplicity
* and performance reasons, as opposed to keeping a global map
* link OTel's Span and OT Span/SpanContext.
*/
final class SpanShim extends BaseShimObject implements Span, ImplicitContextKeyed {
private static final String DEFAULT_EVENT_NAME = "log";
private static final String ERROR = "error";
private static final ContextKey<SpanShim> SPAN_SHIM_KEY =
ContextKey.named("opentracing-shim-key");
private final io.opentelemetry.api.trace.Span span;
private final Object spanContextShimLock;
private volatile SpanContextShim spanContextShim;
public SpanShim(TelemetryInfo telemetryInfo, io.opentelemetry.api.trace.Span span) {
this(telemetryInfo, span, Baggage.empty());
}
public SpanShim(
TelemetryInfo telemetryInfo, io.opentelemetry.api.trace.Span span, Baggage baggage) {
super(telemetryInfo);
this.span = span;
this.spanContextShimLock = new Object();
this.spanContextShim = new SpanContextShim(telemetryInfo, span.getSpanContext(), baggage);
}
io.opentelemetry.api.trace.Span getSpan() {
return span;
}
io.opentelemetry.api.baggage.Baggage getBaggage() {
return spanContextShim.getBaggage();
}
@Nullable
public static SpanShim current() {
return Context.current().get(SPAN_SHIM_KEY);
}
@Override
public Context storeInContext(Context context) {
context = context.with(SPAN_SHIM_KEY, this).with(span).with(spanContextShim.getBaggage());
return context;
}
@Override
public SpanContext context() {
return spanContextShim;
}
@Override
public Span setTag(String key, String value) {
if (Tags.SPAN_KIND.getKey().equals(key)) {
// TODO: confirm we can safely ignore span.kind after Span was created
// https://github.com/bogdandrutu/opentelemetry/issues/42
} else if (Tags.ERROR.getKey().equals(key)) {
StatusCode canonicalCode = Boolean.parseBoolean(value) ? StatusCode.ERROR : StatusCode.UNSET;
span.setStatus(canonicalCode);
} else {
span.setAttribute(key, value);
}
return this;
}
@Override
public Span setTag(String key, boolean value) {
if (Tags.ERROR.getKey().equals(key)) {
StatusCode canonicalCode = value ? StatusCode.ERROR : StatusCode.UNSET;
span.setStatus(canonicalCode);
} else {
span.setAttribute(key, value);
}
return this;
}
@Override
public Span setTag(String key, Number value) {
if (value == null) {
return this;
}
if (value instanceof Integer
|| value instanceof Long
|| value instanceof Short
|| value instanceof Byte) {
span.setAttribute(key, value.longValue());
} else if (value instanceof Float || value instanceof Double) {
span.setAttribute(key, value.doubleValue());
} else {
span.setAttribute(key, value.toString());
}
return this;
}
@Override
public <T> Span setTag(Tag<T> tag, T value) {
if (tag == null) {
return this;
}
tag.set(this, value);
return this;
}
@Override
public Span log(Map<String, ?> fields) {
logInternal(-1, fields);
return this;
}
@Override
public Span log(long timestampMicroseconds, Map<String, ?> fields) {
logInternal(timestampMicroseconds, fields);
return this;
}
@Override
public Span log(String event) {
span.addEvent(event);
return this;
}
@Override
public Span log(long timestampMicroseconds, String event) {
span.addEvent(event, timestampMicroseconds, TimeUnit.MICROSECONDS);
return this;
}
@Override
public Span setBaggageItem(String key, String value) {
// TagKey nor TagValue can be created with null values.
if (key == null || value == null) {
return this;
}
synchronized (spanContextShimLock) {
spanContextShim = spanContextShim.newWithKeyValue(key, value);
}
return this;
}
@Nullable
@Override
public String getBaggageItem(String key) {
if (key == null) {
return null;
}
return spanContextShim.getBaggageItem(key);
}
@Override
public Span setOperationName(String operationName) {
span.updateName(operationName);
return this;
}
@Override
public void finish() {
span.end();
}
@Override
public void finish(long finishMicros) {
span.end(finishMicros, TimeUnit.MICROSECONDS);
}
private void logInternal(long timestampMicroseconds, Map<String, ?> fields) {
String name = getEventNameFromFields(fields);
Throwable throwable = null;
boolean isError = false;
if (name.equals(ERROR)) {
throwable = findThrowable(fields);
isError = true;
if (throwable == null) {
name = SemanticAttributes.EXCEPTION_EVENT_NAME;
}
}
Attributes attributes = convertToAttributes(fields, isError, throwable != null);
if (throwable != null) {
// timestamp is not recorded if specified
span.recordException(throwable, attributes);
} else if (timestampMicroseconds != -1) {
span.addEvent(name, attributes, timestampMicroseconds, TimeUnit.MICROSECONDS);
} else {
span.addEvent(name, attributes);
}
}
private static String getEventNameFromFields(Map<String, ?> fields) {
Object eventValue = fields == null ? null : fields.get(Fields.EVENT);
if (eventValue != null) {
return eventValue.toString();
}
return DEFAULT_EVENT_NAME;
}
private static Attributes convertToAttributes(
Map<String, ?> fields, boolean isError, boolean isRecordingException) {
AttributesBuilder attributesBuilder = Attributes.builder();
for (Map.Entry<String, ?> entry : fields.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// TODO - verify null values are NOT allowed.
if (value == null) {
continue;
}
if (value instanceof Byte
|| value instanceof Short
|| value instanceof Integer
|| value instanceof Long) {
attributesBuilder.put(longKey(key), ((Number) value).longValue());
} else if (value instanceof Float || value instanceof Double) {
attributesBuilder.put(doubleKey(key), ((Number) value).doubleValue());
} else if (value instanceof Boolean) {
attributesBuilder.put(booleanKey(key), (Boolean) value);
} else {
AttributeKey<String> attributeKey = null;
if (isError && !isRecordingException) {
if (key.equals(Fields.ERROR_KIND)) {
attributeKey = SemanticAttributes.EXCEPTION_TYPE;
} else if (key.equals(Fields.MESSAGE)) {
attributeKey = SemanticAttributes.EXCEPTION_MESSAGE;
} else if (key.equals(Fields.STACK)) {
attributeKey = SemanticAttributes.EXCEPTION_STACKTRACE;
}
}
if (isRecordingException && key.equals(Fields.ERROR_OBJECT)) {
// Already recorded as the exception itself so don't add as attribute.
continue;
}
if (attributeKey == null) {
attributeKey = stringKey(key);
}
attributesBuilder.put(attributeKey, value.toString());
}
}
return attributesBuilder.build();
}
@Nullable
private static Throwable findThrowable(Map<String, ?> fields) {
Object value = fields.get(Fields.ERROR_OBJECT);
if (value instanceof Throwable) {
return (Throwable) value;
}
return null;
}
}