|
| 1 | +import type { Context, Span, SpanOptions, Tracer, TracerProvider } from '@opentelemetry/api'; |
| 2 | +import { SpanKind, trace } from '@opentelemetry/api'; |
| 3 | +import { |
| 4 | + SEMANTIC_ATTRIBUTE_SENTRY_OP, |
| 5 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 6 | + startInactiveSpan, |
| 7 | + startSpanManual, |
| 8 | +} from '@sentry/core'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Set up a mock OTEL tracer to allow inter-op with OpenTelemetry emitted spans. |
| 12 | + * This is not perfect but handles easy/common use cases. |
| 13 | + */ |
| 14 | +export function setupOpenTelemetryTracer(): void { |
| 15 | + trace.setGlobalTracerProvider(new SentryDenoTraceProvider()); |
| 16 | +} |
| 17 | + |
| 18 | +class SentryDenoTraceProvider implements TracerProvider { |
| 19 | + private readonly _tracers: Map<string, Tracer> = new Map(); |
| 20 | + |
| 21 | + public getTracer(name: string, version?: string, options?: { schemaUrl?: string }): Tracer { |
| 22 | + const key = `${name}@${version || ''}:${options?.schemaUrl || ''}`; |
| 23 | + if (!this._tracers.has(key)) { |
| 24 | + this._tracers.set(key, new SentryDenoTracer()); |
| 25 | + } |
| 26 | + |
| 27 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 28 | + return this._tracers.get(key)!; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class SentryDenoTracer implements Tracer { |
| 33 | + public startSpan(name: string, options?: SpanOptions): Span { |
| 34 | + // Map OpenTelemetry SpanKind to Sentry operation |
| 35 | + const op = this._mapSpanKindToOp(options?.kind); |
| 36 | + |
| 37 | + return startInactiveSpan({ |
| 38 | + name, |
| 39 | + ...options, |
| 40 | + attributes: { |
| 41 | + ...options?.attributes, |
| 42 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'manual', |
| 43 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: op, |
| 44 | + 'sentry.deno_tracer': true, |
| 45 | + }, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * NOTE: This does not handle `context` being passed in. It will always put spans on the current scope. |
| 51 | + */ |
| 52 | + public startActiveSpan<F extends (span: Span) => unknown>(name: string, fn: F): ReturnType<F>; |
| 53 | + public startActiveSpan<F extends (span: Span) => unknown>(name: string, options: SpanOptions, fn: F): ReturnType<F>; |
| 54 | + public startActiveSpan<F extends (span: Span) => unknown>( |
| 55 | + name: string, |
| 56 | + options: SpanOptions, |
| 57 | + context: Context, |
| 58 | + fn: F, |
| 59 | + ): ReturnType<F>; |
| 60 | + public startActiveSpan<F extends (span: Span) => unknown>( |
| 61 | + name: string, |
| 62 | + options: unknown, |
| 63 | + context?: unknown, |
| 64 | + fn?: F, |
| 65 | + ): ReturnType<F> { |
| 66 | + const opts = (typeof options === 'object' && options !== null ? options : {}) as SpanOptions; |
| 67 | + |
| 68 | + // Map OpenTelemetry SpanKind to Sentry operation |
| 69 | + const op = this._mapSpanKindToOp(opts.kind); |
| 70 | + |
| 71 | + const spanOpts = { |
| 72 | + name, |
| 73 | + ...opts, |
| 74 | + attributes: { |
| 75 | + ...opts.attributes, |
| 76 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'manual', |
| 77 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: op, |
| 78 | + 'sentry.deno_tracer': true, |
| 79 | + }, |
| 80 | + }; |
| 81 | + |
| 82 | + const callback = ( |
| 83 | + typeof options === 'function' |
| 84 | + ? options |
| 85 | + : typeof context === 'function' |
| 86 | + ? context |
| 87 | + : typeof fn === 'function' |
| 88 | + ? fn |
| 89 | + : () => {} |
| 90 | + ) as F; |
| 91 | + |
| 92 | + // In OTEL the semantic matches `startSpanManual` because spans are not auto-ended |
| 93 | + return startSpanManual(spanOpts, callback) as ReturnType<F>; |
| 94 | + } |
| 95 | + |
| 96 | + private _mapSpanKindToOp(kind?: SpanKind): string { |
| 97 | + switch (kind) { |
| 98 | + case SpanKind.CLIENT: |
| 99 | + return 'http.client'; |
| 100 | + case SpanKind.SERVER: |
| 101 | + return 'http.server'; |
| 102 | + case SpanKind.PRODUCER: |
| 103 | + return 'message.produce'; |
| 104 | + case SpanKind.CONSUMER: |
| 105 | + return 'message.consume'; |
| 106 | + default: |
| 107 | + return 'otel.span'; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments