Skip to content

Commit 66c210f

Browse files
committed
feat(nav3): Use serial name mappers in Android samples
1 parent b91b558 commit 66c210f

5 files changed

Lines changed: 158 additions & 92 deletions

File tree

‎sentry-android-integration-tests/sentry-samples-android/build.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ plugins {
1313
id("io.sentry.jdk-target")
1414
alias(libs.plugins.kotlin.android)
1515
alias(libs.plugins.kotlin.compose)
16+
alias(libs.plugins.kotlin.serialization)
1617
alias(libs.plugins.ksp)
1718
alias(libs.plugins.sentry) apply false
1819
alias(libs.plugins.sqldelight)

‎sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3Activity.kt‎

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import androidx.compose.ui.semantics.semantics
4343
import androidx.compose.ui.semantics.testTagsAsResourceId
4444
import androidx.navigation3.runtime.entryProvider
4545
import androidx.navigation3.ui.NavDisplay
46+
import io.sentry.compose.navigation3.BackStackEntryMapper
4647
import io.sentry.compose.navigation3.SentryBackStackEntry
4748
import io.sentry.compose.navigation3.SentryNavEffect
4849
import io.sentry.compose.navigation3.SentryNavOptions
@@ -232,51 +233,64 @@ private fun Nav3SampleApp(
232233
}
233234
val performanceExtractorMode = performanceState.extractorMode
234235
val performanceArgumentMode = performanceState.argumentMode
235-
val backStackEntryMapper =
236+
val performanceMapperMode = performanceState.mapperMode
237+
val backStackEntryMapper: BackStackEntryMapper<Nav3Route> =
236238
remember(
237239
isPerformanceScenario,
238240
performanceExtractorMode,
239241
performanceArgumentMode,
242+
performanceMapperMode,
240243
integrationMode.includeArguments,
241244
) {
242-
{ route: Nav3Route ->
243-
val name =
244-
if (isPerformanceScenario) {
245+
if (!isPerformanceScenario) {
246+
BackStackEntryMapper.SerialNameAndProperties
247+
} else if (performanceMapperMode == Nav3PerformanceMapperMode.SERIAL_NAME_AND_PROPERTIES) {
248+
// The built-in mapper itself, so SentryNavEffect.onBackStackChanged measures its real
249+
// property encoding. Names only when the integration mode records no arguments.
250+
if (integrationMode.includeArguments) {
251+
BackStackEntryMapper.SerialNameAndProperties
252+
} else {
253+
BackStackEntryMapper.SerialName
254+
}
255+
} else {
256+
// A custom mapper, so the performance scenario can time name and argument extraction
257+
// separately and substitute synthetic arguments.
258+
BackStackEntryMapper { route ->
259+
val name =
245260
performanceState.recordNameExtraction("Nav3Stress.nameExtraction") {
246261
consumeNav3PerformanceExtractorWork(
247262
performanceExtractorMode,
248263
route.performanceSeed,
249264
)
250-
route.routeName
265+
checkNotNull(BackStackEntryMapper.SerialName.map(route)) {
266+
"${route::class.simpleName} can't be named by BackStackEntryMapper.SerialName."
267+
}
268+
.name
251269
}
252-
} else {
253-
route.routeName
254-
}
255270

256-
val arguments =
257-
if (isPerformanceScenario && !integrationMode.includeArguments) {
258-
null
259-
} else if (isPerformanceScenario) {
260-
performanceState.recordArgumentExtraction("Nav3Stress.argumentsExtraction") {
261-
consumeNav3PerformanceExtractorWork(
262-
performanceExtractorMode,
263-
route.performanceSeed,
264-
)
265-
if (route is Nav3Route.Performance) {
266-
nav3PerformanceArguments(
267-
performanceArgumentMode,
268-
route.index,
269-
route.generation,
271+
val arguments =
272+
if (!integrationMode.includeArguments) {
273+
null
274+
} else {
275+
performanceState.recordArgumentExtraction("Nav3Stress.argumentsExtraction") {
276+
consumeNav3PerformanceExtractorWork(
277+
performanceExtractorMode,
278+
route.performanceSeed,
270279
)
271-
} else {
272-
route.arguments
280+
if (route is Nav3Route.Performance) {
281+
nav3PerformanceArguments(
282+
performanceArgumentMode,
283+
route.index,
284+
route.generation,
285+
)
286+
} else {
287+
route.arguments
288+
}
273289
}
274290
}
275-
} else {
276-
route.arguments
277-
}
278291

279-
SentryBackStackEntry(name = name, arguments = arguments)
292+
SentryBackStackEntry(name = name, arguments = arguments)
293+
}
280294
}
281295
}
282296

‎sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3MultiActivity.kt‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import androidx.navigation3.ui.NavDisplay
5454
import androidx.navigationevent.compose.LocalNavigationEventDispatcherOwner
5555
import androidx.navigationevent.compose.rememberNavigationEventDispatcherOwner
5656
import io.sentry.compose.SentryModifier.sentryTag
57-
import io.sentry.compose.navigation3.SentryBackStackEntry
57+
import io.sentry.compose.navigation3.BackStackEntryMapper
5858
import io.sentry.compose.navigation3.SentryNavEffect
5959
import io.sentry.compose.navigation3.SentryNavOptions
6060
import io.sentry.samples.android.navigation.common.NavigationSampleConfig
@@ -307,9 +307,7 @@ internal fun MultiRootNavDisplay(
307307
// Keep this before the NavDisplay below: TracedNav3Route's resume tagging relies on it.
308308
SentryNavEffect(
309309
backStack = backStack,
310-
backStackEntryMapper = { route ->
311-
SentryBackStackEntry(name = route.routeName, arguments = route.arguments)
312-
},
310+
backStackEntryMapper = BackStackEntryMapper.SerialNameAndProperties,
313311
options = options,
314312
)
315313
val onBack: () -> Unit = {

‎sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3PerformanceControls.kt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ internal class Nav3PerformanceState(
5252
var autoNavigate by mutableStateOf(false)
5353
var extractorMode by mutableStateOf(Nav3PerformanceExtractorMode.NORMAL)
5454
var argumentMode by mutableStateOf(Nav3PerformanceArgumentMode.FLAT)
55+
var mapperMode by mutableStateOf(Nav3PerformanceMapperMode.CUSTOM)
5556
var integrationMode by mutableStateOf(Nav3PerformanceIntegrationMode.FULL_STACK)
5657
private set
5758

@@ -321,6 +322,7 @@ internal class Nav3PerformanceState(
321322
appendLine("status=$performanceStatus")
322323
appendLine("route=$currentRoute")
323324
appendLine("tracked_stack=$backStack")
325+
appendLine("mapper_mode=${mapperMode.label}")
324326
appendLine("recomposition_requests=$recompositionRequests")
325327
appendLine("navigation_mutations=$navigationMutations")
326328
appendLine("destination_changes=$destinationChanges")
@@ -512,6 +514,20 @@ internal enum class Nav3PerformanceArgumentMode(val label: String) {
512514
LARGE("Large"),
513515
}
514516

517+
/**
518+
* Which mapper the performance scenario feeds to `SentryNavEffect`.
519+
*
520+
* [CUSTOM] times name and argument extraction separately and substitutes the synthetic
521+
* [Nav3PerformanceArgumentMode] shapes. [SERIAL_NAME_AND_PROPERTIES] runs the built-in mapper
522+
* instead, so its property encoding is what `SentryNavEffect.onBackStackChanged` measures. With the
523+
* built-in mapper, the argument shape is fixed by the route's own properties, so the argument-shape
524+
* control and the per-call extractor timings don't apply.
525+
*/
526+
internal enum class Nav3PerformanceMapperMode(val label: String) {
527+
CUSTOM("Custom"),
528+
SERIAL_NAME_AND_PROPERTIES("SerialNameAndProperties"),
529+
}
530+
515531
internal enum class Nav3PerformanceIntegrationMode(
516532
val label: String,
517533
val captureBackStack: Boolean,
@@ -874,6 +890,15 @@ internal fun Nav3PerformancePanel(
874890

875891
if (showExtractorControls) {
876892
PerfCard(title = "Extractor Inputs") {
893+
PerfModeRow(
894+
label = "Mapper",
895+
selectedLabel = state.mapperMode.label,
896+
enabled = !state.performanceRunActive,
897+
options =
898+
Nav3PerformanceMapperMode.entries.map { mode ->
899+
mode.label to { state.mapperMode = mode }
900+
},
901+
)
877902
PerfModeRow(
878903
label = "Extractor cost",
879904
selectedLabel = state.extractorMode.label,

‎sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3Routes.kt‎

Lines changed: 88 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ import androidx.compose.runtime.mutableStateListOf
66
import androidx.compose.runtime.saveable.listSaver
77
import androidx.compose.runtime.saveable.rememberSaveable
88
import androidx.compose.runtime.snapshots.SnapshotStateList
9+
import io.sentry.compose.navigation3.BackStackEntryMapper
910
import io.sentry.samples.android.navigation.common.NavArgs
1011
import io.sentry.samples.android.navigation.common.RouteNames
1112
import io.sentry.samples.android.navigation.common.RouteSpec
1213
import io.sentry.samples.android.navigation.common.RouteSpecs
1314
import io.sentry.samples.android.navigation.common.displayRoute
15+
import kotlinx.serialization.EncodeDefault
16+
import kotlinx.serialization.ExperimentalSerializationApi
17+
import kotlinx.serialization.SerialName
18+
import kotlinx.serialization.Serializable
1419

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

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

242+
/**
243+
* The sample's back stack keys. Each is `@Serializable`, so
244+
* `BackStackEntryMapper.SerialNameAndProperties` can map it: the route's `@SerialName` becomes its
245+
* Sentry name, and each property's `@SerialName` an argument name. Both match the Nav2 sample's
246+
* [RouteNames] and [NavArgs], so the two samples send the same data. Every other member is a
247+
* getter, so it isn't recorded as an argument.
248+
*/
231249
internal sealed interface Nav3Route {
250+
/**
251+
* The route's name as Sentry records it, without the leading `/`. Read from the same mapper
252+
* `SentryNavEffect` uses, so screens, tags, and transaction checks can't drift from it.
253+
*/
232254
val routeName: String
255+
get() =
256+
checkNotNull(BackStackEntryMapper.SerialName.map(this)) {
257+
"${this::class.simpleName} can't be named by BackStackEntryMapper.SerialName."
258+
}
259+
.name
260+
261+
/** The route's arguments as Sentry records them, read from the same mapper as [routeName]. */
233262
val arguments: Map<String, Any?>
234-
get() = emptyMap()
263+
get() = BackStackEntryMapper.SerialNameAndProperties.map(this)?.arguments.orEmpty()
235264

236265
val previewName: String
237266
get() = routeName
238267

239268
val performanceSeed: Int
240269
get() = hashCode()
241270

242-
data object Landing : Nav3Route {
243-
override val routeName: String = RouteNames.LANDING
244-
}
271+
@Serializable @SerialName(RouteNames.LANDING) data object Landing : Nav3Route
245272

246-
data object SingleStack : Nav3Route {
247-
override val routeName: String = RouteNames.HOME
248-
}
273+
@Serializable @SerialName(RouteNames.HOME) data object SingleStack : Nav3Route
249274

250-
data object Custom : Nav3Route {
251-
override val routeName: String = "Custom"
252-
}
275+
@Serializable @SerialName(RouteNames.CUSTOM) data object Custom : Nav3Route
253276

254-
data object MultiActivity : Nav3Route {
255-
override val routeName: String = "MultiActivity"
256-
}
277+
@Serializable @SerialName("MultiActivity") data object MultiActivity : Nav3Route
257278

258-
data object MultiFragment : Nav3Route {
259-
override val routeName: String = "MultiFragment"
260-
}
279+
@Serializable @SerialName("MultiFragment") data object MultiFragment : Nav3Route
261280

262-
data object FragmentOverlay : Nav3Route {
263-
override val routeName: String = "FragmentOverlay"
264-
}
281+
@Serializable @SerialName("FragmentOverlay") data object FragmentOverlay : Nav3Route
265282

266-
data object ViewPager2 : Nav3Route {
267-
override val routeName: String = "ViewPager2"
268-
}
283+
@Serializable @SerialName("ViewPager2") data object ViewPager2 : Nav3Route
269284

270-
data object DeepLink : Nav3Route {
271-
override val routeName: String = RouteNames.DEEP_LINK
272-
}
285+
@Serializable @SerialName(RouteNames.DEEP_LINK) data object DeepLink : Nav3Route
273286

274-
data object ProductList : Nav3Route {
275-
override val routeName: String = RouteNames.PRODUCT_LIST
276-
}
287+
@Serializable @SerialName(RouteNames.PRODUCT_LIST) data object ProductList : Nav3Route
277288

289+
@OptIn(ExperimentalSerializationApi::class)
290+
@Serializable
291+
@SerialName(RouteNames.PRODUCT_DETAIL)
278292
data class ProductDetail(
279-
val productId: String,
280-
val source: String,
293+
@SerialName(NavArgs.PRODUCT_ID) val productId: String,
294+
@SerialName(NavArgs.SOURCE) val source: String,
295+
// Omitted from the arguments when null, like the Nav2 sample.
296+
@EncodeDefault(EncodeDefault.Mode.NEVER)
297+
@SerialName(NavArgs.CAMPAIGN)
281298
val campaign: String? = null,
282299
) : Nav3Route {
283-
override val routeName: String = RouteNames.PRODUCT_DETAIL
284-
override val arguments: Map<String, Any?> =
285-
mapOf(
286-
NavArgs.PRODUCT_ID to productId,
287-
NavArgs.SOURCE to source,
288-
NavArgs.CAMPAIGN to campaign,
289-
)
290-
.filterValues { it != null }
291-
override val previewName: String = "ProductDetail($productId)"
300+
override val previewName: String
301+
get() = "ProductDetail($productId)"
292302
}
293303

294-
data class Checkout(val productId: String) : Nav3Route {
295-
override val routeName: String = RouteNames.CHECKOUT
296-
override val arguments: Map<String, Any?> = mapOf(NavArgs.PRODUCT_ID to productId)
297-
override val previewName: String = "Checkout($productId)"
304+
@Serializable
305+
@SerialName(RouteNames.CHECKOUT)
306+
data class Checkout(@SerialName(NavArgs.PRODUCT_ID) val productId: String) : Nav3Route {
307+
override val previewName: String
308+
get() = "Checkout($productId)"
298309
}
299310

300-
data class Confirmation(val orderId: String) : Nav3Route {
301-
override val routeName: String = RouteNames.CONFIRMATION
302-
override val arguments: Map<String, Any?> = mapOf(NavArgs.ORDER_ID to orderId)
303-
override val previewName: String = "Confirmation($orderId)"
311+
@Serializable
312+
@SerialName(RouteNames.CONFIRMATION)
313+
data class Confirmation(@SerialName(NavArgs.ORDER_ID) val orderId: String) : Nav3Route {
314+
override val previewName: String
315+
get() = "Confirmation($orderId)"
304316
}
305317

306-
data class PromoDialog(val promoId: String) : Nav3Route {
307-
override val routeName: String = RouteNames.PROMO_DIALOG
308-
override val arguments: Map<String, Any?> = mapOf(NavArgs.PROMO_ID to promoId)
309-
override val previewName: String = "PromoDialog($promoId)"
318+
@Serializable
319+
@SerialName(RouteNames.PROMO_DIALOG)
320+
data class PromoDialog(@SerialName(NavArgs.PROMO_ID) val promoId: String) : Nav3Route {
321+
override val previewName: String
322+
get() = "PromoDialog($promoId)"
310323
}
311324

312-
data class ShareSheet(val productId: String) : Nav3Route {
313-
override val routeName: String = RouteNames.SHARE_SHEET
314-
override val arguments: Map<String, Any?> = mapOf(NavArgs.PRODUCT_ID to productId)
315-
override val previewName: String = "ShareSheet($productId)"
325+
@Serializable
326+
@SerialName(RouteNames.SHARE_SHEET)
327+
data class ShareSheet(@SerialName(NavArgs.PRODUCT_ID) val productId: String) : Nav3Route {
328+
override val previewName: String
329+
get() = "ShareSheet($productId)"
316330
}
317331

332+
@Serializable
333+
@SerialName(Performance.NAME)
318334
data class Performance(val index: Int, val generation: Int) : Nav3Route {
319-
override val routeName: String = "Performance"
320-
override val arguments: Map<String, Any?> = mapOf("index" to index, "generation" to generation)
321-
override val previewName: String = "Performance($index:$generation)"
322-
override val performanceSeed: Int = 31 * index + generation
335+
/**
336+
* A constant rather than the mapper's result, so screens and tags don't run the mapper outside
337+
* the performance scenario's timed extraction, e.g. in its disabled baseline.
338+
*/
339+
override val routeName: String
340+
get() = NAME
341+
342+
override val previewName: String
343+
get() = "Performance($index:$generation)"
344+
345+
override val performanceSeed: Int
346+
get() = 31 * index + generation
347+
348+
companion object {
349+
const val NAME = "Performance"
350+
}
323351
}
324352
}
325353

0 commit comments

Comments
 (0)