|
| 1 | +import { createRunner } from '../../../utils/runner'; |
| 2 | + |
| 3 | +// When running docker compose, we need a larger timeout, as this takes some time... |
| 4 | +jest.setTimeout(90000); |
| 5 | + |
| 6 | +describe('knex auto instrumentation', () => { |
| 7 | + // Update this if another knex version is installed |
| 8 | + const KNEX_VERSION = '2.5.1'; |
| 9 | + |
| 10 | + test('should auto-instrument `knex` package when using `pg` client', done => { |
| 11 | + const EXPECTED_TRANSACTION = { |
| 12 | + transaction: 'Test Transaction', |
| 13 | + spans: expect.arrayContaining([ |
| 14 | + expect.objectContaining({ |
| 15 | + data: expect.objectContaining({ |
| 16 | + 'knex.version': KNEX_VERSION, |
| 17 | + 'db.system': 'postgresql', |
| 18 | + 'db.name': 'tests', |
| 19 | + 'sentry.origin': 'auto.db.otel.knex', |
| 20 | + 'sentry.op': 'db', |
| 21 | + 'net.peer.name': 'localhost', |
| 22 | + 'net.peer.port': 5445, |
| 23 | + }), |
| 24 | + status: 'ok', |
| 25 | + description: |
| 26 | + 'create table "User" ("id" serial primary key, "createdAt" timestamptz(3) not null default CURRENT_TIMESTAMP(3), "email" text not null, "name" text not null)', |
| 27 | + origin: 'auto.db.otel.knex', |
| 28 | + }), |
| 29 | + expect.objectContaining({ |
| 30 | + data: expect.objectContaining({ |
| 31 | + 'knex.version': KNEX_VERSION, |
| 32 | + 'db.system': 'postgresql', |
| 33 | + 'db.name': 'tests', |
| 34 | + 'sentry.origin': 'auto.db.otel.knex', |
| 35 | + 'sentry.op': 'db', |
| 36 | + 'net.peer.name': 'localhost', |
| 37 | + 'net.peer.port': 5445, |
| 38 | + }), |
| 39 | + status: 'ok', |
| 40 | + // In the knex-otel spans, the placeholders (e.g., `$1`) are replaced by a `?`. |
| 41 | + description: 'insert into "User" ("email", "name") values (?, ?)', |
| 42 | + origin: 'auto.db.otel.knex', |
| 43 | + }), |
| 44 | + |
| 45 | + expect.objectContaining({ |
| 46 | + data: expect.objectContaining({ |
| 47 | + 'knex.version': KNEX_VERSION, |
| 48 | + 'db.operation': 'select', |
| 49 | + 'db.sql.table': 'User', |
| 50 | + 'db.system': 'postgresql', |
| 51 | + 'db.name': 'tests', |
| 52 | + 'db.statement': 'select * from "User"', |
| 53 | + 'sentry.origin': 'auto.db.otel.knex', |
| 54 | + 'sentry.op': 'db', |
| 55 | + }), |
| 56 | + status: 'ok', |
| 57 | + description: 'select * from "User"', |
| 58 | + origin: 'auto.db.otel.knex', |
| 59 | + }), |
| 60 | + ]), |
| 61 | + }; |
| 62 | + |
| 63 | + createRunner(__dirname, 'scenario-withPostgres.js') |
| 64 | + .withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port 5432'] }) |
| 65 | + .expect({ transaction: EXPECTED_TRANSACTION }) |
| 66 | + .start(done); |
| 67 | + }); |
| 68 | + |
| 69 | + test('should auto-instrument `knex` package when using `mysql2` client', done => { |
| 70 | + const EXPECTED_TRANSACTION = { |
| 71 | + transaction: 'Test Transaction', |
| 72 | + spans: expect.arrayContaining([ |
| 73 | + expect.objectContaining({ |
| 74 | + data: expect.objectContaining({ |
| 75 | + 'knex.version': KNEX_VERSION, |
| 76 | + 'db.system': 'mysql2', |
| 77 | + 'db.name': 'tests', |
| 78 | + 'db.user': 'root', |
| 79 | + 'sentry.origin': 'auto.db.otel.knex', |
| 80 | + 'sentry.op': 'db', |
| 81 | + 'net.peer.name': 'localhost', |
| 82 | + 'net.peer.port': 3307, |
| 83 | + }), |
| 84 | + status: 'ok', |
| 85 | + description: |
| 86 | + 'create table `User` (`id` int unsigned not null auto_increment primary key, `createdAt` timestamp(3) not null default CURRENT_TIMESTAMP(3), `email` text not null, `name` text not null)', |
| 87 | + origin: 'auto.db.otel.knex', |
| 88 | + }), |
| 89 | + expect.objectContaining({ |
| 90 | + data: expect.objectContaining({ |
| 91 | + 'knex.version': KNEX_VERSION, |
| 92 | + 'db.system': 'mysql2', |
| 93 | + 'db.name': 'tests', |
| 94 | + 'db.user': 'root', |
| 95 | + 'sentry.origin': 'auto.db.otel.knex', |
| 96 | + 'sentry.op': 'db', |
| 97 | + 'net.peer.name': 'localhost', |
| 98 | + 'net.peer.port': 3307, |
| 99 | + }), |
| 100 | + status: 'ok', |
| 101 | + description: 'insert into `User` (`email`, `name`) values (?, ?)', |
| 102 | + origin: 'auto.db.otel.knex', |
| 103 | + }), |
| 104 | + |
| 105 | + expect.objectContaining({ |
| 106 | + data: expect.objectContaining({ |
| 107 | + 'knex.version': KNEX_VERSION, |
| 108 | + 'db.operation': 'select', |
| 109 | + 'db.sql.table': 'User', |
| 110 | + 'db.system': 'mysql2', |
| 111 | + 'db.name': 'tests', |
| 112 | + 'db.statement': 'select * from `User`', |
| 113 | + 'db.user': 'root', |
| 114 | + 'sentry.origin': 'auto.db.otel.knex', |
| 115 | + 'sentry.op': 'db', |
| 116 | + }), |
| 117 | + status: 'ok', |
| 118 | + description: 'select * from `User`', |
| 119 | + origin: 'auto.db.otel.knex', |
| 120 | + }), |
| 121 | + ]), |
| 122 | + }; |
| 123 | + |
| 124 | + createRunner(__dirname, 'scenario-withMysql2.js') |
| 125 | + .withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port: 3306'] }) |
| 126 | + .expect({ transaction: EXPECTED_TRANSACTION }) |
| 127 | + .start(done); |
| 128 | + }); |
| 129 | +}); |
0 commit comments