|
| 1 | +import { Scope } from '@sentry/core'; |
| 2 | +import type { Breadcrumb, Transaction } from '@sentry/types'; |
| 3 | +import { dateTimestampInSeconds } from '@sentry/utils'; |
| 4 | + |
| 5 | +import { getActiveSpan } from './trace'; |
| 6 | + |
| 7 | +const DEFAULT_MAX_BREADCRUMBS = 100; |
| 8 | + |
| 9 | +/** |
| 10 | + * This is a fork of the base Transaction with OTEL specific stuff added. |
| 11 | + * Note that we do not solve this via an actual subclass, but by wrapping this in a proxy when we need it - |
| 12 | + * as we can't easily control all the places a transaction may be created. |
| 13 | + */ |
| 14 | +interface TransactionWithBreadcrumbs extends Transaction { |
| 15 | + _breadcrumbs: Breadcrumb[]; |
| 16 | + |
| 17 | + /** Get all breadcrumbs added to this transaction. */ |
| 18 | + getBreadcrumbs(): Breadcrumb[]; |
| 19 | + |
| 20 | + /** Add a breadcrumb to this transaction. */ |
| 21 | + addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): void; |
| 22 | +} |
| 23 | + |
| 24 | +/** A fork of the classic scope with some otel specific stuff. */ |
| 25 | +export class OtelScope extends Scope { |
| 26 | + /** |
| 27 | + * @inheritDoc |
| 28 | + */ |
| 29 | + public static clone(scope?: Scope): Scope { |
| 30 | + const newScope = new OtelScope(); |
| 31 | + if (scope) { |
| 32 | + newScope._breadcrumbs = [...scope['_breadcrumbs']]; |
| 33 | + newScope._tags = { ...scope['_tags'] }; |
| 34 | + newScope._extra = { ...scope['_extra'] }; |
| 35 | + newScope._contexts = { ...scope['_contexts'] }; |
| 36 | + newScope._user = scope['_user']; |
| 37 | + newScope._level = scope['_level']; |
| 38 | + newScope._span = scope['_span']; |
| 39 | + newScope._session = scope['_session']; |
| 40 | + newScope._transactionName = scope['_transactionName']; |
| 41 | + newScope._fingerprint = scope['_fingerprint']; |
| 42 | + newScope._eventProcessors = [...scope['_eventProcessors']]; |
| 43 | + newScope._requestSession = scope['_requestSession']; |
| 44 | + newScope._attachments = [...scope['_attachments']]; |
| 45 | + newScope._sdkProcessingMetadata = { ...scope['_sdkProcessingMetadata'] }; |
| 46 | + newScope._propagationContext = { ...scope['_propagationContext'] }; |
| 47 | + } |
| 48 | + return newScope; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritDoc |
| 53 | + */ |
| 54 | + public addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this { |
| 55 | + const transaction = getActiveTransaction(); |
| 56 | + |
| 57 | + if (transaction) { |
| 58 | + transaction.addBreadcrumb(breadcrumb, maxBreadcrumbs); |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + return super.addBreadcrumb(breadcrumb, maxBreadcrumbs); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @inheritDoc |
| 67 | + */ |
| 68 | + protected _getBreadcrumbs(): Breadcrumb[] { |
| 69 | + const transaction = getActiveTransaction(); |
| 70 | + const transactionBreadcrumbs = transaction ? transaction.getBreadcrumbs() : []; |
| 71 | + |
| 72 | + return this._breadcrumbs.concat(transactionBreadcrumbs); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * This gets the currently active transaction, |
| 78 | + * and ensures to wrap it so that we can store breadcrumbs on it. |
| 79 | + */ |
| 80 | +function getActiveTransaction(): TransactionWithBreadcrumbs | undefined { |
| 81 | + const activeSpan = getActiveSpan(); |
| 82 | + const transaction = activeSpan && activeSpan.transaction; |
| 83 | + |
| 84 | + if (!transaction) { |
| 85 | + return undefined; |
| 86 | + } |
| 87 | + |
| 88 | + if (transactionHasBreadcrumbs(transaction)) { |
| 89 | + return transaction; |
| 90 | + } |
| 91 | + |
| 92 | + return new Proxy(transaction as TransactionWithBreadcrumbs, { |
| 93 | + get(target, prop, receiver) { |
| 94 | + if (prop === 'addBreadcrumb') { |
| 95 | + return addBreadcrumb; |
| 96 | + } |
| 97 | + if (prop === 'getBreadcrumbs') { |
| 98 | + return getBreadcrumbs; |
| 99 | + } |
| 100 | + if (prop === '_breadcrumbs') { |
| 101 | + const breadcrumbs = Reflect.get(target, prop, receiver); |
| 102 | + return breadcrumbs || []; |
| 103 | + } |
| 104 | + return Reflect.get(target, prop, receiver); |
| 105 | + }, |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | +function transactionHasBreadcrumbs(transaction: Transaction): transaction is TransactionWithBreadcrumbs { |
| 110 | + return ( |
| 111 | + typeof (transaction as TransactionWithBreadcrumbs).getBreadcrumbs === 'function' && |
| 112 | + typeof (transaction as TransactionWithBreadcrumbs).addBreadcrumb === 'function' |
| 113 | + ); |
| 114 | +} |
| 115 | + |
| 116 | +/** Add a breadcrumb to a transaction. */ |
| 117 | +function addBreadcrumb(this: TransactionWithBreadcrumbs, breadcrumb: Breadcrumb, maxBreadcrumbs?: number): void { |
| 118 | + const maxCrumbs = typeof maxBreadcrumbs === 'number' ? maxBreadcrumbs : DEFAULT_MAX_BREADCRUMBS; |
| 119 | + |
| 120 | + // No data has been changed, so don't notify scope listeners |
| 121 | + if (maxCrumbs <= 0) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + const mergedBreadcrumb = { |
| 126 | + timestamp: dateTimestampInSeconds(), |
| 127 | + ...breadcrumb, |
| 128 | + }; |
| 129 | + |
| 130 | + const breadcrumbs = this._breadcrumbs; |
| 131 | + breadcrumbs.push(mergedBreadcrumb); |
| 132 | + this._breadcrumbs = breadcrumbs.length > maxCrumbs ? breadcrumbs.slice(-maxCrumbs) : breadcrumbs; |
| 133 | +} |
| 134 | + |
| 135 | +/** Get all breadcrumbs from a transaction. */ |
| 136 | +function getBreadcrumbs(this: TransactionWithBreadcrumbs): Breadcrumb[] { |
| 137 | + return this._breadcrumbs; |
| 138 | +} |
0 commit comments