-
-
Notifications
You must be signed in to change notification settings - Fork 359
feat(tracing): Correlate deep links with the navigation they trigger #6264
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
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ff90614
feat(tracing): Correlate deep links with the navigation they trigger
alwx a2c3a6a
fix(tracing): Address PR review on deep-link / navigation correlation
alwx 9341912
Merge branch 'main' into alwx/features/deep-linking
alwx d728832
fix(tracing): Differentiate cold-start vs warm-open deep link attribuโฆ
alwx 2758a72
fix(tracing): Reject warm-open links that arrived before the dispatch
alwx 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
Some comments aren't visible on the classic Files Changed page.
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
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,50 @@ | ||||||||||||||||||
| /** | ||||||||||||||||||
| * Cross-module hand-off between the {@link deeplinkIntegration} and the | ||||||||||||||||||
| * {@link reactNavigationIntegration} idle navigation span. | ||||||||||||||||||
| * | ||||||||||||||||||
| * When a deep link is received (either via `Linking.getInitialURL()` on cold | ||||||||||||||||||
| * start or via the `'url'` event on warm open), the integration stores the | ||||||||||||||||||
| * raw URL together with a receive timestamp here. The navigation integration | ||||||||||||||||||
| * then attaches it to the next idle navigation span started within | ||||||||||||||||||
| * `routeChangeTimeoutMs` (default 1000ms), so traces can correlate | ||||||||||||||||||
| * "deep link โ navigation" timing. | ||||||||||||||||||
| */ | ||||||||||||||||||
|
|
||||||||||||||||||
| export interface PendingDeepLink { | ||||||||||||||||||
| /** Raw URL as received from React Native's `Linking` API. */ | ||||||||||||||||||
| url: string; | ||||||||||||||||||
| /** Wall-clock timestamp (ms since epoch) when the URL was received. */ | ||||||||||||||||||
| receivedAtMs: number; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| let pending: PendingDeepLink | undefined; | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Stores the most recently received deep link URL together with the current | ||||||||||||||||||
| * timestamp. Overwrites any previous pending value โ only the latest link | ||||||||||||||||||
| * matters for correlation with the next navigation. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export function setPendingDeepLink(url: string): void { | ||||||||||||||||||
| pending = { url, receivedAtMs: Date.now() }; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Returns and clears the pending deep link, but only if it was received | ||||||||||||||||||
| * within `maxAgeMs` of "now". Stale entries are discarded. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export function consumePendingDeepLink(maxAgeMs: number): PendingDeepLink | undefined { | ||||||||||||||||||
| const value = pending; | ||||||||||||||||||
| pending = undefined; | ||||||||||||||||||
| if (!value) { | ||||||||||||||||||
| return undefined; | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+114
to
+117
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about clearing pending only when it is defined?
Suggested change
|
||||||||||||||||||
| if (Date.now() - value.receivedAtMs > maxAgeMs) { | ||||||||||||||||||
| return undefined; | ||||||||||||||||||
| } | ||||||||||||||||||
| return value; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** Test helper โ clears the pending value without consuming it. */ | ||||||||||||||||||
| export function clearPendingDeepLink(): void { | ||||||||||||||||||
| pending = undefined; | ||||||||||||||||||
| } | ||||||||||||||||||
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 |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ import type { UnsafeAction } from '../vendor/react-navigation/types'; | |
| import type { ReactNativeTracingIntegration } from './reactnativetracing'; | ||
|
|
||
| import { getAppRegistryIntegration } from '../integrations/appRegistry'; | ||
| import { sanitizeDeepLinkUrl } from '../integrations/deeplink'; | ||
| import { isSentrySpan } from '../utils/span'; | ||
| import { RN_GLOBAL_OBJ } from '../utils/worldwide'; | ||
| import { NATIVE } from '../wrapper'; | ||
|
|
@@ -27,6 +28,7 @@ import { | |
| markRootSpanForDiscard, | ||
| } from './onSpanEndUtils'; | ||
| import { SPAN_ORIGIN_AUTO_NAVIGATION_REACT_NAVIGATION } from './origin'; | ||
| import { consumePendingDeepLink } from './pendingDeepLink'; | ||
| import { consumePendingExpoRouterNavigation } from './pendingExpoRouterNavigation'; | ||
| import { getReactNativeTracingIntegration } from './reactnativetracing'; | ||
| import { SEMANTIC_ATTRIBUTE_NAVIGATION_ACTION_TYPE, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from './semanticAttributes'; | ||
|
|
@@ -230,6 +232,7 @@ export const reactNavigationIntegration = ({ | |
| let latestNavigationSpan: Span | undefined; | ||
| let latestNavigationSpanNameCustomized: boolean = false; | ||
| let navigationProcessingSpan: Span | undefined; | ||
| let deepLinkAppliedToLatestSpan: boolean = false; | ||
|
|
||
| let initialStateHandled: boolean = false; | ||
| let isSetupComplete: boolean = false; | ||
|
|
@@ -463,6 +466,15 @@ export const reactNavigationIntegration = ({ | |
| if (pendingExpoRouter && latestNavigationSpan) { | ||
| latestNavigationSpan.setAttribute('navigation.method', pendingExpoRouter.method); | ||
| } | ||
|
|
||
| // Try to attribute this span to a recently received deep link. Covers the | ||
| // warm-open case (link arrives โ navigation dispatches). The cold-start | ||
| // case is handled again in `updateLatestNavigationSpanWithCurrentRoute` | ||
| // because `Linking.getInitialURL()` may resolve after this point. | ||
| deepLinkAppliedToLatestSpan = false; | ||
| if (latestNavigationSpan) { | ||
| deepLinkAppliedToLatestSpan = applyPendingDeepLinkToSpan(latestNavigationSpan, routeChangeTimeoutMs); | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| if (ignoreEmptyBackNavigationTransactions) { | ||
| ignoreEmptyBackNavigation(getClient(), latestNavigationSpan); | ||
| } | ||
|
|
@@ -555,6 +567,13 @@ export const reactNavigationIntegration = ({ | |
| routeName = getPathFromState(navigationState) || route.name; | ||
| } | ||
|
|
||
| // Cold-start fallback: if the deep link arrived *after* the idle navigation | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| // span was started (e.g. `getInitialURL()` resolved post-`afterAllSetup`), | ||
| // try again now that the route has mounted. | ||
| if (!deepLinkAppliedToLatestSpan) { | ||
| deepLinkAppliedToLatestSpan = applyPendingDeepLinkToSpan(latestNavigationSpan, routeChangeTimeoutMs); | ||
| } | ||
|
|
||
| navigationProcessingSpan?.updateName(`Navigation dispatch to screen ${routeName} mounted`); | ||
| navigationProcessingSpan?.setStatus({ code: SPAN_STATUS_OK }); | ||
| navigationProcessingSpan?.end(stateChangedTimestamp); | ||
|
|
@@ -600,6 +619,7 @@ export const reactNavigationIntegration = ({ | |
| } | ||
| // Clear the latest transaction as it has been handled. | ||
| latestNavigationSpan = undefined; | ||
| deepLinkAppliedToLatestSpan = false; | ||
| }; | ||
|
|
||
| /** Pushes a recent route key, and removes earlier routes when there is greater than the max length */ | ||
|
|
@@ -624,6 +644,7 @@ export const reactNavigationIntegration = ({ | |
| if (navigationProcessingSpan) { | ||
| navigationProcessingSpan = undefined; | ||
| } | ||
| deepLinkAppliedToLatestSpan = false; | ||
| }; | ||
|
|
||
| const clearStateChangeTimeout = (): void => { | ||
|
|
@@ -673,6 +694,30 @@ interface NavigationContainer { | |
| getState: () => NavigationState | undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to consume the pending deep link and attach it to the given span. | ||
| * | ||
| * Returns `true` when a deep link was consumed (and the span was annotated), | ||
| * `false` otherwise. Callers may invoke this multiple times against the same | ||
| * span โ once the pending value has been consumed it will not be re-applied. | ||
| */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. q: is this comment referring to the |
||
| function applyPendingDeepLinkToSpan(span: Span, maxAgeMs: number): boolean { | ||
| const pending = consumePendingDeepLink(maxAgeMs); | ||
| if (!pending) { | ||
| return false; | ||
| } | ||
|
|
||
| const sendDefaultPii = getClient()?.getOptions()?.sendDefaultPii ?? false; | ||
| const url = sendDefaultPii ? pending.url : sanitizeDeepLinkUrl(pending.url); | ||
|
|
||
| span.setAttributes({ | ||
| 'navigation.trigger': 'deeplink', | ||
| 'deeplink.url': url, | ||
| 'deeplink.received_at': Math.max(0, Date.now() - pending.receivedAtMs), | ||
|
sentry[bot] marked this conversation as resolved.
Outdated
|
||
| }); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the route name from a React Navigation dispatch action payload. | ||
| * | ||
|
|
||
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,57 @@ | ||
| import { clearPendingDeepLink, consumePendingDeepLink, setPendingDeepLink } from '../../src/js/tracing/pendingDeepLink'; | ||
|
|
||
| describe('pendingDeepLink', () => { | ||
| afterEach(() => { | ||
| clearPendingDeepLink(); | ||
| }); | ||
|
|
||
| it('returns undefined when no deep link has been set', () => { | ||
| expect(consumePendingDeepLink(1_000)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('returns the most recently stored URL with a receive timestamp', () => { | ||
| const before = Date.now(); | ||
| setPendingDeepLink('myapp://profile/123'); | ||
| const after = Date.now(); | ||
|
|
||
| const pending = consumePendingDeepLink(1_000); | ||
| expect(pending?.url).toBe('myapp://profile/123'); | ||
| expect(pending?.receivedAtMs).toBeGreaterThanOrEqual(before); | ||
| expect(pending?.receivedAtMs).toBeLessThanOrEqual(after); | ||
| }); | ||
|
|
||
| it('clears the value after a single consume', () => { | ||
| setPendingDeepLink('myapp://a'); | ||
| expect(consumePendingDeepLink(1_000)?.url).toBe('myapp://a'); | ||
| expect(consumePendingDeepLink(1_000)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('overwrites a previous pending value', () => { | ||
| setPendingDeepLink('myapp://old'); | ||
| setPendingDeepLink('myapp://new'); | ||
| expect(consumePendingDeepLink(1_000)?.url).toBe('myapp://new'); | ||
| }); | ||
|
|
||
| it('drops values older than maxAgeMs and still clears the slot', () => { | ||
| const originalNow = Date.now; | ||
| const baseNow = originalNow(); | ||
| Date.now = (): number => baseNow; | ||
| setPendingDeepLink('myapp://stale'); | ||
| Date.now = (): number => baseNow + 5_000; | ||
|
|
||
| try { | ||
| expect(consumePendingDeepLink(1_000)).toBeUndefined(); | ||
| // Slot must be empty even though the value was rejected. | ||
| Date.now = originalNow; | ||
| expect(consumePendingDeepLink(1_000)).toBeUndefined(); | ||
| } finally { | ||
| Date.now = originalNow; | ||
| } | ||
| }); | ||
|
|
||
| it('clearPendingDeepLink removes the value without returning it', () => { | ||
| setPendingDeepLink('myapp://x'); | ||
| clearPendingDeepLink(); | ||
| expect(consumePendingDeepLink(1_000)).toBeUndefined(); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.