-
-
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,146 +6,188 @@ | |
| * - 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 was migrated from the OTel tracer to Sentry's span APIs (`startSpanManual` / | ||
| * `startInactiveSpan`) | ||
| * - The former `index.ts` `spanStart` hook is folded into span creation: the Sentry origin, the | ||
| * `db_query` -> query-text span rename, and the `db.system` backfill for older Prisma versions are | ||
| * applied where the spans are started instead of via a client hook | ||
| */ | ||
| /* eslint-disable */ | ||
|
|
||
| import type { Context } from '@opentelemetry/api'; | ||
| import { context as _context, trace } from '@opentelemetry/api'; | ||
| import type { Span, SpanAttributes, SpanKindValue, SpanLink } from '@sentry/core'; | ||
| import { | ||
| Attributes, | ||
| Context, | ||
| context as _context, | ||
| Span, | ||
| SpanKind, | ||
| SpanOptions, | ||
| trace, | ||
| Tracer, | ||
| TracerProvider, | ||
| } from '@opentelemetry/api'; | ||
| getActiveSpan, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, | ||
| SPAN_KIND, | ||
| startInactiveSpan, | ||
| startSpanManual, | ||
| } from '@sentry/core'; | ||
| import type { EngineSpan, EngineSpanKind, 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 { | ||
| function engineSpanKindToSentrySpanKind(engineSpanKind: EngineSpanKind): SpanKindValue { | ||
| switch (engineSpanKind) { | ||
| case 'client': | ||
| return SpanKind.CLIENT; | ||
| return SPAN_KIND.CLIENT; | ||
| case 'internal': | ||
| default: | ||
| return SpanKind.INTERNAL; | ||
| return SPAN_KIND.INTERNAL; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Folds the former `index.ts` `spanStart` hook into span creation: tags the Sentry origin and | ||
| * backfills `db.system` for older Prisma versions that emit `prisma:engine:db_query` without it. | ||
| */ | ||
| 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; | ||
| } | ||
|
|
||
| /** | ||
| * Uses the query text as the span name for db query spans (e.g. `SELECT * FROM "User"`), matching the | ||
| * behavior the SDK previously applied via the `spanStart` hook. 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(context?: Context): string { | ||
| const spanContext = context ? trace.getSpanContext(context) : 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 { | ||
| public getActiveContext(): Context | undefined { | ||
| return _context.active(); | ||
| } | ||
|
|
||
| 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 context = options.context ?? _context.active(); | ||
|
|
||
| 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, | ||
| }; | ||
|
|
||
| if (options.active === false) { | ||
| const span = tracer.startSpan(name, options, context); | ||
| const span = _context.with(context, () => startInactiveSpan(spanOptions)); | ||
| return endSpan(span, callback(span, context)); | ||
| } | ||
|
|
||
| return tracer.startActiveSpan(name, options, span => endSpan(span, callback(span, context))); | ||
| return _context.with(context, () => startSpanManual(spanOptions, span => endSpan(span, callback(span, context)))); | ||
|
Contributor
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. Bug: A synchronous error in the Suggested FixWrap the Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| } | ||
| } | ||
|
|
||
| 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: engineSpanKindToSentrySpanKind(engineSpan.kind), | ||
| 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 { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: we have a
SPAN_KINDtype in core now that we can use here