forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpan.java
More file actions
349 lines (288 loc) · 8.99 KB
/
Span.java
File metadata and controls
349 lines (288 loc) · 8.99 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
package io.sentry;
import io.sentry.protocol.SentryId;
import io.sentry.util.Objects;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
@ApiStatus.Internal
public final class Span implements ISpan {
/** The moment in time when span was started. */
private final @NotNull Date startTimestamp;
/**
* The moment in time when span has started, read from {@link System#nanoTime()}. Can be {@code
* null} when {@link #startTimestamp} was provided to span constructor.
*/
private final @Nullable Long startNanos;
/** The moment in time when span has finished, read from {@link System#nanoTime()}. */
private @Nullable Long endNanos;
/** The moment in time when span has ended. */
private @Nullable Double timestamp;
private final @NotNull SpanContext context;
/**
* A transaction this span is attached to. Marked as transient to be ignored during JSON
* serialization.
*/
private final @NotNull SentryTracer transaction;
/** A throwable thrown during the execution of the span. */
private @Nullable Throwable throwable;
private final @NotNull IHub hub;
private final @NotNull AtomicBoolean finished = new AtomicBoolean(false);
private @Nullable SpanFinishedCallback spanFinishedCallback;
private final @NotNull Map<String, Object> data = new ConcurrentHashMap<>();
Span(
final @NotNull SentryId traceId,
final @Nullable SpanId parentSpanId,
final @NotNull SentryTracer transaction,
final @NotNull String operation,
final @NotNull IHub hub) {
this(traceId, parentSpanId, transaction, operation, hub, null, null);
}
Span(
final @NotNull SentryId traceId,
final @Nullable SpanId parentSpanId,
final @NotNull SentryTracer transaction,
final @NotNull String operation,
final @NotNull IHub hub,
final @Nullable Date startTimestamp,
final @Nullable SpanFinishedCallback spanFinishedCallback) {
this.context =
new SpanContext(traceId, new SpanId(), operation, parentSpanId, transaction.isSampled());
this.transaction = Objects.requireNonNull(transaction, "transaction is required");
this.hub = Objects.requireNonNull(hub, "hub is required");
this.spanFinishedCallback = spanFinishedCallback;
if (startTimestamp != null) {
this.startTimestamp = startTimestamp;
this.startNanos = null;
} else {
this.startTimestamp = DateUtils.getCurrentDateTime();
this.startNanos = System.nanoTime();
}
}
@VisibleForTesting
public Span(
final @NotNull TransactionContext context,
final @NotNull SentryTracer sentryTracer,
final @NotNull IHub hub,
final @Nullable Date startTimestamp) {
this.context = Objects.requireNonNull(context, "context is required");
this.transaction = Objects.requireNonNull(sentryTracer, "sentryTracer is required");
this.hub = Objects.requireNonNull(hub, "hub is required");
this.spanFinishedCallback = null;
if (startTimestamp != null) {
this.startTimestamp = startTimestamp;
this.startNanos = null;
} else {
this.startTimestamp = DateUtils.getCurrentDateTime();
this.startNanos = System.nanoTime();
}
}
public @NotNull Date getStartTimestamp() {
return startTimestamp;
}
public @Nullable Double getTimestamp() {
return timestamp;
}
@Override
public @NotNull ISpan startChild(final @NotNull String operation) {
return this.startChild(operation, (String) null);
}
@Override
public @NotNull ISpan startChild(
final @NotNull String operation,
final @Nullable String description,
final @Nullable Date timestamp) {
if (finished.get()) {
return NoOpSpan.getInstance();
}
return transaction.startChild(context.getSpanId(), operation, description, timestamp);
}
@Override
public @NotNull ISpan startChild(
final @NotNull String operation, final @Nullable String description) {
if (finished.get()) {
return NoOpSpan.getInstance();
}
return transaction.startChild(context.getSpanId(), operation, description);
}
@Override
public @NotNull SentryTraceHeader toSentryTrace() {
return new SentryTraceHeader(context.getTraceId(), context.getSpanId(), context.getSampled());
}
@Override
public @Nullable TraceState traceState() {
return transaction.traceState();
}
@Override
public @Nullable TraceStateHeader toTraceStateHeader() {
return transaction.toTraceStateHeader();
}
@Override
public void finish() {
this.finish(this.context.getStatus());
}
@Override
public void finish(@Nullable SpanStatus status) {
finish(status, DateUtils.dateToSeconds(DateUtils.getCurrentDateTime()), null);
}
/**
* Used to finish unfinished spans by {@link SentryTracer}.
*
* @param status - status to finish span with
* @param timestamp - the root span timestamp.
*/
void finish(
final @Nullable SpanStatus status,
final @NotNull Double timestamp,
final @Nullable Long endNanos) {
// the span can be finished only once
if (!finished.compareAndSet(false, true)) {
return;
}
this.context.setStatus(status);
this.timestamp = timestamp;
if (throwable != null) {
hub.setSpanContext(throwable, this, this.transaction.getName());
}
if (spanFinishedCallback != null) {
spanFinishedCallback.execute(this);
}
this.endNanos = endNanos == null ? System.nanoTime() : endNanos;
}
@Override
public void setOperation(final @NotNull String operation) {
if (finished.get()) {
return;
}
this.context.setOperation(operation);
}
@Override
public @NotNull String getOperation() {
return this.context.getOperation();
}
@Override
public void setDescription(final @Nullable String description) {
if (finished.get()) {
return;
}
this.context.setDescription(description);
}
@Override
public @Nullable String getDescription() {
return this.context.getDescription();
}
@Override
public void setStatus(final @Nullable SpanStatus status) {
if (finished.get()) {
return;
}
this.context.setStatus(status);
}
@Override
public @Nullable SpanStatus getStatus() {
return this.context.getStatus();
}
@Override
public @NotNull SpanContext getSpanContext() {
return context;
}
@Override
public void setTag(final @NotNull String key, final @NotNull String value) {
if (finished.get()) {
return;
}
this.context.setTag(key, value);
}
@Override
public @Nullable String getTag(@NotNull String key) {
return context.getTags().get(key);
}
@Override
public boolean isFinished() {
return finished.get();
}
public @NotNull Map<String, Object> getData() {
return data;
}
public @Nullable Boolean isSampled() {
return context.getSampled();
}
@Override
public void setThrowable(final @Nullable Throwable throwable) {
if (finished.get()) {
return;
}
this.throwable = throwable;
}
@Override
public @Nullable Throwable getThrowable() {
return throwable;
}
@NotNull
public SentryId getTraceId() {
return context.getTraceId();
}
public @NotNull SpanId getSpanId() {
return context.getSpanId();
}
public @Nullable SpanId getParentSpanId() {
return context.getParentSpanId();
}
public Map<String, String> getTags() {
return context.getTags();
}
@Override
public void setData(@NotNull String key, @NotNull Object value) {
if (finished.get()) {
return;
}
data.put(key, value);
}
@Override
public @Nullable Object getData(@NotNull String key) {
return data.get(key);
}
@Nullable
Long getEndNanos() {
return endNanos;
}
void setSpanFinishedCallback(final @Nullable SpanFinishedCallback callback) {
this.spanFinishedCallback = callback;
}
public @Nullable Double getHighPrecisionTimestamp() {
return getHighPrecisionTimestamp(endNanos);
}
/**
* Returns high precision span finish time represented as {@link Double}.
*
* @return high precision span finish time
*/
@SuppressWarnings("JavaUtilDate")
@Nullable
Double getHighPrecisionTimestamp(final @Nullable Long endNanos) {
final Double duration = getDurationInMillis(endNanos);
if (duration != null) {
return DateUtils.millisToSeconds(startTimestamp.getTime() + duration);
} else if (timestamp != null) {
return timestamp;
} else {
return null;
}
}
/**
* Returns span duration in milliseconds or {@code null} when {@link #startNanos} is not set.
*
* @return span duration in milliseconds
*/
private @Nullable Double getDurationInMillis(final @Nullable Long endNanos) {
if (startNanos != null && endNanos != null) {
return DateUtils.nanosToMillis(endNanos - startNanos);
} else {
return null;
}
}
}