-
-
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
4
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.
+170
−10
Open
Changes from all commits
Commits
Show all changes
4 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 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
113 changes: 113 additions & 0 deletions
113
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,113 @@ | ||
| 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.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.SentryOptions | ||
| import io.sentry.TransactionOptions | ||
| import io.sentry.test.createTestScopes | ||
| import kotlin.test.assertEquals | ||
| 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>() | ||
|
|
||
| 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" }) | ||
| } | ||
| } |
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.