Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Add `SentryWrapper.wrapRunnable` to wrap `Runnable` for use with Sentry ([#4332](https://github.com/getsentry/sentry-java/pull/4332))

### Fixes

- Fix TTFD measurement when API called too early ([#4297](https://github.com/getsentry/sentry-java/pull/4297))
Expand Down
1 change: 1 addition & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -3541,6 +3541,7 @@ public final class io/sentry/SentryUUID {
public final class io/sentry/SentryWrapper {
public fun <init> ()V
public static fun wrapCallable (Ljava/util/concurrent/Callable;)Ljava/util/concurrent/Callable;
public static fun wrapRunnable (Ljava/lang/Runnable;)Ljava/lang/Runnable;
public static fun wrapSupplier (Ljava/util/function/Supplier;)Ljava/util/function/Supplier;
}

Expand Down
19 changes: 19 additions & 0 deletions sentry/src/main/java/io/sentry/SentryWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,23 @@ public static <U> Supplier<U> wrapSupplier(final @NotNull Supplier<U> supplier)
}
};
}

/**
* Helper method to wrap {@link Runnable}
*
* <p>Forks current and isolation scope before execution and restores previous state afterwards.
* This prevents reused threads (e.g. from thread-pools) from getting an incorrect state.
*
* @param runnable - the {@link Runnable} to be wrapped
* @return the wrapped {@link Runnable}
*/
public static Runnable wrapRunnable(final @NotNull Runnable runnable) {
final IScopes newScopes = Sentry.forkedScopes("SentryWrapper.wrapRunnable");

return () -> {
try (ISentryLifecycleToken ignore = newScopes.makeCurrent()) {
runnable.run();
}
};
}
}
81 changes: 81 additions & 0 deletions sentry/src/test/java/io/sentry/SentryWrapperTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,85 @@ class SentryWrapperTest {
assertEquals(threadedScopes, Sentry.getCurrentScopes())
}.get()
}

@Test
fun `wrapped runnable isolates Scopes`() {
val capturedEvents = mutableListOf<SentryEvent>()

initForTest {
it.dsn = dsn
it.beforeSend = SentryOptions.BeforeSendCallback { event, hint ->
capturedEvents.add(event)
event
}
}

Sentry.addBreadcrumb("MyOriginalBreadcrumbBefore")
Sentry.captureMessage("OriginalMessageBefore")
println(Thread.currentThread().name)

val future1 = executor.submit(
SentryWrapper.wrapRunnable {
Thread.sleep(20)
Sentry.addBreadcrumb("MyClonedBreadcrumb")
Sentry.captureMessage("ClonedMessage")
"Result 1"
}
)

val future2 = executor.submit(
SentryWrapper.wrapRunnable {
Thread.sleep(10)
Sentry.addBreadcrumb("MyClonedBreadcrumb2")
Sentry.captureMessage("ClonedMessage2")
"Result 2"
}
)

Sentry.addBreadcrumb("MyOriginalBreadcrumb")
Sentry.captureMessage("OriginalMessage")

future1.get()
future2.get()

val mainEvent = capturedEvents.firstOrNull { it.message?.formatted == "OriginalMessage" }
val clonedEvent = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage" }
val clonedEvent2 = capturedEvents.firstOrNull { it.message?.formatted == "ClonedMessage2" }

assertEquals(2, mainEvent?.breadcrumbs?.size)
assertEquals(2, clonedEvent?.breadcrumbs?.size)
assertEquals(2, clonedEvent2?.breadcrumbs?.size)
}

@Test
fun `scopes is reset to state within the thread after isolated runnable is done`() {
initForTest {
it.dsn = dsn
}

val mainScopes = Sentry.getCurrentScopes()
val threadedScopes = Sentry.getCurrentScopes().forkedCurrentScope("test")

executor.submit {
Sentry.setCurrentScopes(threadedScopes)
}.get()

assertEquals(mainScopes, Sentry.getCurrentScopes())

val runnableFuture =
executor.submit(
SentryWrapper.wrapRunnable {
assertNotEquals(mainScopes, Sentry.getCurrentScopes())
assertNotEquals(threadedScopes, Sentry.getCurrentScopes())
"Result 1"
}
)

runnableFuture.get()

executor.submit {
assertNotEquals(mainScopes, Sentry.getCurrentScopes())
assertEquals(threadedScopes, Sentry.getCurrentScopes())
}.get()
}
}
Loading