|
| 1 | +import { getMainCarrier, Hub } from '@sentry/hub'; |
| 2 | +import { SpanContext } from '@sentry/types'; |
| 3 | + |
| 4 | +import { Span } from './span'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Checks whether given value is instance of Span |
| 8 | + * @param span value to check |
| 9 | + */ |
| 10 | +function isSpanInstance(span: unknown): span is Span { |
| 11 | + return span instanceof Span; |
| 12 | +} |
| 13 | + |
| 14 | +/** Returns all trace headers that are currently on the top scope. */ |
| 15 | +function traceHeaders(): { [key: string]: string } { |
| 16 | + // @ts-ignore |
| 17 | + const that = this as Hub; |
| 18 | + const scope = that.getScope(); |
| 19 | + if (scope) { |
| 20 | + const span = scope.getSpan(); |
| 21 | + if (span) { |
| 22 | + return { |
| 23 | + 'sentry-trace': span.toTraceparent(), |
| 24 | + }; |
| 25 | + } |
| 26 | + } |
| 27 | + return {}; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * This functions starts a span. If argument passed is of type `Span`, it'll run sampling on it if configured |
| 32 | + * and attach a `SpanRecorder`. If it's of type `SpanContext` and there is already a `Span` on the Scope, |
| 33 | + * the created Span will have a reference to it and become it's child. Otherwise it'll crete a new `Span`. |
| 34 | + * |
| 35 | + * @param span Already constructed span which should be started or properties with which the span should be created |
| 36 | + */ |
| 37 | +function startSpan(spanOrSpanContext?: Span | SpanContext, forceNoChild: boolean = false): Span { |
| 38 | + // @ts-ignore |
| 39 | + const that = this as Hub; |
| 40 | + const scope = that.getScope(); |
| 41 | + const client = that.getClient(); |
| 42 | + let span; |
| 43 | + |
| 44 | + if (!isSpanInstance(spanOrSpanContext) && !forceNoChild) { |
| 45 | + if (scope) { |
| 46 | + const parentSpan = scope.getSpan() as Span; |
| 47 | + if (parentSpan) { |
| 48 | + span = parentSpan.child(spanOrSpanContext); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (!isSpanInstance(span)) { |
| 54 | + span = new Span(spanOrSpanContext, that); |
| 55 | + } |
| 56 | + |
| 57 | + if (span.sampled === undefined && span.transaction !== undefined) { |
| 58 | + const sampleRate = (client && client.getOptions().tracesSampleRate) || 0; |
| 59 | + span.sampled = Math.random() < sampleRate; |
| 60 | + } |
| 61 | + |
| 62 | + if (span.sampled) { |
| 63 | + const experimentsOptions = (client && client.getOptions()._experiments) || {}; |
| 64 | + span.initFinishedSpans(experimentsOptions.maxSpans); |
| 65 | + } |
| 66 | + |
| 67 | + return span; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * This patches the global object and injects the APM extensions methods |
| 72 | + */ |
| 73 | +export function addExtensionMethods(): void { |
| 74 | + const carrier = getMainCarrier(); |
| 75 | + if (carrier.__SENTRY__) { |
| 76 | + carrier.__SENTRY__.extensions = carrier.__SENTRY__.extensions || {}; |
| 77 | + if (!carrier.__SENTRY__.extensions.startSpan) { |
| 78 | + carrier.__SENTRY__.extensions.startSpan = startSpan; |
| 79 | + } |
| 80 | + if (!carrier.__SENTRY__.extensions.traceHeaders) { |
| 81 | + carrier.__SENTRY__.extensions.traceHeaders = traceHeaders; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments