diff --git a/packages/node-integration-tests/README.md b/packages/node-integration-tests/README.md index e4354ad4fdee..ec202b5a8252 100644 --- a/packages/node-integration-tests/README.md +++ b/packages/node-integration-tests/README.md @@ -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: diff --git a/packages/tracing-internal/src/node/integrations/prisma.ts b/packages/tracing-internal/src/node/integrations/prisma.ts index 2215cf2a817a..7d23cf6adbb5 100644 --- a/packages/tracing-internal/src/node/integrations/prisma.ts +++ b/packages/tracing-internal/src/node/integrations/prisma.ts @@ -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'; @@ -88,28 +89,9 @@ export class Prisma implements Integration { } this._client.$use((params, next: (params: PrismaMiddlewareParams) => Promise) => { - 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)); }); } } diff --git a/packages/tracing/test/integrations/node/prisma.test.ts b/packages/tracing/test/integrations/node/prisma.test.ts index d1acdc65190e..1eb85a251704 100644 --- a/packages/tracing/test/integrations/node/prisma.test.ts +++ b/packages/tracing/test/integrations/node/prisma.test.ts @@ -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) => Promise; class PrismaClient { @@ -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(); }); });