|
| 1 | +import type { Span } from '@sentry/core'; |
| 2 | +import { isObjectLike } from '@sentry/core'; |
| 3 | +import type * as Context from 'effect/Context'; |
| 4 | +import * as Exit from 'effect/Exit'; |
| 5 | +import type * as Option from 'effect/Option'; |
| 6 | +import type * as EffectTracer from 'effect/Tracer'; |
| 7 | + |
| 8 | +type HrTime = [number, number]; |
| 9 | + |
| 10 | +const SENTRY_SPAN_SYMBOL = Symbol.for('@sentry/effect.SentrySpan'); |
| 11 | + |
| 12 | +export function nanosToHrTime(nanos: bigint): HrTime { |
| 13 | + const seconds = Number(nanos / BigInt(1_000_000_000)); |
| 14 | + const remainingNanos = Number(nanos % BigInt(1_000_000_000)); |
| 15 | + return [seconds, remainingNanos]; |
| 16 | +} |
| 17 | + |
| 18 | +export interface SentrySpanLike extends EffectTracer.Span { |
| 19 | + readonly [SENTRY_SPAN_SYMBOL]: true; |
| 20 | + readonly sentrySpan: Span; |
| 21 | +} |
| 22 | + |
| 23 | +export function isSentrySpan(span: EffectTracer.AnySpan): span is SentrySpanLike { |
| 24 | + return SENTRY_SPAN_SYMBOL in span; |
| 25 | +} |
| 26 | + |
| 27 | +function getErrorMessage(exit: Exit.Exit<unknown, unknown>): string | undefined { |
| 28 | + if (!Exit.isFailure(exit)) { |
| 29 | + return undefined; |
| 30 | + } |
| 31 | + |
| 32 | + const cause = exit.cause as unknown; |
| 33 | + |
| 34 | + // Effect v4: cause.reasons is an array of Reason objects |
| 35 | + if (isObjectLike(cause) && 'reasons' in cause && Array.isArray((cause as { reasons: unknown }).reasons)) { |
| 36 | + const reasons = (cause as { reasons: Array<{ _tag?: string; error?: unknown; defect?: unknown }> }).reasons; |
| 37 | + for (const reason of reasons) { |
| 38 | + if (reason._tag === 'Fail' && reason.error !== undefined) { |
| 39 | + return String(reason.error); |
| 40 | + } |
| 41 | + if (reason._tag === 'Die' && reason.defect !== undefined) { |
| 42 | + return String(reason.defect); |
| 43 | + } |
| 44 | + } |
| 45 | + return 'internal_error'; |
| 46 | + } |
| 47 | + |
| 48 | + // Effect v3: cause has _tag directly |
| 49 | + if (isObjectLike(cause) && '_tag' in cause) { |
| 50 | + const v3Cause = cause as { _tag: string; error?: unknown; defect?: unknown }; |
| 51 | + if (v3Cause._tag === 'Fail') { |
| 52 | + return String(v3Cause.error); |
| 53 | + } |
| 54 | + if (v3Cause._tag === 'Die') { |
| 55 | + return String(v3Cause.defect); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return 'internal_error'; |
| 60 | +} |
| 61 | + |
| 62 | +export class SentrySpanWrapper implements SentrySpanLike { |
| 63 | + public readonly [SENTRY_SPAN_SYMBOL]: true; |
| 64 | + public readonly _tag: 'Span'; |
| 65 | + public readonly spanId: string; |
| 66 | + public readonly traceId: string; |
| 67 | + public readonly attributes: Map<string, unknown>; |
| 68 | + public readonly sampled: boolean; |
| 69 | + public readonly parent: Option.Option<EffectTracer.AnySpan>; |
| 70 | + public readonly links: Array<EffectTracer.SpanLink>; |
| 71 | + public status: EffectTracer.SpanStatus; |
| 72 | + public readonly sentrySpan: Span; |
| 73 | + public readonly annotations: Context.Context<never>; |
| 74 | + |
| 75 | + public constructor( |
| 76 | + public readonly name: string, |
| 77 | + parent: Option.Option<EffectTracer.AnySpan>, |
| 78 | + public readonly context: Context.Context<never>, |
| 79 | + links: ReadonlyArray<EffectTracer.SpanLink>, |
| 80 | + startTime: bigint, |
| 81 | + public readonly kind: EffectTracer.SpanKind, |
| 82 | + existingSpan: Span, |
| 83 | + ) { |
| 84 | + this[SENTRY_SPAN_SYMBOL] = true as const; |
| 85 | + this._tag = 'Span' as const; |
| 86 | + this.attributes = new Map<string, unknown>(); |
| 87 | + this.parent = parent; |
| 88 | + this.links = [...links]; |
| 89 | + this.sentrySpan = existingSpan; |
| 90 | + this.annotations = context; |
| 91 | + |
| 92 | + const spanContext = this.sentrySpan.spanContext(); |
| 93 | + this.spanId = spanContext.spanId; |
| 94 | + this.traceId = spanContext.traceId; |
| 95 | + this.sampled = this.sentrySpan.isRecording(); |
| 96 | + this.status = { |
| 97 | + _tag: 'Started', |
| 98 | + startTime, |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + public attribute(key: string, value: unknown): void { |
| 103 | + if (!this.sentrySpan.isRecording()) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + this.sentrySpan.setAttribute(key, value as Parameters<Span['setAttribute']>[1]); |
| 108 | + this.attributes.set(key, value); |
| 109 | + } |
| 110 | + |
| 111 | + public addLinks(links: ReadonlyArray<EffectTracer.SpanLink>): void { |
| 112 | + this.links.push(...links); |
| 113 | + } |
| 114 | + |
| 115 | + public end(endTime: bigint, exit: Exit.Exit<unknown, unknown>): void { |
| 116 | + this.status = { |
| 117 | + _tag: 'Ended', |
| 118 | + endTime, |
| 119 | + exit, |
| 120 | + startTime: this.status.startTime, |
| 121 | + }; |
| 122 | + |
| 123 | + if (!this.sentrySpan.isRecording()) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + if (Exit.isFailure(exit)) { |
| 128 | + const message = getErrorMessage(exit) ?? 'internal_error'; |
| 129 | + this.sentrySpan.setStatus({ code: 2, message }); |
| 130 | + } else { |
| 131 | + this.sentrySpan.setStatus({ code: 1 }); |
| 132 | + } |
| 133 | + |
| 134 | + this.sentrySpan.end(nanosToHrTime(endTime)); |
| 135 | + } |
| 136 | + |
| 137 | + public event(name: string, startTime: bigint, attributes?: Record<string, unknown>): void { |
| 138 | + if (!this.sentrySpan.isRecording()) { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + this.sentrySpan.addEvent(name, attributes as Parameters<Span['addEvent']>[1], nanosToHrTime(startTime)); |
| 143 | + } |
| 144 | +} |
0 commit comments