-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathScopes.java
More file actions
1173 lines (1062 loc) · 37.5 KB
/
Scopes.java
File metadata and controls
1173 lines (1062 loc) · 37.5 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
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package io.sentry;
import io.sentry.clientreport.DiscardReason;
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.transport.RateLimiter;
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import io.sentry.util.SpanUtils;
import io.sentry.util.TracingUtils;
import java.io.Closeable;
import java.util.List;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class Scopes implements IScopes {
private final @NotNull IScope scope;
private final @NotNull IScope isolationScope;
private final @NotNull IScope globalScope;
private final @Nullable Scopes parentScopes;
private final @NotNull String creator;
private final @NotNull CompositePerformanceCollector compositePerformanceCollector;
private final @NotNull CombinedScopeView combinedScope;
public Scopes(
final @NotNull IScope scope,
final @NotNull IScope isolationScope,
final @NotNull IScope globalScope,
final @NotNull String creator) {
this(scope, isolationScope, globalScope, null, creator);
}
private Scopes(
final @NotNull IScope scope,
final @NotNull IScope isolationScope,
final @NotNull IScope globalScope,
final @Nullable Scopes parentScopes,
final @NotNull String creator) {
this.combinedScope = new CombinedScopeView(globalScope, isolationScope, scope);
this.scope = scope;
this.isolationScope = isolationScope;
this.globalScope = globalScope;
this.parentScopes = parentScopes;
this.creator = creator;
final @NotNull SentryOptions options = getOptions();
validateOptions(options);
this.compositePerformanceCollector = options.getCompositePerformanceCollector();
}
public @NotNull String getCreator() {
return creator;
}
@Override
@ApiStatus.Internal
public @NotNull IScope getScope() {
return scope;
}
@Override
@ApiStatus.Internal
public @NotNull IScope getIsolationScope() {
return isolationScope;
}
@Override
@ApiStatus.Internal
public @NotNull IScope getGlobalScope() {
return globalScope;
}
@Override
@ApiStatus.Internal
public @Nullable IScopes getParentScopes() {
return parentScopes;
}
@Override
@ApiStatus.Internal
public boolean isAncestorOf(final @Nullable IScopes otherScopes) {
if (otherScopes == null) {
return false;
}
if (this == otherScopes) {
return true;
}
if (otherScopes.getParentScopes() != null) {
return isAncestorOf(otherScopes.getParentScopes());
}
return false;
}
@Override
public @NotNull IScopes forkedScopes(final @NotNull String creator) {
return new Scopes(scope.clone(), isolationScope.clone(), globalScope, this, creator);
}
@Override
public @NotNull IScopes forkedCurrentScope(final @NotNull String creator) {
return new Scopes(scope.clone(), isolationScope, globalScope, this, creator);
}
@Override
public @NotNull IScopes forkedRootScopes(final @NotNull String creator) {
return Sentry.forkedRootScopes(creator);
}
@Override
public boolean isEnabled() {
return getClient().isEnabled();
}
@Override
public @NotNull SentryId captureEvent(@NotNull SentryEvent event, @Nullable Hint hint) {
return captureEventInternal(event, hint, null);
}
@Override
public @NotNull SentryId captureEvent(
@NotNull SentryEvent event, @Nullable Hint hint, @NotNull ScopeCallback callback) {
return captureEventInternal(event, hint, callback);
}
private @NotNull SentryId captureEventInternal(
final @NotNull SentryEvent event,
final @Nullable Hint hint,
final @Nullable ScopeCallback scopeCallback) {
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING, "Instance is disabled and this 'captureEvent' call is a no-op.");
} else if (event == null) {
getOptions().getLogger().log(SentryLevel.WARNING, "captureEvent called with null parameter.");
} else {
try {
assignTraceContext(event);
final IScope localScope = buildLocalScope(getCombinedScopeView(), scopeCallback);
sentryId = getClient().captureEvent(event, localScope, hint);
updateLastEventId(sentryId);
} catch (Throwable e) {
getOptions()
.getLogger()
.log(
SentryLevel.ERROR, "Error while capturing event with id: " + event.getEventId(), e);
}
}
return sentryId;
}
private @NotNull ISentryClient getClient() {
return getCombinedScopeView().getClient();
}
private void assignTraceContext(final @NotNull SentryEvent event) {
getCombinedScopeView().assignTraceContext(event);
}
private IScope buildLocalScope(
final @NotNull IScope parentScope, final @Nullable ScopeCallback callback) {
if (callback != null) {
try {
final IScope localScope = parentScope.clone();
callback.run(localScope);
return localScope;
} catch (Throwable t) {
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error in the 'ScopeCallback' callback.", t);
}
}
return parentScope;
}
@Override
public @NotNull SentryId captureMessage(
final @NotNull String message, final @NotNull SentryLevel level) {
return captureMessageInternal(message, level, null);
}
@Override
public @NotNull SentryId captureMessage(
final @NotNull String message,
final @NotNull SentryLevel level,
final @NotNull ScopeCallback callback) {
return captureMessageInternal(message, level, callback);
}
private @NotNull SentryId captureMessageInternal(
final @NotNull String message,
final @NotNull SentryLevel level,
final @Nullable ScopeCallback scopeCallback) {
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'captureMessage' call is a no-op.");
} else if (message == null) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "captureMessage called with null parameter.");
} else {
try {
final IScope localScope = buildLocalScope(getCombinedScopeView(), scopeCallback);
sentryId = getClient().captureMessage(message, level, localScope);
} catch (Throwable e) {
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error while capturing message: " + message, e);
}
}
updateLastEventId(sentryId);
return sentryId;
}
@ApiStatus.Internal
@Override
public @NotNull SentryId captureEnvelope(
final @NotNull SentryEnvelope envelope, final @Nullable Hint hint) {
Objects.requireNonNull(envelope, "SentryEnvelope is required.");
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'captureEnvelope' call is a no-op.");
} else {
try {
final SentryId capturedEnvelopeId = getClient().captureEnvelope(envelope, hint);
if (capturedEnvelopeId != null) {
sentryId = capturedEnvelopeId;
}
} catch (Throwable e) {
getOptions().getLogger().log(SentryLevel.ERROR, "Error while capturing envelope.", e);
}
}
return sentryId;
}
@Override
public @NotNull SentryId captureException(
final @NotNull Throwable throwable, final @Nullable Hint hint) {
return captureExceptionInternal(throwable, hint, null);
}
@Override
public @NotNull SentryId captureException(
final @NotNull Throwable throwable,
final @Nullable Hint hint,
final @NotNull ScopeCallback callback) {
return captureExceptionInternal(throwable, hint, callback);
}
private @NotNull SentryId captureExceptionInternal(
final @NotNull Throwable throwable,
final @Nullable Hint hint,
final @Nullable ScopeCallback scopeCallback) {
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'captureException' call is a no-op.");
} else if (throwable == null) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "captureException called with null parameter.");
} else {
try {
final SentryEvent event = new SentryEvent(throwable);
assignTraceContext(event);
final IScope localScope = buildLocalScope(getCombinedScopeView(), scopeCallback);
sentryId = getClient().captureEvent(event, localScope, hint);
} catch (Throwable e) {
getOptions()
.getLogger()
.log(
SentryLevel.ERROR, "Error while capturing exception: " + throwable.getMessage(), e);
}
}
updateLastEventId(sentryId);
return sentryId;
}
@Override
public void captureUserFeedback(final @NotNull UserFeedback userFeedback) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'captureUserFeedback' call is a no-op.");
} else {
try {
getClient().captureUserFeedback(userFeedback);
} catch (Throwable e) {
getOptions()
.getLogger()
.log(
SentryLevel.ERROR,
"Error while capturing captureUserFeedback: " + userFeedback.toString(),
e);
}
}
}
@Override
public void startSession() {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING, "Instance is disabled and this 'startSession' call is a no-op.");
} else {
final Scope.SessionPair pair = getCombinedScopeView().startSession();
if (pair != null) {
// 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) {
final Hint hint = HintUtils.createWithTypeCheckHint(new SessionEndHint());
getClient().captureSession(pair.getPrevious(), hint);
}
final Hint hint = HintUtils.createWithTypeCheckHint(new SessionStartHint());
getClient().captureSession(pair.getCurrent(), hint);
} else {
getOptions().getLogger().log(SentryLevel.WARNING, "Session could not be started.");
}
}
}
@Override
public void endSession() {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'endSession' call is a no-op.");
} else {
final Session previousSession = getCombinedScopeView().endSession();
if (previousSession != null) {
final Hint hint = HintUtils.createWithTypeCheckHint(new SessionEndHint());
getClient().captureSession(previousSession, hint);
}
}
}
private IScope getCombinedScopeView() {
return combinedScope;
}
@Override
public void close() {
close(false);
}
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void close(final boolean isRestarting) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'close' call is a no-op.");
} else {
try {
for (Integration integration : getOptions().getIntegrations()) {
if (integration instanceof Closeable) {
try {
((Closeable) integration).close();
} catch (Throwable e) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Failed to close the integration {}.", integration, e);
}
}
}
configureScope(scope -> scope.clear());
configureScope(ScopeType.ISOLATION, scope -> scope.clear());
getOptions().getBackpressureMonitor().close();
getOptions().getTransactionProfiler().close();
getOptions().getContinuousProfiler().close();
getOptions().getCompositePerformanceCollector().close();
final @NotNull ISentryExecutorService executorService = getOptions().getExecutorService();
if (isRestarting) {
executorService.submit(
() -> executorService.close(getOptions().getShutdownTimeoutMillis()));
} else {
executorService.close(getOptions().getShutdownTimeoutMillis());
}
// TODO: should we end session before closing client?
configureScope(ScopeType.CURRENT, scope -> scope.getClient().close(isRestarting));
configureScope(ScopeType.ISOLATION, scope -> scope.getClient().close(isRestarting));
configureScope(ScopeType.GLOBAL, scope -> scope.getClient().close(isRestarting));
} catch (Throwable e) {
getOptions().getLogger().log(SentryLevel.ERROR, "Error while closing the Scopes.", e);
}
}
}
@Override
public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable Hint hint) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'addBreadcrumb' call is a no-op.");
} else if (breadcrumb == null) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "addBreadcrumb called with null parameter.");
} else {
getCombinedScopeView().addBreadcrumb(breadcrumb, hint);
}
}
@Override
public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb) {
addBreadcrumb(breadcrumb, new Hint());
}
@Override
public void setLevel(final @Nullable SentryLevel level) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'setLevel' call is a no-op.");
} else {
getCombinedScopeView().setLevel(level);
}
}
@Override
public void setTransaction(final @Nullable String transaction) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'setTransaction' call is a no-op.");
} else if (transaction != null) {
getCombinedScopeView().setTransaction(transaction);
} else {
getOptions().getLogger().log(SentryLevel.WARNING, "Transaction cannot be null");
}
}
@Override
public void setUser(final @Nullable User user) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'setUser' call is a no-op.");
} else {
getCombinedScopeView().setUser(user);
}
}
@Override
public void setFingerprint(final @NotNull List<String> fingerprint) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'setFingerprint' call is a no-op.");
} else if (fingerprint == null) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "setFingerprint called with null parameter.");
} else {
getCombinedScopeView().setFingerprint(fingerprint);
}
}
@Override
public void clearBreadcrumbs() {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'clearBreadcrumbs' call is a no-op.");
} else {
getCombinedScopeView().clearBreadcrumbs();
}
}
@Override
public void setTag(final @Nullable String key, final @Nullable String value) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'setTag' call is a no-op.");
} else if (key == null || value == null) {
getOptions().getLogger().log(SentryLevel.WARNING, "setTag called with null parameter.");
} else {
getCombinedScopeView().setTag(key, value);
}
}
@Override
public void removeTag(final @Nullable String key) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'removeTag' call is a no-op.");
} else if (key == null) {
getOptions().getLogger().log(SentryLevel.WARNING, "removeTag called with null parameter.");
} else {
getCombinedScopeView().removeTag(key);
}
}
@Override
public void setExtra(final @Nullable String key, final @Nullable String value) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'setExtra' call is a no-op.");
} else if (key == null || value == null) {
getOptions().getLogger().log(SentryLevel.WARNING, "setExtra called with null parameter.");
} else {
getCombinedScopeView().setExtra(key, value);
}
}
@Override
public void removeExtra(final @Nullable String key) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'removeExtra' call is a no-op.");
} else if (key == null) {
getOptions().getLogger().log(SentryLevel.WARNING, "removeExtra called with null parameter.");
} else {
getCombinedScopeView().removeExtra(key);
}
}
private void updateLastEventId(final @NotNull SentryId lastEventId) {
getCombinedScopeView().setLastEventId(lastEventId);
}
@Override
public @NotNull SentryId getLastEventId() {
return getCombinedScopeView().getLastEventId();
}
@Override
public ISentryLifecycleToken pushScope() {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'pushScope' call is a no-op.");
return NoOpScopesLifecycleToken.getInstance();
} else {
final @NotNull IScopes scopes = this.forkedCurrentScope("pushScope");
return scopes.makeCurrent();
}
}
@Override
public ISentryLifecycleToken pushIsolationScope() {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'pushIsolationScope' call is a no-op.");
return NoOpScopesLifecycleToken.getInstance();
} else {
final @NotNull IScopes scopes = this.forkedScopes("pushIsolationScope");
return scopes.makeCurrent();
}
}
@Override
public @NotNull ISentryLifecycleToken makeCurrent() {
return Sentry.setCurrentScopes(this);
}
/**
* @deprecated please call {@link ISentryLifecycleToken#close()} on the token returned by {@link
* IScopes#pushScope()} or {@link IScopes#pushIsolationScope()} instead.
*/
@Override
@Deprecated
public void popScope() {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'popScope' call is a no-op.");
} else {
final @Nullable Scopes parent = parentScopes;
if (parent != null) {
parent.makeCurrent();
}
}
}
@Override
public void withScope(final @NotNull ScopeCallback callback) {
if (!isEnabled()) {
try {
callback.run(NoOpScope.getInstance());
} catch (Throwable e) {
getOptions().getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e);
}
} else {
final @NotNull IScopes forkedScopes = forkedCurrentScope("withScope");
try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) {
callback.run(forkedScopes.getScope());
} catch (Throwable e) {
getOptions().getLogger().log(SentryLevel.ERROR, "Error in the 'withScope' callback.", e);
}
}
}
@Override
public void withIsolationScope(final @NotNull ScopeCallback callback) {
if (!isEnabled()) {
try {
callback.run(NoOpScope.getInstance());
} catch (Throwable e) {
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error in the 'withIsolationScope' callback.", e);
}
} else {
final @NotNull IScopes forkedScopes = forkedScopes("withIsolationScope");
try (final @NotNull ISentryLifecycleToken ignored = forkedScopes.makeCurrent()) {
callback.run(forkedScopes.getIsolationScope());
} catch (Throwable e) {
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error in the 'withIsolationScope' callback.", e);
}
}
}
@Override
public void configureScope(
final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'configureScope' call is a no-op.");
} else {
try {
callback.run(combinedScope.getSpecificScope(scopeType));
} catch (Throwable e) {
getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error in the 'configureScope' callback.", e);
}
}
}
@Override
public void bindClient(final @NotNull ISentryClient client) {
if (client != null) {
getOptions().getLogger().log(SentryLevel.DEBUG, "New client bound to scope.");
getCombinedScopeView().bindClient(client);
} else {
getOptions().getLogger().log(SentryLevel.DEBUG, "NoOp client bound to scope.");
getCombinedScopeView().bindClient(NoOpSentryClient.getInstance());
}
}
@Override
public boolean isHealthy() {
return getClient().isHealthy();
}
@Override
public void flush(long timeoutMillis) {
if (!isEnabled()) {
getOptions()
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'flush' call is a no-op.");
} else {
try {
getClient().flush(timeoutMillis);
} catch (Throwable e) {
getOptions().getLogger().log(SentryLevel.ERROR, "Error in the 'client.flush'.", e);
}
}
}
/**
* @deprecated please use {@link IScopes#forkedScopes(String)} or {@link
* IScopes#forkedCurrentScope(String)} instead.
*/
@Override
@Deprecated
@SuppressWarnings("deprecation")
public @NotNull IHub clone() {
if (!isEnabled()) {
getOptions().getLogger().log(SentryLevel.WARNING, "Disabled Scopes cloned.");
}
return new HubScopesWrapper(forkedScopes("scopes clone"));
}
@ApiStatus.Internal
@Override
public @NotNull SentryId captureTransaction(
final @NotNull SentryTransaction transaction,
final @Nullable TraceContext traceContext,
final @Nullable Hint hint,
final @Nullable ProfilingTraceData profilingTraceData) {
Objects.requireNonNull(transaction, "transaction is required");
SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'captureTransaction' call is a no-op.");
} else {
if (!transaction.isFinished()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Transaction: %s is not finished and this 'captureTransaction' call is a no-op.",
transaction.getEventId());
} else {
if (!Boolean.TRUE.equals(transaction.isSampled())) {
getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Transaction %s was dropped due to sampling decision.",
transaction.getEventId());
if (getOptions().getBackpressureMonitor().getDownsampleFactor() > 0) {
getOptions()
.getClientReportRecorder()
.recordLostEvent(DiscardReason.BACKPRESSURE, DataCategory.Transaction);
getOptions()
.getClientReportRecorder()
.recordLostEvent(
DiscardReason.BACKPRESSURE,
DataCategory.Span,
transaction.getSpans().size() + 1);
} else {
getOptions()
.getClientReportRecorder()
.recordLostEvent(DiscardReason.SAMPLE_RATE, DataCategory.Transaction);
getOptions()
.getClientReportRecorder()
.recordLostEvent(
DiscardReason.SAMPLE_RATE,
DataCategory.Span,
transaction.getSpans().size() + 1);
}
} else {
try {
sentryId =
getClient()
.captureTransaction(
transaction,
traceContext,
getCombinedScopeView(),
hint,
profilingTraceData);
} catch (Throwable e) {
getOptions()
.getLogger()
.log(
SentryLevel.ERROR,
"Error while capturing transaction with id: " + transaction.getEventId(),
e);
}
}
}
}
return sentryId;
}
@ApiStatus.Internal
@Override
public @NotNull SentryId captureProfileChunk(
final @NotNull ProfileChunk profilingContinuousData) {
Objects.requireNonNull(profilingContinuousData, "profilingContinuousData is required");
@NotNull SentryId sentryId = SentryId.EMPTY_ID;
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'captureTransaction' call is a no-op.");
} else {
try {
sentryId = getClient().captureProfileChunk(profilingContinuousData, getScope());
} catch (Throwable e) {
getOptions()
.getLogger()
.log(
SentryLevel.ERROR,
"Error while capturing profile chunk with id: "
+ profilingContinuousData.getChunkId(),
e);
}
}
return sentryId;
}
@Override
public @NotNull ITransaction startTransaction(
final @NotNull TransactionContext transactionContext,
final @NotNull TransactionOptions transactionOptions) {
return createTransaction(transactionContext, transactionOptions);
}
private @NotNull ITransaction createTransaction(
final @NotNull TransactionContext transactionContext,
final @NotNull TransactionOptions transactionOptions) {
Objects.requireNonNull(transactionContext, "transactionContext is required");
transactionContext.setOrigin(transactionOptions.getOrigin());
ITransaction transaction;
if (!isEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Instance is disabled and this 'startTransaction' returns a no-op.");
transaction = NoOpTransaction.getInstance();
} else if (SpanUtils.isIgnored(
getOptions().getIgnoredSpanOrigins(), transactionContext.getOrigin())) {
getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Returning no-op for span origin %s as the SDK has been configured to ignore it",
transactionContext.getOrigin());
transaction = NoOpTransaction.getInstance();
} else if (!getOptions().getInstrumenter().equals(transactionContext.getInstrumenter())) {
getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Returning no-op for instrumenter %s as the SDK has been configured to use instrumenter %s",
transactionContext.getInstrumenter(),
getOptions().getInstrumenter());
transaction = NoOpTransaction.getInstance();
} else if (!getOptions().isTracingEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.INFO, "Tracing is disabled and this 'startTransaction' returns a no-op.");
transaction = NoOpTransaction.getInstance();
} else {
final Double sampleRand = getSampleRand(transactionContext);
final SamplingContext samplingContext =
new SamplingContext(
transactionContext, transactionOptions.getCustomSamplingContext(), sampleRand, null);
final @NotNull TracesSampler tracesSampler = getOptions().getInternalTracesSampler();
@NotNull TracesSamplingDecision samplingDecision = tracesSampler.sample(samplingContext);
transactionContext.setSamplingDecision(samplingDecision);
final @Nullable ISpanFactory maybeSpanFactory = transactionOptions.getSpanFactory();
final @NotNull ISpanFactory spanFactory =
maybeSpanFactory == null ? getOptions().getSpanFactory() : maybeSpanFactory;
transaction =
spanFactory.createTransaction(
transactionContext, this, transactionOptions, compositePerformanceCollector);
// new SentryTracer(
// transactionContext, this, transactionOptions,
// compositePerformanceCollector);
// The listener is called only if the transaction exists, as the transaction is needed to
// stop it
if (samplingDecision.getSampled()) {
// If transaction profiler is sampled, let's start it
if (samplingDecision.getProfileSampled()) {
final ITransactionProfiler transactionProfiler = getOptions().getTransactionProfiler();
// If the profiler is not running, we start and bind it here.
if (!transactionProfiler.isRunning()) {
transactionProfiler.start();
transactionProfiler.bindTransaction(transaction);
} else if (transactionOptions.isAppStartTransaction()) {
// If the profiler is running and the current transaction is the app start, we bind it.
transactionProfiler.bindTransaction(transaction);
}
}
// If continuous profiling is enabled in trace mode, let's start it. Profiler will sample on
// its own.
if (getOptions().isContinuousProfilingEnabled()
&& getOptions().getProfileLifecycle() == ProfileLifecycle.TRACE) {
getOptions()
.getContinuousProfiler()
.startProfiler(ProfileLifecycle.TRACE, getOptions().getInternalTracesSampler());
}
}
}
if (transactionOptions.isBindToScope()) {
transaction.makeCurrent();
}
return transaction;
}
private @NotNull Double getSampleRand(final @NotNull TransactionContext transactionContext) {
final @Nullable Baggage baggage = transactionContext.getBaggage();
if (baggage != null) {
final @Nullable Double sampleRandFromBaggageMaybe = baggage.getSampleRand();
if (sampleRandFromBaggageMaybe != null) {
return sampleRandFromBaggageMaybe;
}
}
return getCombinedScopeView().getPropagationContext().getSampleRand();
}
@Override
public void startProfiler() {
if (getOptions().isContinuousProfilingEnabled()) {
if (getOptions().getProfileLifecycle() != ProfileLifecycle.MANUAL) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Profiling lifecycle is %s. Profiling cannot be started manually.",
getOptions().getProfileLifecycle().name());
return;
}
getOptions()
.getContinuousProfiler()
.startProfiler(ProfileLifecycle.MANUAL, getOptions().getInternalTracesSampler());
} else if (getOptions().isProfilingEnabled()) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Continuous Profiling is not enabled. Set profilesSampleRate and profilesSampler to null to enable it.");
}
}
@Override
public void stopProfiler() {
if (getOptions().isContinuousProfilingEnabled()) {
if (getOptions().getProfileLifecycle() != ProfileLifecycle.MANUAL) {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Profiling lifecycle is %s. Profiling cannot be stopped manually.",
getOptions().getProfileLifecycle().name());
return;
}
getOptions().getLogger().log(SentryLevel.DEBUG, "Stopped continuous Profiling.");
getOptions().getContinuousProfiler().stopProfiler(ProfileLifecycle.MANUAL);
} else {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Continuous Profiling is not enabled. Set profilesSampleRate and profilesSampler to null to enable it.");
}
}
@Override