-
-
Notifications
You must be signed in to change notification settings - Fork 475
Feat/androidx navigation tracing #2137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
romtsn
merged 39 commits into
feat/compose-navigation-support
from
feat/androidx-navigation-tracing
Jun 29, 2022
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
e3bd4b1
Add compose module
romtsn 15085f3
Add sentry-android-navigation module for jetpack.navigation support
romtsn 4076929
Compose sample + tweaks
romtsn 2d64f04
Merge branch 'main' into feat/navigation-breadcrumbs
romtsn 962466a
feat: androidx-navigation support
romtsn 9ab0299
feat: androidx-compose support for navigation
romtsn 9660cd1
Merge branch 'feat/androidx-navigation' into feat/androidx-compose
romtsn 048df40
api dump
romtsn 608d751
Compose sample + tweaks
romtsn ddbd29d
Fix compose activity
romtsn 40372df
api dump
romtsn db06058
Address PR reviews
romtsn 29c8107
Add JvmOverloads for java-interop
romtsn bf7bce2
Add tests
romtsn 1c87f90
Switch to compileOnly navigation-runtime dep
romtsn 3ec43e0
Add runtime dep in tests
romtsn 6a3c581
Merge branch 'feat/androidx-navigation' into feat/androidx-compose
romtsn 28e2606
Address PR reviews
romtsn 8133b05
Add kotlin language verison for compatibility
romtsn 721c35c
Merge branch 'feat/androidx-navigation' into feat/androidx-compose
romtsn 3426dfd
Merge branch 'feat/androidx-compose' into feat/compose-sample
romtsn 828fbcf
Fix test
romtsn a474427
Merge branch 'feat/androidx-compose' into feat/compose-sample
romtsn 6815f62
Rename extension method to withSentryObservableEffect
romtsn 4e23337
Merge branch 'feat/androidx-compose' into feat/compose-sample
romtsn 47f315d
Update sample
romtsn dc1e5fa
api dump
romtsn a35a0e1
Merge branch 'feat/androidx-compose' into feat/compose-sample
romtsn 2cf6c87
Navigation tracing WIP
romtsn 65654d3
Finalize nav tracing
romtsn 6cf851f
Set nav tx to scope and refine the name of it
romtsn 815c346
Add tests
romtsn d54672e
spotless & api
romtsn 2c0fcc8
Merge branch 'feat/compose-navigation-support' into feat/androidx-nav…
romtsn 1e2c0af
Fix detekt
romtsn 9ad489a
Format code
getsentry-bot c5ceca7
Add new modules to craft.yml (#2138)
romtsn 02facea
Attach hint
romtsn fd5b2e0
Address PR reviews
romtsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| style: | ||
| ReturnCount: | ||
| max: 3 | ||
|
marandaneto marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 109 additions & 22 deletions
131
...android-navigation/src/main/java/io/sentry/android/navigation/SentryNavigationListener.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,64 +1,151 @@ | ||
| package io.sentry.android.navigation | ||
|
|
||
| import android.content.res.Resources.NotFoundException | ||
| import android.os.Bundle | ||
| import androidx.navigation.NavController | ||
| import androidx.navigation.NavDestination | ||
| import io.sentry.Breadcrumb | ||
| import io.sentry.Hint | ||
| import io.sentry.HubAdapter | ||
| import io.sentry.IHub | ||
| import io.sentry.ITransaction | ||
| import io.sentry.SentryLevel.DEBUG | ||
| import io.sentry.SentryLevel.INFO | ||
| import io.sentry.SpanStatus | ||
| import io.sentry.TypeCheckHint | ||
| import java.lang.ref.WeakReference | ||
|
|
||
| class SentryNavigationListener @JvmOverloads constructor( | ||
| private val hub: IHub = HubAdapter.getInstance() | ||
| private val hub: IHub = HubAdapter.getInstance(), | ||
| private val enableNavigationBreadcrumbs: Boolean = true, | ||
| private val enableNavigationTracing: Boolean = true | ||
| ) : NavController.OnDestinationChangedListener { | ||
|
|
||
| private var previousDestinationRef: WeakReference<NavDestination>? = null | ||
| private var previousArgs: Bundle? = null | ||
|
|
||
| private val isPerformanceEnabled get() = hub.options.isTracingEnabled && enableNavigationTracing | ||
|
|
||
| private var activeTransaction: ITransaction? = null | ||
|
|
||
| override fun onDestinationChanged( | ||
| controller: NavController, | ||
| destination: NavDestination, | ||
| arguments: Bundle? | ||
| ) { | ||
| addBreadcrumb(destination, arguments) | ||
| val toArguments = arguments.refined() | ||
|
|
||
| addBreadcrumb(destination, toArguments) | ||
| startTracing(controller, destination, toArguments) | ||
| previousDestinationRef = WeakReference(destination) | ||
| previousArgs = arguments | ||
| } | ||
|
|
||
| private fun addBreadcrumb(destination: NavDestination, arguments: Bundle?) { | ||
| private fun addBreadcrumb(destination: NavDestination, arguments: Map<String, Any?>) { | ||
| if (!enableNavigationBreadcrumbs) { | ||
| return | ||
| } | ||
| val breadcrumb = Breadcrumb().apply { | ||
| type = "navigation" | ||
| category = "navigation" | ||
| type = NAVIGATION_OP | ||
| category = NAVIGATION_OP | ||
|
|
||
| val from = previousDestinationRef?.get()?.route | ||
| from?.let { data["from"] = it } | ||
| previousArgs?.let { args -> | ||
| val fromArguments = args.keySet().filter { | ||
| it != NavController.KEY_DEEP_LINK_INTENT // there's a lot of unrelated stuff | ||
| }.associateWith { args[it] } | ||
| if (fromArguments.isNotEmpty()) { | ||
| data["from_arguments"] = fromArguments | ||
| } | ||
| from?.let { data["from"] = "/$it" } | ||
| val fromArguments = previousArgs.refined() | ||
| if (fromArguments.isNotEmpty()) { | ||
| data["from_arguments"] = fromArguments | ||
| } | ||
|
|
||
| val to = destination.route | ||
| to?.let { data["to"] = it } | ||
| arguments?.let { args -> | ||
| val toArguments = args.keySet().filter { | ||
| it != NavController.KEY_DEEP_LINK_INTENT // there's a lot of unrelated stuff | ||
| }.associateWith { args[it] } | ||
| if (toArguments.isNotEmpty()) { | ||
| data["to_arguments"] = toArguments | ||
| } | ||
| to?.let { data["to"] = "/$it" } | ||
| if (arguments.isNotEmpty()) { | ||
| data["to_arguments"] = arguments | ||
| } | ||
|
|
||
| level = INFO | ||
| } | ||
| val hint = Hint() | ||
| hint.set(TypeCheckHint.ANDROID_NAV_DESTINATION, destination) | ||
| hub.addBreadcrumb(breadcrumb) | ||
| hub.addBreadcrumb(breadcrumb, hint) | ||
| } | ||
|
|
||
| private fun startTracing( | ||
| controller: NavController, | ||
| destination: NavDestination, | ||
| arguments: Map<String, Any?> | ||
| ) { | ||
| if (!isPerformanceEnabled) { | ||
| return | ||
| } | ||
|
|
||
| // we can only have one nav transaction at a time | ||
| if (activeTransaction != null) { | ||
| stopTracing() | ||
| } | ||
|
|
||
| if (destination.navigatorName == "activity") { | ||
| // we do not trace navigation between activities to avoid clashing with activity lifecycle tracing | ||
| hub.options.logger.log( | ||
| DEBUG, | ||
| "Navigating to activity destination, no transaction captured." | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| @Suppress("SwallowedException") // we swallow it on purpose | ||
| var name = destination.route ?: try { | ||
| controller.context.resources.getResourceEntryName(destination.id) | ||
| } catch (e: NotFoundException) { | ||
| hub.options.logger.log( | ||
| DEBUG, | ||
| "Destination id cannot be retrieved from Resources, no transaction captured." | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| // we add '/' to the name to match dart and web pattern | ||
| name = "/" + name.substringBefore('/') // strip out arguments from the tx name | ||
|
romtsn marked this conversation as resolved.
|
||
|
|
||
| val transaction = | ||
| hub.startTransaction(name, NAVIGATION_OP, true, hub.options.idleTimeout, true) | ||
|
romtsn marked this conversation as resolved.
|
||
|
|
||
| if (arguments.isNotEmpty()) { | ||
| transaction.setData("arguments", arguments) | ||
| } | ||
| hub.configureScope { scope -> | ||
| scope.withTransaction { tx -> | ||
| if (tx == null) { | ||
| scope.transaction = transaction | ||
| } | ||
| } | ||
| } | ||
| activeTransaction = transaction | ||
| } | ||
|
|
||
| private fun stopTracing() { | ||
| val status = activeTransaction?.status ?: SpanStatus.OK | ||
| activeTransaction?.finish(status) | ||
|
|
||
| // clear transaction from scope so others can bind to it | ||
| hub.configureScope { scope -> | ||
| scope.withTransaction { tx -> | ||
| if (tx == activeTransaction) { | ||
| scope.clearTransaction() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| activeTransaction = null | ||
| } | ||
|
|
||
| private fun Bundle?.refined(): Map<String, Any?> = | ||
| this?.let { args -> | ||
| args.keySet().filter { | ||
| it != NavController.KEY_DEEP_LINK_INTENT // there's a lot of unrelated stuff | ||
| }.associateWith { args[it] } | ||
| } ?: emptyMap() | ||
|
|
||
| companion object { | ||
| const val NAVIGATION_OP = "navigation" | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.