New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(tracing): Add Prisma ORM integration. #4931
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
363d866
feat(tracing): Add Prisma ORM integration.
onurtemizkan fdcad2a
Disable `max-lines` rule for hubextensions.
onurtemizkan 65e9c8c
Switch to instance based manual instrumentation.
onurtemizkan 9fda712
Add integration test.
onurtemizkan a92f781
Skip test on Node 10.
onurtemizkan b599f6d
Update packages/tracing/src/integrations/node/prisma.ts
onurtemizkan ff59aac
Update assertions.
onurtemizkan 3eba535
Merge branch '7.x' into onur/prisma-orm-integration
onurtemizkan 3963d1d
Add a simple unit test.
onurtemizkan 050d8dd
Remove integration tests.
onurtemizkan 215beaf
Fix linter issues.
onurtemizkan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/node-integration-tests/suites/tracing/prisma-orm/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
version: '3.9' | ||
|
||
services: | ||
db: | ||
image: postgres:13 | ||
restart: always | ||
container_name: integration-tests-prisma | ||
ports: | ||
- '5433:5432' | ||
environment: | ||
POSTGRES_USER: prisma | ||
POSTGRES_PASSWORD: prisma | ||
POSTGRES_DB: tests |
22 changes: 22 additions & 0 deletions
22
packages/node-integration-tests/suites/tracing/prisma-orm/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "sentry-prisma-test", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"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": "3.12.0", | ||
"prisma": "^3.12.0" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...es/node-integration-tests/suites/tracing/prisma-orm/prisma/migrations/migration_lock.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "postgresql" |
12 changes: 12 additions & 0 deletions
12
...e-integration-tests/suites/tracing/prisma-orm/prisma/migrations/sentry_test/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- CreateTable | ||
CREATE TABLE "User" ( | ||
"id" SERIAL NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"email" TEXT NOT NULL, | ||
"name" TEXT, | ||
|
||
CONSTRAINT "User_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); |
15 changes: 15 additions & 0 deletions
15
packages/node-integration-tests/suites/tracing/prisma-orm/prisma/schema.prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
datasource db { | ||
url = "postgresql://prisma:prisma@localhost:5433/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? | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/node-integration-tests/suites/tracing/prisma-orm/scenario.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | ||
import { PrismaClient } from '@prisma/client'; | ||
import * as Sentry from '@sentry/node'; | ||
import * as Tracing from '@sentry/tracing'; | ||
import { randomBytes } from 'crypto'; | ||
|
||
const client = new PrismaClient(); | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
integrations: [new Tracing.Integrations.Prisma({ client })], | ||
}); | ||
|
||
async function run(): Promise<void> { | ||
const transaction = Sentry.startTransaction({ | ||
name: 'Test Transaction', | ||
op: 'transaction', | ||
}); | ||
|
||
Sentry.configureScope(scope => { | ||
scope.setSpan(transaction); | ||
}); | ||
|
||
try { | ||
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', | ||
}, | ||
}, | ||
}); | ||
} finally { | ||
if (transaction) transaction.finish(); | ||
} | ||
} | ||
|
||
void run(); |
10 changes: 10 additions & 0 deletions
10
packages/node-integration-tests/suites/tracing/prisma-orm/setup.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash | ||
|
||
NODE_MAJOR=$(node -v | cut -c2- | cut -d. -f1) | ||
|
||
if [ "$NODE_MAJOR" -lt "12" ]; then | ||
echo "Skipping Prisma tests on Node: $NODE_MAJOR" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think going forward we would like everything to be node scripts. Can we convert this to a TS script (we have |
||
exit 0 | ||
fi | ||
|
||
yarn && yarn setup |
17 changes: 17 additions & 0 deletions
17
packages/node-integration-tests/suites/tracing/prisma-orm/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { assertSentryTransaction, conditionalTest, getEnvelopeRequest, runServer } from '../../../utils'; | ||
|
||
conditionalTest({ min: 12 })('Prisma ORM Integration', () => { | ||
test('should instrument Prisma client for tracing.', async () => { | ||
const url = await runServer(__dirname); | ||
const envelope = await getEnvelopeRequest(url); | ||
|
||
assertSentryTransaction(envelope[2], { | ||
transaction: 'Test Transaction', | ||
spans: [ | ||
{ description: 'User create', op: 'db.prisma' }, | ||
{ description: 'User findMany', op: 'db.prisma' }, | ||
{ description: 'User deleteMany', op: 'db.prisma' }, | ||
], | ||
}); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
packages/node-integration-tests/suites/tracing/prisma-orm/yarn.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
"@prisma/client@3.12.0": | ||
version "3.12.0" | ||
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-3.12.0.tgz#a0eb49ffea5c128dd11dffb896d7139a60073d12" | ||
integrity sha512-4NEQjUcWja/NVBvfuDFscWSk1/rXg3+wj+TSkqXCb1tKlx/bsUE00rxsvOvGg7VZ6lw1JFpGkwjwmsOIc4zvQw== | ||
dependencies: | ||
"@prisma/engines-version" "3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980" | ||
|
||
"@prisma/engines-version@3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980": | ||
version "3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980" | ||
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980.tgz#829ca3d9d0d92555f44644606d4edfd45b2f5886" | ||
integrity sha512-o+jo8d7ZEiVpcpNWUDh3fj2uPQpBxl79XE9ih9nkogJbhw6P33274SHnqheedZ7PyvPIK/mvU8MLNYgetgXPYw== | ||
|
||
"@prisma/engines@3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980": | ||
version "3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980" | ||
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980.tgz#e52e364084c4d05278f62768047b788665e64a45" | ||
integrity sha512-zULjkN8yhzS7B3yeEz4aIym4E2w1ChrV12i14pht3ePFufvsAvBSoZ+tuXMvfSoNTgBS5E4bolRzLbMmbwkkMQ== | ||
|
||
prisma@^3.12.0: | ||
version "3.12.0" | ||
resolved "https://registry.yarnpkg.com/prisma/-/prisma-3.12.0.tgz#9675e0e72407122759d3eadcb6d27cdccd3497bd" | ||
integrity sha512-ltCMZAx1i0i9xuPM692Srj8McC665h6E5RqJom999sjtVSccHSD8Z+HSdBN2183h9PJKvC5dapkn78dd0NWMBg== | ||
dependencies: | ||
"@prisma/engines" "3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Hub } from '@sentry/hub'; | ||
import { EventProcessor, Integration } from '@sentry/types'; | ||
import { isThenable, logger } from '@sentry/utils'; | ||
|
||
import { IS_DEBUG_BUILD } from '../../flags'; | ||
|
||
type PrismaAction = | ||
| 'findUnique' | ||
| 'findMany' | ||
| 'findFirst' | ||
| 'create' | ||
| 'createMany' | ||
| 'update' | ||
| 'updateMany' | ||
| 'upsert' | ||
| 'delete' | ||
| 'deleteMany' | ||
| 'executeRaw' | ||
| 'queryRaw' | ||
| 'aggregate' | ||
| 'count' | ||
| 'runCommandRaw'; | ||
|
||
interface PrismaMiddlewareParams { | ||
model?: any; | ||
action: PrismaAction; | ||
args: any; | ||
dataPath: string[]; | ||
runInTransaction: boolean; | ||
} | ||
|
||
type PrismaMiddleware<T = any> = ( | ||
params: PrismaMiddlewareParams, | ||
next: (params: PrismaMiddlewareParams) => Promise<T>, | ||
) => Promise<T>; | ||
|
||
interface PrismaClient { | ||
$use: (cb: PrismaMiddleware) => void; | ||
} | ||
|
||
/** Tracing integration for @prisma/client package */ | ||
export class Prisma implements Integration { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static id: string = 'Prisma'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public name: string = Prisma.id; | ||
|
||
/** | ||
* Prisma ORM Client Instance | ||
*/ | ||
private readonly _client?: PrismaClient; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public constructor(options: { client?: PrismaClient } = {}) { | ||
this._client = options.client; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { | ||
if (!this._client) { | ||
IS_DEBUG_BUILD && logger.error('PrismaIntegration is missing a Prisma Client Instance'); | ||
return; | ||
} | ||
|
||
this._client.$use((params: PrismaMiddlewareParams, next: (params: PrismaMiddlewareParams) => Promise<unknown>) => { | ||
const scope = getCurrentHub().getScope(); | ||
const parentSpan = scope?.getSpan(); | ||
|
||
const action = params.action; | ||
const model = params.model; | ||
|
||
const span = parentSpan?.startChild({ | ||
description: model ? `${model} ${action}` : action, | ||
op: 'db.prisma', | ||
}); | ||
|
||
const rv = next(params); | ||
|
||
if (isThenable(rv)) { | ||
return rv.then((res: unknown) => { | ||
span?.finish(); | ||
return res; | ||
}); | ||
} | ||
|
||
span?.finish(); | ||
return rv; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's move this out into another PR so we can discuss the integration test separately? In the mean time, we can add sanity check unit tests to this PR (similar to what we have for mongo for ex.)