Skip to content

Commit

Permalink
feat(tracing): Improve data collection for prisma spans (#8779)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad authored Aug 10, 2023
1 parent fc7344f commit f30da60
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ conditionalTest({ min: 12 })('Prisma ORM Integration', () => {
assertSentryTransaction(envelope[2], {
transaction: 'Test Transaction',
spans: [
{ description: 'User create', op: 'db.sql.prisma', data: { 'db.system': 'prisma' } },
{ description: 'User findMany', op: 'db.sql.prisma', data: { 'db.system': 'prisma' } },
{ description: 'User deleteMany', op: 'db.sql.prisma', data: { 'db.system': 'prisma' } },
{
description: 'User create',
op: 'db.sql.prisma',
data: { 'db.system': 'postgresql', 'db.operation': 'create', 'db.prisma.version': '3.12.0' },
},
{
description: 'User findMany',
op: 'db.sql.prisma',
data: { 'db.system': 'postgresql', 'db.operation': 'findMany', 'db.prisma.version': '3.12.0' },
},
{
description: 'User deleteMany',
op: 'db.sql.prisma',
data: { 'db.system': 'postgresql', 'db.operation': 'deleteMany', 'db.prisma.version': '3.12.0' },
},
],
});
});
Expand Down
18 changes: 15 additions & 3 deletions packages/node-integration-tests/suites/tracing/prisma-orm/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ conditionalTest({ min: 12 })('Prisma ORM Integration', () => {
assertSentryTransaction(envelope[2], {
transaction: 'Test Transaction',
spans: [
{ description: 'User create', op: 'db.sql.prisma', data: { 'db.system': 'prisma' } },
{ description: 'User findMany', op: 'db.sql.prisma', data: { 'db.system': 'prisma' } },
{ description: 'User deleteMany', op: 'db.sql.prisma', data: { 'db.system': 'prisma' } },
{
description: 'User create',
op: 'db.sql.prisma',
data: { 'db.system': 'postgresql', 'db.operation': 'create', 'db.prisma.version': '3.12.0' },
},
{
description: 'User findMany',
op: 'db.sql.prisma',
data: { 'db.system': 'postgresql', 'db.operation': 'findMany', 'db.prisma.version': '3.12.0' },
},
{
description: 'User deleteMany',
op: 'db.sql.prisma',
data: { 'db.system': 'postgresql', 'db.operation': 'deleteMany', 'db.prisma.version': '3.12.0' },
},
],
});
});
Expand Down
27 changes: 26 additions & 1 deletion packages/tracing-internal/src/node/integrations/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type PrismaMiddleware<T = unknown> = (

interface PrismaClient {
_sentryInstrumented?: boolean;
_engineConfig?: {
activeProvider?: string;
clientVersion?: string;
};
$use: (cb: PrismaMiddleware) => void;
}

Expand Down Expand Up @@ -70,15 +74,36 @@ export class Prisma implements Integration {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
addNonEnumerableProperty(options.client as any, '_sentryInstrumented', true);

const clientData: Record<string, string | number> = {};
try {
const engineConfig = (options.client as PrismaClient)._engineConfig;
if (engineConfig) {
const { activeProvider, clientVersion } = engineConfig;
if (activeProvider) {
clientData['db.system'] = activeProvider;
}
if (clientVersion) {
clientData['db.prisma.version'] = clientVersion;
}
}
} catch (e) {
// ignore
}

options.client.$use((params, next: (params: PrismaMiddlewareParams) => Promise<unknown>) => {
if (shouldDisableAutoInstrumentation(getCurrentHub)) {
return next(params);
}

const action = params.action;
const model = params.model;

return trace(
{ name: model ? `${model} ${action}` : action, op: 'db.sql.prisma', data: { 'db.system': 'prisma' } },
{
name: model ? `${model} ${action}` : action,
op: 'db.sql.prisma',
data: { ...clientData, 'db.operation': action },
},
() => next(params),
);
});
Expand Down
11 changes: 10 additions & 1 deletion packages/tracing/test/integrations/node/prisma.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class PrismaClient {
create: () => this._middleware?.({ action: 'create', model: 'user' }, () => Promise.resolve('result')),
};

public _engineConfig = {
activeProvider: 'postgresql',
clientVersion: '3.1.2',
};

private _middleware?: PrismaMiddleware;

constructor() {
Expand All @@ -48,7 +53,11 @@ describe('setupOnce', function () {
void prismaClient.user.create()?.then(() => {
expect(mockTrace).toHaveBeenCalledTimes(1);
expect(mockTrace).toHaveBeenLastCalledWith(
{ name: 'user create', op: 'db.sql.prisma', data: { 'db.system': 'prisma' } },
{
name: 'user create',
op: 'db.sql.prisma',
data: { 'db.system': 'postgresql', 'db.prisma.version': '3.1.2', 'db.operation': 'create' },
},
expect.any(Function),
);
done();
Expand Down

0 comments on commit f30da60

Please sign in to comment.