Skip to content

Commit 3d502fb

Browse files
authored
Merge a9dadc4 into 55b103c
2 parents 55b103c + a9dadc4 commit 3d502fb

File tree

15 files changed

+146
-10
lines changed

15 files changed

+146
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Breaking changes:
1515
- Apollo v2 BeforeSpanCallback now allows returning null ([#2890](https://github.com/getsentry/sentry-java/pull/2890))
1616
- Automatic user interaction tracking: every click now starts a new automatic transaction ([#2891](https://github.com/getsentry/sentry-java/pull/2891))
1717
- Previously performing a click on the same UI widget twice would keep the existing transaction running, the new behavior now better aligns with other SDKs
18+
- Android: If global hub mode is enabled (default on Android), Sentry.getSpan() returns the root span instead of the latest span ([#2855](https://github.com/getsentry/sentry-java/pull/2855))
1819

1920
### Fixes
2021

sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpEvent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req
3737
val method: String = request.method
3838

3939
// We start the call span that will contain all the others
40-
callRootSpan = hub.span?.startChild("http.client", "$method $url")
40+
callRootSpan = hub.transaction?.startChild("http.client", "$method $url")
4141
callRootSpan?.spanContext?.origin = TRACE_ORIGIN
4242
urlDetails.applyToSpan(callRootSpan)
4343

sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class SentryOkHttpInterceptor(
7878
isFromEventListener = true
7979
} else {
8080
// read the span from the bound scope
81-
span = hub.span?.startChild("http.client", "$method $url")
81+
span = hub.transaction?.startChild("http.client", "$method $url")
8282
isFromEventListener = false
8383
}
8484

sentry/api/sentry.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ public final class io/sentry/Hub : io/sentry/IHub {
364364
public fun getOptions ()Lio/sentry/SentryOptions;
365365
public fun getSpan ()Lio/sentry/ISpan;
366366
public fun getTraceparent ()Lio/sentry/SentryTraceHeader;
367+
public fun getTransaction ()Lio/sentry/ITransaction;
367368
public fun isCrashedLastRun ()Ljava/lang/Boolean;
368369
public fun isEnabled ()Z
369370
public fun popScope ()V
@@ -411,6 +412,7 @@ public final class io/sentry/HubAdapter : io/sentry/IHub {
411412
public fun getOptions ()Lio/sentry/SentryOptions;
412413
public fun getSpan ()Lio/sentry/ISpan;
413414
public fun getTraceparent ()Lio/sentry/SentryTraceHeader;
415+
public fun getTransaction ()Lio/sentry/ITransaction;
414416
public fun isCrashedLastRun ()Ljava/lang/Boolean;
415417
public fun isEnabled ()Z
416418
public fun popScope ()V
@@ -483,6 +485,7 @@ public abstract interface class io/sentry/IHub {
483485
public abstract fun getOptions ()Lio/sentry/SentryOptions;
484486
public abstract fun getSpan ()Lio/sentry/ISpan;
485487
public abstract fun getTraceparent ()Lio/sentry/SentryTraceHeader;
488+
public abstract fun getTransaction ()Lio/sentry/ITransaction;
486489
public abstract fun isCrashedLastRun ()Ljava/lang/Boolean;
487490
public abstract fun isEnabled ()Z
488491
public abstract fun popScope ()V
@@ -869,6 +872,7 @@ public final class io/sentry/NoOpHub : io/sentry/IHub {
869872
public fun getOptions ()Lio/sentry/SentryOptions;
870873
public fun getSpan ()Lio/sentry/ISpan;
871874
public fun getTraceparent ()Lio/sentry/SentryTraceHeader;
875+
public fun getTransaction ()Lio/sentry/ITransaction;
872876
public fun isCrashedLastRun ()Ljava/lang/Boolean;
873877
public fun isEnabled ()Z
874878
public fun popScope ()V

sentry/src/main/java/io/sentry/Hub.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,22 @@ public void flush(long timeoutMillis) {
762762
return span;
763763
}
764764

765+
@Override
766+
@ApiStatus.Internal
767+
public @Nullable ITransaction getTransaction() {
768+
ITransaction span = null;
769+
if (!isEnabled()) {
770+
options
771+
.getLogger()
772+
.log(
773+
SentryLevel.WARNING,
774+
"Instance is disabled and this 'getTransaction' call is a no-op.");
775+
} else {
776+
span = stack.peek().getScope().getTransaction();
777+
}
778+
return span;
779+
}
780+
765781
@Override
766782
@ApiStatus.Internal
767783
public void setSpanContext(

sentry/src/main/java/io/sentry/HubAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ public void setSpanContext(
221221
return Sentry.getCurrentHub().getSpan();
222222
}
223223

224+
@Override
225+
@ApiStatus.Internal
226+
public @Nullable ITransaction getTransaction() {
227+
return Sentry.getCurrentHub().getTransaction();
228+
}
229+
224230
@Override
225231
public @NotNull SentryOptions getOptions() {
226232
return Sentry.getCurrentHub().getOptions();

sentry/src/main/java/io/sentry/IHub.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,15 @@ void setSpanContext(
554554
@Nullable
555555
ISpan getSpan();
556556

557+
/**
558+
* Returns the transaction.
559+
*
560+
* @return the transaction or null when no active transaction is running.
561+
*/
562+
@ApiStatus.Internal
563+
@Nullable
564+
ITransaction getTransaction();
565+
557566
/**
558567
* Gets the {@link SentryOptions} attached to current scope.
559568
*

sentry/src/main/java/io/sentry/NoOpHub.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ public void setSpanContext(
179179
return null;
180180
}
181181

182+
@Override
183+
public @Nullable ITransaction getTransaction() {
184+
return null;
185+
}
186+
182187
@Override
183188
public @NotNull SentryOptions getOptions() {
184189
return emptyOptions;

sentry/src/main/java/io/sentry/Sentry.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.sentry.transport.NoOpEnvelopeCache;
1616
import io.sentry.util.DebugMetaPropertiesApplier;
1717
import io.sentry.util.FileUtils;
18+
import io.sentry.util.Platform;
1819
import io.sentry.util.thread.IMainThreadChecker;
1920
import io.sentry.util.thread.MainThreadChecker;
2021
import io.sentry.util.thread.NoOpMainThreadChecker;
@@ -918,10 +919,16 @@ public static void endSession() {
918919
/**
919920
* Gets the current active transaction or span.
920921
*
921-
* @return the active span or null when no active transaction is running
922+
* @return the active span or null when no active transaction is running. In case of
923+
* globalHubMode=true, always the active transaction is returned, rather than the last active
924+
* span.
922925
*/
923926
public static @Nullable ISpan getSpan() {
924-
return getCurrentHub().getSpan();
927+
if (globalHubMode && Platform.isAndroid()) {
928+
return getCurrentHub().getTransaction();
929+
} else {
930+
return getCurrentHub().getSpan();
931+
}
925932
}
926933

927934
/**

sentry/src/main/java/io/sentry/instrumentation/file/FileIOSpanManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class FileIOSpanManager {
2929
private final @NotNull SentryStackTraceFactory stackTraceFactory;
3030

3131
static @Nullable ISpan startSpan(final @NotNull IHub hub, final @NotNull String op) {
32-
final ISpan parent = hub.getSpan();
32+
final ISpan parent = hub.getTransaction();
3333
return parent != null ? parent.startChild(op) : null;
3434
}
3535

0 commit comments

Comments
 (0)