-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
Hub.java
661 lines (609 loc) · 20.6 KB
/
Hub.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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
package io.sentry;
import io.sentry.Stack.StackItem;
import io.sentry.hints.SessionEndHint;
import io.sentry.hints.SessionStartHint;
import io.sentry.protocol.SentryId;
import io.sentry.protocol.SentryTransaction;
import io.sentry.protocol.User;
import io.sentry.util.Objects;
import io.sentry.util.Pair;
import java.io.Closeable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class Hub implements IHub {
private volatile @NotNull SentryId lastEventId;
private final @NotNull SentryOptions options;
private volatile boolean isEnabled;
private final @NotNull Stack stack;
private final @NotNull TracesSampler tracesSampler;
private final @NotNull Map<Throwable, Pair<ISpan, String>> throwableToSpan =
Collections.synchronizedMap(new WeakHashMap<>());
public Hub(final @NotNull SentryOptions options) {
this(options, createRootStackItem(options));
// Integrations are no longer registered on Hub ctor, but on Sentry.init
}
private Hub(final @NotNull SentryOptions options, final @NotNull Stack stack) {
validateOptions(options);
this.options = options;
this.tracesSampler = new TracesSampler(options);
this.stack = stack;
this.lastEventId = SentryId.EMPTY_ID;
// Integrations will use this Hub instance once registered.
// Make sure Hub ready to be used then.
this.isEnabled = true;
}
private Hub(final @NotNull SentryOptions options, final @NotNull StackItem rootStackItem) {
this(options, new Stack(options.getLogger(), rootStackItem));
}
private static void validateOptions(final @NotNull SentryOptions options) {
Objects.requireNonNull(options, "SentryOptions is required.");
if (options.getDsn() == null || options.getDsn().isEmpty()) {
throw new IllegalArgumentException(
"Hub requires a DSN to be instantiated. Considering using the NoOpHub is no DSN is available.");
}
}
private static StackItem createRootStackItem(final @NotNull SentryOptions options) {
validateOptions(options);
final Scope scope = new Scope(options);
final ISentryClient client = new SentryClient(options);
return new StackItem(options, client, scope);
}
@Override
public boolean isEnabled() {
return isEnabled;
}
@Override
public @NotNull SentryId captureEvent(
final @NotNull SentryEvent event, final @Nullable Object hint) {
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING, "Instance is disabled and this 'captureEvent' call is a no-op.");
} else if (event == null) {
options.getLogger().log(SentryLevel.WARNING, "captureEvent called with null parameter.");
} else {
try {
assignTraceContext(event);
final StackItem item = stack.peek();
sentryId = item.getClient().captureEvent(event, item.getScope(), hint);
} catch (Exception e) {
options
.getLogger()
.log(
SentryLevel.ERROR, "Error while capturing event with id: " + event.getEventId(), e);
}
}
this.lastEventId = sentryId;
return sentryId;
}
@Override
public @NotNull SentryId captureMessage(
final @NotNull String message, final @NotNull SentryLevel level) {
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'captureMessage' call is a no-op.");
} else if (message == null) {
options.getLogger().log(SentryLevel.WARNING, "captureMessage called with null parameter.");
} else {
try {
final StackItem item = stack.peek();
sentryId = item.getClient().captureMessage(message, level, item.getScope());
} catch (Exception e) {
options.getLogger().log(SentryLevel.ERROR, "Error while capturing message: " + message, e);
}
}
this.lastEventId = sentryId;
return sentryId;
}
@ApiStatus.Internal
@Override
public SentryId captureEnvelope(
final @NotNull SentryEnvelope envelope, final @Nullable Object hint) {
Objects.requireNonNull(envelope, "SentryEnvelope is required.");
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'captureEnvelope' call is a no-op.");
} else {
try {
sentryId = stack.peek().getClient().captureEnvelope(envelope, hint);
} catch (Exception e) {
options.getLogger().log(SentryLevel.ERROR, "Error while capturing envelope.", e);
}
}
this.lastEventId = sentryId;
return sentryId;
}
@Override
public @NotNull SentryId captureException(
final @NotNull Throwable throwable, final @Nullable Object hint) {
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'captureException' call is a no-op.");
} else if (throwable == null) {
options.getLogger().log(SentryLevel.WARNING, "captureException called with null parameter.");
} else {
try {
final StackItem item = stack.peek();
final SentryEvent event = new SentryEvent(throwable);
assignTraceContext(event);
sentryId = item.getClient().captureEvent(event, item.getScope(), hint);
} catch (Exception e) {
options
.getLogger()
.log(
SentryLevel.ERROR, "Error while capturing exception: " + throwable.getMessage(), e);
}
}
this.lastEventId = sentryId;
return sentryId;
}
private void assignTraceContext(final @NotNull SentryEvent event) {
if (event.getThrowable() != null) {
final Pair<ISpan, String> pair = throwableToSpan.get(event.getThrowable());
if (pair != null) {
if (event.getContexts().getTrace() == null) {
event.getContexts().setTrace(pair.getFirst().getSpanContext());
}
if (event.getTransaction() == null) {
event.setTransaction(pair.getSecond());
}
}
}
}
@Override
public void captureUserFeedback(UserFeedback userFeedback) {
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'captureUserFeedback' call is a no-op.");
} else {
try {
final StackItem item = stack.peek();
item.getClient().captureUserFeedback(userFeedback);
} catch (Exception e) {
options
.getLogger()
.log(
SentryLevel.ERROR,
"Error while capturing captureUserFeedback: " + userFeedback.toString(),
e);
}
}
}
@Override
public void startSession() {
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING, "Instance is disabled and this 'startSession' call is a no-op.");
} else {
final StackItem item = this.stack.peek();
final Scope.SessionPair pair = item.getScope().startSession();
// TODO: add helper overload `captureSessions` to pass a list of sessions and submit a
// single envelope
// Or create the envelope here with both items and call `captureEnvelope`
if (pair.getPrevious() != null) {
item.getClient().captureSession(pair.getPrevious(), new SessionEndHint());
}
item.getClient().captureSession(pair.getCurrent(), new SessionStartHint());
}
}
@Override
public void endSession() {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'endSession' call is a no-op.");
} else {
final StackItem item = this.stack.peek();
final Session previousSession = item.getScope().endSession();
if (previousSession != null) {
item.getClient().captureSession(previousSession, new SessionEndHint());
}
}
}
@Override
public void close() {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'close' call is a no-op.");
} else {
try {
for (Integration integration : options.getIntegrations()) {
if (integration instanceof Closeable) {
((Closeable) integration).close();
}
}
options.getExecutorService().close(options.getShutdownTimeout());
// Close the top-most client
final StackItem item = stack.peek();
// TODO: should we end session before closing client?
item.getClient().close();
} catch (Exception e) {
options.getLogger().log(SentryLevel.ERROR, "Error while closing the Hub.", e);
}
isEnabled = false;
}
}
@Override
public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable Object hint) {
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'addBreadcrumb' call is a no-op.");
} else if (breadcrumb == null) {
options.getLogger().log(SentryLevel.WARNING, "addBreadcrumb called with null parameter.");
} else {
stack.peek().getScope().addBreadcrumb(breadcrumb, hint);
}
}
@Override
public void setLevel(final @Nullable SentryLevel level) {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'setLevel' call is a no-op.");
} else {
stack.peek().getScope().setLevel(level);
}
}
@Override
public void setTransaction(final @Nullable String transaction) {
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'setTransaction' call is a no-op.");
} else {
stack.peek().getScope().setTransaction(transaction);
}
}
@Override
public void setUser(final @Nullable User user) {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'setUser' call is a no-op.");
} else {
stack.peek().getScope().setUser(user);
}
}
@Override
public void setFingerprint(final @NotNull List<String> fingerprint) {
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'setFingerprint' call is a no-op.");
} else if (fingerprint == null) {
options.getLogger().log(SentryLevel.WARNING, "setFingerprint called with null parameter.");
} else {
stack.peek().getScope().setFingerprint(fingerprint);
}
}
@Override
public void clearBreadcrumbs() {
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'clearBreadcrumbs' call is a no-op.");
} else {
stack.peek().getScope().clearBreadcrumbs();
}
}
@Override
public void setTag(final @NotNull String key, final @NotNull String value) {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'setTag' call is a no-op.");
} else if (key == null || value == null) {
options.getLogger().log(SentryLevel.WARNING, "setTag called with null parameter.");
} else {
stack.peek().getScope().setTag(key, value);
}
}
@Override
public void removeTag(final @NotNull String key) {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'removeTag' call is a no-op.");
} else if (key == null) {
options.getLogger().log(SentryLevel.WARNING, "removeTag called with null parameter.");
} else {
stack.peek().getScope().removeTag(key);
}
}
@Override
public void setExtra(final @NotNull String key, final @NotNull String value) {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'setExtra' call is a no-op.");
} else if (key == null || value == null) {
options.getLogger().log(SentryLevel.WARNING, "setExtra called with null parameter.");
} else {
stack.peek().getScope().setExtra(key, value);
}
}
@Override
public void removeExtra(final @NotNull String key) {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'removeExtra' call is a no-op.");
} else if (key == null) {
options.getLogger().log(SentryLevel.WARNING, "removeExtra called with null parameter.");
} else {
stack.peek().getScope().removeExtra(key);
}
}
@Override
public @NotNull SentryId getLastEventId() {
return lastEventId;
}
@Override
public void pushScope() {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'pushScope' call is a no-op.");
} else {
final StackItem item = stack.peek();
Scope clone = null;
try {
clone = item.getScope().clone();
} catch (CloneNotSupportedException e) {
options.getLogger().log(SentryLevel.ERROR, "An error has occurred when cloning a Scope", e);
}
if (clone != null) {
final StackItem newItem = new StackItem(options, item.getClient(), clone);
stack.push(newItem);
}
}
}
@Override
public @NotNull SentryOptions getOptions() {
return this.stack.peek().getOptions();
}
@Override
public void popScope() {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'popScope' call is a no-op.");
} else {
stack.pop();
}
}
@Override
public void withScope(final @NotNull ScopeCallback callback) {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'withScope' call is a no-op.");
} else {
pushScope();
try {
callback.run(stack.peek().getScope());
} catch (Exception e) {
options.getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e);
}
popScope();
}
}
@Override
public void configureScope(final @NotNull ScopeCallback callback) {
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'configureScope' call is a no-op.");
} else {
try {
callback.run(stack.peek().getScope());
} catch (Exception e) {
options.getLogger().log(SentryLevel.ERROR, "Error in the 'configureScope' callback.", e);
}
}
}
@Override
public void bindClient(final @NotNull ISentryClient client) {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'bindClient' call is a no-op.");
} else {
final StackItem item = stack.peek();
if (client != null) {
options.getLogger().log(SentryLevel.DEBUG, "New client bound to scope.");
item.setClient(client);
} else {
options.getLogger().log(SentryLevel.DEBUG, "NoOp client bound to scope.");
item.setClient(NoOpSentryClient.getInstance());
}
}
}
@Override
public void flush(long timeoutMillis) {
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'flush' call is a no-op.");
} else {
try {
stack.peek().getClient().flush(timeoutMillis);
} catch (Exception e) {
options.getLogger().log(SentryLevel.ERROR, "Error in the 'client.flush'.", e);
}
}
}
@Override
public @NotNull IHub clone() {
if (!isEnabled()) {
options.getLogger().log(SentryLevel.WARNING, "Disabled Hub cloned.");
}
// Clone will be invoked in parallel
return new Hub(this.options, new Stack(this.stack));
}
@ApiStatus.Internal
@Override
public @NotNull SentryId captureTransaction(
final @NotNull SentryTransaction transaction, final @Nullable Object hint) {
Objects.requireNonNull(transaction, "transaction is required");
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'captureTransaction' call is a no-op.");
} else {
if (!transaction.isFinished()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Capturing unfinished transaction: %s",
transaction.getEventId());
}
if (!Boolean.TRUE.equals(transaction.isSampled())) {
options
.getLogger()
.log(
SentryLevel.DEBUG,
"Transaction %s was dropped due to sampling decision.",
transaction.getEventId());
} else {
StackItem item = null;
try {
item = stack.peek();
sentryId = item.getClient().captureTransaction(transaction, item.getScope(), hint);
} catch (Exception e) {
options
.getLogger()
.log(
SentryLevel.ERROR,
"Error while capturing transaction with id: " + transaction.getEventId(),
e);
}
}
}
this.lastEventId = sentryId;
return sentryId;
}
@Override
public @NotNull ITransaction startTransaction(
final @NotNull TransactionContext transactionContext,
final @Nullable CustomSamplingContext customSamplingContext,
final boolean bindToScope) {
Objects.requireNonNull(transactionContext, "transactionContext is required");
ITransaction transaction;
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'startTransaction' returns a no-op.");
transaction = NoOpTransaction.getInstance();
} else if (!options.isTracingEnabled()) {
options
.getLogger()
.log(
SentryLevel.INFO, "Tracing is disabled and this 'startTransaction' returns a no-op.");
transaction = NoOpTransaction.getInstance();
} else {
final SamplingContext samplingContext =
new SamplingContext(transactionContext, customSamplingContext);
boolean samplingDecision = tracesSampler.sample(samplingContext);
transactionContext.setSampled(samplingDecision);
transaction = new SentryTracer(transactionContext, this);
}
if (bindToScope) {
configureScope(scope -> scope.setTransaction(transaction));
}
return transaction;
}
@Override
public @Nullable SentryTraceHeader traceHeaders() {
SentryTraceHeader traceHeader = null;
if (!isEnabled()) {
options
.getLogger()
.log(
SentryLevel.WARNING, "Instance is disabled and this 'traceHeaders' call is a no-op.");
} else {
final ISpan span = stack.peek().getScope().getSpan();
if (span != null) {
traceHeader = span.toSentryTrace();
}
}
return traceHeader;
}
@Override
public @Nullable ISpan getSpan() {
ISpan span = null;
if (!isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'getSpan' call is a no-op.");
} else {
span = stack.peek().getScope().getSpan();
}
return span;
}
@Override
@ApiStatus.Internal
public void setSpanContext(
final @NotNull Throwable throwable,
final @NotNull ISpan span,
final @NotNull String transactionName) {
Objects.requireNonNull(throwable, "throwable is required");
Objects.requireNonNull(span, "span is required");
Objects.requireNonNull(transactionName, "transactionName is required");
// the most inner span should be assigned to a throwable
if (!throwableToSpan.containsKey(throwable)) {
throwableToSpan.put(throwable, new Pair<>(span, transactionName));
}
}
@Nullable
SpanContext getSpanContext(final @NotNull Throwable throwable) {
Objects.requireNonNull(throwable, "throwable is required");
final Pair<ISpan, String> span = this.throwableToSpan.get(throwable);
if (span != null) {
return span.getFirst().getSpanContext();
}
return null;
}
}