Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
.
  • Loading branch information
Luca Forstner committed Jan 29, 2025
commit 2a51a0b6071b10d310e1b41b5cc7cbaab91ab2ae
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ conditionalTest({ min: 16 })('Prisma ORM Tests', () => {
.expect({
transaction: transaction => {
expect(transaction.transaction).toBe('Test Transaction');

const spans = transaction.spans || [];
expect(spans.length).toBeGreaterThanOrEqual(5);

Expand Down Expand Up @@ -42,6 +43,65 @@ conditionalTest({ min: 16 })('Prisma ORM Tests', () => {
status: 'ok',
}),
);

expect(spans).toContainEqual(
expect.objectContaining({
data: {
'sentry.origin': 'auto.db.otel.prisma',
},
description: 'prisma:engine',
status: 'ok',
}),
);
expect(spans).toContainEqual(
expect.objectContaining({
data: {
'sentry.origin': 'auto.db.otel.prisma',
'sentry.op': 'db',
'db.system': 'postgresql',
},
description: 'prisma:engine:connection',
status: 'ok',
op: 'db',
}),
);

expect(spans).toContainEqual(
expect.objectContaining({
data: {
'db.statement': expect.stringContaining(
'INSERT INTO "public"."User" ("createdAt","email","name") VALUES ($1,$2,$3) RETURNING "public"."User"."id", "public"."User"."createdAt", "public"."User"."email", "public"."User"."name" /* traceparent',
),
'sentry.origin': 'auto.db.otel.prisma',
'sentry.op': 'db',
'db.system': 'postgresql',
'otel.kind': 'CLIENT',
},
description: expect.stringContaining(
'INSERT INTO "public"."User" ("createdAt","email","name") VALUES ($1,$2,$3) RETURNING "public"."User"."id", "public"."User"."createdAt", "public"."User"."email", "public"."User"."name" /* traceparent',
),
status: 'ok',
op: 'db',
}),
);
expect(spans).toContainEqual(
expect.objectContaining({
data: {
'sentry.origin': 'auto.db.otel.prisma',
},
description: 'prisma:engine:serialize',
status: 'ok',
}),
);
expect(spans).toContainEqual(
expect.objectContaining({
data: {
'sentry.origin': 'auto.db.otel.prisma',
},
description: 'prisma:engine:response_json_serialization',
status: 'ok',
}),
);
expect(spans).toContainEqual(
expect.objectContaining({
data: {
Expand All @@ -63,6 +123,15 @@ conditionalTest({ min: 16 })('Prisma ORM Tests', () => {
status: 'ok',
}),
);
expect(spans).toContainEqual(
expect.objectContaining({
data: {
'sentry.origin': 'auto.db.otel.prisma',
},
description: 'prisma:engine',
status: 'ok',
}),
);
},
})
.start(done);
Expand Down
12 changes: 6 additions & 6 deletions packages/node/src/integrations/tracing/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const EsmInteropPrismaInstrumentation: typeof prismaInstrumentation.PrismaInstru

type CompatibilityLayerTraceHelper = PrismaV5TracingHelper & PrismaV6TracingHelper;

function isPrismaV6TracingHelper(helper: unknown): helper is PrismaV6TracingHelper {
return !!helper && typeof helper === 'object' && 'dispatchEngineSpans' in helper;
function isPrismaV5TracingHelper(helper: unknown): helper is PrismaV5TracingHelper {
return !!helper && typeof helper === 'object' && 'createEngineSpan' in helper;
}

class SentryPrismaInteropInstrumentation extends EsmInteropPrismaInstrumentation {
Expand All @@ -27,7 +27,7 @@ class SentryPrismaInteropInstrumentation extends EsmInteropPrismaInstrumentation
public enable(): void {
super.enable();

// The PrismaIntegration (super class) defines a global variable `global["PRISMA_INSTRUMENTATION"]` when `enable()` is called. This global variable holds a "TracingHelper" which Prisma uses internally to create tracing data. It's their way of not depending on OTEL with their main package. The sucky thing is, prisma broke the interface of the tracing helper with the v6 major update. This means that if you use Prisma 5 with the v6 instrumentation (or vice versa) Prisma just blows up, because tries to call methods on the helper that no longer exist.
// The PrismaIntegration (super class) defines a global variable `global["PRISMA_INSTRUMENTATION"]` when `enable()` is called. This global variable holds a "TracingHelper" which Prisma uses internally to create tracing data. It's their way of not depending on OTEL with their main package. The sucky thing is, prisma broke the interface of the tracing helper with the v6 major update. This means that if you use Prisma 6 with the v5 instrumentation (or vice versa) Prisma just blows up, because tries to call methods on the helper that no longer exist.
// Because we actually want to use the v6 instrumentation and not blow up in Prisma 5 user's faces, what we're doing here is backfilling the v5 method (`createEngineSpan`) with a noop so that no longer crashes when it attempts to call that function.
// We still won't fully emit all the spans, but this could potentially be implemented in the future.
const prismaInstrumentationObject = (globalThis as Record<string, unknown>).PRISMA_INSTRUMENTATION;
Expand All @@ -40,14 +40,14 @@ class SentryPrismaInteropInstrumentation extends EsmInteropPrismaInstrumentation

let emittedWarning = false;

if (isPrismaV6TracingHelper(prismaTracingHelper)) {
(prismaTracingHelper as CompatibilityLayerTraceHelper).createEngineSpan = () => {
if (isPrismaV5TracingHelper(prismaTracingHelper)) {
(prismaTracingHelper as CompatibilityLayerTraceHelper).dispatchEngineSpans = () => {
consoleSandbox(() => {
if (!emittedWarning) {
emittedWarning = true;
// eslint-disable-next-line no-console
console.warn(
'[Sentry] The Sentry SDK supports tracing with Prisma version 5 only with limited capabilities. For full tracing capabilities pass `prismaInstrumentation` for version 5 to the Sentry `prismaIntegration`. Read more: https://docs.sentry.io/platforms/javascript/guides/node/configuration/integrations/prisma/',
'[Sentry] This version of the Sentry SDK supports tracing with Prisma 6 only with very limited capabilities. For full tracing capabilities pass `prismaInstrumentation` for version 6 to the Sentry `prismaIntegration`. Read more: https://docs.sentry.io/platforms/javascript/guides/node/configuration/integrations/prisma/',
);
}
});
Expand Down