Skip to content

Commit b6a4a4a

Browse files
authored
fix(node/v8): Add compatibility layer for Prisma v5 (#15210)
1 parent 3673689 commit b6a4a4a

File tree

23 files changed

+342
-19
lines changed

23 files changed

+342
-19
lines changed

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

+3-3
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 && ts-node ./setup.ts)",
19+
"prisma-v5:init": "cd suites/tracing/prisma-orm-v5 && ts-node ./setup.ts",
20+
"prisma-v6:init": "cd suites/tracing/prisma-orm-v6 && ts-node ./setup.ts",
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": "5.22.0",
3434
"@sentry/aws-serverless": "8.53.0",
3535
"@sentry/core": "8.53.0",
3636
"@sentry/node": "8.53.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

+1-1
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:

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

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
6-
"engines": {
7-
"node": ">=16"
8-
},
96
"scripts": {
107
"db-up": "docker compose up -d",
118
"generate": "prisma generate",
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://[email protected]/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();
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,19 @@
1+
{
2+
"name": "sentry-prisma-test",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"db-up": "docker compose up -d",
8+
"generate": "prisma generate",
9+
"migrate": "prisma migrate dev -n sentry-test",
10+
"setup": "run-s --silent db-up generate migrate"
11+
},
12+
"keywords": [],
13+
"author": "",
14+
"license": "ISC",
15+
"dependencies": {
16+
"@prisma/client": "5.22.0",
17+
"prisma": "5.22.0"
18+
}
19+
}
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");
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { execSync } from 'child_process';
2+
import { parseSemver } from '@sentry/core';
3+
4+
const NODE_VERSION = parseSemver(process.versions.node);
5+
6+
// Prisma v5 requires Node.js v16+
7+
// https://www.prisma.io/docs/orm/more/upgrade-guides/upgrading-versions/upgrading-to-prisma-5#nodejs-minimum-version-change
8+
if (NODE_VERSION.major && NODE_VERSION.major < 16) {
9+
// eslint-disable-next-line no-console
10+
console.warn(`Skipping Prisma tests on Node: ${NODE_VERSION.major}`);
11+
process.exit(0);
12+
}
13+
14+
try {
15+
execSync('yarn && yarn setup');
16+
} catch (_) {
17+
process.exit(1);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { conditionalTest } from '../../../utils';
2+
import { createRunner } from '../../../utils/runner';
3+
4+
conditionalTest({ min: 16 })('Prisma ORM Tests', () => {
5+
test('CJS - should instrument PostgreSQL queries from Prisma ORM', done => {
6+
createRunner(__dirname, 'scenario.js')
7+
.expect({
8+
transaction: transaction => {
9+
expect(transaction.transaction).toBe('Test Transaction');
10+
11+
const spans = transaction.spans || [];
12+
expect(spans).toHaveLength(0);
13+
},
14+
})
15+
.start(done);
16+
});
17+
});
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+
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+
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+
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+
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+
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+
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+
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"

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

+52-7
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 isPrismaV5TracingHelper(helper: unknown): helper is PrismaV5TracingHelper {
19+
return !!helper && typeof helper === 'object' && 'createEngineSpan' 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 6 with the v5 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 (isPrismaV5TracingHelper(prismaTracingHelper)) {
44+
(prismaTracingHelper as CompatibilityLayerTraceHelper).dispatchEngineSpans = () => {
45+
consoleSandbox(() => {
46+
if (!emittedWarning) {
47+
emittedWarning = true;
48+
// eslint-disable-next-line no-console
49+
console.warn(
50+
'[Sentry] This version (v8) of the Sentry SDK does not support tracing with Prisma version 6 out of the box. To trace Prisma version 6, pass a `prismaInstrumentation` for version 6 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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Vendored from https://github.com/prisma/prisma/blob/718358aa37975c18e5ea62f5b659fb47630b7609/packages/internals/src/tracing/types.ts#L1
2+
3+
import type { Context, Span, SpanOptions } from '@opentelemetry/api';
4+
5+
type V5SpanCallback<R> = (span?: Span, context?: Context) => R;
6+
7+
type V5ExtendedSpanOptions = SpanOptions & {
8+
name: string;
9+
internal?: boolean;
10+
middleware?: boolean;
11+
active?: boolean;
12+
context?: Context;
13+
};
14+
15+
type EngineSpanEvent = {
16+
span: boolean;
17+
spans: V5EngineSpan[];
18+
};
19+
20+
type V5EngineSpanKind = 'client' | 'internal';
21+
22+
type V5EngineSpan = {
23+
span: boolean;
24+
name: string;
25+
trace_id: string;
26+
span_id: string;
27+
parent_span_id: string;
28+
start_time: [number, number];
29+
end_time: [number, number];
30+
attributes?: Record<string, string>;
31+
links?: { trace_id: string; span_id: string }[];
32+
kind: V5EngineSpanKind;
33+
};
34+
35+
export interface PrismaV5TracingHelper {
36+
isEnabled(): boolean;
37+
getTraceParent(context?: Context): string;
38+
createEngineSpan(engineSpanEvent: EngineSpanEvent): void;
39+
getActiveContext(): Context | undefined;
40+
runInChildSpan<R>(nameOrOptions: string | V5ExtendedSpanOptions, callback: V5SpanCallback<R>): R;
41+
}

0 commit comments

Comments
 (0)