Skip to content

Commit

Permalink
fix(node/v8): Add compatibility layer for Prisma v5 (#15210)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst authored Feb 3, 2025
1 parent 3673689 commit b6a4a4a
Show file tree
Hide file tree
Showing 23 changed files with 342 additions and 19 deletions.
6 changes: 3 additions & 3 deletions dev-packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
"build:types": "tsc -p tsconfig.types.json",
"clean": "rimraf -g **/node_modules && run-p clean:script",
"clean:script": "node scripts/clean.js",
"prisma:init": "(cd suites/tracing/prisma-orm && ts-node ./setup.ts)",
"prisma-v5:init": "cd suites/tracing/prisma-orm-v5 && ts-node ./setup.ts",
"prisma-v6:init": "cd suites/tracing/prisma-orm-v6 && ts-node ./setup.ts",
"lint": "eslint . --format stylish",
"fix": "eslint . --format stylish --fix",
"type-check": "tsc",
"pretest": "run-s --silent prisma:init",
"pretest": "run-s --silent prisma-v5:init prisma-v6:init",
"test": "jest --config ./jest.config.js",
"test:watch": "yarn test --watch"
},
Expand All @@ -30,7 +31,6 @@
"@nestjs/common": "10.4.6",
"@nestjs/core": "10.4.6",
"@nestjs/platform-express": "10.4.6",
"@prisma/client": "5.22.0",
"@sentry/aws-serverless": "8.53.0",
"@sentry/core": "8.53.0",
"@sentry/node": "8.53.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
db:
image: postgres:13
restart: always
container_name: integration-tests-prisma
container_name: integration-tests-prisma-v5
ports:
- '5433:5432'
environment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": ">=16"
},
"scripts": {
"db-up": "docker compose up -d",
"generate": "prisma generate",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const Sentry = require('@sentry/node');
const { loggingTransport } = require('@sentry-internal/node-integration-tests');

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
tracesSampleRate: 1.0,
transport: loggingTransport,
integrations: [Sentry.prismaIntegration()],
});

const { randomBytes } = require('crypto');
const { PrismaClient } = require('@prisma/client');

// Stop the process from exiting before the transaction is sent
setInterval(() => {}, 1000);

async function run() {
const client = new PrismaClient();

await Sentry.startSpanManual(
{
name: 'Test Transaction',
op: 'transaction',
},
async span => {
await client.user.create({
data: {
name: 'Tilda',
email: `tilda_${randomBytes(4).toString('hex')}@sentry.io`,
},
});

await client.user.findMany();

await client.user.deleteMany({
where: {
email: {
contains: 'sentry.io',
},
},
});

setTimeout(async () => {
span.end();
await client.$disconnect();
}, 500);
},
);
}

run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3.9'

services:
db:
image: postgres:13
restart: always
container_name: integration-tests-prisma-v6
ports:
- '5434:5432'
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
POSTGRES_DB: tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "sentry-prisma-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"db-up": "docker compose up -d",
"generate": "prisma generate",
"migrate": "prisma migrate dev -n sentry-test",
"setup": "run-s --silent db-up generate migrate"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@prisma/client": "5.22.0",
"prisma": "5.22.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"email" TEXT NOT NULL,
"name" TEXT,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
datasource db {
url = "postgresql://prisma:prisma@localhost:5434/tests"
provider = "postgresql"
}

generator client {
provider = "prisma-client-js"
}

model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { execSync } from 'child_process';
import { parseSemver } from '@sentry/core';

const NODE_VERSION = parseSemver(process.versions.node);

// Prisma v5 requires Node.js v16+
// https://www.prisma.io/docs/orm/more/upgrade-guides/upgrading-versions/upgrading-to-prisma-5#nodejs-minimum-version-change
if (NODE_VERSION.major && NODE_VERSION.major < 16) {
// eslint-disable-next-line no-console
console.warn(`Skipping Prisma tests on Node: ${NODE_VERSION.major}`);
process.exit(0);
}

try {
execSync('yarn && yarn setup');
} catch (_) {
process.exit(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { conditionalTest } from '../../../utils';
import { createRunner } from '../../../utils/runner';

conditionalTest({ min: 16 })('Prisma ORM Tests', () => {
test('CJS - should instrument PostgreSQL queries from Prisma ORM', done => {
createRunner(__dirname, 'scenario.js')
.expect({
transaction: transaction => {
expect(transaction.transaction).toBe('Test Transaction');

const spans = transaction.spans || [];
expect(spans).toHaveLength(0);
},
})
.start(done);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@prisma/client@5.22.0":
version "5.22.0"
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-5.22.0.tgz#da1ca9c133fbefe89e0da781c75e1c59da5f8802"
integrity sha512-M0SVXfyHnQREBKxCgyo7sffrKttwE6R8PMq330MIUF0pTwjUhLbW84pFDlf06B27XyCR++VtjugEnIHdr07SVA==

"@prisma/debug@5.22.0":
version "5.22.0"
resolved "https://registry.yarnpkg.com/@prisma/debug/-/debug-5.22.0.tgz#58af56ed7f6f313df9fb1042b6224d3174bbf412"
integrity sha512-AUt44v3YJeggO2ZU5BkXI7M4hu9BF2zzH2iF2V5pyXT/lRTyWiElZ7It+bRH1EshoMRxHgpYg4VB6rCM+mG5jQ==

"@prisma/engines-version@5.22.0-44.605197351a3c8bdd595af2d2a9bc3025bca48ea2":
version "5.22.0-44.605197351a3c8bdd595af2d2a9bc3025bca48ea2"
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-5.22.0-44.605197351a3c8bdd595af2d2a9bc3025bca48ea2.tgz#d534dd7235c1ba5a23bacd5b92cc0ca3894c28f4"
integrity sha512-2PTmxFR2yHW/eB3uqWtcgRcgAbG1rwG9ZriSvQw+nnb7c4uCr3RAcGMb6/zfE88SKlC1Nj2ziUvc96Z379mHgQ==

"@prisma/engines@5.22.0":
version "5.22.0"
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-5.22.0.tgz#28f3f52a2812c990a8b66eb93a0987816a5b6d84"
integrity sha512-UNjfslWhAt06kVL3CjkuYpHAWSO6L4kDCVPegV6itt7nD1kSJavd3vhgAEhjglLJJKEdJ7oIqDJ+yHk6qO8gPA==
dependencies:
"@prisma/debug" "5.22.0"
"@prisma/engines-version" "5.22.0-44.605197351a3c8bdd595af2d2a9bc3025bca48ea2"
"@prisma/fetch-engine" "5.22.0"
"@prisma/get-platform" "5.22.0"

"@prisma/fetch-engine@5.22.0":
version "5.22.0"
resolved "https://registry.yarnpkg.com/@prisma/fetch-engine/-/fetch-engine-5.22.0.tgz#4fb691b483a450c5548aac2f837b267dd50ef52e"
integrity sha512-bkrD/Mc2fSvkQBV5EpoFcZ87AvOgDxbG99488a5cexp5Ccny+UM6MAe/UFkUC0wLYD9+9befNOqGiIJhhq+HbA==
dependencies:
"@prisma/debug" "5.22.0"
"@prisma/engines-version" "5.22.0-44.605197351a3c8bdd595af2d2a9bc3025bca48ea2"
"@prisma/get-platform" "5.22.0"

"@prisma/get-platform@5.22.0":
version "5.22.0"
resolved "https://registry.yarnpkg.com/@prisma/get-platform/-/get-platform-5.22.0.tgz#fc675bc9d12614ca2dade0506c9c4a77e7dddacd"
integrity sha512-pHhpQdr1UPFpt+zFfnPazhulaZYCUqeIcPpJViYoq9R+D/yw4fjE+CtnsnKzPYm0ddUbeXUzjGVGIRVgPDCk4Q==
dependencies:
"@prisma/debug" "5.22.0"

fsevents@2.3.3:
version "2.3.3"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.3.tgz#cac6407785d03675a2a5e1a5305c697b347d90d6"
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==

prisma@5.22.0:
version "5.22.0"
resolved "https://registry.yarnpkg.com/prisma/-/prisma-5.22.0.tgz#1f6717ff487cdef5f5799cc1010459920e2e6197"
integrity sha512-vtpjW3XuYCSnMsNVBjLMNkTj6OZbudcPPTPYHqX0CJfpcdWciI1dM8uHETwmDxxiqEwCIE6WvXucWUetJgfu/A==
dependencies:
"@prisma/engines" "5.22.0"
optionalDependencies:
fsevents "2.3.3"
59 changes: 52 additions & 7 deletions packages/node/src/integrations/tracing/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,61 @@
import type { Instrumentation } from '@opentelemetry/instrumentation';
// When importing CJS modules into an ESM module, we cannot import the named exports directly.
import * as prismaInstrumentation from '@prisma/instrumentation';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, defineIntegration, spanToJSON } from '@sentry/core';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, consoleSandbox, defineIntegration, spanToJSON } from '@sentry/core';
import { generateInstrumentOnce } from '../../otel/instrument';
import type { PrismaV5TracingHelper } from './prisma/vendor/v5-tracing-helper';
import type { PrismaV6TracingHelper } from './prisma/vendor/v6-tracing-helper';

const INTEGRATION_NAME = 'Prisma';

const EsmInteropPrismaInstrumentation: typeof prismaInstrumentation.PrismaInstrumentation =
// @ts-expect-error We need to do the following for interop reasons
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
prismaInstrumentation.default?.PrismaInstrumentation || prismaInstrumentation.PrismaInstrumentation;

type CompatibilityLayerTraceHelper = PrismaV5TracingHelper & PrismaV6TracingHelper;

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

class SentryPrismaInteropInstrumentation extends EsmInteropPrismaInstrumentation {
public constructor() {
super();
}

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 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;
const prismaTracingHelper =
prismaInstrumentationObject &&
typeof prismaInstrumentationObject === 'object' &&
'helper' in prismaInstrumentationObject
? prismaInstrumentationObject.helper
: undefined;

let emittedWarning = false;

if (isPrismaV5TracingHelper(prismaTracingHelper)) {
(prismaTracingHelper as CompatibilityLayerTraceHelper).dispatchEngineSpans = () => {
consoleSandbox(() => {
if (!emittedWarning) {
emittedWarning = true;
// eslint-disable-next-line no-console
console.warn(
'[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/',
);
}
});
};
}
}
}

export const instrumentPrisma = generateInstrumentOnce<{ prismaInstrumentation?: Instrumentation }>(
INTEGRATION_NAME,
options => {
Expand All @@ -14,12 +64,7 @@ export const instrumentPrisma = generateInstrumentOnce<{ prismaInstrumentation?:
return options.prismaInstrumentation;
}

const EsmInteropPrismaInstrumentation: typeof prismaInstrumentation.PrismaInstrumentation =
// @ts-expect-error We need to do the following for interop reasons
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
prismaInstrumentation.default?.PrismaInstrumentation || prismaInstrumentation.PrismaInstrumentation;

return new EsmInteropPrismaInstrumentation({});
return new SentryPrismaInteropInstrumentation();
},
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Vendored from https://github.com/prisma/prisma/blob/718358aa37975c18e5ea62f5b659fb47630b7609/packages/internals/src/tracing/types.ts#L1

import type { Context, Span, SpanOptions } from '@opentelemetry/api';

type V5SpanCallback<R> = (span?: Span, context?: Context) => R;

type V5ExtendedSpanOptions = SpanOptions & {
name: string;
internal?: boolean;
middleware?: boolean;
active?: boolean;
context?: Context;
};

type EngineSpanEvent = {
span: boolean;
spans: V5EngineSpan[];
};

type V5EngineSpanKind = 'client' | 'internal';

type V5EngineSpan = {
span: boolean;
name: string;
trace_id: string;
span_id: string;
parent_span_id: string;
start_time: [number, number];
end_time: [number, number];
attributes?: Record<string, string>;
links?: { trace_id: string; span_id: string }[];
kind: V5EngineSpanKind;
};

export interface PrismaV5TracingHelper {
isEnabled(): boolean;
getTraceParent(context?: Context): string;
createEngineSpan(engineSpanEvent: EngineSpanEvent): void;
getActiveContext(): Context | undefined;
runInChildSpan<R>(nameOrOptions: string | V5ExtendedSpanOptions, callback: V5SpanCallback<R>): R;
}
Loading

0 comments on commit b6a4a4a

Please sign in to comment.