-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathprisma.ts
115 lines (97 loc) · 2.72 KB
/
prisma.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import type { Hub } from '@sentry/core';
import type { EventProcessor, Integration } from '@sentry/types';
import { isThenable, logger } from '@sentry/utils';
import { shouldDisableAutoInstrumentation } from './utils/node-utils';
type PrismaAction =
| 'findUnique'
| 'findMany'
| 'findFirst'
| 'create'
| 'createMany'
| 'update'
| 'updateMany'
| 'upsert'
| 'delete'
| 'deleteMany'
| 'executeRaw'
| 'queryRaw'
| 'aggregate'
| 'count'
| 'runCommandRaw';
interface PrismaMiddlewareParams {
model?: unknown;
action: PrismaAction;
args: unknown;
dataPath: string[];
runInTransaction: boolean;
}
type PrismaMiddleware<T = unknown> = (
params: PrismaMiddlewareParams,
next: (params: PrismaMiddlewareParams) => Promise<T>,
) => Promise<T>;
interface PrismaClient {
$use: (cb: PrismaMiddleware) => void;
}
function isValidPrismaClient(possibleClient: unknown): possibleClient is PrismaClient {
return possibleClient && !!(possibleClient as PrismaClient)['$use'];
}
/** 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?: unknown } = {}) {
if (isValidPrismaClient(options.client)) {
this._client = options.client;
} else {
__DEBUG_BUILD__ &&
logger.warn(
`Unsupported Prisma client provided to PrismaIntegration. Provided client: ${JSON.stringify(options.client)}`,
);
}
}
/**
* @inheritDoc
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
if (!this._client) {
__DEBUG_BUILD__ && logger.error('PrismaIntegration is missing a Prisma Client Instance');
return;
}
if (shouldDisableAutoInstrumentation(getCurrentHub)) {
__DEBUG_BUILD__ && logger.log('Prisma Integration is skipped because of instrumenter configuration.');
return;
}
this._client.$use((params, 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.sql.prisma',
});
const rv = next(params);
if (isThenable(rv)) {
return rv.then((res: unknown) => {
span?.finish();
return res;
});
}
span?.finish();
return rv;
});
}
}