|
| 1 | +import { |
| 2 | + WINDOW, |
| 3 | + browserTracingIntegration, |
| 4 | + getActiveSpan, |
| 5 | + getRootSpan, |
| 6 | + spanToJSON, |
| 7 | + startBrowserTracingNavigationSpan, |
| 8 | + startBrowserTracingPageLoadSpan, |
| 9 | +} from '@sentry/browser'; |
| 10 | +import { |
| 11 | + SEMANTIC_ATTRIBUTE_SENTRY_OP, |
| 12 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 13 | + SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, |
| 14 | + getClient, |
| 15 | +} from '@sentry/core'; |
| 16 | +import type { Client, Integration, Span } from '@sentry/types'; |
| 17 | +import { logger } from '@sentry/utils'; |
| 18 | +import type { BeforeLeaveEventArgs, Location, RouteSectionProps, RouterProps } from '@solidjs/router'; |
| 19 | +import { createEffect, mergeProps, splitProps } from 'solid-js'; |
| 20 | +import type { Component, JSX, ParentProps } from 'solid-js'; |
| 21 | +import { createComponent } from 'solid-js/web'; |
| 22 | +import { DEBUG_BUILD } from './debug-build'; |
| 23 | + |
| 24 | +const CLIENTS_WITH_INSTRUMENT_NAVIGATION: Client[] = []; |
| 25 | + |
| 26 | +type UserBeforeLeave = (listener: (e: BeforeLeaveEventArgs) => void) => void; |
| 27 | +type UseLocation = () => Location; |
| 28 | + |
| 29 | +let _useBeforeLeave: UserBeforeLeave; |
| 30 | +let _useLocation: UseLocation; |
| 31 | + |
| 32 | +interface SolidRouterOptions { |
| 33 | + useBeforeLeave: UserBeforeLeave; |
| 34 | + useLocation: UseLocation; |
| 35 | +} |
| 36 | + |
| 37 | +function handleNavigation(location: string): void { |
| 38 | + const client = getClient(); |
| 39 | + if (!client || !CLIENTS_WITH_INSTRUMENT_NAVIGATION.includes(client)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + startBrowserTracingNavigationSpan(client, { |
| 44 | + name: location, |
| 45 | + attributes: { |
| 46 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation', |
| 47 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.solidjs.solidrouter', |
| 48 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url', |
| 49 | + }, |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +function getActiveRootSpan(): Span | undefined { |
| 54 | + const span = getActiveSpan(); |
| 55 | + return span ? getRootSpan(span) : undefined; |
| 56 | +} |
| 57 | + |
| 58 | +/** Pass-through component in case user didn't specify a root **/ |
| 59 | +function SentryDefaultRoot(props: ParentProps): JSX.Element { |
| 60 | + return props.children; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Unfortunately, we cannot use router hooks directly in the Router, so we |
| 65 | + * need to wrap the `root` prop to instrument navigation. |
| 66 | + */ |
| 67 | +function withSentryRouterRoot(Root: Component<RouteSectionProps>): Component<RouteSectionProps> { |
| 68 | + const SentryRouterRoot = (props: RouteSectionProps): JSX.Element => { |
| 69 | + // TODO: This is a rudimentary first version of handling navigation spans |
| 70 | + // It does not |
| 71 | + // - use query params |
| 72 | + // - parameterize the route |
| 73 | + |
| 74 | + _useBeforeLeave(({ to }: BeforeLeaveEventArgs) => { |
| 75 | + // `to` could be `-1` if the browser back-button was used |
| 76 | + handleNavigation(to.toString()); |
| 77 | + }); |
| 78 | + |
| 79 | + const location = _useLocation(); |
| 80 | + createEffect(() => { |
| 81 | + const name = location.pathname; |
| 82 | + const rootSpan = getActiveRootSpan(); |
| 83 | + |
| 84 | + if (rootSpan) { |
| 85 | + const { op, description } = spanToJSON(rootSpan); |
| 86 | + |
| 87 | + // We only need to update navigation spans that have been created by |
| 88 | + // a browser back-button navigation (stored as `-1` by solid router) |
| 89 | + // everything else was already instrumented correctly in `useBeforeLeave` |
| 90 | + if (op === 'navigation' && description === '-1') { |
| 91 | + rootSpan.updateName(name); |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + return createComponent(Root, props); |
| 97 | + }; |
| 98 | + |
| 99 | + return SentryRouterRoot; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * A browser tracing integration that uses Solid Router to instrument navigations. |
| 104 | + */ |
| 105 | +export function solidRouterBrowserTracingIntegration( |
| 106 | + options: Parameters<typeof browserTracingIntegration>[0] & SolidRouterOptions, |
| 107 | +): Integration { |
| 108 | + const integration = browserTracingIntegration({ |
| 109 | + ...options, |
| 110 | + instrumentPageLoad: false, |
| 111 | + instrumentNavigation: false, |
| 112 | + }); |
| 113 | + |
| 114 | + const { instrumentPageLoad = true, instrumentNavigation = true, useBeforeLeave, useLocation } = options; |
| 115 | + |
| 116 | + return { |
| 117 | + ...integration, |
| 118 | + setup() { |
| 119 | + _useBeforeLeave = useBeforeLeave; |
| 120 | + _useLocation = useLocation; |
| 121 | + }, |
| 122 | + afterAllSetup(client) { |
| 123 | + integration.afterAllSetup(client); |
| 124 | + |
| 125 | + const initPathName = WINDOW && WINDOW.location && WINDOW.location.pathname; |
| 126 | + if (instrumentPageLoad && initPathName) { |
| 127 | + startBrowserTracingPageLoadSpan(client, { |
| 128 | + name: initPathName, |
| 129 | + attributes: { |
| 130 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url', |
| 131 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload', |
| 132 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.solidjs.solidrouter', |
| 133 | + }, |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + if (instrumentNavigation) { |
| 138 | + CLIENTS_WITH_INSTRUMENT_NAVIGATION.push(client); |
| 139 | + } |
| 140 | + }, |
| 141 | + }; |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * A higher-order component to instrument Solid Router to create navigation spans. |
| 146 | + */ |
| 147 | +export function withSentryRouterRouting(Router: Component<RouterProps>): Component<RouterProps> { |
| 148 | + if (!_useBeforeLeave || !_useLocation) { |
| 149 | + DEBUG_BUILD && |
| 150 | + logger.warn(`solidRouterBrowserTracingIntegration was unable to wrap Solid Router because of one or more missing hooks. |
| 151 | + useBeforeLeave: ${_useBeforeLeave}. useLocation: ${_useLocation}.`); |
| 152 | + |
| 153 | + return Router; |
| 154 | + } |
| 155 | + |
| 156 | + const SentryRouter = (props: RouterProps): JSX.Element => { |
| 157 | + const [local, others] = splitProps(props, ['root']); |
| 158 | + // We need to wrap root here in case the user passed in their own root |
| 159 | + const Root = withSentryRouterRoot(local.root ? local.root : SentryDefaultRoot); |
| 160 | + |
| 161 | + return createComponent(Router, mergeProps({ root: Root }, others)); |
| 162 | + }; |
| 163 | + |
| 164 | + return SentryRouter; |
| 165 | +} |
0 commit comments