Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
ref(node): Streamline Prisma instrumentation (v6 and v7)#21819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
026197c6a334119421a67001af16ab7077fcb0a99f1cac1a5c849c838f0844ef769463File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,149 +6,184 @@ | ||
| * - Vendored from: https://github.com/prisma/prisma/tree/b6feea5565ec577545a79547d24273ccdd11b4c7/packages/instrumentation | ||
| * - Upstream version: @prisma/instrumentation@7.8.0 | ||
| * - Replaced `@prisma/instrumentation-contract` imports with local vendored types | ||
| * - Minor TypeScript strictness adjustments for this repository's compiler settings | ||
| * - Span creation uses Sentry's span APIs (`startSpanManual` / `startInactiveSpan`) instead of the OTel tracer | ||
| * - Span creation sets the Sentry origin, renames `db_query` spans to their SQL text, and backfills | ||
| * `db.system` for older Prisma versions | ||
| */ | ||
| /* eslint-disable */ | ||
| import type { Span, SpanAttributes, SpanKindValue, SpanLink } from '@sentry/core'; | ||
| import { | ||
| Attributes, | ||
| Context, | ||
| context as _context, | ||
| Span, | ||
| SpanKind, | ||
| SpanOptions, | ||
| trace, | ||
| Tracer, | ||
| TracerProvider, | ||
| } from '@opentelemetry/api'; | ||
| import type { EngineSpan, EngineSpanKind, ExtendedSpanOptions, SpanCallback, TracingHelper } from './types'; | ||
| getActiveSpan, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, | ||
| SPAN_KIND, | ||
| startInactiveSpan, | ||
| startSpanManual, | ||
| } from '@sentry/core'; | ||
| import type { EngineSpan, ExtendedSpanOptions, SpanCallback, TracingHelper } from './types'; | ||
| const showAllTraces = process.env.PRISMA_SHOW_ALL_TRACES === 'true'; | ||
| const nonSampledTraceParent = `00-10-10-00`; | ||
| const PRISMA_ORIGIN = 'auto.db.otel.prisma'; | ||
| type Options = { | ||
| tracerProvider: TracerProvider; | ||
| ignoreSpanTypes: (string | RegExp)[]; | ||
| }; | ||
| function engineSpanKindToOtelSpanKind(engineSpanKind: EngineSpanKind): SpanKind { | ||
| switch (engineSpanKind) { | ||
| case 'client': | ||
| return SpanKind.CLIENT; | ||
| case 'internal': | ||
| default: | ||
| return SpanKind.INTERNAL; | ||
| /** | ||
| * Older Prisma versions emit `prisma:engine:db_query` spans without a `db.system`, so it's backfilled here. | ||
| */ | ||
| function buildSpanAttributes(name: string, attributes: Record<string, unknown> | undefined): SpanAttributes { | ||
| const merged: SpanAttributes = { | ||
| ...(attributes as SpanAttributes | undefined), | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: PRISMA_ORIGIN, | ||
| }; | ||
| if (name === 'prisma:engine:db_query' && merged['db.system'] == null) { | ||
| merged['db.system'] = 'prisma'; | ||
| } | ||
| return merged; | ||
| } | ||
| /** | ||
| * Db query spans are named after their SQL text (e.g. `SELECT * FROM "User"`) rather than the generic | ||
| * engine name. v5/v6 emit `prisma:engine:db_query`; v7 inlined the engine and emits `prisma:client:db_query`. | ||
| */ | ||
| function buildSpanName(name: string, attributes: SpanAttributes): string { | ||
| const queryText = attributes['db.query.text']; | ||
| if ((name === 'prisma:engine:db_query' || name === 'prisma:client:db_query') && typeof queryText === 'string') { | ||
| return queryText; | ||
| } | ||
| return name; | ||
| } | ||
| export class ActiveTracingHelper implements TracingHelper { | ||
| private tracerProvider: TracerProvider; | ||
| private ignoreSpanTypes: (string | RegExp)[]; | ||
| constructor({ tracerProvider, ignoreSpanTypes }: Options) { | ||
| this.tracerProvider = tracerProvider; | ||
| public constructor({ ignoreSpanTypes }: Options) { | ||
| this.ignoreSpanTypes = ignoreSpanTypes; | ||
| } | ||
| isEnabled(): boolean { | ||
| public isEnabled(): boolean { | ||
| return true; | ||
| } | ||
| getTraceParent(context?: Context | undefined): string { | ||
| const span = trace.getSpanContext(context ?? _context.active()); | ||
| if (span) { | ||
| return `00-${span.traceId}-${span.spanId}-0${span.traceFlags}`; | ||
| public getTraceParent(span?: Span): string { | ||
| const spanContext = (span ?? getActiveSpan())?.spanContext(); | ||
| if (spanContext) { | ||
| return `00-${spanContext.traceId}-${spanContext.spanId}-0${spanContext.traceFlags}`; | ||
| } | ||
| return nonSampledTraceParent; | ||
| } | ||
| dispatchEngineSpans(spans: EngineSpan[]): void { | ||
| const tracer = this.tracerProvider.getTracer('prisma'); | ||
| public dispatchEngineSpans(spans: EngineSpan[]): void { | ||
| const linkIds = new Map<string, string>(); | ||
| const roots = spans.filter(span => span.parentId === null); | ||
| for (const root of roots) { | ||
| dispatchEngineSpan(tracer, root, spans, linkIds, this.ignoreSpanTypes); | ||
| dispatchEngineSpan(root, spans, linkIds, this.ignoreSpanTypes); | ||
| } | ||
| } | ||
| getActiveContext(): Context | undefined { | ||
| return _context.active(); | ||
| public getActiveContext(): Span | undefined { | ||
| return getActiveSpan(); | ||
| } | ||
Comment on lines
+90
to
92
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems weird to me 😬 but I can't tell if it's ok or not. We'd need to investigate when exactly Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from what I can tell this should be fine, the output of | ||
| runInChildSpan<R>(options: string | ExtendedSpanOptions, callback: SpanCallback<R>): R { | ||
| if (typeof options === 'string') { | ||
| options = { name: options }; | ||
| } | ||
| public runInChildSpan<R>(nameOrOptions: string | ExtendedSpanOptions, callback: SpanCallback<R>): R { | ||
| const options: ExtendedSpanOptions = typeof nameOrOptions === 'string' ? { name: nameOrOptions } : nameOrOptions; | ||
| if (options.internal && !showAllTraces) { | ||
| return callback(); | ||
| } | ||
| const tracer = this.tracerProvider.getTracer('prisma'); | ||
| const context = options.context ?? this.getActiveContext(); | ||
| const name = `prisma:client:${options.name}`; | ||
| if (shouldIgnoreSpan(name, this.ignoreSpanTypes)) { | ||
| return callback(); | ||
| } | ||
| const parentSpan = getActiveSpan(); | ||
| const attributes = buildSpanAttributes(name, options.attributes as Record<string, unknown> | undefined); | ||
| const spanOptions = { | ||
| name: buildSpanName(name, attributes), | ||
| attributes, | ||
| kind: options.kind as SpanKindValue | undefined, | ||
| links: options.links as SpanLink[] | undefined, | ||
| startTime: options.startTime, | ||
| parentSpan, | ||
| }; | ||
| if (options.active === false) { | ||
| const span = tracer.startSpan(name, options, context); | ||
| return endSpan(span, callback(span, context)); | ||
| const span = startInactiveSpan(spanOptions); | ||
| return endSpan(span, () => callback(span, parentSpan)); | ||
nicohrubec marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| return tracer.startActiveSpan(name, options, span => endSpan(span, callback(span, context))); | ||
| return startSpanManual(spanOptions, span => endSpan(span, () => callback(span, parentSpan))); | ||
| } | ||
| } | ||
| function dispatchEngineSpan( | ||
| tracer: Tracer, | ||
| engineSpan: EngineSpan, | ||
| allSpans: EngineSpan[], | ||
| linkIds: Map<string, string>, | ||
| ignoreSpanTypes: (string | RegExp)[], | ||
| ) { | ||
| if (shouldIgnoreSpan(engineSpan.name, ignoreSpanTypes)) return; | ||
| const spanOptions = { | ||
| attributes: engineSpan.attributes as Attributes, | ||
| kind: engineSpanKindToOtelSpanKind(engineSpan.kind), | ||
| startTime: engineSpan.startTime, | ||
| } satisfies SpanOptions; | ||
| tracer.startActiveSpan(engineSpan.name, spanOptions, span => { | ||
| linkIds.set(engineSpan.id, span.spanContext().spanId); | ||
| if (engineSpan.links) { | ||
| span.addLinks( | ||
| engineSpan.links.flatMap(link => { | ||
| const linkedId = linkIds.get(link); | ||
| if (!linkedId) { | ||
| return []; | ||
| } | ||
| return { | ||
| context: { | ||
| spanId: linkedId, | ||
| traceId: span.spanContext().traceId, | ||
| traceFlags: span.spanContext().traceFlags, | ||
| }, | ||
| }; | ||
| }), | ||
| ); | ||
| } | ||
| const children = allSpans.filter(s => s.parentId === engineSpan.id); | ||
| for (const child of children) { | ||
| dispatchEngineSpan(tracer, child, allSpans, linkIds, ignoreSpanTypes); | ||
| } | ||
| ): void { | ||
| if (shouldIgnoreSpan(engineSpan.name, ignoreSpanTypes)) { | ||
| return; | ||
| } | ||
| span.end(engineSpan.endTime); | ||
| }); | ||
| const attributes = buildSpanAttributes(engineSpan.name, engineSpan.attributes); | ||
| startSpanManual( | ||
| { | ||
| name: buildSpanName(engineSpan.name, attributes), | ||
| attributes, | ||
| kind: engineSpan.kind === 'client' ? SPAN_KIND.CLIENT : SPAN_KIND.INTERNAL, | ||
| startTime: engineSpan.startTime, | ||
| }, | ||
| span => { | ||
| linkIds.set(engineSpan.id, span.spanContext().spanId); | ||
| if (engineSpan.links) { | ||
| span.addLinks( | ||
| engineSpan.links.flatMap(link => { | ||
| const linkedId = linkIds.get(link); | ||
| if (!linkedId) { | ||
| return []; | ||
| } | ||
| return { | ||
| context: { | ||
| spanId: linkedId, | ||
| traceId: span.spanContext().traceId, | ||
| traceFlags: span.spanContext().traceFlags, | ||
| }, | ||
| }; | ||
| }), | ||
| ); | ||
| } | ||
| const children = allSpans.filter(s => s.parentId === engineSpan.id); | ||
| for (const child of children) { | ||
| dispatchEngineSpan(child, allSpans, linkIds, ignoreSpanTypes); | ||
| } | ||
| span.end(engineSpan.endTime); | ||
| }, | ||
| ); | ||
| } | ||
| function endSpan<T>(span: Span, result: T): T { | ||
| function endSpan<T>(span: Span, run: () => T): T { | ||
| let result: T; | ||
| try { | ||
| result = run(); | ||
| } catch (reason) { | ||
| span.end(); | ||
| throw reason; | ||
| } | ||
| if (isPromiseLike(result)) { | ||
| return result.then( | ||
| value => { | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.