Skip to content

Commit bfee4ed

Browse files
authored
Merge c4df37e into cae0eea
2 parents cae0eea + c4df37e commit bfee4ed

File tree

9 files changed

+104
-6
lines changed

9 files changed

+104
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
Breaking changes:
88
- Capture failed HTTP requests by default ([#2794](https://github.com/getsentry/sentry-java/pull/2794))
9+
- 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))
910

1011
### Fixes
1112

sentry/api/sentry.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ public final class io/sentry/Hub : io/sentry/IHub {
358358
public fun getOptions ()Lio/sentry/SentryOptions;
359359
public fun getSpan ()Lio/sentry/ISpan;
360360
public fun getTraceparent ()Lio/sentry/SentryTraceHeader;
361+
public fun getTransaction ()Lio/sentry/ITransaction;
361362
public fun isCrashedLastRun ()Ljava/lang/Boolean;
362363
public fun isEnabled ()Z
363364
public fun popScope ()V
@@ -405,6 +406,7 @@ public final class io/sentry/HubAdapter : io/sentry/IHub {
405406
public fun getOptions ()Lio/sentry/SentryOptions;
406407
public fun getSpan ()Lio/sentry/ISpan;
407408
public fun getTraceparent ()Lio/sentry/SentryTraceHeader;
409+
public fun getTransaction ()Lio/sentry/ITransaction;
408410
public fun isCrashedLastRun ()Ljava/lang/Boolean;
409411
public fun isEnabled ()Z
410412
public fun popScope ()V
@@ -477,6 +479,7 @@ public abstract interface class io/sentry/IHub {
477479
public abstract fun getOptions ()Lio/sentry/SentryOptions;
478480
public abstract fun getSpan ()Lio/sentry/ISpan;
479481
public abstract fun getTraceparent ()Lio/sentry/SentryTraceHeader;
482+
public abstract fun getTransaction ()Lio/sentry/ITransaction;
480483
public abstract fun isCrashedLastRun ()Ljava/lang/Boolean;
481484
public abstract fun isEnabled ()Z
482485
public abstract fun popScope ()V
@@ -839,6 +842,7 @@ public final class io/sentry/NoOpHub : io/sentry/IHub {
839842
public fun getOptions ()Lio/sentry/SentryOptions;
840843
public fun getSpan ()Lio/sentry/ISpan;
841844
public fun getTraceparent ()Lio/sentry/SentryTraceHeader;
845+
public fun getTransaction ()Lio/sentry/ITransaction;
842846
public fun isCrashedLastRun ()Ljava/lang/Boolean;
843847
public fun isEnabled ()Z
844848
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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,10 +918,16 @@ public static void endSession() {
918918
/**
919919
* Gets the current active transaction or span.
920920
*
921-
* @return the active span or null when no active transaction is running
921+
* @return the active span or null when no active transaction is running. In case of
922+
* globalHubMode=true, always the active transaction is returned, rather than the last active
923+
* span.
922924
*/
923925
public static @Nullable ISpan getSpan() {
924-
return getCurrentHub().getSpan();
926+
if (globalHubMode) {
927+
return getCurrentHub().getTransaction();
928+
} else {
929+
return getCurrentHub().getSpan();
930+
}
925931
}
926932

927933
/**

sentry/src/test/java/io/sentry/HubTest.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,15 +1607,23 @@ class HubTest {
16071607
@Test
16081608
fun `when there is no active transaction, getSpan returns null`() {
16091609
val hub = generateHub()
1610-
assertNull(hub.getSpan())
1610+
assertNull(hub.span)
16111611
}
16121612

16131613
@Test
1614-
fun `when there is active transaction bound to the scope, getSpan returns active transaction`() {
1614+
fun `when there is no active transaction, getTransaction returns null`() {
1615+
val hub = generateHub()
1616+
assertNull(hub.transaction)
1617+
}
1618+
1619+
@Test
1620+
fun `when there is active transaction bound to the scope, getTransaction and getSpan return active transaction`() {
16151621
val hub = generateHub()
16161622
val tx = hub.startTransaction("aTransaction", "op")
1617-
hub.configureScope { it.setTransaction(tx) }
1618-
assertEquals(tx, hub.getSpan())
1623+
hub.configureScope { it.transaction = tx }
1624+
1625+
assertEquals(tx, hub.transaction)
1626+
assertEquals(tx, hub.span)
16191627
}
16201628

16211629
@Test
@@ -1625,6 +1633,8 @@ class HubTest {
16251633
hub.configureScope { it.setTransaction(tx) }
16261634
hub.configureScope { it.setTransaction(tx) }
16271635
val span = tx.startChild("op")
1636+
1637+
assertEquals(tx, hub.transaction)
16281638
assertEquals(span, hub.span)
16291639
}
16301640
// endregion

sentry/src/test/java/io/sentry/SentryTest.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,47 @@ class SentryTest {
723723
assertFalse(previousSessionFile.exists())
724724
}
725725

726+
@Test
727+
fun `getSpan calls hub getSpan`() {
728+
val hub = mock<IHub>()
729+
Sentry.init({
730+
it.dsn = dsn
731+
}, false)
732+
Sentry.setCurrentHub(hub)
733+
Sentry.getSpan()
734+
verify(hub).span
735+
}
736+
737+
@Test
738+
fun `getSpan calls returns root span if globalhub mode is enabled`() {
739+
Sentry.init({
740+
it.dsn = dsn
741+
it.enableTracing = true
742+
it.sampleRate = 1.0
743+
}, true)
744+
745+
val transaction = Sentry.startTransaction("name", "op-root", true)
746+
transaction.startChild("op-child")
747+
748+
val span = Sentry.getSpan()!!
749+
assertEquals("op-root", span.operation)
750+
}
751+
752+
@Test
753+
fun `getSpan calls returns child span if globalhub mode is disabled`() {
754+
Sentry.init({
755+
it.dsn = dsn
756+
it.enableTracing = true
757+
it.sampleRate = 1.0
758+
}, false)
759+
760+
val transaction = Sentry.startTransaction("name", "op-root", true)
761+
transaction.startChild("op-child")
762+
763+
val span = Sentry.getSpan()!!
764+
assertEquals("op-child", span.operation)
765+
}
766+
726767
private class InMemoryOptionsObserver : IOptionsObserver {
727768
var release: String? = null
728769
private set

0 commit comments

Comments
 (0)