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
181 lines (146 loc) · 4.61 KB
/
Span.java
File metadata and controls
181 lines (146 loc) · 4.61 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
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.atomic.AtomicBoolean;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@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 ended. */
private @Nullable Date 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);
Span(
final @NotNull SentryId traceId,
final @Nullable SpanId parentSpanId,
final @NotNull SentryTracer transaction,
final @NotNull String operation,
final @NotNull IHub hub) {
this.context =
new SpanContext(traceId, new SpanId(), operation, parentSpanId, transaction.isSampled());
this.transaction = Objects.requireNonNull(transaction, "transaction is required");
this.startTimestamp = DateUtils.getCurrentDateTime();
this.hub = Objects.requireNonNull(hub, "hub is required");
}
Span(
final @NotNull TransactionContext context,
final @NotNull SentryTracer sentryTracer,
final @NotNull IHub hub) {
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.startTimestamp = DateUtils.getCurrentDateTime();
}
public @NotNull Date getStartTimestamp() {
return startTimestamp;
}
public @Nullable Date getTimestamp() {
return timestamp;
}
@Override
public @NotNull ISpan startChild(final @NotNull String operation) {
return this.startChild(operation, null);
}
@Override
public @NotNull ISpan startChild(
final @NotNull String operation, final @Nullable String description) {
return transaction.startChild(context.getSpanId(), operation, description);
}
@Override
public @NotNull SentryTraceHeader toSentryTrace() {
return new SentryTraceHeader(context.getTraceId(), context.getSpanId(), context.getSampled());
}
@Override
public void finish() {
this.finish(this.context.getStatus());
}
@Override
public void finish(@Nullable SpanStatus status) {
// the span can be finished only once
if (!finished.compareAndSet(false, true)) {
return;
}
this.context.setStatus(status);
timestamp = DateUtils.getCurrentDateTime();
if (throwable != null) {
hub.setSpanContext(throwable, this);
}
}
@Override
public void setOperation(final @NotNull String operation) {
this.context.setOperation(operation);
}
@Override
public @NotNull String getOperation() {
return this.context.getOperation();
}
@Override
public void setDescription(final @Nullable String description) {
this.context.setDescription(description);
}
@Override
public @Nullable String getDescription() {
return this.context.getDescription();
}
@Override
public void setStatus(final @Nullable SpanStatus status) {
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) {
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 @Nullable Boolean isSampled() {
return context.getSampled();
}
@Override
public void setThrowable(final @Nullable Throwable throwable) {
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();
}
}