|
| 1 | +import type { Span as OtelSpan, Tracer } from '@opentelemetry/api'; |
| 2 | +import { trace } from '@opentelemetry/api'; |
| 3 | +import { getCurrentHub, hasTracingEnabled } from '@sentry/core'; |
| 4 | +import { addOtelSpanData, getSentrySpan } from '@sentry/opentelemetry-node'; |
| 5 | +import type { Span, TransactionContext } from '@sentry/types'; |
| 6 | +import { isThenable } from '@sentry/utils'; |
| 7 | + |
| 8 | +import type { NodeExperimentalClient } from './client'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Wraps a function with a transaction/span and finishes the span after the function is done. |
| 12 | + * The created span is the active span and will be used as parent by other spans created inside the function |
| 13 | + * and can be accessed via `Sentry.getSpan()`, as long as the function is executed while the scope is active. |
| 14 | + * |
| 15 | + * If you want to create a span that is not set as active, use {@link startSpan}. |
| 16 | + * |
| 17 | + * Note that if you have not enabled tracing extensions via `addTracingExtensions` |
| 18 | + * or you didn't set `tracesSampleRate`, this function will not generate spans |
| 19 | + * and the `span` returned from the callback will be undefined. |
| 20 | + */ |
| 21 | +export function startActiveSpan<T>(context: TransactionContext, callback: (span: Span | undefined) => T): T { |
| 22 | + const tracer = getTracer(); |
| 23 | + if (!tracer) { |
| 24 | + return callback(undefined); |
| 25 | + } |
| 26 | + |
| 27 | + const name = context.description || context.op || '<unknown>'; |
| 28 | + const additionalData = { |
| 29 | + metadata: context.metadata, |
| 30 | + }; |
| 31 | + |
| 32 | + return tracer.startActiveSpan(name, (span: OtelSpan): T => { |
| 33 | + const otelSpanId = span.spanContext().spanId; |
| 34 | + addOtelSpanData(otelSpanId, additionalData); |
| 35 | + |
| 36 | + const sentrySpan = getSentrySpan(otelSpanId); |
| 37 | + |
| 38 | + function finishSpan(): void { |
| 39 | + span.end(); |
| 40 | + } |
| 41 | + |
| 42 | + let maybePromiseResult: T; |
| 43 | + try { |
| 44 | + maybePromiseResult = callback(sentrySpan); |
| 45 | + } catch (e) { |
| 46 | + sentrySpan && sentrySpan.setStatus('internal_error'); |
| 47 | + finishSpan(); |
| 48 | + throw e; |
| 49 | + } |
| 50 | + |
| 51 | + if (isThenable(maybePromiseResult)) { |
| 52 | + Promise.resolve(maybePromiseResult).then( |
| 53 | + () => { |
| 54 | + finishSpan(); |
| 55 | + }, |
| 56 | + () => { |
| 57 | + sentrySpan && sentrySpan.setStatus('internal_error'); |
| 58 | + finishSpan(); |
| 59 | + }, |
| 60 | + ); |
| 61 | + } else { |
| 62 | + finishSpan(); |
| 63 | + } |
| 64 | + |
| 65 | + return maybePromiseResult; |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Creates a span. This span is not set as active, so will not get automatic instrumentation spans |
| 71 | + * as children or be able to be accessed via `Sentry.getSpan()`. |
| 72 | + * |
| 73 | + * If you want to create a span that is set as active, use {@link startActiveSpan}. |
| 74 | + * |
| 75 | + * Note that if you have not enabled tracing extensions via `addTracingExtensions` |
| 76 | + * or you didn't set `tracesSampleRate` or `tracesSampler`, this function will not generate spans |
| 77 | + * and the `span` returned from the callback will be undefined. |
| 78 | + */ |
| 79 | +export function startSpan(context: TransactionContext): Span | undefined { |
| 80 | + const tracer = getTracer(); |
| 81 | + if (!tracer) { |
| 82 | + return undefined; |
| 83 | + } |
| 84 | + |
| 85 | + const name = context.description || context.op || '<unknown>'; |
| 86 | + const otelSpan = tracer.startSpan(name); |
| 87 | + |
| 88 | + const otelSpanId = otelSpan.spanContext().spanId; |
| 89 | + const additionalData = { |
| 90 | + metadata: context.metadata, |
| 91 | + }; |
| 92 | + addOtelSpanData(otelSpanId, additionalData); |
| 93 | + |
| 94 | + return getSentrySpan(otelSpanId); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Returns the currently active span. |
| 99 | + */ |
| 100 | +export function getActiveSpan(): Span | undefined { |
| 101 | + const otelSpan = trace.getActiveSpan(); |
| 102 | + const spanId = otelSpan && otelSpan.spanContext().spanId; |
| 103 | + return spanId ? getSentrySpan(spanId) : undefined; |
| 104 | +} |
| 105 | + |
| 106 | +function getTracer(): Tracer | undefined { |
| 107 | + if (!hasTracingEnabled()) { |
| 108 | + return undefined; |
| 109 | + } |
| 110 | + |
| 111 | + const client = getCurrentHub().getClient<NodeExperimentalClient>(); |
| 112 | + return client && client.tracer; |
| 113 | +} |
0 commit comments