Skip to content
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

## Unreleased

### Improvements

- Nav3: Record why a back stack entry is `/unknown` via an `unknown_entry_reason` next to it in the Sentry UI ([#6801](https://github.com/getsentry/sentry-java/pull/6801))
- A `BackStackEntryMapper` that throws now records `"unknown_entry_reason": "mapping_failed"` instead of `"dropped_by_sentry": "mapping_failed"` in the entry's arguments.

### Fixes

- Session Replay: Mask Compose images loaded with Coil 3.1+ `AsyncImage` when `maskAllImages` is enabled ([#6759](https://github.com/getsentry/sentry-java/pull/6759))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public class SentryBackStackEntry(
* `/detail` throws, the back stack record will be `/home -> /unknown -> /settings` rather than
* `/home -> /settings`.
*
* Each "/unknown" entry records why it has no name under `unknown_entry_reason` (e.g.,
* `"mapping_failed"` if [map] threw).
*
* **Choosing appropriate arguments**
*
* Argument values may be any of the following scalar types:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import io.sentry.compose.navigation3.internal.contract.ArgumentDropReason
import io.sentry.compose.navigation3.internal.contract.ArgumentDropReason.Companion.ARGUMENT_DROP_REASON_KEY
import io.sentry.compose.navigation3.internal.contract.BACK_STACK_ENTRY_ARGUMENTS_KEY
import io.sentry.compose.navigation3.internal.contract.BACK_STACK_ENTRY_KEY
import io.sentry.compose.navigation3.internal.contract.TRANSACTION_ARGUMENTS_KEY
import io.sentry.compose.navigation3.internal.contract.UNKNOWN_ENTRY_NAME
import io.sentry.compose.navigation3.internal.contract.UnknownEntryReason
import io.sentry.compose.navigation3.internal.contract.UnknownEntryReason.Companion.UNKNOWN_ENTRY_REASON_KEY
import io.sentry.util.ExceptionUtils
import java.util.IdentityHashMap

Expand Down Expand Up @@ -81,12 +84,15 @@ internal class BackStackConverter<T : Any>(
warningState.logMapperFailureWarning(logger, t)
return NormalizedSentryBackStackEntry(
name = UNKNOWN_ENTRY_NAME,
argumentDropReason = ArgumentDropReason.MAPPING_FAILED,
unknownEntryReason = UnknownEntryReason.MAPPING_FAILED,
)
}

if (info == null) {
return NormalizedSentryBackStackEntry(name = UNKNOWN_ENTRY_NAME)
return NormalizedSentryBackStackEntry(
name = UNKNOWN_ENTRY_NAME,
unknownEntryReason = UnknownEntryReason.UNMAPPED,
)
}

val sanitizedArguments =
Expand All @@ -104,6 +110,7 @@ internal class BackStackConverter<T : Any>(
name = UNKNOWN_ENTRY_NAME,
arguments = sanitizedArguments.values,
argumentDropReason = sanitizedArguments.dropReason,
unknownEntryReason = UnknownEntryReason.BLANK_NAME,
)
} else {
NormalizedSentryBackStackEntry(
Expand Down Expand Up @@ -376,6 +383,8 @@ internal data class NormalizedSentryBackStackEntry(
* The reason why the SDK dropped host-provided arguments, or null if no arguments were dropped.
*/
val argumentDropReason: ArgumentDropReason? = null,
/** Why the entry was recorded as [UNKNOWN_ENTRY_NAME], or `null` if it has a name. */
val unknownEntryReason: UnknownEntryReason? = null,
) {

companion object {
Expand Down Expand Up @@ -415,15 +424,30 @@ internal data class NormalizedSentryBackStackEntry(
* }
* }
* ```
*
* An unnamed entry also has its [unknownEntryReason], e.g.:
* ```
* {
* "entry": "/unknown"
* "unknown_entry_reason": "unmapped"
* }
* ```
*/
fun serialize(): Map<String, Any?> = buildMap {
put(BACK_STACK_ENTRY_KEY, name)
unknownEntryReason?.let { put(UNKNOWN_ENTRY_REASON_KEY, it.serializedValue) }
argumentsWithMetadata()
.takeIf { it.isNotEmpty() }
?.let { put(BACK_STACK_ENTRY_ARGUMENTS_KEY, it) }
}
}

/** Returns the data to set on the nav transaction of a destination this entry is the top of. */
internal fun NormalizedSentryBackStackEntry.transactionData(): Map<String, Any> = buildMap {
argumentsWithMetadata().takeIf { it.isNotEmpty() }?.let { put(TRANSACTION_ARGUMENTS_KEY, it) }
unknownEntryReason?.let { put(UNKNOWN_ENTRY_REASON_KEY, it.serializedValue) }
}

internal fun List<NormalizedSentryBackStackEntry>.serialize(): List<Map<String, Any?>> =
map(NormalizedSentryBackStackEntry::serialize)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import io.sentry.compose.navigation3.internal.backstack.PreparedChange.BackStack
import io.sentry.compose.navigation3.internal.contract.BACK_STACK_KEY
import io.sentry.compose.navigation3.internal.contract.BREADCRUMB_FROM_ARGUMENTS_KEY
import io.sentry.compose.navigation3.internal.contract.BREADCRUMB_FROM_KEY
import io.sentry.compose.navigation3.internal.contract.BREADCRUMB_FROM_UNKNOWN_ENTRY_REASON_KEY
import io.sentry.compose.navigation3.internal.contract.BREADCRUMB_TO_ARGUMENTS_KEY
import io.sentry.compose.navigation3.internal.contract.BREADCRUMB_TO_KEY
import io.sentry.compose.navigation3.internal.contract.BREADCRUMB_TO_UNKNOWN_ENTRY_REASON_KEY
import io.sentry.compose.navigation3.internal.contract.NAVIGATION_CONTEXT_KEY
import io.sentry.compose.navigation3.internal.contract.NAVIGATION_OP
import io.sentry.compose.navigation3.internal.lifecycle.INavScope
Expand Down Expand Up @@ -242,7 +244,7 @@ internal class BackStackObserver<T : Any>(

return NavTransactionRequest.forDestination(
name = top.name,
arguments = top.argumentsWithMetadata(),
data = top.transactionData(),
contexts = navContext.forTransaction(backStack.capturedEntriesNormalized),
viewNames = if (scopes.options.isEnableScreenTracking) listOf(top.name) else null,
options = scopes.options,
Expand Down Expand Up @@ -363,6 +365,9 @@ private class NavBreadcrumbs(private val scopes: INavScopes) {

fromEntry?.let { entry ->
data[BREADCRUMB_FROM_KEY] = entry.name
entry.unknownEntryReason?.let {
data[BREADCRUMB_FROM_UNKNOWN_ENTRY_REASON_KEY] = it.serializedValue
}
entry
.argumentsWithMetadata()
.takeIf { it.isNotEmpty() }
Expand All @@ -372,6 +377,9 @@ private class NavBreadcrumbs(private val scopes: INavScopes) {
}

data[BREADCRUMB_TO_KEY] = toEntry.name
toEntry.unknownEntryReason?.let {
data[BREADCRUMB_TO_UNKNOWN_ENTRY_REASON_KEY] = it.serializedValue
}
toEntry
.argumentsWithMetadata()
.takeIf { it.isNotEmpty() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,37 @@ internal const val BREADCRUMB_FROM_KEY = "from"
internal const val BREADCRUMB_TO_KEY = "to"
internal const val BREADCRUMB_FROM_ARGUMENTS_KEY = "from_arguments"
internal const val BREADCRUMB_TO_ARGUMENTS_KEY = "to_arguments"
internal const val BREADCRUMB_FROM_UNKNOWN_ENTRY_REASON_KEY = "from_unknown_entry_reason"
internal const val BREADCRUMB_TO_UNKNOWN_ENTRY_REASON_KEY = "to_unknown_entry_reason"

internal const val NAVIGATION_OP = "navigation"
internal const val NAVIGATION_TRANSACTION_ORIGIN = "auto.navigation.nav3"

/** The string displayed in the Sentry UI if the name of a back stack entry can't be determined. */
internal const val UNKNOWN_ENTRY_NAME = "/unknown"

/**
* Why a back stack entry was reported as [UNKNOWN_ENTRY_NAME].
*
* Recorded under [UNKNOWN_ENTRY_REASON_KEY] (prefixed with `to_` or `from_` on breadcrumbs).
*/
internal enum class UnknownEntryReason(val serializedValue: String) {

/** The mapper returned a name that was empty after trimming. */
BLANK_NAME("blank_name"),

/** The mapper threw. */
MAPPING_FAILED("mapping_failed"),

/** The mapper returned `null`, i.e., it chose not to name the entry. */
UNMAPPED("unmapped");

companion object {

const val UNKNOWN_ENTRY_REASON_KEY: String = "unknown_entry_reason"
}
}

/**
* Why host-provided arguments were unavailable in emitted nav state.
*
Expand All @@ -41,7 +65,6 @@ internal enum class ArgumentDropReason(
CHARACTER_LIMIT("max_character_limit_exceeded", exhaustsUpdateBudget = true),
DUPLICATE_KEY("duplicate_key"),
INVALID_STRUCTURE("invalid_structure"),
MAPPING_FAILED("mapping_failed"),
MAX_COUNT("max_argument_count_exceeded", exhaustsUpdateBudget = true),
SANITIZATION_FAILED("sanitization_failed");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import io.sentry.TransactionContext
import io.sentry.TransactionOptions
import io.sentry.compose.navigation3.internal.contract.NAVIGATION_OP
import io.sentry.compose.navigation3.internal.contract.NAVIGATION_TRANSACTION_ORIGIN
import io.sentry.compose.navigation3.internal.contract.TRANSACTION_ARGUMENTS_KEY
import io.sentry.protocol.App
import io.sentry.protocol.Contexts
import io.sentry.protocol.TransactionNameSource
Expand Down Expand Up @@ -44,7 +43,7 @@ internal class NavTransactionRequest(
/** Creates a request whose transaction options are read when each transaction starts. */
fun forDestination(
name: String,
arguments: Map<String, Any?>,
data: Map<String, Any>,
contexts: Map<String, Any>,
viewNames: List<String>?,
options: SentryOptions,
Expand All @@ -61,8 +60,7 @@ internal class NavTransactionRequest(
it.origin = NAVIGATION_TRANSACTION_ORIGIN
}
},
data =
if (arguments.isNotEmpty()) mapOf(TRANSACTION_ARGUMENTS_KEY to arguments) else emptyMap(),
data = data,
contexts = contexts,
viewNames = viewNames,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.sentry.compose.navigation3.SentryBackStackEntry
import io.sentry.compose.navigation3.internal.backstack.BackStackConverter.RetentionPolicy
import io.sentry.compose.navigation3.internal.contract.ArgumentDropReason
import io.sentry.compose.navigation3.internal.contract.UNKNOWN_ENTRY_NAME
import io.sentry.compose.navigation3.internal.contract.UnknownEntryReason
import java.io.StringWriter
import kotlin.test.assertFailsWith
import org.junit.Test
Expand Down Expand Up @@ -157,7 +158,14 @@ class BackStackConverterTest {
}

@Test
fun `convert() - returns unknown name with arguments if mapper name is blank`() {
fun `convert() - returns a named entry without an unknown entry reason if mapper returns a name`() {
val converter = newConverter()

assertThat(converter.convert(HomeScreen()).unknownEntryReason).isNull()
}

@Test
fun `convert() - returns unknown name with the blank name reason and arguments if mapper name is blank`() {
val converter =
newConverter(entryMapper = { SentryBackStackEntry(" ", mapOf("userId" to "123")) })

Expand All @@ -166,6 +174,7 @@ class BackStackConverterTest {
NormalizedSentryBackStackEntry(
name = "/unknown",
arguments = mapOf("userId" to "123"),
unknownEntryReason = UnknownEntryReason.BLANK_NAME,
),
)
verify(logger)
Expand All @@ -187,6 +196,7 @@ class BackStackConverterTest {
NormalizedSentryBackStackEntry(
name = "/unknown",
argumentDropReason = ArgumentDropReason.MAX_COUNT,
unknownEntryReason = UnknownEntryReason.BLANK_NAME,
),
)
}
Expand All @@ -212,22 +222,27 @@ class BackStackConverterTest {
}

@Test
fun `convert() - returns unknown name without arguments if mapper returns null`() {
fun `convert() - returns unknown name with the unmapped reason if mapper returns null`() {
val converter = newConverter(entryMapper = { null })

assertThat(converter.convert(HomeScreen()))
.isEqualTo(NormalizedSentryBackStackEntry(UNKNOWN_ENTRY_NAME))
.isEqualTo(
NormalizedSentryBackStackEntry(
UNKNOWN_ENTRY_NAME,
unknownEntryReason = UnknownEntryReason.UNMAPPED,
),
)
}

@Test
fun `convert() - returns unknown name with the mapping failed drop reason if mapper throws`() {
fun `convert() - returns unknown name with the mapping failed reason and no arguments if mapper throws`() {
val converter = newConverter(entryMapper = { error("boom") })

assertThat(converter.convert(HomeScreen()))
.isEqualTo(
NormalizedSentryBackStackEntry(
UNKNOWN_ENTRY_NAME,
argumentDropReason = ArgumentDropReason.MAPPING_FAILED,
unknownEntryReason = UnknownEntryReason.MAPPING_FAILED,
),
)
verify(logger)
Expand Down
Loading
Loading