|
| 1 | +import type { SerializedStreamedSpan, SerializedStreamedSpanContainer } from '@sentry/core'; |
1 | 2 | import { afterAll, describe, expect } from 'vitest'; |
| 3 | +import { conditionalTest } from '../../../utils'; |
2 | 4 | import { cleanupChildProcesses, createEsmAndCjsTests, describeWithDockerCompose } from '../../../utils/runner'; |
3 | 5 |
|
4 | 6 | afterAll(() => { |
5 | 7 | cleanupChildProcesses(); |
6 | 8 | }); |
7 | 9 |
|
8 | | -// Prisma 8 ("Prisma Next") has no tracing surface (its scorecard lists OpenTelemetry spans as not in 8.0), so |
9 | | -// `prismaIntegration` is inert and only the `pg` spans from `postgresIntegration` remain. Pinned here so a |
10 | | -// Prisma release that adds a tracing surface shows up. The runtime runs on every Node version in the matrix; |
11 | | -// only the Prisma 8 CLI needs Node 22.18+, which is why the generated files are committed. |
| 10 | +function getSegment(container: SerializedStreamedSpanContainer): SerializedStreamedSpan { |
| 11 | + const segment = container.items.find(item => item.is_segment); |
| 12 | + expect(segment?.name).toBe('Test Transaction'); |
| 13 | + return segment!; |
| 14 | +} |
| 15 | + |
| 16 | +function getQuerySpans(container: SerializedStreamedSpanContainer): SerializedStreamedSpan[] { |
| 17 | + const querySpans = container.items.filter( |
| 18 | + item => item.attributes['sentry.origin']?.value === 'auto.db.postgres' && item.attributes['db.query.text']?.value, |
| 19 | + ); |
| 20 | + expect(querySpans.map(span => span.name)).toEqual( |
| 21 | + expect.arrayContaining(['INSERT "public"."user"', 'SELECT "public"."user"', 'DELETE "public"."user"']), |
| 22 | + ); |
| 23 | + querySpans.forEach(span => { |
| 24 | + expect(span.attributes['sentry.op']?.value).toBe('db'); |
| 25 | + expect(span.attributes['db.system.name']?.value).toBe('postgresql'); |
| 26 | + }); |
| 27 | + return querySpans; |
| 28 | +} |
| 29 | + |
| 30 | +function getOperationSpans(container: SerializedStreamedSpanContainer): SerializedStreamedSpan[] { |
| 31 | + return container.items.filter(item => item.attributes['sentry.origin']?.value === 'auto.db.prisma'); |
| 32 | +} |
| 33 | + |
| 34 | +// Prisma 8 has no tracing surface: the operation spans come from the orchestrion channels the runtime hook |
| 35 | +// injects, and the `pg` spans underneath stand in for v7's `db_query` spans. |
12 | 36 | describe('Prisma ORM v8 Tests', () => { |
13 | 37 | describeWithDockerCompose('Prisma ORM v8', { workingDirectory: [__dirname] }, () => { |
14 | 38 | createEsmAndCjsTests( |
15 | 39 | __dirname, |
16 | 40 | 'scenario.mjs', |
17 | 41 | 'instrument.mjs', |
18 | | - (createRunner, test) => { |
19 | | - test('should instrument PostgreSQL queries from Prisma ORM via pg', { timeout: 75_000 }, async () => { |
20 | | - await createRunner() |
21 | | - .unordered() |
22 | | - .expect({ |
23 | | - span: container => { |
24 | | - const segment = container.items.find(item => item.is_segment); |
25 | | - expect(segment?.name).toBe('Test Transaction'); |
26 | | - |
27 | | - const querySpans = container.items.filter( |
28 | | - item => |
29 | | - item.attributes['sentry.origin']?.value === 'auto.db.postgres' && |
30 | | - item.attributes['db.query.text']?.value, |
31 | | - ); |
32 | | - expect(querySpans.map(span => span.name)).toEqual( |
33 | | - expect.arrayContaining([ |
34 | | - 'INSERT "public"."user"', |
35 | | - 'SELECT "public"."user"', |
36 | | - 'DELETE "public"."user"', |
37 | | - ]), |
38 | | - ); |
39 | | - querySpans.forEach(span => { |
40 | | - expect(span.attributes['sentry.op']?.value).toBe('db'); |
41 | | - expect(span.attributes['db.system.name']?.value).toBe('postgresql'); |
42 | | - expect(span.parent_span_id).toBe(segment?.span_id); |
43 | | - }); |
44 | | - |
45 | | - expect( |
46 | | - container.items.filter(item => item.attributes['sentry.origin']?.value === 'auto.db.prisma'), |
47 | | - ).toEqual([]); |
48 | | - }, |
49 | | - }) |
50 | | - .start() |
51 | | - .completed(); |
| 42 | + (createRunner, test, mode) => { |
| 43 | + const testInstrumentedOperations = (): void => { |
| 44 | + test('should instrument Prisma ORM operations and nest their queries', { timeout: 75_000 }, async () => { |
| 45 | + await createRunner() |
| 46 | + .unordered() |
| 47 | + .expect({ |
| 48 | + span: container => { |
| 49 | + const segment = getSegment(container); |
| 50 | + const querySpans = getQuerySpans(container); |
| 51 | + const operationSpans = getOperationSpans(container); |
| 52 | + |
| 53 | + expect(operationSpans.map(span => span.name)).toEqual([ |
| 54 | + 'prisma:client:operation', |
| 55 | + 'prisma:client:operation', |
| 56 | + 'prisma:client:operation', |
| 57 | + ]); |
| 58 | + expect(operationSpans.map(span => span.attributes['method']?.value)).toEqual([ |
| 59 | + 'create', |
| 60 | + 'all', |
| 61 | + 'delete', |
| 62 | + ]); |
| 63 | + operationSpans.forEach(span => { |
| 64 | + const method = span.attributes['method']?.value; |
| 65 | + expect(span.parent_span_id).toBe(segment.span_id); |
| 66 | + expect(span.attributes).toMatchObject({ |
| 67 | + 'sentry.origin': { value: 'auto.db.prisma', type: 'string' }, |
| 68 | + 'sentry.op': { value: 'db', type: 'string' }, |
| 69 | + 'db.operation.name': { value: method, type: 'string' }, |
| 70 | + 'db.collection.name': { value: 'user', type: 'string' }, |
| 71 | + model: { value: 'User', type: 'string' }, |
| 72 | + name: { value: `User.${method}`, type: 'string' }, |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + const queriesByOperation = (method: string): unknown[] => { |
| 77 | + const operation = operationSpans.find(span => span.attributes['method']?.value === method); |
| 78 | + return querySpans |
| 79 | + .filter(span => span.parent_span_id === operation?.span_id) |
| 80 | + .map(span => span.attributes['db.query.text']?.value); |
| 81 | + }; |
| 82 | + expect(queriesByOperation('create')).toEqual( |
| 83 | + expect.arrayContaining([expect.stringMatching(/^INSERT INTO "public"\."user" /)]), |
| 84 | + ); |
| 85 | + expect(queriesByOperation('all')).toEqual([ |
| 86 | + expect.stringMatching(/^SELECT .* FROM "public"\."user"$/), |
| 87 | + ]); |
| 88 | + expect(queriesByOperation('delete')).toEqual( |
| 89 | + expect.arrayContaining([expect.stringMatching(/^DELETE FROM "public"\."user" /)]), |
| 90 | + ); |
| 91 | + expect(querySpans.filter(span => span.parent_span_id === segment.span_id)).toEqual([]); |
| 92 | + }, |
| 93 | + }) |
| 94 | + .start() |
| 95 | + .completed(); |
| 96 | + }); |
| 97 | + }; |
| 98 | + |
| 99 | + if (mode === 'esm') { |
| 100 | + testInstrumentedOperations(); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // The CJS scenario loads the ESM-only package via `require(esm)`, which Node's module hooks only see |
| 105 | + // from Node 22 on; on Node 20 the ORM terminals load uninstrumented. |
| 106 | + conditionalTest({ min: 22 })('with require(esm) reaching the module hooks', testInstrumentedOperations); |
| 107 | + |
| 108 | + conditionalTest({ max: 21 })('with require(esm) bypassing the module hooks', () => { |
| 109 | + test('should keep the pg spans on the transaction', { timeout: 75_000 }, async () => { |
| 110 | + await createRunner() |
| 111 | + .unordered() |
| 112 | + .expect({ |
| 113 | + span: container => { |
| 114 | + const segment = getSegment(container); |
| 115 | + expect(getOperationSpans(container)).toEqual([]); |
| 116 | + getQuerySpans(container).forEach(span => expect(span.parent_span_id).toBe(segment.span_id)); |
| 117 | + }, |
| 118 | + }) |
| 119 | + .start() |
| 120 | + .completed(); |
| 121 | + }); |
52 | 122 | }); |
53 | 123 | }, |
54 | 124 | { |
|
0 commit comments