Skip to content

Commit f0f9b8a

Browse files
authored
ref(tracing): Convert prisma integration to use trace func (#7776)
1 parent e741dd1 commit f0f9b8a

File tree

3 files changed

+23
-42
lines changed

3 files changed

+23
-42
lines changed

packages/node-integration-tests/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Tests can be run locally with:
4747

4848
To run tests with Jest's watch mode:
4949

50-
`yarn test:jest`
50+
`yarn test:watch`
5151

5252
To filter tests by their title:
5353

packages/tracing-internal/src/node/integrations/prisma.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Hub } from '@sentry/core';
2+
import { trace } from '@sentry/core';
23
import type { EventProcessor, Integration } from '@sentry/types';
3-
import { isThenable, logger } from '@sentry/utils';
4+
import { logger } from '@sentry/utils';
45

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

@@ -88,28 +89,9 @@ export class Prisma implements Integration {
8889
}
8990

9091
this._client.$use((params, next: (params: PrismaMiddlewareParams) => Promise<unknown>) => {
91-
const scope = getCurrentHub().getScope();
92-
const parentSpan = scope?.getSpan();
93-
9492
const action = params.action;
9593
const model = params.model;
96-
97-
const span = parentSpan?.startChild({
98-
description: model ? `${model} ${action}` : action,
99-
op: 'db.sql.prisma',
100-
});
101-
102-
const rv = next(params);
103-
104-
if (isThenable(rv)) {
105-
return rv.then((res: unknown) => {
106-
span?.finish();
107-
return res;
108-
});
109-
}
110-
111-
span?.finish();
112-
return rv;
94+
return trace({ name: model ? `${model} ${action}` : action, op: 'db.sql.prisma' }, () => next(params));
11395
});
11496
}
11597
}

packages/tracing/test/integrations/node/prisma.test.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@
33
import { Hub, Scope } from '@sentry/core';
44
import { logger } from '@sentry/utils';
55

6-
import { Integrations, Span } from '../../../src';
6+
import { Integrations } from '../../../src';
77
import { getTestClient } from '../../testutils';
88

9+
const mockTrace = jest.fn();
10+
11+
jest.mock('@sentry/core', () => {
12+
const original = jest.requireActual('@sentry/core');
13+
return {
14+
...original,
15+
trace: (...args: unknown[]) => {
16+
mockTrace(...args);
17+
return original.trace(...args);
18+
},
19+
};
20+
});
21+
922
type PrismaMiddleware = (params: unknown, next: (params?: unknown) => Promise<unknown>) => Promise<unknown>;
1023

1124
class PrismaClient {
@@ -27,35 +40,21 @@ class PrismaClient {
2740
describe('setupOnce', function () {
2841
const Client: PrismaClient = new PrismaClient();
2942

30-
let scope = new Scope();
31-
let parentSpan: Span;
32-
let childSpan: Span;
33-
3443
beforeAll(() => {
3544
new Integrations.Prisma({ client: Client }).setupOnce(
3645
() => undefined,
37-
() => new Hub(undefined, scope),
46+
() => new Hub(undefined, new Scope()),
3847
);
3948
});
4049

4150
beforeEach(() => {
42-
scope = new Scope();
43-
parentSpan = new Span();
44-
childSpan = parentSpan.startChild();
45-
jest.spyOn(scope, 'getSpan').mockReturnValueOnce(parentSpan);
46-
jest.spyOn(parentSpan, 'startChild').mockReturnValueOnce(childSpan);
47-
jest.spyOn(childSpan, 'finish');
51+
mockTrace.mockClear();
4852
});
4953

5054
it('should add middleware with $use method correctly', done => {
51-
void Client.user.create()?.then(res => {
52-
expect(res).toBe('result');
53-
expect(scope.getSpan).toBeCalled();
54-
expect(parentSpan.startChild).toBeCalledWith({
55-
description: 'user create',
56-
op: 'db.sql.prisma',
57-
});
58-
expect(childSpan.finish).toBeCalled();
55+
void Client.user.create()?.then(() => {
56+
expect(mockTrace).toHaveBeenCalledTimes(1);
57+
expect(mockTrace).toHaveBeenLastCalledWith({ name: 'user create', op: 'db.sql.prisma' }, expect.any(Function));
5958
done();
6059
});
6160
});

0 commit comments

Comments
 (0)