forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpanContext.java
More file actions
303 lines (263 loc) · 9.09 KB
/
SpanContext.java
File metadata and controls
303 lines (263 loc) · 9.09 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
package io.sentry;
import com.jakewharton.nopen.annotation.Open;
import io.sentry.protocol.SentryId;
import io.sentry.util.CollectionUtils;
import io.sentry.util.Objects;
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;
@Open
public class SpanContext implements JsonUnknown, JsonSerializable {
public static final String TYPE = "trace";
/** 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 final @Nullable SpanId parentSpanId;
/** If trace is sampled. */
private transient @Nullable Boolean sampled;
/** 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<>();
private @Nullable Map<String, Object> unknown;
public SpanContext(final @NotNull String operation, final @Nullable Boolean sampled) {
this(new SentryId(), new SpanId(), operation, null, sampled);
}
/**
* 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 Boolean sampled) {
this(traceId, spanId, parentSpanId, operation, null, sampled, null);
}
@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 Boolean sampled,
final @Nullable SpanStatus status) {
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.sampled = sampled;
this.description = description;
this.status = status;
}
/**
* 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;
this.sampled = spanContext.sampled;
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;
}
}
public void setOperation(final @NotNull String operation) {
this.op = Objects.requireNonNull(operation, "operation is required");
}
public void setTag(final @NotNull String name, final @NotNull String value) {
Objects.requireNonNull(name, "name is required");
Objects.requireNonNull(value, "value is required");
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;
}
public @NotNull Map<String, String> getTags() {
return tags;
}
public @Nullable Boolean getSampled() {
return sampled;
}
@ApiStatus.Internal
public void setSampled(final @Nullable Boolean sampled) {
this.sampled = sampled;
}
// 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";
}
@Override
public void serialize(@NotNull JsonObjectWriter writer, @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 (status != null) {
writer.name(JsonKeys.STATUS).value(logger, status);
}
if (!tags.isEmpty()) {
writer.name(JsonKeys.TAGS).value(logger, tags);
}
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 JsonObjectReader 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;
Map<String, String> tags = 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.TAGS:
tags =
CollectionUtils.newConcurrentHashMap(
(Map<String, String>) 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) {
String message = "Missing required field \"" + JsonKeys.OP + "\"";
Exception exception = new IllegalStateException(message);
logger.log(SentryLevel.ERROR, message, exception);
throw exception;
}
SpanContext spanContext = new SpanContext(traceId, spanId, op, parentSpanId, null);
spanContext.setDescription(description);
spanContext.setStatus(status);
if (tags != null) {
spanContext.tags = tags;
}
spanContext.setUnknown(unknown);
reader.endObject();
return spanContext;
}
}
}