Skip to content

Commit e97547e

Browse files
committed
feat(nav3): Record why back stack entries are /unknown
An entry recorded as /unknown didn't say why, so outside debug logs a mapper that threw, returned a blank name, or chose not to name an entry all looked the same, as did the built-in mapper's failures, many of which only happen in minified release builds. Record the reason next to the entry as unnamed_reason: entry_unnamed_reason in the back stack context, to_unnamed_reason and from_unnamed_reason on breadcrumbs, and unnamed_reason in the nav transaction's data. The values are mapping_failed, blank_name, and unmapped for any mapper, and not_serializable, generic_type, serializer_not_found, serializer_lookup_threw, or kotlinx_serialization_version for SerialName. A mapper that throws now records mapping_failed as its unnamed reason instead of as its arguments' drop reason, so one cause has one name. SerialName returns null, which is public API, so it reports its reason through an internal per-thread side channel that the converter reads around each mapping call. A custom mapper that forwards SerialName's null forwards its reason too. Each type's reason is cached with its failure. Generic @serializable types are now detected before the serializer lookup, which can't find their serializers either, so that they record generic_type and log a warning that says so.
1 parent 6937bd5 commit e97547e

19 files changed

Lines changed: 903 additions & 125 deletions

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
### Features
1111

1212
- Nav3: Add `BackStackEntryMapper.SerialName`, a convenience mapper that names back stack entries after their kotlinx.serialization serial names ([#6703](https://github.com/getsentry/sentry-java/pull/6703))
13+
- Nav3: Record why a back stack entry is `/unknown`, as `unnamed_reason` next to it in the back stack record (`entry_unnamed_reason`), breadcrumbs (`to_unnamed_reason`, `from_unnamed_reason`), and navigation transactions (e.g., `not_serializable` or `serializer_not_found` for the built-in mappers, `unmapped` if a custom mapper returns `null`)
14+
- A mapper that throws now records `"entry_unnamed_reason": "mapping_failed"` instead of `"dropped_by_sentry": "mapping_failed"` in the entry's arguments
1315

1416
### Fixes
1517

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackEntryMapper.kt‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ public class SentryBackStackEntry(
8888
* `/detail` throws, the back stack record will be `/home -> /unknown -> /settings` rather than
8989
* `/home -> /settings`.
9090
*
91+
* Each "/unknown" entry records why it has no name, as `unnamed_reason` in the back stack record,
92+
* breadcrumbs, and navigation transactions (e.g., `"entry_unnamed_reason": "unmapped"`):
93+
* `mapping_failed` if [map] threw, `blank_name` for a blank name, and `unmapped` if [map] returned
94+
* `null`. The built-in mappers record more specific reasons when they return `null` (see
95+
* [SerialName]), and so does a custom mapper that returns the `null` a built-in mapper returned.
96+
*
9197
* **Choosing appropriate arguments**
9298
*
9399
* Argument values may be any of the following scalar types:
@@ -155,23 +161,29 @@ public fun interface BackStackEntryMapper<in T : Any> {
155161
*
156162
* **Unknown names**
157163
*
158-
* This mapper returns `null` for an entry, which Sentry displays as `"/unknown"`, if:
164+
* This mapper returns `null` for an entry, which Sentry displays as `"/unknown"`, with the
165+
* `unnamed_reason` in parentheses, if:
159166
*
160167
* - the entry class isn't annotated with `@Serializable` itself, even if kotlinx.serialization
161168
* can serialize it without the annotation (e.g., `String`, `List`, an unannotated enum, or a
162-
* class marked with a custom `@MetaSerializable` annotation);
169+
* class marked with a custom `@MetaSerializable` annotation) (`not_serializable`);
163170
*
164171
* - the entry class is generic (e.g., `SettingsScreen<T>`), as its serializer needs type
165-
* arguments that can't be recovered from the entry at runtime; or
166-
*
167-
* - looking up the entry's serializer fails, e.g., because a custom serializer throws, R8
168-
* removed what the lookup needs, or the app's kotlinx.serialization version is incompatible
169-
* with this SDK. In particular, if the app is minified by R8 and uses a kotlinx.serialization
170-
* version below 1.10.0, an entry class with a named companion object (e.g., `companion object
171-
* Factory`) may be named in unminified debug builds but not in minified release builds,
172-
* depending on the kotlinx.serialization version and R8 mode. To fix this, upgrade
173-
* kotlinx.serialization to 1.10.0 or later, or add the keep rules from Troubleshooting
174-
* (linked below).
172+
* arguments that can't be recovered from the entry at runtime (`generic_type`);
173+
*
174+
* - looking up the entry's serializer finds none, e.g., because R8 removed what the lookup
175+
* needs (`serializer_not_found`), or throws, e.g., because a custom serializer throws
176+
* (`serializer_lookup_threw`); or
177+
*
178+
* - the app's kotlinx.serialization version is incompatible with this SDK, in which case every
179+
* entry is unnamed (`kotlinx_serialization_version`).
180+
*
181+
* `serializer_not_found` often only happens in minified release builds. In particular, if the
182+
* app is minified by R8 and uses a kotlinx.serialization version below 1.10.0, an entry class
183+
* with a named companion object (e.g., `companion object Factory`) may be named in unminified
184+
* debug builds but not in minified release builds, depending on the kotlinx.serialization
185+
* version and R8 mode. To fix this, upgrade kotlinx.serialization to 1.10.0 or later, or add
186+
* the keep rules from Troubleshooting (linked below).
175187
*
176188
* The first entry of each class that can't be named logs a warning explaining why, visible when
177189
* the SDK's `debug` option is enabled. For fixes, see

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/SentryNavEffect.kt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ import org.jetbrains.annotations.ApiStatus
5555
* - a screen name
5656
* - a record of the current back stack (last 5 entries)
5757
*
58+
* A destination that can't be named is recorded as "/unknown", with a reason why. See
59+
* [BackStackEntryMapper].
60+
*
5861
* You can configure the above defaults via [SentryNavOptions]. (Screen names can be disabled via
5962
* [SentryOptions.setEnableScreenTracking].)
6063
*

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/BackStackConverter.kt‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.sentry.compose.navigation3.BackStackEntryMapper
77
import io.sentry.compose.navigation3.SentryBackStackEntry
88
import io.sentry.compose.navigation3.internal.backstack.ArgumentDropReason.Companion.ARGUMENT_DROP_REASON_KEY
99
import io.sentry.compose.navigation3.internal.backstack.NormalizedSentryBackStackEntry.Companion.UNKNOWN_ENTRY_NAME
10+
import io.sentry.compose.navigation3.internal.backstack.UnnamedReason.Companion.UNNAMED_REASON_KEY
1011
import io.sentry.util.ExceptionUtils
1112

1213
/**
@@ -65,21 +66,24 @@ internal class BackStackConverter<T : Any>(
6566
sanitizer: ArgumentSanitizer,
6667
warningState: WarningState,
6768
): NormalizedSentryBackStackEntry {
68-
val info =
69+
val (info, reportedReason) =
6970
try {
70-
entryMapper.map(backStackEntry)
71+
UnnamedReasonReports.collect { entryMapper.map(backStackEntry) }
7172
} catch (t: Throwable) {
7273
// Back stack entry mappers are host app callbacks.
7374
ExceptionUtils.rethrowIfFatal(t)
7475
warningState.logMapperFailureWarning(logger, t)
7576
return NormalizedSentryBackStackEntry(
7677
name = UNKNOWN_ENTRY_NAME,
77-
argumentDropReason = ArgumentDropReason.MAPPING_FAILED,
78+
unnamedReason = UnnamedReason.MAPPING_FAILED,
7879
)
7980
}
8081

8182
if (info == null) {
82-
return NormalizedSentryBackStackEntry(name = UNKNOWN_ENTRY_NAME)
83+
return NormalizedSentryBackStackEntry(
84+
name = UNKNOWN_ENTRY_NAME,
85+
unnamedReason = reportedReason ?: UnnamedReason.UNMAPPED,
86+
)
8387
}
8488

8589
val sanitizedArguments =
@@ -97,6 +101,7 @@ internal class BackStackConverter<T : Any>(
97101
name = UNKNOWN_ENTRY_NAME,
98102
arguments = sanitizedArguments.values,
99103
argumentDropReason = sanitizedArguments.dropReason,
104+
unnamedReason = UnnamedReason.BLANK_NAME,
100105
)
101106
} else {
102107
NormalizedSentryBackStackEntry(
@@ -265,6 +270,8 @@ internal data class NormalizedSentryBackStackEntry(
265270
* The reason why the SDK dropped host-provided arguments, or null if no arguments were dropped.
266271
*/
267272
val argumentDropReason: ArgumentDropReason? = null,
273+
/** Why the entry was recorded as [UNKNOWN_ENTRY_NAME], or `null` if it has a name. */
274+
val unnamedReason: UnnamedReason? = null,
268275
) {
269276

270277
companion object {
@@ -306,13 +313,32 @@ internal data class NormalizedSentryBackStackEntry(
306313
* }
307314
* }
308315
* ```
316+
*
317+
* An unnamed entry also has its [unnamedReason], e.g.:
318+
* ```
319+
* {
320+
* "entry": "/unknown"
321+
* "entry_unnamed_reason": "not_serializable"
322+
* }
323+
* ```
309324
*/
310325
fun serialize(): Map<String, Any?> = buildMap {
311326
put("entry", name)
327+
unnamedReason?.let { put("entry_$UNNAMED_REASON_KEY", it.serializedValue) }
312328
argumentsWithMetadata().takeIf { it.isNotEmpty() }?.let { put("entry_arguments", it) }
313329
}
314330
}
315331

332+
/**
333+
* Returns the data to set on the nav transaction of a destination this entry is the top of: its
334+
* `arguments`, as for [NormalizedSentryBackStackEntry.argumentsWithMetadata], and its
335+
* `unnamed_reason`, each if it has any.
336+
*/
337+
internal fun NormalizedSentryBackStackEntry.transactionData(): Map<String, Any> = buildMap {
338+
argumentsWithMetadata().takeIf { it.isNotEmpty() }?.let { put("arguments", it) }
339+
unnamedReason?.let { put(UNNAMED_REASON_KEY, it.serializedValue) }
340+
}
341+
316342
internal fun List<NormalizedSentryBackStackEntry>.serialize(): List<Map<String, Any?>> =
317343
map(NormalizedSentryBackStackEntry::serialize)
318344

@@ -325,7 +351,6 @@ internal enum class ArgumentDropReason(
325351
CHARACTER_LIMIT("max_character_limit_exceeded", exhaustsUpdateBudget = true),
326352
DUPLICATE_KEY("duplicate_key"),
327353
INVALID_STRUCTURE("invalid_structure"),
328-
MAPPING_FAILED("mapping_failed"),
329354
MAX_COUNT("max_argument_count_exceeded", exhaustsUpdateBudget = true),
330355
SANITIZATION_FAILED("sanitization_failed");
331356

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/BackStackObserver.kt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import io.sentry.compose.navigation3.internal.backstack.BackStackConverter.Reten
1515
import io.sentry.compose.navigation3.internal.backstack.PreparedChange.BackStackHasNewTop
1616
import io.sentry.compose.navigation3.internal.backstack.PreparedChange.BackStackHasSameTop
1717
import io.sentry.compose.navigation3.internal.backstack.PreparedChange.BackStackIsEmpty
18+
import io.sentry.compose.navigation3.internal.backstack.UnnamedReason.Companion.UNNAMED_REASON_KEY
1819
import io.sentry.compose.navigation3.internal.lifecycle.INavScope
1920
import io.sentry.compose.navigation3.internal.lifecycle.INavScopes
2021
import io.sentry.compose.navigation3.internal.lifecycle.NAVIGATION_CONTEXT_KEY
@@ -237,7 +238,7 @@ internal class BackStackObserver<T : Any>(
237238

238239
return NavTransactionRequest.forDestination(
239240
name = top.name,
240-
arguments = top.argumentsWithMetadata(),
241+
data = top.transactionData(),
241242
contexts = navContext.forTransaction(backStack.capturedEntriesNormalized),
242243
viewNames = if (scopes.options.isEnableScreenTracking) listOf(top.name) else null,
243244
options = scopes.options,
@@ -362,6 +363,7 @@ private class NavBreadcrumbs(private val scopes: INavScopes) {
362363

363364
fromEntry?.let { entry ->
364365
data["from"] = entry.name
366+
entry.unnamedReason?.let { data["from_$UNNAMED_REASON_KEY"] = it.serializedValue }
365367
entry
366368
.argumentsWithMetadata()
367369
.takeIf { it.isNotEmpty() }
@@ -371,6 +373,7 @@ private class NavBreadcrumbs(private val scopes: INavScopes) {
371373
}
372374

373375
data["to"] = toEntry.name
376+
toEntry.unnamedReason?.let { data["to_$UNNAMED_REASON_KEY"] = it.serializedValue }
374377
toEntry
375378
.argumentsWithMetadata()
376379
.takeIf { it.isNotEmpty() }

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/BuiltInBackStackEntryMappers.kt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ internal class DeferredBackStackEntryMapper(
9191
/**
9292
* A fallback for when a built-in [BackStackEntryMapper] isn't supported by the host app.
9393
*
94-
* Maps every back stack entry to `null`, which Sentry records as [UNKNOWN_ENTRY_NAME]. Logs once
95-
* per entry type.
94+
* Maps every back stack entry to `null`, which Sentry records as [UNKNOWN_ENTRY_NAME] with
95+
* [UnnamedReason.KOTLINX_SERIALIZATION_VERSION]. Logs once per entry type.
9696
*/
9797
internal class UnavailableBackStackEntryMapper(
9898
private val logger: () -> ILogger,
@@ -113,6 +113,7 @@ internal class UnavailableBackStackEntryMapper(
113113
)
114114
}
115115

116+
UnnamedReasonReports.report(UnnamedReason.KOTLINX_SERIALIZATION_VERSION)
116117
return null
117118
}
118119
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package io.sentry.compose.navigation3.internal.backstack
2+
3+
import io.sentry.compose.navigation3.BackStackEntryMapper
4+
import io.sentry.compose.navigation3.internal.backstack.NormalizedSentryBackStackEntry.Companion.UNKNOWN_ENTRY_NAME
5+
6+
/**
7+
* Why a back stack entry was recorded as [UNKNOWN_ENTRY_NAME], recorded next to it under
8+
* [UNNAMED_REASON_KEY] so that the reason reaches Sentry without debug logs.
9+
*
10+
* Values contain no app data or class names.
11+
*/
12+
internal enum class UnnamedReason(val serializedValue: String) {
13+
14+
/** The mapper returned a name that's empty after trimming. The entry's arguments are kept. */
15+
BLANK_NAME("blank_name"),
16+
17+
/** A built-in mapper can't name the entry, as its `@Serializable` class has type parameters. */
18+
GENERIC_TYPE("generic_type"),
19+
20+
/**
21+
* The built-in mappers couldn't be created, as the app's kotlinx.serialization version can't be
22+
* linked or failed their compatibility check.
23+
*/
24+
KOTLINX_SERIALIZATION_VERSION("kotlinx_serialization_version"),
25+
26+
/** The mapper threw. */
27+
MAPPING_FAILED("mapping_failed"),
28+
29+
/** A built-in mapper can't name the entry, as its class isn't annotated with `@Serializable`. */
30+
NOT_SERIALIZABLE("not_serializable"),
31+
32+
/**
33+
* A built-in mapper can't name the entry, as reading its class's annotations, serializer, or
34+
* descriptor threw.
35+
*/
36+
SERIALIZER_LOOKUP_THREW("serializer_lookup_threw"),
37+
38+
/**
39+
* A built-in mapper can't name the entry, as looking up its `@Serializable` class's serializer
40+
* found none, e.g., because R8 removed what the lookup needs.
41+
*/
42+
SERIALIZER_NOT_FOUND("serializer_not_found"),
43+
44+
/**
45+
* The mapper returned `null` without a built-in mapper reporting why, i.e., a custom mapper chose
46+
* not to name the entry.
47+
*/
48+
UNMAPPED("unmapped");
49+
50+
companion object {
51+
const val UNNAMED_REASON_KEY: String = "unnamed_reason"
52+
}
53+
}
54+
55+
/**
56+
* Passes the [UnnamedReason] a built-in [BackStackEntryMapper] knows to the back stack converter,
57+
* as the mapper reports a failure by returning `null`, which is public API.
58+
*
59+
* A built-in mapper [report]s the reason right before returning `null`, and the converter
60+
* [collect]s it around each call to the app's mapper. So a custom mapper that forwards a built-in
61+
* mapper's `null` forwards its reason too, while one that returns `null` itself reports nothing,
62+
* and one that returns a name has its reason ignored. If a custom mapper calls built-in mappers
63+
* several times for one entry, the last reason reported is kept.
64+
*
65+
* Kept per thread, as mappers run on the converter's thread.
66+
*/
67+
internal object UnnamedReasonReports {
68+
69+
private val reported = ThreadLocal<UnnamedReason?>()
70+
71+
/** Reports why the built-in mapper that's about to return `null` can't name its entry. */
72+
fun report(reason: UnnamedReason) {
73+
reported.set(reason)
74+
}
75+
76+
/**
77+
* Runs [map] and returns its result with the last reason reported while it ran, or `null` if none
78+
* was. Clears the report before and after, even if [map] throws, so a reason can't carry over to
79+
* another entry.
80+
*/
81+
fun <R> collect(map: () -> R): Pair<R, UnnamedReason?> {
82+
reported.remove()
83+
try {
84+
val result = map()
85+
return result to reported.get()
86+
} finally {
87+
reported.remove()
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)