Repository navigation
Expand file tree
/
Copy pathSpanContext.java
More file actions
502 lines (435 loc) · 15 KB
/
Copy pathSpanContext.java
File metadata and controls
502 lines (435 loc) · 15 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package io.sentry;
import com.jakewharton.nopen.annotation.Open;
import io.sentry.featureflags.IFeatureFlagBuffer;
import io.sentry.featureflags.SpanFeatureFlagBuffer;
import io.sentry.protocol.SentryId;
import io.sentry.util.CollectionUtils;
import io.sentry.util.Objects;
import io.sentry.util.thread.IThreadChecker;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
// temp fix for JDK 21: Constructors call overridable methods; making them final would break the
// public API.
@SuppressWarnings("this-escape")
@Open
public class SpanContext implements JsonUnknown, JsonSerializable {
public static final String TYPE = "trace";
public static final String DEFAULT_ORIGIN = "manual";
/** Determines which trace the Span belongs to. */
private final @NotNull SentryId traceId;
/** Span id. */
private final @NotNull SpanId spanId;
/** Id of a parent span. */
private @Nullable SpanId parentSpanId;
private transient @Nullable TracesSamplingDecision samplingDecision;
/** Short code identifying the type of operation the span is measuring. */
protected @NotNull String op;
/**
* Longer description of the span's operation, which uniquely identifies the span but is
* consistent across instances of the span.
*/
protected @Nullable String description;
/** Describes the status of the Transaction. */
protected @Nullable SpanStatus status;
/** A map or list of tags for this event. Each tag must be less than 200 characters. */
protected @NotNull Map<String, @NotNull String> tags = new ConcurrentHashMap<>();
protected @Nullable String origin = DEFAULT_ORIGIN;
protected @NotNull Map<String, Object> data = new ConcurrentHashMap<>();
private @Nullable Map<String, Object> unknown;
protected @Nullable Baggage baggage;
protected @NotNull IFeatureFlagBuffer featureFlags = SpanFeatureFlagBuffer.create();
/**
* Set the profiler id associated with this transaction. If set to a non-empty id, this value will
* be sent to sentry instead of {@link SentryOptions#getContinuousProfiler}
*/
private @NotNull SentryId profilerId = SentryId.EMPTY_ID;
public SpanContext(
final @NotNull String operation, final @Nullable TracesSamplingDecision samplingDecision) {
this(new SentryId(), new SpanId(), operation, null, samplingDecision);
}
/**
* Creates trace context with deferred sampling decision.
*
* @param operation the operation
*/
public SpanContext(final @NotNull String operation) {
this(new SentryId(), new SpanId(), operation, null, null);
}
public SpanContext(
final @NotNull SentryId traceId,
final @NotNull SpanId spanId,
final @NotNull String operation,
final @Nullable SpanId parentSpanId,
final @Nullable TracesSamplingDecision samplingDecision) {
this(traceId, spanId, parentSpanId, operation, null, samplingDecision, null, DEFAULT_ORIGIN);
}
@ApiStatus.Internal
public SpanContext(
final @NotNull SentryId traceId,
final @NotNull SpanId spanId,
final @Nullable SpanId parentSpanId,
final @NotNull String operation,
final @Nullable String description,
final @Nullable TracesSamplingDecision samplingDecision,
final @Nullable SpanStatus status,
final @Nullable String origin) {
this.traceId = Objects.requireNonNull(traceId, "traceId is required");
this.spanId = Objects.requireNonNull(spanId, "spanId is required");
this.op = Objects.requireNonNull(operation, "operation is required");
this.parentSpanId = parentSpanId;
this.description = description;
this.status = status;
this.origin = origin;
setSamplingDecision(samplingDecision);
final IThreadChecker threadChecker =
ScopesAdapter.getInstance().getOptions().getThreadChecker();
this.data.put(
SpanDataConvention.THREAD_ID, String.valueOf(threadChecker.currentThreadSystemId()));
this.data.put(SpanDataConvention.THREAD_NAME, threadChecker.getCurrentThreadName());
}
/**
* Copy constructor.
*
* @param spanContext the spanContext to copy
*/
public SpanContext(final @NotNull SpanContext spanContext) {
this.traceId = spanContext.traceId;
this.spanId = spanContext.spanId;
this.parentSpanId = spanContext.parentSpanId;
setSamplingDecision(spanContext.samplingDecision);
this.op = spanContext.op;
this.description = spanContext.description;
this.status = spanContext.status;
final Map<String, String> copiedTags = CollectionUtils.newConcurrentHashMap(spanContext.tags);
if (copiedTags != null) {
this.tags = copiedTags;
}
final Map<String, Object> copiedUnknown =
CollectionUtils.newConcurrentHashMap(spanContext.unknown);
if (copiedUnknown != null) {
this.unknown = copiedUnknown;
}
this.baggage = spanContext.baggage;
final Map<String, Object> copiedData = CollectionUtils.newConcurrentHashMap(spanContext.data);
if (copiedData != null) {
this.data = copiedData;
}
}
public void setOperation(final @NotNull String operation) {
this.op = Objects.requireNonNull(operation, "operation is required");
}
public void setTag(final @Nullable String name, final @Nullable String value) {
if (name == null) {
return;
}
if (value == null) {
this.tags.remove(name);
} else {
this.tags.put(name, value);
}
}
public void setDescription(final @Nullable String description) {
this.description = description;
}
public void setStatus(final @Nullable SpanStatus status) {
this.status = status;
}
@NotNull
public SentryId getTraceId() {
return traceId;
}
@NotNull
public SpanId getSpanId() {
return spanId;
}
@Nullable
@TestOnly
public SpanId getParentSpanId() {
return parentSpanId;
}
public @NotNull String getOperation() {
return op;
}
public @Nullable String getDescription() {
return description;
}
public @Nullable SpanStatus getStatus() {
return status;
}
@NotNull
SpanContext withDefaultStatus() {
final @NotNull SpanContext traceContext = new SpanContext(this);
traceContext.setOrigin(getOrigin());
if (traceContext.getStatus() == null) {
traceContext.setStatus(SpanStatus.OK);
}
return traceContext;
}
public @NotNull Map<String, String> getTags() {
return tags;
}
public @Nullable TracesSamplingDecision getSamplingDecision() {
return samplingDecision;
}
public @Nullable Boolean getSampled() {
if (samplingDecision == null) {
return null;
}
return samplingDecision.getSampled();
}
public @Nullable Boolean getProfileSampled() {
if (samplingDecision == null) {
return null;
}
return samplingDecision.getProfileSampled();
}
@ApiStatus.Internal
public void setSampled(final @Nullable Boolean sampled) {
if (sampled == null) {
setSamplingDecision(null);
} else {
setSamplingDecision(new TracesSamplingDecision(sampled));
}
}
@ApiStatus.Internal
public void setSampled(final @Nullable Boolean sampled, final @Nullable Boolean profileSampled) {
if (sampled == null) {
setSamplingDecision(null);
} else if (profileSampled == null) {
setSamplingDecision(new TracesSamplingDecision(sampled));
} else {
setSamplingDecision(new TracesSamplingDecision(sampled, null, profileSampled, null));
}
}
@ApiStatus.Internal
public void setSamplingDecision(final @Nullable TracesSamplingDecision samplingDecision) {
this.samplingDecision = samplingDecision;
if (this.baggage != null) {
this.baggage.setValuesFromSamplingDecision(this.samplingDecision);
}
}
public @Nullable String getOrigin() {
return origin;
}
public void setOrigin(final @Nullable String origin) {
this.origin = origin;
}
public @Nullable Baggage getBaggage() {
return baggage;
}
public @NotNull Map<String, Object> getData() {
return data;
}
public void setData(final @Nullable String key, final @Nullable Object value) {
if (key == null) {
return;
}
if (value == null) {
data.remove(key);
} else {
data.put(key, value);
}
}
@ApiStatus.Internal
public SpanContext copyForChild(
final @NotNull String operation,
final @Nullable SpanId parentSpanId,
final @Nullable SpanId spanId) {
return new SpanContext(
traceId,
spanId == null ? new SpanId() : spanId,
parentSpanId,
operation,
null,
samplingDecision,
null,
DEFAULT_ORIGIN);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SpanContext)) return false;
SpanContext that = (SpanContext) o;
return traceId.equals(that.traceId)
&& spanId.equals(that.spanId)
&& Objects.equals(parentSpanId, that.parentSpanId)
&& op.equals(that.op)
&& Objects.equals(description, that.description)
&& getStatus() == that.getStatus();
}
@Override
public int hashCode() {
return Objects.hash(traceId, spanId, parentSpanId, op, description, getStatus());
}
@ApiStatus.Internal
public @NotNull SentryId getProfilerId() {
return profilerId;
}
@ApiStatus.Internal
public void setProfilerId(@NotNull SentryId profilerId) {
this.profilerId = profilerId;
}
@ApiStatus.Internal
public void addFeatureFlag(final @Nullable String flag, final @Nullable Boolean result) {
featureFlags.add(flag, result);
}
@ApiStatus.Internal
public @NotNull IFeatureFlagBuffer getFeatureFlagBuffer() {
return featureFlags;
}
// region JsonSerializable
public static final class JsonKeys {
public static final String TRACE_ID = "trace_id";
public static final String SPAN_ID = "span_id";
public static final String PARENT_SPAN_ID = "parent_span_id";
public static final String OP = "op";
public static final String DESCRIPTION = "description";
public static final String STATUS = "status";
public static final String TAGS = "tags";
public static final String ORIGIN = "origin";
public static final String DATA = "data";
}
@Override
public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger logger)
throws IOException {
writer.beginObject();
writer.name(JsonKeys.TRACE_ID);
traceId.serialize(writer, logger);
writer.name(JsonKeys.SPAN_ID);
spanId.serialize(writer, logger);
if (parentSpanId != null) {
writer.name(JsonKeys.PARENT_SPAN_ID);
parentSpanId.serialize(writer, logger);
}
writer.name(JsonKeys.OP).value(op);
if (description != null) {
writer.name(JsonKeys.DESCRIPTION).value(description);
}
if (getStatus() != null) {
writer.name(JsonKeys.STATUS).value(logger, getStatus());
}
if (origin != null) {
writer.name(JsonKeys.ORIGIN).value(logger, origin);
}
if (!tags.isEmpty()) {
writer.name(JsonKeys.TAGS).value(logger, tags);
}
if (!data.isEmpty()) {
writer.name(JsonKeys.DATA).value(logger, data);
}
if (unknown != null) {
for (String key : unknown.keySet()) {
Object value = unknown.get(key);
writer.name(key).value(logger, value);
}
}
writer.endObject();
}
@Nullable
@Override
public Map<String, Object> getUnknown() {
return unknown;
}
@Override
public void setUnknown(@Nullable Map<String, Object> unknown) {
this.unknown = unknown;
}
public static final class Deserializer implements JsonDeserializer<SpanContext> {
@SuppressWarnings("unchecked")
@Override
public @NotNull SpanContext deserialize(@NotNull ObjectReader reader, @NotNull ILogger logger)
throws Exception {
reader.beginObject();
SentryId traceId = null;
SpanId spanId = null;
SpanId parentSpanId = null;
String op = null;
String description = null;
SpanStatus status = null;
String origin = null;
Map<String, String> tags = null;
Map<String, Object> data = null;
Map<String, Object> unknown = null;
while (reader.peek() == JsonToken.NAME) {
final String nextName = reader.nextName();
switch (nextName) {
case JsonKeys.TRACE_ID:
traceId = new SentryId.Deserializer().deserialize(reader, logger);
break;
case JsonKeys.SPAN_ID:
spanId = new SpanId.Deserializer().deserialize(reader, logger);
break;
case JsonKeys.PARENT_SPAN_ID:
parentSpanId = reader.nextOrNull(logger, new SpanId.Deserializer());
break;
case JsonKeys.OP:
op = reader.nextString();
break;
case JsonKeys.DESCRIPTION:
description = reader.nextString();
break;
case JsonKeys.STATUS:
status = reader.nextOrNull(logger, new SpanStatus.Deserializer());
break;
case JsonKeys.ORIGIN:
origin = reader.nextString();
break;
case JsonKeys.TAGS:
tags =
CollectionUtils.newConcurrentHashMap(
(Map<String, String>) reader.nextObjectOrNull());
break;
case JsonKeys.DATA:
data = (Map<String, Object>) reader.nextObjectOrNull();
break;
default:
if (unknown == null) {
unknown = new ConcurrentHashMap<>();
}
reader.nextUnknown(logger, unknown, nextName);
break;
}
}
if (traceId == null) {
String message = "Missing required field \"" + JsonKeys.TRACE_ID + "\"";
Exception exception = new IllegalStateException(message);
logger.log(SentryLevel.ERROR, message, exception);
throw exception;
}
if (spanId == null) {
String message = "Missing required field \"" + JsonKeys.SPAN_ID + "\"";
Exception exception = new IllegalStateException(message);
logger.log(SentryLevel.ERROR, message, exception);
throw exception;
}
if (op == null) {
/*
This is the case for hybrid SDKs. In fact, 'op' field is not required as part of the
trace context, but we utilise this class heavily also for transactions and spans, so it
would be a lot of changes to make it optional and we just duct-tape it here.
See doc https://develop.sentry.dev/sdk/event-payloads/contexts/#trace-context
*/
op = "";
}
SpanContext spanContext = new SpanContext(traceId, spanId, op, parentSpanId, null);
spanContext.setDescription(description);
spanContext.setStatus(status);
spanContext.setOrigin(origin);
if (tags != null) {
spanContext.tags = tags;
}
if (data != null) {
spanContext.data = data;
}
spanContext.setUnknown(unknown);
reader.endObject();
return spanContext;
}
}
}