|
| 1 | +import { getMainCarrier, Hub } from '@sentry/hub'; |
| 2 | +import { SpanContext, TransactionContext } from '@sentry/types'; |
| 3 | +import { logger } from '@sentry/utils'; |
| 4 | + |
| 5 | +import { Span } from './span'; |
| 6 | +import { Transaction } from './transaction'; |
| 7 | + |
| 8 | +/** Returns all trace headers that are currently on the top scope. */ |
| 9 | +function traceHeaders(this: Hub): { [key: string]: string } { |
| 10 | + const scope = this.getScope(); |
| 11 | + if (scope) { |
| 12 | + const span = scope.getSpan(); |
| 13 | + if (span) { |
| 14 | + return { |
| 15 | + 'sentry-trace': span.toTraceparent(), |
| 16 | + }; |
| 17 | + } |
| 18 | + } |
| 19 | + return {}; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * {@see Hub.startTransaction} |
| 24 | + */ |
| 25 | +function startTransaction(this: Hub, context: TransactionContext): Transaction { |
| 26 | + const transaction = new Transaction(context, this); |
| 27 | + |
| 28 | + const client = this.getClient(); |
| 29 | + // Roll the dice for sampling transaction, all child spans inherit the sampling decision. |
| 30 | + if (transaction.sampled === undefined) { |
| 31 | + const sampleRate = (client && client.getOptions().tracesSampleRate) || 0; |
| 32 | + // if true = we want to have the transaction |
| 33 | + // if false = we don't want to have it |
| 34 | + // Math.random (inclusive of 0, but not 1) |
| 35 | + transaction.sampled = Math.random() < sampleRate; |
| 36 | + } |
| 37 | + |
| 38 | + // We only want to create a span list if we sampled the transaction |
| 39 | + // If sampled == false, we will discard the span anyway, so we can save memory by not storing child spans |
| 40 | + if (transaction.sampled) { |
| 41 | + const experimentsOptions = (client && client.getOptions()._experiments) || {}; |
| 42 | + transaction.initSpanRecorder(experimentsOptions.maxSpans as number); |
| 43 | + } |
| 44 | + |
| 45 | + return transaction; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * {@see Hub.startSpan} |
| 50 | + */ |
| 51 | +function startSpan(this: Hub, context: SpanContext): Transaction | Span { |
| 52 | + /** |
| 53 | + * @deprecated |
| 54 | + * TODO: consider removing this in a future release. |
| 55 | + * |
| 56 | + * This is for backwards compatibility with releases before startTransaction |
| 57 | + * existed, to allow for a smoother transition. |
| 58 | + */ |
| 59 | + { |
| 60 | + // The `TransactionContext.name` field used to be called `transaction`. |
| 61 | + const transactionContext = context as Partial<TransactionContext & { transaction: string }>; |
| 62 | + if (transactionContext.transaction !== undefined) { |
| 63 | + transactionContext.name = transactionContext.transaction; |
| 64 | + } |
| 65 | + // Check for not undefined since we defined it's ok to start a transaction |
| 66 | + // with an empty name. |
| 67 | + if (transactionContext.name !== undefined) { |
| 68 | + logger.warn('Deprecated: Use startTransaction to start transactions and Transaction.startChild to start spans.'); |
| 69 | + return this.startTransaction(transactionContext as TransactionContext); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + const scope = this.getScope(); |
| 74 | + if (scope) { |
| 75 | + // If there is a Span on the Scope we start a child and return that instead |
| 76 | + const parentSpan = scope.getSpan(); |
| 77 | + if (parentSpan) { |
| 78 | + return parentSpan.startChild(context); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Otherwise we return a new Span |
| 83 | + return new Span(context); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * This patches the global object and injects the APM extensions methods |
| 88 | + */ |
| 89 | +export function addExtensionMethods(): void { |
| 90 | + const carrier = getMainCarrier(); |
| 91 | + if (carrier.__SENTRY__) { |
| 92 | + carrier.__SENTRY__.extensions = carrier.__SENTRY__.extensions || {}; |
| 93 | + if (!carrier.__SENTRY__.extensions.startTransaction) { |
| 94 | + carrier.__SENTRY__.extensions.startTransaction = startTransaction; |
| 95 | + } |
| 96 | + if (!carrier.__SENTRY__.extensions.startSpan) { |
| 97 | + carrier.__SENTRY__.extensions.startSpan = startSpan; |
| 98 | + } |
| 99 | + if (!carrier.__SENTRY__.extensions.traceHeaders) { |
| 100 | + carrier.__SENTRY__.extensions.traceHeaders = traceHeaders; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments