1+ import {
2+ DB_NAMESPACE ,
3+ DB_OPERATION_NAME ,
4+ DB_QUERY_SUMMARY ,
5+ DB_QUERY_TEXT ,
6+ DB_SYSTEM_NAME ,
7+ DB_USER ,
8+ SENTRY_OP ,
9+ SENTRY_ORIGIN ,
10+ SERVER_ADDRESS ,
11+ SERVER_PORT ,
12+ } from '@sentry/conventions/attributes' ;
13+ import { DB } from '@sentry/conventions/op' ;
114import { describe , expect } from 'vitest' ;
215import { createEsmAndCjsTests , describeWithDockerCompose } from '../../../../utils/runner' ;
316
@@ -9,93 +22,75 @@ describeWithDockerCompose('knex auto instrumentation', { workingDirectory: [__di
922 describe ( 'with `mysql2` client' , ( ) => {
1023 createEsmAndCjsTests ( __dirname , 'scenario.mjs' , 'instrument.mjs' , ( createRunner , test ) => {
1124 test ( 'should auto-instrument `knex` package' , { timeout : 60_000 } , async ( ) => {
12- const EXPECTED_TRANSACTION = {
13- transaction : 'Test Transaction' ,
14- spans : expect . arrayContaining ( [
15- expect . objectContaining ( {
16- data : expect . objectContaining ( {
17- 'knex.version' : KNEX_VERSION ,
18- 'db.system.name' : 'mysql2' ,
19- 'db.namespace' : 'tests' ,
20- 'db.user' : 'root' ,
21- 'sentry.origin' : ORIGIN ,
22- 'sentry.op' : 'db' ,
23- 'server.address' : 'localhost' ,
24- 'server.port' : 3307 ,
25- } ) ,
26- status : 'ok' ,
27- description :
28- 'create table `User` (`id` int unsigned not null auto_increment primary key, `createdAt` timestamp(?) not null default CURRENT_TIMESTAMP(?), `email` text not null, `name` text not null)' ,
29- origin : ORIGIN ,
25+ const EXPECTED_SPANS = [
26+ expect . objectContaining ( {
27+ attributes : expect . objectContaining ( {
28+ 'knex.version' : { type : 'string' , value : KNEX_VERSION } ,
29+ [ DB_SYSTEM_NAME ] : { type : 'string' , value : 'mysql2' } ,
30+ [ DB_NAMESPACE ] : { type : 'string' , value : 'tests' } ,
31+ [ DB_USER ] : { type : 'string' , value : 'root' } ,
32+ [ SENTRY_ORIGIN ] : { type : 'string' , value : ORIGIN } ,
33+ [ SENTRY_OP ] : { type : 'string' , value : DB } ,
34+ [ SERVER_ADDRESS ] : { type : 'string' , value : 'localhost' } ,
35+ [ SERVER_PORT ] : { type : 'integer' , value : 3307 } ,
36+ [ DB_QUERY_TEXT ] : {
37+ type : 'string' ,
38+ value :
39+ 'create table `User` (`id` int unsigned not null auto_increment primary key, `createdAt` timestamp(?) not null default CURRENT_TIMESTAMP(?), `email` text not null, `name` text not null)' ,
40+ } ,
41+ [ DB_QUERY_SUMMARY ] : { type : 'string' , value : 'create table `User`' } ,
3042 } ) ,
31- expect . objectContaining ( {
32- data : expect . objectContaining ( {
33- 'knex.version' : KNEX_VERSION ,
34- 'db.system.name' : 'mysql2' ,
35- 'db.namespace' : 'tests' ,
36- 'db.user' : 'root' ,
37- 'sentry.origin' : ORIGIN ,
38- 'sentry.op' : 'db' ,
39- 'server.address' : 'localhost' ,
40- 'server.port' : 3307 ,
41- } ) ,
42- status : 'ok' ,
43- description : 'insert into `User` (`email`, `name`) values (?, ?)' ,
44- origin : ORIGIN ,
43+ status : 'ok' ,
44+ name : 'create table `User`' ,
45+ } ) ,
46+ expect . objectContaining ( {
47+ attributes : expect . objectContaining ( {
48+ 'knex.version' : { type : 'string' , value : KNEX_VERSION } ,
49+ [ DB_SYSTEM_NAME ] : { type : 'string' , value : 'mysql2' } ,
50+ [ DB_NAMESPACE ] : { type : 'string' , value : 'tests' } ,
51+ [ DB_USER ] : { type : 'string' , value : 'root' } ,
52+ [ SENTRY_ORIGIN ] : { type : 'string' , value : ORIGIN } ,
53+ [ SENTRY_OP ] : { type : 'string' , value : DB } ,
54+ [ SERVER_ADDRESS ] : { type : 'string' , value : 'localhost' } ,
55+ [ SERVER_PORT ] : { type : 'integer' , value : 3307 } ,
56+ [ DB_QUERY_TEXT ] : { type : 'string' , value : 'insert into `User` (`email`, `name`) values (?, ?)' } ,
57+ [ DB_QUERY_SUMMARY ] : { type : 'string' , value : 'insert `User`' } ,
4558 } ) ,
59+ status : 'ok' ,
60+ name : 'insert `User`' ,
61+ } ) ,
4662
47- expect . objectContaining ( {
48- data : expect . objectContaining ( {
49- 'knex.version' : KNEX_VERSION ,
50- 'db.operation.name' : 'select' ,
51- 'db.sql.table' : 'User' ,
52- 'db.system.name' : 'mysql2' ,
53- 'db.namespace' : 'tests' ,
54- 'db.query.text' : 'select * from `User`' ,
55- 'db.user' : 'root' ,
56- 'sentry.origin' : ORIGIN ,
57- 'sentry.op' : 'db' ,
58- } ) ,
59- status : 'ok' ,
60- description : 'select * from `User`' ,
61- origin : ORIGIN ,
63+ expect . objectContaining ( {
64+ attributes : expect . objectContaining ( {
65+ 'knex.version' : { type : 'string' , value : KNEX_VERSION } ,
66+ [ DB_OPERATION_NAME ] : { type : 'string' , value : 'select' } ,
67+ 'db.sql.table' : { type : 'string' , value : 'User' } ,
68+ [ DB_SYSTEM_NAME ] : { type : 'string' , value : 'mysql2' } ,
69+ [ DB_NAMESPACE ] : { type : 'string' , value : 'tests' } ,
70+ [ DB_QUERY_TEXT ] : { type : 'string' , value : 'select * from `User`' } ,
71+ [ DB_USER ] : { type : 'string' , value : 'root' } ,
72+ [ SENTRY_ORIGIN ] : { type : 'string' , value : ORIGIN } ,
73+ [ SENTRY_OP ] : { type : 'string' , value : DB } ,
74+ [ DB_QUERY_SUMMARY ] : { type : 'string' , value : 'select `User`' } ,
6275 } ) ,
63- ] ) ,
64- } ;
65-
66- await createRunner ( ) . expect ( { transaction : EXPECTED_TRANSACTION } ) . start ( ) . completed ( ) ;
67- } ) ;
68- } ) ;
76+ status : 'ok' ,
77+ name : 'select `User`' ,
78+ } ) ,
79+ expect . objectContaining ( {
80+ attributes : expect . objectContaining ( {
81+ [ DB_QUERY_TEXT ] : { type : 'string' , value : 'drop table `User`' } ,
82+ [ DB_QUERY_SUMMARY ] : { type : 'string' , value : 'drop table `User`' } ,
83+ } ) ,
84+ name : 'drop table `User`' ,
85+ } ) ,
86+ ] ;
6987
70- createEsmAndCjsTests ( __dirname , 'scenario.mjs' , 'instrument-span-streaming.mjs' , ( createRunner , test ) => {
71- test ( 'should name spans after the query summary with span streaming' , { timeout : 60_000 } , async ( ) => {
7288 await createRunner ( )
7389 . expect ( {
7490 span : container => {
75- // The `mysql2` driver spans underneath the knex spans come from a different integration and
76- // are named after the full statement, so they are filtered out here.
77- const knexSpans = container . items . filter ( item => item . attributes [ 'sentry.origin' ] ?. value === ORIGIN ) ;
78-
79- expect (
80- knexSpans . map ( span => ( {
81- name : span . name ,
82- summary : span . attributes [ 'db.query.summary' ] ?. value ,
83- text : span . attributes [ 'db.query.text' ] ?. value ,
84- } ) ) ,
85- ) . toEqual ( [
86- {
87- name : 'create table `User`' ,
88- summary : 'create table `User`' ,
89- text : 'create table `User` (`id` int unsigned not null auto_increment primary key, `createdAt` timestamp(?) not null default CURRENT_TIMESTAMP(?), `email` text not null, `name` text not null)' ,
90- } ,
91- {
92- name : 'insert `User`' ,
93- summary : 'insert `User`' ,
94- text : 'insert into `User` (`email`, `name`) values (?, ?)' ,
95- } ,
96- { name : 'select `User`' , summary : 'select `User`' , text : 'select * from `User`' } ,
97- { name : 'drop table `User`' , summary : 'drop table `User`' , text : 'drop table `User`' } ,
98- ] ) ;
91+ expect ( container . items . find ( span => span . is_segment ) ?. name ) . toBe ( 'Test Transaction' ) ;
92+ const knexSpans = container . items . filter ( span => span . attributes [ SENTRY_ORIGIN ] ?. value === ORIGIN ) ;
93+ expect ( knexSpans ) . toEqual ( EXPECTED_SPANS ) ;
9994 } ,
10095 } )
10196 . start ( )
0 commit comments