-
Notifications
You must be signed in to change notification settings - Fork 858
/
Copy pathSpanBuilderShim.java
293 lines (246 loc) · 8.86 KB
/
SpanBuilderShim.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
292
293
/*
* 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 com.google.auto.value.AutoValue;
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.baggage.BaggageBuilder;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentracing.References;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer.SpanBuilder;
import io.opentracing.tag.Tag;
import io.opentracing.tag.Tags;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
final class SpanBuilderShim implements SpanBuilder {
private static final AttributeKey<String> OPENTRACING_REF_TYPE =
stringKey("opentracing.ref_type");
private static final String OPEN_TRACING_REF_TYPE_CHILD_OF = "child_of";
private static final String OPEN_TRACING_REF_TYPE_FOLLOWS_FROM = "follows_from";
private static final Attributes CHILD_OF_ATTR =
Attributes.of(OPENTRACING_REF_TYPE, OPEN_TRACING_REF_TYPE_CHILD_OF);
private static final Attributes FOLLOWS_FROM_ATTR =
Attributes.of(OPENTRACING_REF_TYPE, OPEN_TRACING_REF_TYPE_FOLLOWS_FROM);
private final Tracer tracer;
private final String spanName;
// *All* parents are saved in this list.
private List<SpanParentInfo> allParents = Collections.emptyList();
private boolean ignoreActiveSpan;
private final List<AttributeKey<?>> spanBuilderAttributeKeys = new ArrayList<>();
private final List<Object> spanBuilderAttributeValues = new ArrayList<>();
@Nullable private Boolean error;
private long startTimestampMicros;
SpanBuilderShim(Tracer tracer, String spanName) {
this.tracer = tracer;
this.spanName = spanName;
}
@Override
public SpanBuilder asChildOf(Span parent) {
SpanShim spanShim = ShimUtil.getSpanShim(parent);
if (spanShim != null) {
addReference(References.CHILD_OF, spanShim.context());
}
return this;
}
@Override
public SpanBuilder asChildOf(SpanContext parent) {
return addReference(References.CHILD_OF, parent);
}
@Override
public SpanBuilder addReference(@Nullable String referenceType, SpanContext referencedContext) {
SpanContextShim contextShim = ShimUtil.getContextShim(referencedContext);
if (contextShim == null) {
return this;
}
ReferenceType refType;
if (References.CHILD_OF.equals(referenceType)) {
refType = ReferenceType.CHILD_OF;
} else if (References.FOLLOWS_FROM.equals(referenceType)) {
refType = ReferenceType.FOLLOWS_FROM;
} else {
// Discard references with unrecognized type.
return this;
}
// Optimization for 99% situations, when there is only one parent.
if (allParents.size() == 0) {
allParents =
Collections.singletonList(
SpanParentInfo.create(
contextShim.getSpanContext(), contextShim.getBaggage(), refType));
} else {
if (allParents.size() == 1) {
allParents = new ArrayList<>(allParents);
}
allParents.add(
SpanParentInfo.create(contextShim.getSpanContext(), contextShim.getBaggage(), refType));
}
return this;
}
@Override
public SpanBuilder ignoreActiveSpan() {
ignoreActiveSpan = true;
return this;
}
@Override
public SpanBuilder withTag(String key, String value) {
if (Tags.ERROR.getKey().equals(key)) {
error = Boolean.parseBoolean(value);
} else {
this.spanBuilderAttributeKeys.add(stringKey(key));
this.spanBuilderAttributeValues.add(value);
}
return this;
}
@Override
public SpanBuilder withTag(String key, boolean value) {
if (Tags.ERROR.getKey().equals(key)) {
error = value;
} else {
this.spanBuilderAttributeKeys.add(booleanKey(key));
this.spanBuilderAttributeValues.add(value);
}
return this;
}
@Override
public SpanBuilder withTag(String key, Number value) {
if (value == null) {
return this;
}
if (value instanceof Integer
|| value instanceof Long
|| value instanceof Short
|| value instanceof Byte) {
this.spanBuilderAttributeKeys.add(longKey(key));
this.spanBuilderAttributeValues.add(value.longValue());
} else if (value instanceof Float || value instanceof Double) {
this.spanBuilderAttributeKeys.add(doubleKey(key));
this.spanBuilderAttributeValues.add(value.doubleValue());
} else {
this.spanBuilderAttributeKeys.add(stringKey(key));
this.spanBuilderAttributeValues.add(value.toString());
}
return this;
}
@Override
public <T> SpanBuilder withTag(Tag<T> tag, T value) {
if (tag == null) {
return this;
}
if (value instanceof String) {
this.withTag(tag.getKey(), (String) value);
} else if (value instanceof Boolean) {
this.withTag(tag.getKey(), (Boolean) value);
} else if (value instanceof Number) {
this.withTag(tag.getKey(), (Number) value);
} else {
this.withTag(tag.getKey(), value.toString());
}
return this;
}
@Override
public SpanBuilder withStartTimestamp(long microseconds) {
this.startTimestampMicros = microseconds;
return this;
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public Span start() {
Baggage baggage;
io.opentelemetry.api.trace.SpanBuilder builder = tracer.spanBuilder(spanName);
io.opentelemetry.api.trace.SpanContext mainParent = getMainParent(allParents);
if (ignoreActiveSpan && mainParent == null) {
builder.setNoParent();
baggage = Baggage.empty();
} else if (mainParent != null) {
builder.setParent(Context.root().with(io.opentelemetry.api.trace.Span.wrap(mainParent)));
baggage = getAllBaggage(allParents);
} else {
// No explicit parent Span, but extracted baggage may be available.
baggage = Baggage.current();
}
// *All* parents are processed as Links, in order to keep the reference type value.
for (SpanParentInfo parentInfo : allParents) {
builder.addLink(
parentInfo.getSpanContext(),
parentInfo.getRefType() == ReferenceType.CHILD_OF ? CHILD_OF_ATTR : FOLLOWS_FROM_ATTR);
}
if (startTimestampMicros > 0) {
builder.setStartTimestamp(startTimestampMicros, TimeUnit.MICROSECONDS);
}
// Attributes passed to the OT SpanBuilder MUST
// be set before the OTel Span is created,
// so those attributes are available to the Sampling API.
for (int i = 0; i < this.spanBuilderAttributeKeys.size(); i++) {
AttributeKey key = this.spanBuilderAttributeKeys.get(i);
Object value = this.spanBuilderAttributeValues.get(i);
builder.setAttribute(key, value);
}
io.opentelemetry.api.trace.Span span = builder.startSpan();
if (error != null) {
span.setStatus(error ? StatusCode.ERROR : StatusCode.OK);
}
return new SpanShim(span, baggage);
}
// The first SpanContext with Child Of type in the entire list is used as parent,
// else the first SpanContext is used as parent.
@Nullable
private static io.opentelemetry.api.trace.SpanContext getMainParent(
List<SpanParentInfo> parents) {
if (parents.size() == 0) {
return null;
}
SpanParentInfo mainParent = parents.get(0);
for (SpanParentInfo parentInfo : parents) {
if (parentInfo.getRefType() == ReferenceType.CHILD_OF) {
mainParent = parentInfo;
break;
}
}
return mainParent.getSpanContext();
}
private static Baggage getAllBaggage(List<SpanParentInfo> parents) {
if (parents.size() == 0) {
return Baggage.empty();
}
if (parents.size() == 1) {
return parents.get(0).getBaggage();
}
BaggageBuilder builder = Baggage.builder();
for (SpanParentInfo parent : parents) {
parent.getBaggage().forEach((key, entry) -> builder.put(key, entry.getValue()));
}
return builder.build();
}
@AutoValue
@Immutable
abstract static class SpanParentInfo {
private static SpanParentInfo create(
io.opentelemetry.api.trace.SpanContext spanContext,
Baggage baggage,
ReferenceType refType) {
return new AutoValue_SpanBuilderShim_SpanParentInfo(spanContext, baggage, refType);
}
abstract io.opentelemetry.api.trace.SpanContext getSpanContext();
abstract Baggage getBaggage();
abstract ReferenceType getRefType();
}
enum ReferenceType {
CHILD_OF,
FOLLOWS_FROM
}
}