|
| 1 | +import type { SerializedStreamedSpan } from '@sentry/core'; |
| 2 | +import { |
| 3 | + DB_NAMESPACE, |
| 4 | + DB_OPERATION_BATCH_SIZE, |
| 5 | + DB_QUERY_SUMMARY, |
| 6 | + DB_QUERY_TEXT, |
| 7 | + DB_SYSTEM_NAME, |
| 8 | + DB_USER, |
| 9 | + SENTRY_KIND, |
| 10 | + SENTRY_OP, |
| 11 | + SENTRY_ORIGIN, |
| 12 | + SERVER_ADDRESS, |
| 13 | + SERVER_PORT, |
| 14 | +} from '@sentry/conventions/attributes'; |
| 15 | +import { DB } from '@sentry/conventions/op'; |
| 16 | +import { afterAll, describe, expect } from 'vitest'; |
| 17 | +import { RUNTIME, streamedAttribute as attr } from '../../../utils'; |
| 18 | +import { cleanupChildProcesses, createEsmAndCjsTests, describeWithDockerCompose } from '../../../utils/runner'; |
| 19 | + |
| 20 | +const ORIGIN = 'auto.db.neon'; |
| 21 | + |
| 22 | +const CREATE_USER_TABLE_STATEMENT = |
| 23 | + 'CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(?) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"))'; |
| 24 | + |
| 25 | +// Both drivers report the same connection attributes: the WebSocket driver from pg's |
| 26 | +// `connectionParameters`, the HTTP driver from the resolved connection URL. |
| 27 | +const CONNECTION_ATTRIBUTES = { |
| 28 | + [DB_SYSTEM_NAME]: attr('postgresql'), |
| 29 | + [DB_NAMESPACE]: attr('tests'), |
| 30 | + [DB_USER]: attr('test'), |
| 31 | + [SERVER_ADDRESS]: attr('db.localtest.me'), |
| 32 | + [SERVER_PORT]: attr(5432, 'integer'), |
| 33 | +}; |
| 34 | + |
| 35 | +function expectedDbSpan({ |
| 36 | + name, |
| 37 | + statement, |
| 38 | + status = 'ok', |
| 39 | + batchSize, |
| 40 | +}: { |
| 41 | + name: string; |
| 42 | + statement: string; |
| 43 | + status?: 'ok' | 'error'; |
| 44 | + batchSize?: number; |
| 45 | +}): unknown { |
| 46 | + return expect.objectContaining({ |
| 47 | + attributes: expect.objectContaining({ |
| 48 | + ...CONNECTION_ATTRIBUTES, |
| 49 | + [SENTRY_OP]: attr(DB), |
| 50 | + [SENTRY_KIND]: attr('client'), |
| 51 | + [SENTRY_ORIGIN]: attr(ORIGIN), |
| 52 | + [DB_QUERY_TEXT]: attr(statement), |
| 53 | + [DB_QUERY_SUMMARY]: attr(name), |
| 54 | + ...(batchSize ? { [DB_OPERATION_BATCH_SIZE]: attr(batchSize, 'integer') } : {}), |
| 55 | + }), |
| 56 | + is_segment: false, |
| 57 | + name, |
| 58 | + parent_span_id: expect.stringMatching(/^[\da-f]{16}$/), |
| 59 | + status, |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +function getNeonSpans(spans: SerializedStreamedSpan[]): SerializedStreamedSpan[] { |
| 64 | + const neonSpans = spans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === ORIGIN); |
| 65 | + // `server.address`/`server.port` describe the connection; the deprecated string is not emitted. |
| 66 | + expect(neonSpans.some(span => 'db.connection_string' in span.attributes)).toBe(false); |
| 67 | + return neonSpans; |
| 68 | +} |
| 69 | + |
| 70 | +describeWithDockerCompose('neon auto instrumentation', { workingDirectory: [__dirname] }, () => { |
| 71 | + afterAll(() => { |
| 72 | + cleanupChildProcesses(); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('http driver', () => { |
| 76 | + createEsmAndCjsTests(__dirname, 'scenario-http.mjs', 'instrument.mjs', (createTestRunner, test) => { |
| 77 | + test('instruments `neon()` queries, templates and transactions', { timeout: 90_000 }, async () => { |
| 78 | + const runner = createTestRunner(); |
| 79 | + const spansPromise = runner.collectStreamedSpansUntilSegment('Test Span'); |
| 80 | + |
| 81 | + await runner.start().completed(); |
| 82 | + |
| 83 | + const spans = await spansPromise; |
| 84 | + const segment = spans.find(span => span.is_segment && span.name === 'Test Span'); |
| 85 | + expect(segment).toBeDefined(); |
| 86 | + |
| 87 | + const neonSpans = getNeonSpans(spans); |
| 88 | + expect(neonSpans.every(span => span.parent_span_id === segment!.span_id)).toBe(true); |
| 89 | + expect(neonSpans).toEqual([ |
| 90 | + expectedDbSpan({ name: 'CREATE TABLE "User"', statement: CREATE_USER_TABLE_STATEMENT }), |
| 91 | + expectedDbSpan({ name: 'INSERT "User"', statement: 'INSERT INTO "User" ("email", "name") VALUES ($1, $2)' }), |
| 92 | + expectedDbSpan({ name: 'SELECT "User"', statement: 'SELECT * FROM "User" WHERE "name" = $1' }), |
| 93 | + expectedDbSpan({ |
| 94 | + name: 'SELECT "User"; SELECT "User"', |
| 95 | + statement: 'SELECT "email" FROM "User"; SELECT "name" FROM "User"', |
| 96 | + batchSize: 2, |
| 97 | + }), |
| 98 | + expectedDbSpan({ |
| 99 | + name: 'SELECT "does_not_exist_table"', |
| 100 | + statement: 'SELECT * FROM "does_not_exist_table"', |
| 101 | + status: 'error', |
| 102 | + }), |
| 103 | + expectedDbSpan({ name: 'DROP TABLE "User"', statement: 'DROP TABLE "User"' }), |
| 104 | + ]); |
| 105 | + }); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('websocket driver', () => { |
| 110 | + createEsmAndCjsTests(__dirname, 'scenario-ws.mjs', 'instrument.mjs', (createTestRunner, test) => { |
| 111 | + test('instruments `Client` queries', { timeout: 90_000 }, async () => { |
| 112 | + const runner = createTestRunner(); |
| 113 | + const spansPromise = runner.collectStreamedSpansUntilSegment('Test Span'); |
| 114 | + |
| 115 | + await runner.start().completed(); |
| 116 | + |
| 117 | + const spans = await spansPromise; |
| 118 | + const segment = spans.find(span => span.is_segment && span.name === 'Test Span'); |
| 119 | + expect(segment).toBeDefined(); |
| 120 | + |
| 121 | + const neonSpans = getNeonSpans(spans); |
| 122 | + expect(neonSpans.every(span => span.parent_span_id === segment!.span_id)).toBe(true); |
| 123 | + expect(neonSpans).toEqual([ |
| 124 | + expectedDbSpan({ name: 'CREATE TABLE "User"', statement: CREATE_USER_TABLE_STATEMENT }), |
| 125 | + expectedDbSpan({ name: 'INSERT "User"', statement: 'INSERT INTO "User" ("email", "name") VALUES ($1, $2)' }), |
| 126 | + expectedDbSpan({ name: 'SELECT "User"', statement: 'SELECT * FROM "User"' }), |
| 127 | + expectedDbSpan({ |
| 128 | + name: 'SELECT "does_not_exist_table"', |
| 129 | + statement: 'SELECT * FROM "does_not_exist_table"', |
| 130 | + status: 'error', |
| 131 | + }), |
| 132 | + expectedDbSpan({ name: 'DROP TABLE "User"', statement: 'DROP TABLE "User"' }), |
| 133 | + ]); |
| 134 | + }); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('websocket pool', () => { |
| 139 | + createEsmAndCjsTests(__dirname, 'scenario-pool.mjs', 'instrument.mjs', (createTestRunner, test) => { |
| 140 | + // On Bun and Deno the pool's connect callback runs outside the segment's async context, so |
| 141 | + // the queries it issues have no parent span. |
| 142 | + test.skipIf(RUNTIME !== 'node')('instruments `Pool` queries', { timeout: 90_000 }, async () => { |
| 143 | + const runner = createTestRunner(); |
| 144 | + const spansPromise = runner.collectStreamedSpansUntilSegment('Test Span'); |
| 145 | + |
| 146 | + await runner.start().completed(); |
| 147 | + |
| 148 | + const spans = await spansPromise; |
| 149 | + const segment = spans.find(span => span.is_segment && span.name === 'Test Span'); |
| 150 | + expect(segment).toBeDefined(); |
| 151 | + |
| 152 | + const neonSpans = getNeonSpans(spans); |
| 153 | + expect(neonSpans.every(span => span.parent_span_id === segment!.span_id)).toBe(true); |
| 154 | + expect(neonSpans).toEqual([ |
| 155 | + expectedDbSpan({ |
| 156 | + name: 'SELECT pg_catalog.pg_user', |
| 157 | + statement: 'SELECT "email", "name" FROM pg_catalog.pg_user LIMIT ?', |
| 158 | + status: 'error', |
| 159 | + }), |
| 160 | + expectedDbSpan({ name: 'SELECT pg_catalog.pg_user', statement: 'SELECT "usename" FROM pg_catalog.pg_user' }), |
| 161 | + ]); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments