-
Notifications
You must be signed in to change notification settings - Fork 774
/
SdkSpanBuilder.java
256 lines (227 loc) · 8.1 KB
/
SdkSpanBuilder.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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.trace;
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.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.internal.ImmutableSpanContext;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.internal.AttributeUtil;
import io.opentelemetry.sdk.trace.data.LinkData;
import io.opentelemetry.sdk.trace.samplers.SamplingDecision;
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
/** {@link SdkSpanBuilder} is SDK implementation of {@link SpanBuilder}. */
final class SdkSpanBuilder implements SpanBuilder {
private final String spanName;
private final InstrumentationScopeInfo instrumentationScopeInfo;
private final TracerSharedState tracerSharedState;
private final SpanLimits spanLimits;
@Nullable private Context parent; // null means: Use current context.
private SpanKind spanKind = SpanKind.INTERNAL;
@Nullable private AttributesMap attributes;
@Nullable private List<LinkData> links;
private int totalNumberOfLinksAdded = 0;
private long startEpochNanos = 0;
SdkSpanBuilder(
String spanName,
InstrumentationScopeInfo instrumentationScopeInfo,
TracerSharedState tracerSharedState,
SpanLimits spanLimits) {
this.spanName = spanName;
this.instrumentationScopeInfo = instrumentationScopeInfo;
this.tracerSharedState = tracerSharedState;
this.spanLimits = spanLimits;
}
@Override
public SpanBuilder setParent(Context context) {
if (context == null) {
return this;
}
this.parent = context;
return this;
}
@Override
public SpanBuilder setNoParent() {
this.parent = Context.root();
return this;
}
@Override
public SpanBuilder setSpanKind(SpanKind spanKind) {
if (spanKind == null) {
return this;
}
this.spanKind = spanKind;
return this;
}
@Override
public SpanBuilder addLink(SpanContext spanContext) {
if (spanContext == null || !spanContext.isValid()) {
return this;
}
addLink(LinkData.create(spanContext));
return this;
}
@Override
public SpanBuilder addLink(SpanContext spanContext, Attributes attributes) {
if (spanContext == null || !spanContext.isValid()) {
return this;
}
if (attributes == null) {
attributes = Attributes.empty();
}
int totalAttributeCount = attributes.size();
addLink(
LinkData.create(
spanContext,
AttributeUtil.applyAttributesLimit(
attributes,
spanLimits.getMaxNumberOfAttributesPerLink(),
spanLimits.getMaxAttributeValueLength()),
totalAttributeCount));
return this;
}
private void addLink(LinkData link) {
totalNumberOfLinksAdded++;
if (links == null) {
links = new ArrayList<>(spanLimits.getMaxNumberOfLinks());
}
// don't bother doing anything with any links beyond the max.
if (links.size() == spanLimits.getMaxNumberOfLinks()) {
return;
}
links.add(link);
}
@Override
public SpanBuilder setAttribute(String key, String value) {
return setAttribute(stringKey(key), value);
}
@Override
public SpanBuilder setAttribute(String key, long value) {
return setAttribute(longKey(key), value);
}
@Override
public SpanBuilder setAttribute(String key, double value) {
return setAttribute(doubleKey(key), value);
}
@Override
public SpanBuilder setAttribute(String key, boolean value) {
return setAttribute(booleanKey(key), value);
}
@Override
public <T> SpanBuilder setAttribute(AttributeKey<T> key, T value) {
if (key == null || key.getKey().isEmpty() || value == null) {
return this;
}
attributes().put(key, value);
return this;
}
@Override
public SpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) {
if (startTimestamp < 0 || unit == null) {
return this;
}
startEpochNanos = unit.toNanos(startTimestamp);
return this;
}
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Span startSpan() {
Context parentContext = parent == null ? Context.current() : parent;
Span parentSpan = Span.fromContext(parentContext);
SpanContext parentSpanContext = parentSpan.getSpanContext();
String traceId;
IdGenerator idGenerator = tracerSharedState.getIdGenerator();
String spanId = idGenerator.generateSpanId();
if (!parentSpanContext.isValid()) {
// New root span.
traceId = idGenerator.generateTraceId();
} else {
// New child span.
traceId = parentSpanContext.getTraceId();
}
List<LinkData> immutableLinks =
links == null ? Collections.emptyList() : Collections.unmodifiableList(links);
// Avoid any possibility to modify the links list by adding links to the Builder after the
// startSpan is called. If that happens all the links will be added in a new list.
links = null;
Attributes immutableAttributes = attributes == null ? Attributes.empty() : attributes;
SamplingResult samplingResult =
tracerSharedState
.getSampler()
.shouldSample(
parentContext, traceId, spanName, spanKind, immutableAttributes, immutableLinks);
SamplingDecision samplingDecision = samplingResult.getDecision();
TraceState samplingResultTraceState =
samplingResult.getUpdatedTraceState(parentSpanContext.getTraceState());
SpanContext spanContext =
ImmutableSpanContext.create(
traceId,
spanId,
isSampled(samplingDecision) ? TraceFlags.getSampled() : TraceFlags.getDefault(),
samplingResultTraceState,
/* remote= */ false,
tracerSharedState.isIdGeneratorSafeToSkipIdValidation());
if (!isRecording(samplingDecision)) {
return Span.wrap(spanContext);
}
Attributes samplingAttributes = samplingResult.getAttributes();
if (!samplingAttributes.isEmpty()) {
samplingAttributes.forEach((key, value) -> attributes().put((AttributeKey) key, value));
}
// Avoid any possibility to modify the attributes by adding attributes to the Builder after the
// startSpan is called. If that happens all the attributes will be added in a new map.
AttributesMap recordedAttributes = attributes;
attributes = null;
return SdkSpan.startSpan(
spanContext,
spanName,
instrumentationScopeInfo,
spanKind,
parentSpan,
parentContext,
spanLimits,
tracerSharedState.getActiveSpanProcessor(),
tracerSharedState.getClock(),
tracerSharedState.getResource(),
recordedAttributes,
immutableLinks,
totalNumberOfLinksAdded,
startEpochNanos);
}
private AttributesMap attributes() {
AttributesMap attributes = this.attributes;
if (attributes == null) {
this.attributes =
new AttributesMap(
spanLimits.getMaxNumberOfAttributes(), spanLimits.getMaxAttributeValueLength());
attributes = this.attributes;
}
return attributes;
}
// Visible for testing
static boolean isRecording(SamplingDecision decision) {
return SamplingDecision.RECORD_ONLY.equals(decision)
|| SamplingDecision.RECORD_AND_SAMPLE.equals(decision);
}
// Visible for testing
static boolean isSampled(SamplingDecision decision) {
return SamplingDecision.RECORD_AND_SAMPLE.equals(decision);
}
}