Skip to content

Commit 0963af0

Browse files
authored
fix(node): Add compatibility layer for Prisma v5 (#15169)
1 parent ec02f84 commit 0963af0

File tree

21 files changed

+380
-16
lines changed

21 files changed

+380
-16
lines changed

dev-packages/node-integration-tests/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
"build:types": "tsc -p tsconfig.types.json",
1717
"clean": "rimraf -g **/node_modules && run-p clean:script",
1818
"clean:script": "node scripts/clean.js",
19-
"prisma:init": "cd suites/tracing/prisma-orm && yarn && yarn setup",
19+
"prisma-v5:init": "cd suites/tracing/prisma-orm-v5 && yarn && yarn setup",
20+
"prisma-v6:init": "cd suites/tracing/prisma-orm-v6 && yarn && yarn setup",
2021
"lint": "eslint . --format stylish",
2122
"fix": "eslint . --format stylish --fix",
2223
"type-check": "tsc",
23-
"pretest": "run-s --silent prisma:init",
24+
"pretest": "run-s --silent prisma-v5:init prisma-v6:init",
2425
"test": "jest --config ./jest.config.js",
2526
"test:watch": "yarn test --watch"
2627
},
@@ -30,7 +31,6 @@
3031
"@nestjs/common": "10.4.6",
3132
"@nestjs/core": "10.4.6",
3233
"@nestjs/platform-express": "10.4.6",
33-
"@prisma/client": "6.2.1",
3434
"@sentry/aws-serverless": "9.0.0-alpha.0",
3535
"@sentry/core": "9.0.0-alpha.0",
3636
"@sentry/node": "9.0.0-alpha.0",

dev-packages/node-integration-tests/suites/tracing/prisma-orm/docker-compose.yml renamed to dev-packages/node-integration-tests/suites/tracing/prisma-orm-v5/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
db:
55
image: postgres:13
66
restart: always
7-
container_name: integration-tests-prisma
7+
container_name: integration-tests-prisma-v5
88
ports:
99
- '5433:5432'
1010
environment:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "sentry-prisma-test",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"engines": {
7+
"node": ">=18"
8+
},
9+
"scripts": {
10+
"db-up": "docker compose up -d",
11+
"generate": "prisma generate",
12+
"migrate": "prisma migrate dev -n sentry-test",
13+
"setup": "run-s --silent db-up generate migrate"
14+
},
15+
"keywords": [],
16+
"author": "",
17+
"license": "ISC",
18+
"dependencies": {
19+
"@prisma/client": "5.22.0",
20+
"prisma": "5.22.0"
21+
}
22+
}

dev-packages/node-integration-tests/suites/tracing/prisma-orm/prisma/schema.prisma renamed to dev-packages/node-integration-tests/suites/tracing/prisma-orm-v5/prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ datasource db {
55

66
generator client {
77
provider = "prisma-client-js"
8+
previewFeatures = ["tracing"]
89
}
910

1011
model User {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const Sentry = require('@sentry/node');
2+
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
transport: loggingTransport,
9+
integrations: [Sentry.prismaIntegration()],
10+
});
11+
12+
const { randomBytes } = require('crypto');
13+
const { PrismaClient } = require('@prisma/client');
14+
15+
// Stop the process from exiting before the transaction is sent
16+
setInterval(() => {}, 1000);
17+
18+
async function run() {
19+
const client = new PrismaClient();
20+
21+
await Sentry.startSpanManual(
22+
{
23+
name: 'Test Transaction',
24+
op: 'transaction',
25+
},
26+
async span => {
27+
await client.user.create({
28+
data: {
29+
name: 'Tilda',
30+
email: `tilda_${randomBytes(4).toString('hex')}@sentry.io`,
31+
},
32+
});
33+
34+
await client.user.findMany();
35+
36+
await client.user.deleteMany({
37+
where: {
38+
email: {
39+
contains: 'sentry.io',
40+
},
41+
},
42+
});
43+
44+
setTimeout(async () => {
45+
span.end();
46+
await client.$disconnect();
47+
}, 500);
48+
},
49+
);
50+
}
51+
52+
run();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { createRunner } from '../../../utils/runner';
2+
3+
describe('Prisma ORM Tests', () => {
4+
test('CJS - should instrument PostgreSQL queries from Prisma ORM', done => {
5+
createRunner(__dirname, 'scenario.js')
6+
.expect({
7+
transaction: transaction => {
8+
expect(transaction.transaction).toBe('Test Transaction');
9+
const spans = transaction.spans || [];
10+
expect(spans.length).toBeGreaterThanOrEqual(5);
11+
12+
expect(spans).toContainEqual(
13+
expect.objectContaining({
14+
data: {
15+
method: 'create',
16+
model: 'User',
17+
name: 'User.create',
18+
'sentry.origin': 'auto.db.otel.prisma',
19+
},
20+
description: 'prisma:client:operation',
21+
status: 'ok',
22+
}),
23+
);
24+
25+
expect(spans).toContainEqual(
26+
expect.objectContaining({
27+
data: {
28+
'sentry.origin': 'auto.db.otel.prisma',
29+
},
30+
description: 'prisma:client:serialize',
31+
status: 'ok',
32+
}),
33+
);
34+
35+
expect(spans).toContainEqual(
36+
expect.objectContaining({
37+
data: {
38+
'sentry.origin': 'auto.db.otel.prisma',
39+
},
40+
description: 'prisma:client:connect',
41+
status: 'ok',
42+
}),
43+
);
44+
expect(spans).toContainEqual(
45+
expect.objectContaining({
46+
data: {
47+
method: 'findMany',
48+
model: 'User',
49+
name: 'User.findMany',
50+
'sentry.origin': 'auto.db.otel.prisma',
51+
},
52+
description: 'prisma:client:operation',
53+
status: 'ok',
54+
}),
55+
);
56+
expect(spans).toContainEqual(
57+
expect.objectContaining({
58+
data: {
59+
'sentry.origin': 'auto.db.otel.prisma',
60+
},
61+
description: 'prisma:client:serialize',
62+
status: 'ok',
63+
}),
64+
);
65+
},
66+
})
67+
.start(done);
68+
});
69+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"@prisma/client@5.22.0":
6+
version "5.22.0"
7+
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-5.22.0.tgz#da1ca9c133fbefe89e0da781c75e1c59da5f8802"
8+
integrity sha512-M0SVXfyHnQREBKxCgyo7sffrKttwE6R8PMq330MIUF0pTwjUhLbW84pFDlf06B27XyCR++VtjugEnIHdr07SVA==
9+
10+
"@prisma/debug@5.22.0":
11+
version "5.22.0"
12+
resolved "https://registry.yarnpkg.com/@prisma/debug/-/debug-5.22.0.tgz#58af56ed7f6f313df9fb1042b6224d3174bbf412"
13+
integrity sha512-AUt44v3YJeggO2ZU5BkXI7M4hu9BF2zzH2iF2V5pyXT/lRTyWiElZ7It+bRH1EshoMRxHgpYg4VB6rCM+mG5jQ==
14+
15+
"@prisma/engines-version@5.22.0-44.605197351a3c8bdd595af2d2a9bc3025bca48ea2":
16+
version "5.22.0-44.605197351a3c8bdd595af2d2a9bc3025bca48ea2"
17+
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-5.22.0-44.605197351a3c8bdd595af2d2a9bc3025bca48ea2.tgz#d534dd7235c1ba5a23bacd5b92cc0ca3894c28f4"
18+
integrity sha512-2PTmxFR2yHW/eB3uqWtcgRcgAbG1rwG9ZriSvQw+nnb7c4uCr3RAcGMb6/zfE88SKlC1Nj2ziUvc96Z379mHgQ==
19+
20+
"@prisma/engines@5.22.0":
21+
version "5.22.0"
22+
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-5.22.0.tgz#28f3f52a2812c990a8b66eb93a0987816a5b6d84"
23+
integrity sha512-UNjfslWhAt06kVL3CjkuYpHAWSO6L4kDCVPegV6itt7nD1kSJavd3vhgAEhjglLJJKEdJ7oIqDJ+yHk6qO8gPA==
24+
dependencies:
25+
"@prisma/debug" "5.22.0"
26+
"@prisma/engines-version" "5.22.0-44.605197351a3c8bdd595af2d2a9bc3025bca48ea2"
27+
"@prisma/fetch-engine" "5.22.0"
28+
"@prisma/get-platform" "5.22.0"
29+
30+
"@prisma/fetch-engine@5.22.0":
31+
version "5.22.0"
32+
resolved "https://registry.yarnpkg.com/@prisma/fetch-engine/-/fetch-engine-5.22.0.tgz#4fb691b483a450c5548aac2f837b267dd50ef52e"
33+
integrity sha512-bkrD/Mc2fSvkQBV5EpoFcZ87AvOgDxbG99488a5cexp5Ccny+UM6MAe/UFkUC0wLYD9+9befNOqGiIJhhq+HbA==
34+
dependencies:
35+
"@prisma/debug" "5.22.0"
36+
"@prisma/engines-version" "5.22.0-44.605197351a3c8bdd595af2d2a9bc3025bca48ea2"
37+
"@prisma/get-platform" "5.22.0"
38+
39+
"@prisma/get-platform@5.22.0":
40+
version "5.22.0"
41+
resolved "https://registry.yarnpkg.com/@prisma/get-platform/-/get-platform-5.22.0.tgz#fc675bc9d12614ca2dade0506c9c4a77e7dddacd"
42+
integrity sha512-pHhpQdr1UPFpt+zFfnPazhulaZYCUqeIcPpJViYoq9R+D/yw4fjE+CtnsnKzPYm0ddUbeXUzjGVGIRVgPDCk4Q==
43+
dependencies:
44+
"@prisma/debug" "5.22.0"
45+
46+
fsevents@2.3.3:
47+
version "2.3.3"
48+
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.3.tgz#cac6407785d03675a2a5e1a5305c697b347d90d6"
49+
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==
50+
51+
prisma@5.22.0:
52+
version "5.22.0"
53+
resolved "https://registry.yarnpkg.com/prisma/-/prisma-5.22.0.tgz#1f6717ff487cdef5f5799cc1010459920e2e6197"
54+
integrity sha512-vtpjW3XuYCSnMsNVBjLMNkTj6OZbudcPPTPYHqX0CJfpcdWciI1dM8uHETwmDxxiqEwCIE6WvXucWUetJgfu/A==
55+
dependencies:
56+
"@prisma/engines" "5.22.0"
57+
optionalDependencies:
58+
fsevents "2.3.3"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.9'
2+
3+
services:
4+
db:
5+
image: postgres:13
6+
restart: always
7+
container_name: integration-tests-prisma-v6
8+
ports:
9+
- '5434:5432'
10+
environment:
11+
POSTGRES_USER: prisma
12+
POSTGRES_PASSWORD: prisma
13+
POSTGRES_DB: tests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (i.e. Git)
3+
provider = "postgresql"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- CreateTable
2+
CREATE TABLE "User" (
3+
"id" SERIAL NOT NULL,
4+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
5+
"email" TEXT NOT NULL,
6+
"name" TEXT,
7+
8+
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
9+
);
10+
11+
-- CreateIndex
12+
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
datasource db {
2+
url = "postgresql://prisma:prisma@localhost:5434/tests"
3+
provider = "postgresql"
4+
}
5+
6+
generator client {
7+
provider = "prisma-client-js"
8+
}
9+
10+
model User {
11+
id Int @id @default(autoincrement())
12+
createdAt DateTime @default(now())
13+
email String @unique
14+
name String?
15+
}

packages/node/src/integrations/tracing/prisma.ts

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,61 @@
11
import type { Instrumentation } from '@opentelemetry/instrumentation';
22
// When importing CJS modules into an ESM module, we cannot import the named exports directly.
33
import * as prismaInstrumentation from '@prisma/instrumentation';
4-
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, defineIntegration, spanToJSON } from '@sentry/core';
4+
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, consoleSandbox, defineIntegration, spanToJSON } from '@sentry/core';
55
import { generateInstrumentOnce } from '../../otel/instrument';
6+
import type { PrismaV5TracingHelper } from './prisma/vendor/v5-tracing-helper';
7+
import type { PrismaV6TracingHelper } from './prisma/vendor/v6-tracing-helper';
68

79
const INTEGRATION_NAME = 'Prisma';
810

11+
const EsmInteropPrismaInstrumentation: typeof prismaInstrumentation.PrismaInstrumentation =
12+
// @ts-expect-error We need to do the following for interop reasons
13+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
14+
prismaInstrumentation.default?.PrismaInstrumentation || prismaInstrumentation.PrismaInstrumentation;
15+
16+
type CompatibilityLayerTraceHelper = PrismaV5TracingHelper & PrismaV6TracingHelper;
17+
18+
function isPrismaV6TracingHelper(helper: unknown): helper is PrismaV6TracingHelper {
19+
return !!helper && typeof helper === 'object' && 'dispatchEngineSpans' in helper;
20+
}
21+
22+
class SentryPrismaInteropInstrumentation extends EsmInteropPrismaInstrumentation {
23+
public constructor() {
24+
super();
25+
}
26+
27+
public enable(): void {
28+
super.enable();
29+
30+
// 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.
31+
// 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.
32+
// We still won't fully emit all the spans, but this could potentially be implemented in the future.
33+
const prismaInstrumentationObject = (globalThis as Record<string, unknown>).PRISMA_INSTRUMENTATION;
34+
const prismaTracingHelper =
35+
prismaInstrumentationObject &&
36+
typeof prismaInstrumentationObject === 'object' &&
37+
'helper' in prismaInstrumentationObject
38+
? prismaInstrumentationObject.helper
39+
: undefined;
40+
41+
let emittedWarning = false;
42+
43+
if (isPrismaV6TracingHelper(prismaTracingHelper)) {
44+
(prismaTracingHelper as CompatibilityLayerTraceHelper).createEngineSpan = () => {
45+
consoleSandbox(() => {
46+
if (!emittedWarning) {
47+
emittedWarning = true;
48+
// eslint-disable-next-line no-console
49+
console.warn(
50+
'[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/',
51+
);
52+
}
53+
});
54+
};
55+
}
56+
}
57+
}
58+
959
export const instrumentPrisma = generateInstrumentOnce<{ prismaInstrumentation?: Instrumentation }>(
1060
INTEGRATION_NAME,
1161
options => {
@@ -14,12 +64,7 @@ export const instrumentPrisma = generateInstrumentOnce<{ prismaInstrumentation?:
1464
return options.prismaInstrumentation;
1565
}
1666

17-
const EsmInteropPrismaInstrumentation: typeof prismaInstrumentation.PrismaInstrumentation =
18-
// @ts-expect-error We need to do the following for interop reasons
19-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
20-
prismaInstrumentation.default?.PrismaInstrumentation || prismaInstrumentation.PrismaInstrumentation;
21-
22-
return new EsmInteropPrismaInstrumentation({});
67+
return new SentryPrismaInteropInstrumentation();
2368
},
2469
);
2570

0 commit comments

Comments
 (0)