@@ -6,9 +6,18 @@ import type { LazyLoadedIntegration } from './lazy';
66import { shouldDisableAutoInstrumentation } from './utils/node-utils' ;
77
88interface MysqlConnection {
9+ prototype : {
10+ connect : ( ) => void ;
11+ } ;
912 createQuery : ( ) => void ;
1013}
1114
15+ interface MysqlConnectionConfig {
16+ host : string ;
17+ port : number ;
18+ user : string ;
19+ }
20+
1221/** Tracing integration for node-mysql package */
1322export class Mysql implements LazyLoadedIntegration < MysqlConnection > {
1423 /**
@@ -48,6 +57,32 @@ export class Mysql implements LazyLoadedIntegration<MysqlConnection> {
4857 return ;
4958 }
5059
60+ let mySqlConfig : MysqlConnectionConfig | undefined = undefined ;
61+
62+ try {
63+ pkg . prototype . connect = new Proxy ( pkg . prototype . connect , {
64+ apply ( wrappingTarget , thisArg : { config : MysqlConnectionConfig } , args ) {
65+ if ( ! mySqlConfig ) {
66+ mySqlConfig = thisArg . config ;
67+ }
68+ return wrappingTarget . apply ( thisArg , args ) ;
69+ } ,
70+ } ) ;
71+ } catch ( e ) {
72+ __DEBUG_BUILD__ && logger . error ( 'Mysql Integration was unable to instrument `mysql` config.' ) ;
73+ }
74+
75+ function spanDataFromConfig ( ) : Record < string , unknown > {
76+ if ( ! mySqlConfig ) {
77+ return { } ;
78+ }
79+ return {
80+ 'server.address' : mySqlConfig . host ,
81+ 'server.port' : mySqlConfig . port ,
82+ 'db.user' : mySqlConfig . user ,
83+ } ;
84+ }
85+
5186 // The original function will have one of these signatures:
5287 // function (callback) => void
5388 // function (options, callback) => void
@@ -60,6 +95,7 @@ export class Mysql implements LazyLoadedIntegration<MysqlConnection> {
6095 description : typeof options === 'string' ? options : ( options as { sql : string } ) . sql ,
6196 op : 'db' ,
6297 data : {
98+ ...spanDataFromConfig ( ) ,
6399 'db.system' : 'mysql' ,
64100 } ,
65101 } ) ;
0 commit comments