-
-
Notifications
You must be signed in to change notification settings - Fork 476
fix(compose): allow overriding the IScopes used by SentryTraced via LocalSentryScopes #5838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tabishahmad
wants to merge
11
commits into
getsentry:main
Choose a base branch
from
Tabishahmad:fix/sentry-traced-custom-scopes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+314
−10
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d3d38c7
fix(compose): allow overriding the IScopes used by SentryTraced via L…
Tabishahmad d1e90a3
Apply suggestion from @romtsn
romtsn 5a43f30
Apply suggestion from @romtsn
romtsn 105fd6d
fix(compose): address PR review feedback on LocalSentryScopes root sp…
Tabishahmad dfa46df
fix(compose): regenerate api dump for LocalSentryScopes
Tabishahmad 446749f
fix(compose): fix WeakHashMap leak and stale LocalSentryScopes default
Tabishahmad 6702434
fix(compose): use deterministic ref-counting instead of weak cache fo…
Tabishahmad 6bd3aa5
fix(compose): retain root span cache entries synchronously to avoid a…
Tabishahmad 863894f
fix(compose): release root span refcount on abandoned composition too
Tabishahmad 20dafa5
fix(compose): finish evicted root spans instead of leaving them open
Tabishahmad 0c54ea1
fix(compose): stop finishing evicted root spans, restore retain on re…
Tabishahmad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
sentry-compose/src/androidUnitTest/kotlin/io/sentry/compose/SentryTracedTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| package io.sentry.compose | ||
|
|
||
| import android.app.Application | ||
| import android.content.ComponentName | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.runtime.CompositionLocalProvider | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.key | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.ExperimentalComposeUiApi | ||
| import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import io.sentry.IScopes | ||
| import io.sentry.ITransaction | ||
| import io.sentry.Sentry | ||
| import io.sentry.SentryOptions | ||
| import io.sentry.TransactionOptions | ||
| import io.sentry.test.createTestScopes | ||
| import kotlin.test.assertEquals | ||
| import org.junit.After | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.rules.TestWatcher | ||
| import org.junit.runner.Description | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.Shadows | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| @OptIn(ExperimentalComposeUiApi::class) | ||
| @RunWith(AndroidJUnit4::class) | ||
| @Config(sdk = [30]) | ||
| class SentryTracedTest { | ||
| // workaround for robolectric tests with composeRule | ||
| // from https://github.com/robolectric/robolectric/pull/4736#issuecomment-1831034882 | ||
| @get:Rule(order = 1) | ||
| val addActivityToRobolectricRule = | ||
| object : TestWatcher() { | ||
| override fun starting(description: Description?) { | ||
| super.starting(description) | ||
| val appContext: Application = ApplicationProvider.getApplicationContext() | ||
| Shadows.shadowOf(appContext.packageManager) | ||
| .addActivityIfNotPresent( | ||
| ComponentName(appContext.packageName, ComponentActivity::class.java.name) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| @get:Rule(order = 2) val rule = createAndroidComposeRule<ComponentActivity>() | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| Sentry.close() | ||
| } | ||
|
|
||
| private fun newTracingScopes(): IScopes = | ||
| createTestScopes( | ||
| SentryOptions().apply { | ||
| dsn = "https://key@sentry.io/proj" | ||
| tracesSampleRate = 1.0 | ||
| } | ||
| ) | ||
|
|
||
| private fun IScopes.startBoundTransaction(name: String): ITransaction = | ||
| startTransaction(name, "test", TransactionOptions().apply { isBindToScope = true }) | ||
|
|
||
| @Test | ||
| fun `SentryTraced creates its root span on the scopes provided via LocalSentryScopes`() { | ||
| val scopes = newTracingScopes() | ||
| val tx = scopes.startBoundTransaction("custom-scopes-tx") | ||
|
|
||
| rule.setContent { | ||
| CompositionLocalProvider(LocalSentryScopes provides scopes) { | ||
| SentryTraced(tag = "custom") { Box {} } | ||
| } | ||
| } | ||
| rule.waitForIdle() | ||
|
|
||
| assertEquals(1, tx.spans.count { it.operation == "ui.compose.composition" }) | ||
| assertEquals(1, tx.spans.count { it.operation == "ui.compose" }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `sibling SentryTraced composables under the same scopes share one root span`() { | ||
| val scopes = newTracingScopes() | ||
| val tx = scopes.startBoundTransaction("custom-scopes-tx") | ||
|
|
||
| rule.setContent { | ||
| CompositionLocalProvider(LocalSentryScopes provides scopes) { | ||
| SentryTraced(tag = "first") { Box {} } | ||
| SentryTraced(tag = "second") { Box {} } | ||
| } | ||
| } | ||
| rule.waitForIdle() | ||
|
|
||
| assertEquals(1, tx.spans.count { it.operation == "ui.compose.composition" }) | ||
| assertEquals(2, tx.spans.count { it.operation == "ui.compose" }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `SentryTraced composables under different scopes do not interfere with each other`() { | ||
| val scopesA = newTracingScopes() | ||
| val txA = scopesA.startBoundTransaction("scopes-a-tx") | ||
| val scopesB = newTracingScopes() | ||
| val txB = scopesB.startBoundTransaction("scopes-b-tx") | ||
|
|
||
| rule.setContent { | ||
| CompositionLocalProvider(LocalSentryScopes provides scopesA) { | ||
| SentryTraced(tag = "a") { Box {} } | ||
| } | ||
| CompositionLocalProvider(LocalSentryScopes provides scopesB) { | ||
| SentryTraced(tag = "b") { Box {} } | ||
| } | ||
| } | ||
| rule.waitForIdle() | ||
|
|
||
| assertEquals(1, txA.spans.count { it.operation == "ui.compose.composition" }) | ||
| assertEquals(1, txA.spans.count { it.operation == "ui.compose" }) | ||
| assertEquals(1, txB.spans.count { it.operation == "ui.compose.composition" }) | ||
| assertEquals(1, txB.spans.count { it.operation == "ui.compose" }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `repeatedly replacing a traced composable under the same scopes keeps sharing the root span`() { | ||
| // Each swap disposes the outgoing keyed composable and mounts a new one under the same | ||
| // scopes within a single recomposition. Compose dispatches the outgoing composable's | ||
| // onForgotten before the incoming one's onRemembered, so a second swap is needed to surface | ||
| // a root span cache that was cleared out from under a still-live retain. | ||
| val scopes = newTracingScopes() | ||
| val tx = scopes.startBoundTransaction("custom-scopes-tx") | ||
| var step by mutableStateOf(0) | ||
|
|
||
| rule.setContent { | ||
| CompositionLocalProvider(LocalSentryScopes provides scopes) { | ||
| key(step) { SentryTraced(tag = "step-$step") { Box {} } } | ||
| } | ||
| } | ||
| rule.waitForIdle() | ||
|
|
||
| step = 1 | ||
| rule.waitForIdle() | ||
|
|
||
| step = 2 | ||
| rule.waitForIdle() | ||
|
|
||
| assertEquals(1, tx.spans.count { it.operation == "ui.compose.composition" }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `fully unmounting and remounting under the same scopes creates a fresh root span`() { | ||
| // Confirms the cache entry is actually dropped once the last consumer of a scopes leaves | ||
| // composition (the fix for the original RootSpans leak), rather than being reused forever. | ||
| val scopes = newTracingScopes() | ||
| val tx = scopes.startBoundTransaction("custom-scopes-tx") | ||
| var mounted by mutableStateOf(true) | ||
|
|
||
| rule.setContent { | ||
| if (mounted) { | ||
| CompositionLocalProvider(LocalSentryScopes provides scopes) { | ||
| SentryTraced(tag = "first") { Box {} } | ||
| } | ||
| } | ||
| } | ||
| rule.waitForIdle() | ||
|
|
||
| mounted = false | ||
| rule.waitForIdle() | ||
|
|
||
| mounted = true | ||
| rule.waitForIdle() | ||
|
|
||
| assertEquals(2, tx.spans.count { it.operation == "ui.compose.composition" }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.