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