Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions dev-packages/node-integration-tests/package.json
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions dev-packages/node-integration-tests/package.json
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions dev-packages/node-integration-tests/package.json
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions dev-packages/node-integration-tests/package.json
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions dev-packages/node-integration-tests/package.json
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions dev-packages/node-integration-tests/package.json
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions dev-packages/node-integration-tests/package.json
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions dev-packages/node-integration-tests/package.json
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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