|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import { createRunner } from '../../../utils/runner'; |
| 3 | + |
| 4 | +const EXISTING_TEST_EMAIL = 'bar@baz.com'; |
| 5 | +const NON_EXISTING_TEST_EMAIL = 'foo@baz.com'; |
| 6 | + |
| 7 | +describe('postgresjs auto instrumentation', () => { |
| 8 | + test('should auto-instrument `postgres` package', { timeout: 60_000 }, async () => { |
| 9 | + const EXPECTED_TRANSACTION = { |
| 10 | + transaction: 'Test Transaction', |
| 11 | + spans: expect.arrayContaining([ |
| 12 | + expect.objectContaining({ |
| 13 | + data: expect.objectContaining({ |
| 14 | + 'db.namespace': 'test_db', |
| 15 | + 'db.system.name': 'postgres', |
| 16 | + 'db.operation.name': 'CREATE TABLE', |
| 17 | + 'db.query.text': |
| 18 | + '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"))', |
| 19 | + 'sentry.op': 'db', |
| 20 | + 'sentry.origin': 'auto.db.otel.postgres', |
| 21 | + 'server.address': 'localhost', |
| 22 | + 'server.port': 5444, |
| 23 | + }), |
| 24 | + description: |
| 25 | + '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"))', |
| 26 | + op: 'db', |
| 27 | + status: 'ok', |
| 28 | + origin: 'auto.db.otel.postgres', |
| 29 | + parent_span_id: expect.any(String), |
| 30 | + span_id: expect.any(String), |
| 31 | + start_timestamp: expect.any(Number), |
| 32 | + timestamp: expect.any(Number), |
| 33 | + trace_id: expect.any(String), |
| 34 | + }), |
| 35 | + expect.objectContaining({ |
| 36 | + data: expect.objectContaining({ |
| 37 | + 'db.namespace': 'test_db', |
| 38 | + 'db.system.name': 'postgres', |
| 39 | + 'db.operation.name': 'SELECT', |
| 40 | + 'db.query.text': |
| 41 | + "select b.oid, b.typarray from pg_catalog.pg_type a left join pg_catalog.pg_type b on b.oid = a.typelem where a.typcategory = 'A' group by b.oid, b.typarray order by b.oid", |
| 42 | + 'sentry.op': 'db', |
| 43 | + 'sentry.origin': 'auto.db.otel.postgres', |
| 44 | + 'server.address': 'localhost', |
| 45 | + 'server.port': 5444, |
| 46 | + }), |
| 47 | + description: |
| 48 | + "select b.oid, b.typarray from pg_catalog.pg_type a left join pg_catalog.pg_type b on b.oid = a.typelem where a.typcategory = 'A' group by b.oid, b.typarray order by b.oid", |
| 49 | + op: 'db', |
| 50 | + status: 'ok', |
| 51 | + origin: 'auto.db.otel.postgres', |
| 52 | + parent_span_id: expect.any(String), |
| 53 | + span_id: expect.any(String), |
| 54 | + start_timestamp: expect.any(Number), |
| 55 | + timestamp: expect.any(Number), |
| 56 | + trace_id: expect.any(String), |
| 57 | + }), |
| 58 | + expect.objectContaining({ |
| 59 | + data: expect.objectContaining({ |
| 60 | + 'db.namespace': 'test_db', |
| 61 | + 'db.system.name': 'postgres', |
| 62 | + 'db.operation.name': 'INSERT', |
| 63 | + 'db.query.text': `INSERT INTO "User" ("email", "name") VALUES ('Foo', '${EXISTING_TEST_EMAIL}')`, |
| 64 | + 'sentry.origin': 'auto.db.otel.postgres', |
| 65 | + 'sentry.op': 'db', |
| 66 | + 'server.address': 'localhost', |
| 67 | + 'server.port': 5444, |
| 68 | + }), |
| 69 | + description: `INSERT INTO "User" ("email", "name") VALUES ('Foo', '${EXISTING_TEST_EMAIL}')`, |
| 70 | + op: 'db', |
| 71 | + status: 'ok', |
| 72 | + origin: 'auto.db.otel.postgres', |
| 73 | + parent_span_id: expect.any(String), |
| 74 | + span_id: expect.any(String), |
| 75 | + start_timestamp: expect.any(Number), |
| 76 | + timestamp: expect.any(Number), |
| 77 | + trace_id: expect.any(String), |
| 78 | + }), |
| 79 | + expect.objectContaining({ |
| 80 | + data: expect.objectContaining({ |
| 81 | + 'db.namespace': 'test_db', |
| 82 | + 'db.system.name': 'postgres', |
| 83 | + 'db.operation.name': 'UPDATE', |
| 84 | + 'db.query.text': `UPDATE "User" SET "name" = 'Foo' WHERE "email" = '${EXISTING_TEST_EMAIL}'`, |
| 85 | + 'sentry.op': 'db', |
| 86 | + 'sentry.origin': 'auto.db.otel.postgres', |
| 87 | + 'server.address': 'localhost', |
| 88 | + 'server.port': 5444, |
| 89 | + }), |
| 90 | + description: `UPDATE "User" SET "name" = 'Foo' WHERE "email" = '${EXISTING_TEST_EMAIL}'`, |
| 91 | + op: 'db', |
| 92 | + status: 'ok', |
| 93 | + origin: 'auto.db.otel.postgres', |
| 94 | + parent_span_id: expect.any(String), |
| 95 | + span_id: expect.any(String), |
| 96 | + start_timestamp: expect.any(Number), |
| 97 | + timestamp: expect.any(Number), |
| 98 | + trace_id: expect.any(String), |
| 99 | + }), |
| 100 | + expect.objectContaining({ |
| 101 | + data: expect.objectContaining({ |
| 102 | + 'db.namespace': 'test_db', |
| 103 | + 'db.system.name': 'postgres', |
| 104 | + 'db.operation.name': 'SELECT', |
| 105 | + 'db.query.text': `SELECT * FROM "User" WHERE "email" = '${EXISTING_TEST_EMAIL}'`, |
| 106 | + 'sentry.op': 'db', |
| 107 | + 'sentry.origin': 'auto.db.otel.postgres', |
| 108 | + 'server.address': 'localhost', |
| 109 | + 'server.port': 5444, |
| 110 | + }), |
| 111 | + description: `SELECT * FROM "User" WHERE "email" = '${EXISTING_TEST_EMAIL}'`, |
| 112 | + op: 'db', |
| 113 | + status: 'ok', |
| 114 | + origin: 'auto.db.otel.postgres', |
| 115 | + parent_span_id: expect.any(String), |
| 116 | + span_id: expect.any(String), |
| 117 | + start_timestamp: expect.any(Number), |
| 118 | + timestamp: expect.any(Number), |
| 119 | + trace_id: expect.any(String), |
| 120 | + }), |
| 121 | + expect.objectContaining({ |
| 122 | + data: expect.objectContaining({ |
| 123 | + 'db.namespace': 'test_db', |
| 124 | + 'db.system.name': 'postgres', |
| 125 | + 'db.operation.name': 'SELECT', |
| 126 | + 'db.query.text': 'SELECT * from generate_series(?,?) as x', |
| 127 | + 'sentry.op': 'db', |
| 128 | + 'sentry.origin': 'auto.db.otel.postgres', |
| 129 | + 'server.address': 'localhost', |
| 130 | + 'server.port': 5444, |
| 131 | + }), |
| 132 | + description: 'SELECT * from generate_series(?,?) as x', |
| 133 | + op: 'db', |
| 134 | + status: 'ok', |
| 135 | + origin: 'auto.db.otel.postgres', |
| 136 | + parent_span_id: expect.any(String), |
| 137 | + span_id: expect.any(String), |
| 138 | + start_timestamp: expect.any(Number), |
| 139 | + timestamp: expect.any(Number), |
| 140 | + trace_id: expect.any(String), |
| 141 | + }), |
| 142 | + expect.objectContaining({ |
| 143 | + data: expect.objectContaining({ |
| 144 | + 'db.namespace': 'test_db', |
| 145 | + 'db.system.name': 'postgres', |
| 146 | + 'db.operation.name': 'DROP TABLE', |
| 147 | + 'db.query.text': 'DROP TABLE "User"', |
| 148 | + 'sentry.op': 'db', |
| 149 | + 'sentry.origin': 'auto.db.otel.postgres', |
| 150 | + 'server.address': 'localhost', |
| 151 | + 'server.port': 5444, |
| 152 | + }), |
| 153 | + description: 'DROP TABLE "User"', |
| 154 | + op: 'db', |
| 155 | + status: 'ok', |
| 156 | + origin: 'auto.db.otel.postgres', |
| 157 | + parent_span_id: expect.any(String), |
| 158 | + span_id: expect.any(String), |
| 159 | + start_timestamp: expect.any(Number), |
| 160 | + timestamp: expect.any(Number), |
| 161 | + trace_id: expect.any(String), |
| 162 | + }), |
| 163 | + expect.objectContaining({ |
| 164 | + data: expect.objectContaining({ |
| 165 | + 'db.namespace': 'test_db', |
| 166 | + 'db.system.name': 'postgres', |
| 167 | + // No db.operation.name here, as this is an errored span |
| 168 | + 'db.response.status_code': '42P01', |
| 169 | + 'error.type': 'PostgresError', |
| 170 | + 'db.query.text': `SELECT * FROM "User" WHERE "email" = '${NON_EXISTING_TEST_EMAIL}'`, |
| 171 | + 'sentry.op': 'db', |
| 172 | + 'sentry.origin': 'auto.db.otel.postgres', |
| 173 | + 'server.address': 'localhost', |
| 174 | + 'server.port': 5444, |
| 175 | + }), |
| 176 | + description: `SELECT * FROM "User" WHERE "email" = '${NON_EXISTING_TEST_EMAIL}'`, |
| 177 | + op: 'db', |
| 178 | + status: 'unknown_error', |
| 179 | + origin: 'auto.db.otel.postgres', |
| 180 | + parent_span_id: expect.any(String), |
| 181 | + span_id: expect.any(String), |
| 182 | + start_timestamp: expect.any(Number), |
| 183 | + timestamp: expect.any(Number), |
| 184 | + trace_id: expect.any(String), |
| 185 | + }), |
| 186 | + ]), |
| 187 | + }; |
| 188 | + |
| 189 | + const EXPECTED_ERROR_EVENT = { |
| 190 | + event_id: expect.any(String), |
| 191 | + contexts: { |
| 192 | + trace: { |
| 193 | + trace_id: expect.any(String), |
| 194 | + span_id: expect.any(String), |
| 195 | + }, |
| 196 | + }, |
| 197 | + exception: { |
| 198 | + values: [ |
| 199 | + { |
| 200 | + type: 'PostgresError', |
| 201 | + value: 'relation "User" does not exist', |
| 202 | + stacktrace: expect.objectContaining({ |
| 203 | + frames: expect.arrayContaining([ |
| 204 | + expect.objectContaining({ |
| 205 | + function: 'handle', |
| 206 | + module: 'postgres.cjs.src:connection', |
| 207 | + filename: expect.any(String), |
| 208 | + lineno: expect.any(Number), |
| 209 | + colno: expect.any(Number), |
| 210 | + }), |
| 211 | + ]), |
| 212 | + }), |
| 213 | + }, |
| 214 | + ], |
| 215 | + }, |
| 216 | + }; |
| 217 | + |
| 218 | + await createRunner(__dirname, 'scenario.js') |
| 219 | + .withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port 5432'] }) |
| 220 | + .expect({ transaction: EXPECTED_TRANSACTION }) |
| 221 | + .expect({ event: EXPECTED_ERROR_EVENT }) |
| 222 | + .start() |
| 223 | + .completed(); |
| 224 | + }); |
| 225 | +}); |
0 commit comments