99import  io .sentry .protocol .SentryTransaction ;
1010import  io .sentry .protocol .User ;
1111import  io .sentry .transport .RateLimiter ;
12- import  io .sentry .util .ExceptionUtils ;
1312import  io .sentry .util .HintUtils ;
1413import  io .sentry .util .Objects ;
15- import  io .sentry .util .Pair ;
1614import  io .sentry .util .TracingUtils ;
1715import  java .io .Closeable ;
1816import  java .io .IOException ;
19- import  java .lang .ref .WeakReference ;
2017import  java .util .Collections ;
2118import  java .util .HashMap ;
2219import  java .util .List ;
2320import  java .util .Map ;
24- import  java .util .WeakHashMap ;
2521import  org .jetbrains .annotations .ApiStatus ;
2622import  org .jetbrains .annotations .NotNull ;
2723import  org .jetbrains .annotations .Nullable ;
@@ -40,10 +36,6 @@ public final class Scopes implements IScopes, MetricsApi.IMetricsInterface {
4036  private  final  @ NotNull  SentryOptions  options ;
4137  private  volatile  boolean  isEnabled ;
4238  private  final  @ NotNull  TracesSampler  tracesSampler ;
43- 
44-   // TODO should this go on global scope? 
45-   private  final  @ NotNull  Map <Throwable , Pair <WeakReference <ISpan >, String >> throwableToSpan  =
46-       Collections .synchronizedMap (new  WeakHashMap <>());
4739  private  final  @ NotNull  TransactionPerformanceCollector  transactionPerformanceCollector ;
4840  private  final  @ NotNull  MetricsApi  metricsApi ;
4941
@@ -120,7 +112,10 @@ public boolean isAncestorOf(final @Nullable Scopes otherScopes) {
120112
121113  // TODO add to IScopes interface 
122114  public  @ NotNull  Scopes  forkedCurrentScope (final  @ NotNull  String  creator ) {
123-     return  new  Scopes (scope .clone (), isolationScope , this , options , creator );
115+     IScope  clone  = scope .clone ();
116+     // TODO should use isolation scope 
117+     //    return new Scopes(clone, isolationScope, this, options, creator); 
118+     return  new  Scopes (clone , clone , this , options , creator );
124119  }
125120
126121  //  // TODO in Sentry.init? 
@@ -180,23 +175,7 @@ public boolean isEnabled() {
180175  }
181176
182177  private  void  assignTraceContext (final  @ NotNull  SentryEvent  event ) {
183-     if  (options .isTracingEnabled () && event .getThrowable () != null ) {
184-       final  Pair <WeakReference <ISpan >, String > pair  =
185-           throwableToSpan .get (ExceptionUtils .findRootCause (event .getThrowable ()));
186-       if  (pair  != null ) {
187-         final  WeakReference <ISpan > spanWeakRef  = pair .getFirst ();
188-         if  (event .getContexts ().getTrace () == null  && spanWeakRef  != null ) {
189-           final  ISpan  span  = spanWeakRef .get ();
190-           if  (span  != null ) {
191-             event .getContexts ().setTrace (span .getSpanContext ());
192-           }
193-         }
194-         final  String  transactionName  = pair .getSecond ();
195-         if  (event .getTransaction () == null  && transactionName  != null ) {
196-           event .setTransaction (transactionName );
197-         }
198-       }
199-     }
178+     Sentry .getGlobalScope ().assignTraceContext (event );
200179  }
201180
202181  private  IScope  buildLocalScope (
@@ -691,10 +670,10 @@ public void bindClient(final @NotNull ISentryClient client) {
691670    } else  {
692671      if  (client  != null ) {
693672        options .getLogger ().log (SentryLevel .DEBUG , "New client bound to scope." );
694-         getDefaultWriteScope ().setClient (client );
673+         getDefaultWriteScope ().bindClient (client );
695674      } else  {
696675        options .getLogger ().log (SentryLevel .DEBUG , "NoOp client bound to scope." );
697-         getDefaultWriteScope ().setClient (NoOpSentryClient .getInstance ());
676+         getDefaultWriteScope ().bindClient (NoOpSentryClient .getInstance ());
698677      }
699678    }
700679  }
@@ -871,34 +850,26 @@ public void setSpanContext(
871850      final  @ NotNull  Throwable  throwable ,
872851      final  @ NotNull  ISpan  span ,
873852      final  @ NotNull  String  transactionName ) {
874-     Objects .requireNonNull (throwable , "throwable is required" );
875-     Objects .requireNonNull (span , "span is required" );
876-     Objects .requireNonNull (transactionName , "transactionName is required" );
877-     // to match any cause, span context is always attached to the root cause of the exception 
878-     final  Throwable  rootCause  = ExceptionUtils .findRootCause (throwable );
879-     // the most inner span should be assigned to a throwable 
880-     if  (!throwableToSpan .containsKey (rootCause )) {
881-       throwableToSpan .put (rootCause , new  Pair <>(new  WeakReference <>(span ), transactionName ));
882-     }
883-   }
884- 
885-   // TODO this seems unused 
886-   @ Nullable 
887-   SpanContext  getSpanContext (final  @ NotNull  Throwable  throwable ) {
888-     Objects .requireNonNull (throwable , "throwable is required" );
889-     final  Throwable  rootCause  = ExceptionUtils .findRootCause (throwable );
890-     final  Pair <WeakReference <ISpan >, String > pair  = this .throwableToSpan .get (rootCause );
891-     if  (pair  != null ) {
892-       final  WeakReference <ISpan > spanWeakRef  = pair .getFirst ();
893-       if  (spanWeakRef  != null ) {
894-         final  ISpan  span  = spanWeakRef .get ();
895-         if  (span  != null ) {
896-           return  span .getSpanContext ();
897-         }
898-       }
899-     }
900-     return  null ;
901-   }
853+     Sentry .getGlobalScope ().setSpanContext (throwable , span , transactionName );
854+   }
855+ 
856+   //  // TODO this seems unused 
857+   //  @Nullable 
858+   //  SpanContext getSpanContext(final @NotNull Throwable throwable) { 
859+   //    Objects.requireNonNull(throwable, "throwable is required"); 
860+   //    final Throwable rootCause = ExceptionUtils.findRootCause(throwable); 
861+   //    final Pair<WeakReference<ISpan>, String> pair = this.throwableToSpan.get(rootCause); 
862+   //    if (pair != null) { 
863+   //      final WeakReference<ISpan> spanWeakRef = pair.getFirst(); 
864+   //      if (spanWeakRef != null) { 
865+   //        final ISpan span = spanWeakRef.get(); 
866+   //        if (span != null) { 
867+   //          return span.getSpanContext(); 
868+   //        } 
869+   //      } 
870+   //    } 
871+   //    return null; 
872+   //  } 
902873
903874  @ Override 
904875  public  @ Nullable  ISpan  getSpan () {
0 commit comments