|
| 1 | +import { Hub } from '@sentry/hub'; |
| 2 | +import { EventProcessor, Integration, Transaction as TransactionType, TransactionContext } from '@sentry/types'; |
| 3 | +import { logger } from '@sentry/utils'; |
| 4 | + |
| 5 | +import { startIdleTransaction } from '../hubextensions'; |
| 6 | +import { DEFAULT_IDLE_TIMEOUT } from '../idletransaction'; |
| 7 | +import { Span } from '../span'; |
| 8 | + |
| 9 | +import { defaultBeforeNavigate, defaultRoutingInstrumentation } from './router'; |
| 10 | + |
| 11 | +/** Options for Browser Tracing integration */ |
| 12 | +export interface BrowserTracingOptions { |
| 13 | + /** |
| 14 | + * The time to wait in ms until the transaction will be finished. The transaction will use the end timestamp of |
| 15 | + * the last finished span as the endtime for the transaction. |
| 16 | + * Time is in ms. |
| 17 | + * |
| 18 | + * Default: 1000 |
| 19 | + */ |
| 20 | + idleTimeout: number; |
| 21 | + |
| 22 | + /** |
| 23 | + * Flag to enable/disable creation of `navigation` transaction on history changes. |
| 24 | + * |
| 25 | + * Default: true |
| 26 | + */ |
| 27 | + startTransactionOnLocationChange: boolean; |
| 28 | + |
| 29 | + /** |
| 30 | + * Flag to enable/disable creation of `pageload` transaction on first pageload. |
| 31 | + * |
| 32 | + * Default: true |
| 33 | + */ |
| 34 | + startTransactionOnPageLoad: boolean; |
| 35 | + |
| 36 | + /** |
| 37 | + * beforeNavigate is called before a pageload/navigation transaction is created and allows for users |
| 38 | + * to set a custom navigation transaction name. Defaults behaviour is to return `window.location.pathname`. |
| 39 | + * |
| 40 | + * If undefined is returned, a pageload/navigation transaction will not be created. |
| 41 | + */ |
| 42 | + beforeNavigate(context: TransactionContext): TransactionContext | undefined; |
| 43 | + |
| 44 | + /** |
| 45 | + * Instrumentation that creates routing change transactions. By default creates |
| 46 | + * pageload and navigation transactions. |
| 47 | + */ |
| 48 | + routingInstrumentation<T extends TransactionType>( |
| 49 | + startTransaction: (context: TransactionContext) => T | undefined, |
| 50 | + startTransactionOnPageLoad?: boolean, |
| 51 | + startTransactionOnLocationChange?: boolean, |
| 52 | + ): void; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * The Browser Tracing integration automatically instruments browser pageload/navigation |
| 57 | + * actions as transactions, and captures requests, metrics and errors as spans. |
| 58 | + * |
| 59 | + * The integration can be configured with a variety of options, and can be extended to use |
| 60 | + * any routing library. This integration uses {@see IdleTransaction} to create transactions. |
| 61 | + */ |
| 62 | +export class BrowserTracing implements Integration { |
| 63 | + /** |
| 64 | + * @inheritDoc |
| 65 | + */ |
| 66 | + public static id: string = 'BrowserTracing'; |
| 67 | + |
| 68 | + /** Browser Tracing integration options */ |
| 69 | + public options: BrowserTracingOptions = { |
| 70 | + beforeNavigate: defaultBeforeNavigate, |
| 71 | + idleTimeout: DEFAULT_IDLE_TIMEOUT, |
| 72 | + routingInstrumentation: defaultRoutingInstrumentation, |
| 73 | + startTransactionOnLocationChange: true, |
| 74 | + startTransactionOnPageLoad: true, |
| 75 | + }; |
| 76 | + |
| 77 | + /** |
| 78 | + * @inheritDoc |
| 79 | + */ |
| 80 | + public name: string = BrowserTracing.id; |
| 81 | + |
| 82 | + private _getCurrentHub?: () => Hub; |
| 83 | + |
| 84 | + // navigationTransactionInvoker() -> Uses history API NavigationTransaction[] |
| 85 | + |
| 86 | + public constructor(_options?: Partial<BrowserTracingOptions>) { |
| 87 | + this.options = { |
| 88 | + ...this.options, |
| 89 | + ..._options, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @inheritDoc |
| 95 | + */ |
| 96 | + public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { |
| 97 | + this._getCurrentHub = getCurrentHub; |
| 98 | + |
| 99 | + const { routingInstrumentation, startTransactionOnLocationChange, startTransactionOnPageLoad } = this.options; |
| 100 | + |
| 101 | + routingInstrumentation( |
| 102 | + (context: TransactionContext) => this._createRouteTransaction(context), |
| 103 | + startTransactionOnPageLoad, |
| 104 | + startTransactionOnLocationChange, |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + /** Create routing idle transaction. */ |
| 109 | + private _createRouteTransaction(context: TransactionContext): TransactionType | undefined { |
| 110 | + if (!this._getCurrentHub) { |
| 111 | + logger.warn(`[Tracing] Did not creeate ${context.op} idleTransaction due to invalid _getCurrentHub`); |
| 112 | + return undefined; |
| 113 | + } |
| 114 | + |
| 115 | + const { beforeNavigate, idleTimeout } = this.options; |
| 116 | + |
| 117 | + // if beforeNavigate returns undefined, we should not start a transaction. |
| 118 | + const ctx = beforeNavigate({ |
| 119 | + ...context, |
| 120 | + ...getHeaderContext(), |
| 121 | + }); |
| 122 | + |
| 123 | + if (ctx === undefined) { |
| 124 | + logger.log(`[Tracing] Did not create ${context.op} idleTransaction due to beforeNavigate`); |
| 125 | + return undefined; |
| 126 | + } |
| 127 | + |
| 128 | + const hub = this._getCurrentHub(); |
| 129 | + logger.log(`[Tracing] starting ${ctx.op} idleTransaction on scope with context:`, ctx); |
| 130 | + return startIdleTransaction(hub, ctx, idleTimeout, true) as TransactionType; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Gets transaction context from a sentry-trace meta. |
| 136 | + */ |
| 137 | +function getHeaderContext(): Partial<TransactionContext> { |
| 138 | + const header = getMetaContent('sentry-trace'); |
| 139 | + if (header) { |
| 140 | + const span = Span.fromTraceparent(header); |
| 141 | + if (span) { |
| 142 | + return { |
| 143 | + parentSpanId: span.parentSpanId, |
| 144 | + sampled: span.sampled, |
| 145 | + traceId: span.traceId, |
| 146 | + }; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return {}; |
| 151 | +} |
| 152 | + |
| 153 | +/** Returns the value of a meta tag */ |
| 154 | +export function getMetaContent(metaName: string): string | null { |
| 155 | + const el = document.querySelector(`meta[name=${metaName}]`); |
| 156 | + return el ? el.getAttribute('content') : null; |
| 157 | +} |
0 commit comments