Skip to content

feat(tracing): Improve data collection for prisma spans #8779

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

Merged
merged 4 commits into from
Aug 10, 2023
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
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