Skip to content

Commit cfbf6ca

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 unknown_entry_reason in the back stack context and the nav transaction's data, and as to_unknown_entry_reason and from_unknown_entry_reason on breadcrumbs. 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 unknown entry 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 cfbf6ca

19 files changed

Lines changed: 927 additions & 124 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 `unknown_entry_reason` next to it in the back stack record and navigation transactions, and as `to_unknown_entry_reason` and `from_unknown_entry_reason` on breadcrumbs (e.g., `not_serializable` or `serializer_not_found` for the built-in mappers, `unmapped` if a custom mapper returns `null`) ([#6781](https://github.com/getsentry/sentry-java/pull/6781))
14+
- A mapper that throws now records `"unknown_entry_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: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ 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 `unknown_entry_reason` in the back stack
92+
* record and navigation transactions, and as `to_unknown_entry_reason` or
93+
* `from_unknown_entry_reason` on breadcrumbs (e.g., `"unknown_entry_reason": "unmapped"`):
94+
* `mapping_failed` if [map] threw, `blank_name` for a blank name, and `unmapped` if [map] returned
95+
* `null`. The built-in mappers record more specific reasons when they return `null` (see
96+
* [SerialName]), and so does a custom mapper that returns the `null` a built-in mapper returned.
97+
*
9198
* **Choosing appropriate arguments**
9299
*
93100
* Argument values may be any of the following scalar types:
@@ -155,23 +162,29 @@ public fun interface BackStackEntryMapper<in T : Any> {
155162
*
156163
* **Unknown names**
157164
*
158-
* This mapper returns `null` for an entry, which Sentry displays as `"/unknown"`, if:
165+
* This mapper returns `null` for an entry, which Sentry displays as `"/unknown"`, with the
166+
* `unknown_entry_reason` in parentheses, if:
159167
*
160168
* - the entry class isn't annotated with `@Serializable` itself, even if kotlinx.serialization
161169
* can serialize it without the annotation (e.g., `String`, `List`, an unannotated enum, or a
162-
* class marked with a custom `@MetaSerializable` annotation);
170+
* class marked with a custom `@MetaSerializable` annotation) (`not_serializable`);
163171
*
164172
* - 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).
173+
* arguments that can't be recovered from the entry at runtime (`generic_type`);
174+
*
175+
* - looking up the entry's serializer finds none, e.g., because R8 removed what the lookup
176+
* needs (`serializer_not_found`), or throws, e.g., because a custom serializer throws
177+
* (`serializer_lookup_threw`); or
178+
*
179+
* - the app's kotlinx.serialization version is incompatible with this SDK, in which case every
180+
* entry is unnamed (`kotlinx_serialization_version`).
181+
*
182+
* `serializer_not_found` often only happens in minified release builds. In particular, if the
183+
* app is minified by R8 and uses a kotlinx.serialization version below 1.10.0, an entry class
184+
* with a named companion object (e.g., `companion object Factory`) may be named in unminified
185+
* debug builds but not in minified release builds, depending on the kotlinx.serialization
186+
* version and R8 mode. To fix this, upgrade kotlinx.serialization to 1.10.0 or later, or add
187+
* the keep rules from Troubleshooting (linked below).
175188
*
176189
* The first entry of each class that can't be named logs a warning explaining why, visible when
177190
* 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.UnknownEntryReason.Companion.UNKNOWN_ENTRY_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+
UnknownEntryReasonReports.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+
unknownEntryReason = UnknownEntryReason.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+
unknownEntryReason = reportedReason ?: UnknownEntryReason.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+
unknownEntryReason = UnknownEntryReason.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 unknownEntryReason: UnknownEntryReason? = 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 [unknownEntryReason], e.g.:
318+
* ```
319+
* {
320+
* "entry": "/unknown"
321+
* "unknown_entry_reason": "not_serializable"
322+
* }
323+
* ```
309324
*/
310325
fun serialize(): Map<String, Any?> = buildMap {
311326
put("entry", name)
327+
unknownEntryReason?.let { put(UNKNOWN_ENTRY_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+
* `unknown_entry_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+
unknownEntryReason?.let { put(UNKNOWN_ENTRY_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: 8 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.UnknownEntryReason.Companion.UNKNOWN_ENTRY_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,9 @@ private class NavBreadcrumbs(private val scopes: INavScopes) {
362363

363364
fromEntry?.let { entry ->
364365
data["from"] = entry.name
366+
entry.unknownEntryReason?.let {
367+
data["from_$UNKNOWN_ENTRY_REASON_KEY"] = it.serializedValue
368+
}
365369
entry
366370
.argumentsWithMetadata()
367371
.takeIf { it.isNotEmpty() }
@@ -371,6 +375,9 @@ private class NavBreadcrumbs(private val scopes: INavScopes) {
371375
}
372376

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

0 commit comments

Comments
 (0)