Skip to content

Commit 363d866

Browse files
committed
feat(tracing): Add Prisma ORM integration.
1 parent 0cf5fb5 commit 363d866

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

packages/tracing/src/hubextensions.ts

+6
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ function _autoloadDatabaseIntegrations(): void {
261261
};
262262
return new integration.Postgres();
263263
},
264+
prisma() {
265+
const integration = dynamicRequire(module, './integrations/node/prisma') as {
266+
Prisma: IntegrationClass<Integration>;
267+
};
268+
return new integration.Prisma();
269+
},
264270
};
265271

266272
const mappedPackages = Object.keys(packageToIntegrationMapping)

packages/tracing/src/integrations/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { Express } from './node/express';
22
export { Postgres } from './node/postgres';
33
export { Mysql } from './node/mysql';
44
export { Mongo } from './node/mongo';
5+
export { Prisma } from './node/prisma';
56

67
// TODO(v7): Remove this export
78
// Please see `src/index.ts` for more details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Hub } from '@sentry/hub';
2+
import { EventProcessor, Integration } from '@sentry/types';
3+
import { fill, isThenable, loadModule, logger } from '@sentry/utils';
4+
5+
import { IS_DEBUG_BUILD } from '../../flags';
6+
7+
interface PrismaClient {
8+
prototype: {
9+
query: () => void | Promise<unknown>;
10+
};
11+
}
12+
13+
/** Tracing integration for @prisma/client package */
14+
export class Prisma implements Integration {
15+
/**
16+
* @inheritDoc
17+
*/
18+
public static id: string = 'Prisma';
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public name: string = Prisma.id;
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
29+
const pkg = loadModule<{ PrismaClient: PrismaClient }>('@prisma/client');
30+
31+
if (!pkg) {
32+
IS_DEBUG_BUILD && logger.error('Prisma integration was unable to require `@prisma/client` package.');
33+
return;
34+
}
35+
36+
fill(pkg.PrismaClient.prototype, '_executeRequest', function (orig: () => void | Promise<unknown>) {
37+
return function (this: unknown, config: unknown) {
38+
const scope = getCurrentHub().getScope();
39+
const parentSpan = scope?.getSpan();
40+
const span = parentSpan?.startChild({
41+
description: typeof config === 'string' ? config : (config as { clientMethod: string }).clientMethod,
42+
op: 'db',
43+
});
44+
45+
const rv = orig.call(this, config);
46+
47+
if (isThenable(rv)) {
48+
return rv.then((res: unknown) => {
49+
span?.finish();
50+
return res;
51+
});
52+
}
53+
54+
span?.finish();
55+
return rv;
56+
};
57+
});
58+
}
59+
}

0 commit comments

Comments
 (0)