Skip to content
Draft
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
1 change: 1 addition & 0 deletions sentry-android-integration-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.compose) apply false
alias(libs.plugins.kotlin.serialization) apply false
alias(libs.plugins.errorprone) apply false
alias(libs.plugins.detekt) apply false
alias(libs.plugins.gradle.versions) apply false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ plugins {
id("io.sentry.jdk-target")
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.compose)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.ksp)
alias(libs.plugins.sentry) apply false
alias(libs.plugins.sqldelight)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTagsAsResourceId
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.ui.NavDisplay
import io.sentry.compose.navigation3.BackStackEntryMapper
import io.sentry.compose.navigation3.SentryBackStackEntry
import io.sentry.compose.navigation3.SentryNavEffect
import io.sentry.compose.navigation3.SentryNavOptions
Expand Down Expand Up @@ -232,51 +233,64 @@ private fun Nav3SampleApp(
}
val performanceExtractorMode = performanceState.extractorMode
val performanceArgumentMode = performanceState.argumentMode
val backStackEntryMapper =
val performanceMapperMode = performanceState.mapperMode
val backStackEntryMapper: BackStackEntryMapper<Nav3Route> =
remember(
isPerformanceScenario,
performanceExtractorMode,
performanceArgumentMode,
performanceMapperMode,
integrationMode.includeArguments,
) {
{ route: Nav3Route ->
val name =
if (isPerformanceScenario) {
if (!isPerformanceScenario) {
BackStackEntryMapper.SerialNameAndProperties
} else if (performanceMapperMode == Nav3PerformanceMapperMode.SERIAL_NAME_AND_PROPERTIES) {
// The built-in mapper itself, so SentryNavEffect.onBackStackChanged measures its real
// property encoding. Names only when the integration mode records no arguments.
if (integrationMode.includeArguments) {
BackStackEntryMapper.SerialNameAndProperties
} else {
BackStackEntryMapper.SerialName
}
} else {
// A custom mapper, so the performance scenario can time name and argument extraction
// separately and substitute synthetic arguments.
BackStackEntryMapper { route ->
val name =
performanceState.recordNameExtraction("Nav3Stress.nameExtraction") {
consumeNav3PerformanceExtractorWork(
performanceExtractorMode,
route.performanceSeed,
)
route.routeName
checkNotNull(BackStackEntryMapper.SerialName.map(route)) {
"${route::class.simpleName} can't be named by BackStackEntryMapper.SerialName."
}
.name
}
} else {
route.routeName
}

val arguments =
if (isPerformanceScenario && !integrationMode.includeArguments) {
null
} else if (isPerformanceScenario) {
performanceState.recordArgumentExtraction("Nav3Stress.argumentsExtraction") {
consumeNav3PerformanceExtractorWork(
performanceExtractorMode,
route.performanceSeed,
)
if (route is Nav3Route.Performance) {
nav3PerformanceArguments(
performanceArgumentMode,
route.index,
route.generation,
val arguments =
if (!integrationMode.includeArguments) {
null
} else {
performanceState.recordArgumentExtraction("Nav3Stress.argumentsExtraction") {
consumeNav3PerformanceExtractorWork(
performanceExtractorMode,
route.performanceSeed,
)
} else {
route.arguments
if (route is Nav3Route.Performance) {
nav3PerformanceArguments(
performanceArgumentMode,
route.index,
route.generation,
)
} else {
route.arguments
}
}
}
} else {
route.arguments
}

SentryBackStackEntry(name = name, arguments = arguments)
SentryBackStackEntry(name = name, arguments = arguments)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import androidx.navigation3.ui.NavDisplay
import androidx.navigationevent.compose.LocalNavigationEventDispatcherOwner
import androidx.navigationevent.compose.rememberNavigationEventDispatcherOwner
import io.sentry.compose.SentryModifier.sentryTag
import io.sentry.compose.navigation3.SentryBackStackEntry
import io.sentry.compose.navigation3.BackStackEntryMapper
import io.sentry.compose.navigation3.SentryNavEffect
import io.sentry.compose.navigation3.SentryNavOptions
import io.sentry.samples.android.navigation.common.NavigationSampleConfig
Expand Down Expand Up @@ -307,9 +307,7 @@ internal fun MultiRootNavDisplay(
// Keep this before the NavDisplay below: TracedNav3Route's resume tagging relies on it.
SentryNavEffect(
backStack = backStack,
backStackEntryMapper = { route ->
SentryBackStackEntry(name = route.routeName, arguments = route.arguments)
},
backStackEntryMapper = BackStackEntryMapper.SerialNameAndProperties,
options = options,
)
val onBack: () -> Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal class Nav3PerformanceState(
var autoNavigate by mutableStateOf(false)
var extractorMode by mutableStateOf(Nav3PerformanceExtractorMode.NORMAL)
var argumentMode by mutableStateOf(Nav3PerformanceArgumentMode.FLAT)
var mapperMode by mutableStateOf(Nav3PerformanceMapperMode.CUSTOM)
var integrationMode by mutableStateOf(Nav3PerformanceIntegrationMode.FULL_STACK)
private set

Expand Down Expand Up @@ -321,6 +322,7 @@ internal class Nav3PerformanceState(
appendLine("status=$performanceStatus")
appendLine("route=$currentRoute")
appendLine("tracked_stack=$backStack")
appendLine("mapper_mode=${mapperMode.label}")
appendLine("recomposition_requests=$recompositionRequests")
appendLine("navigation_mutations=$navigationMutations")
appendLine("destination_changes=$destinationChanges")
Expand Down Expand Up @@ -512,6 +514,20 @@ internal enum class Nav3PerformanceArgumentMode(val label: String) {
LARGE("Large"),
}

/**
* Which mapper the performance scenario feeds to `SentryNavEffect`.
*
* [CUSTOM] times name and argument extraction separately and substitutes the synthetic
* [Nav3PerformanceArgumentMode] shapes. [SERIAL_NAME_AND_PROPERTIES] runs the built-in mapper
* instead, so its property encoding is what `SentryNavEffect.onBackStackChanged` measures. With the
* built-in mapper, the argument shape is fixed by the route's own properties, so the argument-shape
* control and the per-call extractor timings don't apply.
*/
internal enum class Nav3PerformanceMapperMode(val label: String) {
CUSTOM("Custom"),
SERIAL_NAME_AND_PROPERTIES("SerialNameAndProperties"),
}

internal enum class Nav3PerformanceIntegrationMode(
val label: String,
val captureBackStack: Boolean,
Expand Down Expand Up @@ -874,6 +890,15 @@ internal fun Nav3PerformancePanel(

if (showExtractorControls) {
PerfCard(title = "Extractor Inputs") {
PerfModeRow(
label = "Mapper",
selectedLabel = state.mapperMode.label,
enabled = !state.performanceRunActive,
options =
Nav3PerformanceMapperMode.entries.map { mode ->
mode.label to { state.mapperMode = mode }
},
)
PerfModeRow(
label = "Extractor cost",
selectedLabel = state.extractorMode.label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.snapshots.SnapshotStateList
import io.sentry.compose.navigation3.BackStackEntryMapper
import io.sentry.samples.android.navigation.common.NavArgs
import io.sentry.samples.android.navigation.common.RouteNames
import io.sentry.samples.android.navigation.common.RouteSpec
import io.sentry.samples.android.navigation.common.RouteSpecs
import io.sentry.samples.android.navigation.common.displayRoute
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

internal fun Nav3Route.displayRoute(): String = routeSpec().displayRoute(arguments)
internal fun Nav3Route.displayRoute(): String =
when (this) {
// Built without the back stack entry mappers, whose cost the performance scenario measures
// only through SentryNavEffect.
is Nav3Route.Performance -> "/$previewName"
else -> routeSpec().displayRoute(arguments)
}

internal fun Nav3Route.routeSpec(): RouteSpec =
when (this) {
Expand Down Expand Up @@ -228,98 +239,115 @@ internal fun SnapshotStateList<Nav3Route>.replacePerformanceTop(generation: Int)
set(lastIndex, Nav3Route.Performance(index = lastIndex, generation = generation))
}

/**
* The sample's back stack keys. Each is `@Serializable`, so
* `BackStackEntryMapper.SerialNameAndProperties` can map it: the route's `@SerialName` becomes its
* Sentry name, and each property's `@SerialName` an argument name. Both match the Nav2 sample's
* [RouteNames] and [NavArgs], so the two samples send the same data. Every other member is a
* getter, so it isn't recorded as an argument.
*/
internal sealed interface Nav3Route {
/**
* The route's name as Sentry records it, without the leading `/`. Read from the same mapper
* `SentryNavEffect` uses, so screens, tags, and transaction checks can't drift from it.
*/
val routeName: String
get() =
checkNotNull(BackStackEntryMapper.SerialName.map(this)) {
"${this::class.simpleName} can't be named by BackStackEntryMapper.SerialName."
}
.name

/** The route's arguments as Sentry records them, read from the same mapper as [routeName]. */
val arguments: Map<String, Any?>
get() = emptyMap()
get() = BackStackEntryMapper.SerialNameAndProperties.map(this)?.arguments.orEmpty()

val previewName: String
get() = routeName

val performanceSeed: Int
get() = hashCode()

data object Landing : Nav3Route {
override val routeName: String = RouteNames.LANDING
}
@Serializable @SerialName(RouteNames.LANDING) data object Landing : Nav3Route

data object SingleStack : Nav3Route {
override val routeName: String = RouteNames.HOME
}
@Serializable @SerialName(RouteNames.HOME) data object SingleStack : Nav3Route

data object Custom : Nav3Route {
override val routeName: String = "Custom"
}
@Serializable @SerialName(RouteNames.CUSTOM) data object Custom : Nav3Route

data object MultiActivity : Nav3Route {
override val routeName: String = "MultiActivity"
}
@Serializable @SerialName("MultiActivity") data object MultiActivity : Nav3Route

data object MultiFragment : Nav3Route {
override val routeName: String = "MultiFragment"
}
@Serializable @SerialName("MultiFragment") data object MultiFragment : Nav3Route

data object FragmentOverlay : Nav3Route {
override val routeName: String = "FragmentOverlay"
}
@Serializable @SerialName("FragmentOverlay") data object FragmentOverlay : Nav3Route

data object ViewPager2 : Nav3Route {
override val routeName: String = "ViewPager2"
}
@Serializable @SerialName("ViewPager2") data object ViewPager2 : Nav3Route

data object DeepLink : Nav3Route {
override val routeName: String = RouteNames.DEEP_LINK
}
@Serializable @SerialName(RouteNames.DEEP_LINK) data object DeepLink : Nav3Route

data object ProductList : Nav3Route {
override val routeName: String = RouteNames.PRODUCT_LIST
}
@Serializable @SerialName(RouteNames.PRODUCT_LIST) data object ProductList : Nav3Route

@OptIn(ExperimentalSerializationApi::class)
@Serializable
@SerialName(RouteNames.PRODUCT_DETAIL)
data class ProductDetail(
val productId: String,
val source: String,
@SerialName(NavArgs.PRODUCT_ID) val productId: String,
@SerialName(NavArgs.SOURCE) val source: String,
// Omitted from the arguments when null, like the Nav2 sample.
@EncodeDefault(EncodeDefault.Mode.NEVER)
@SerialName(NavArgs.CAMPAIGN)
val campaign: String? = null,
) : Nav3Route {
override val routeName: String = RouteNames.PRODUCT_DETAIL
override val arguments: Map<String, Any?> =
mapOf(
NavArgs.PRODUCT_ID to productId,
NavArgs.SOURCE to source,
NavArgs.CAMPAIGN to campaign,
)
.filterValues { it != null }
override val previewName: String = "ProductDetail($productId)"
override val previewName: String
get() = "ProductDetail($productId)"
}

data class Checkout(val productId: String) : Nav3Route {
override val routeName: String = RouteNames.CHECKOUT
override val arguments: Map<String, Any?> = mapOf(NavArgs.PRODUCT_ID to productId)
override val previewName: String = "Checkout($productId)"
@Serializable
@SerialName(RouteNames.CHECKOUT)
data class Checkout(@SerialName(NavArgs.PRODUCT_ID) val productId: String) : Nav3Route {
override val previewName: String
get() = "Checkout($productId)"
}

data class Confirmation(val orderId: String) : Nav3Route {
override val routeName: String = RouteNames.CONFIRMATION
override val arguments: Map<String, Any?> = mapOf(NavArgs.ORDER_ID to orderId)
override val previewName: String = "Confirmation($orderId)"
@Serializable
@SerialName(RouteNames.CONFIRMATION)
data class Confirmation(@SerialName(NavArgs.ORDER_ID) val orderId: String) : Nav3Route {
override val previewName: String
get() = "Confirmation($orderId)"
}

data class PromoDialog(val promoId: String) : Nav3Route {
override val routeName: String = RouteNames.PROMO_DIALOG
override val arguments: Map<String, Any?> = mapOf(NavArgs.PROMO_ID to promoId)
override val previewName: String = "PromoDialog($promoId)"
@Serializable
@SerialName(RouteNames.PROMO_DIALOG)
data class PromoDialog(@SerialName(NavArgs.PROMO_ID) val promoId: String) : Nav3Route {
override val previewName: String
get() = "PromoDialog($promoId)"
}

data class ShareSheet(val productId: String) : Nav3Route {
override val routeName: String = RouteNames.SHARE_SHEET
override val arguments: Map<String, Any?> = mapOf(NavArgs.PRODUCT_ID to productId)
override val previewName: String = "ShareSheet($productId)"
@Serializable
@SerialName(RouteNames.SHARE_SHEET)
data class ShareSheet(@SerialName(NavArgs.PRODUCT_ID) val productId: String) : Nav3Route {
override val previewName: String
get() = "ShareSheet($productId)"
}

@Serializable
@SerialName(Performance.NAME)
data class Performance(val index: Int, val generation: Int) : Nav3Route {
override val routeName: String = "Performance"
override val arguments: Map<String, Any?> = mapOf("index" to index, "generation" to generation)
override val previewName: String = "Performance($index:$generation)"
override val performanceSeed: Int = 31 * index + generation
/**
* A constant rather than the mapper's result, so screens and tags don't run the mapper outside
* the performance scenario's timed extraction, e.g. in its disabled baseline.
*/
override val routeName: String
get() = NAME

override val previewName: String
get() = "Performance($index:$generation)"

override val performanceSeed: Int
get() = 31 * index + generation

companion object {
const val NAME = "Performance"
}
}
}

Expand Down
Loading