Skip to content

ref(tracing): Convert prisma integration to use trace func #7776

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 2 commits into from
Apr 6, 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
2 changes: 1 addition & 1 deletion packages/node-integration-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Tests can be run locally with:

To run tests with Jest's watch mode:

`yarn test:jest`
`yarn test:watch`

To filter tests by their title:

Expand Down
24 changes: 3 additions & 21 deletions packages/tracing-internal/src/node/integrations/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Hub } from '@sentry/core';
import { trace } from '@sentry/core';
import type { EventProcessor, Integration } from '@sentry/types';
import { isThenable, logger } from '@sentry/utils';
import { logger } from '@sentry/utils';

import { shouldDisableAutoInstrumentation } from './utils/node-utils';

Expand Down Expand Up @@ -88,28 +89,9 @@ export class Prisma implements Integration {
}

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;
return trace({ name: model ? `${model} ${action}` : action, op: 'db.sql.prisma' }, () => next(params));
});
}
}
39 changes: 19 additions & 20 deletions packages/tracing/test/integrations/node/prisma.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
import { Hub, Scope } from '@sentry/core';
import { logger } from '@sentry/utils';

import { Integrations, Span } from '../../../src';
import { Integrations } from '../../../src';
import { getTestClient } from '../../testutils';

const mockTrace = jest.fn();

jest.mock('@sentry/core', () => {
const original = jest.requireActual('@sentry/core');
return {
...original,
trace: (...args: unknown[]) => {
mockTrace(...args);
return original.trace(...args);
},
};
});

type PrismaMiddleware = (params: unknown, next: (params?: unknown) => Promise<unknown>) => Promise<unknown>;

class PrismaClient {
Expand All @@ -27,35 +40,21 @@ class PrismaClient {
describe('setupOnce', function () {
const Client: PrismaClient = new PrismaClient();

let scope = new Scope();
let parentSpan: Span;
let childSpan: Span;

beforeAll(() => {
new Integrations.Prisma({ client: Client }).setupOnce(
() => undefined,
() => new Hub(undefined, scope),
() => new Hub(undefined, new Scope()),
);
});

beforeEach(() => {
scope = new Scope();
parentSpan = new Span();
childSpan = parentSpan.startChild();
jest.spyOn(scope, 'getSpan').mockReturnValueOnce(parentSpan);
jest.spyOn(parentSpan, 'startChild').mockReturnValueOnce(childSpan);
jest.spyOn(childSpan, 'finish');
mockTrace.mockClear();
});

it('should add middleware with $use method correctly', done => {
void Client.user.create()?.then(res => {
expect(res).toBe('result');
expect(scope.getSpan).toBeCalled();
expect(parentSpan.startChild).toBeCalledWith({
description: 'user create',
op: 'db.sql.prisma',
});
expect(childSpan.finish).toBeCalled();
void Client.user.create()?.then(() => {
expect(mockTrace).toHaveBeenCalledTimes(1);
expect(mockTrace).toHaveBeenLastCalledWith({ name: 'user create', op: 'db.sql.prisma' }, expect.any(Function));
done();
});
});
Expand Down