@@ -6,9 +6,18 @@ import type { LazyLoadedIntegration } from './lazy';
6
6
import { shouldDisableAutoInstrumentation } from './utils/node-utils' ;
7
7
8
8
interface MysqlConnection {
9
+ prototype : {
10
+ connect : ( ) => void ;
11
+ } ;
9
12
createQuery : ( ) => void ;
10
13
}
11
14
15
+ interface MysqlConnectionConfig {
16
+ host : string ;
17
+ port : number ;
18
+ user : string ;
19
+ }
20
+
12
21
/** Tracing integration for node-mysql package */
13
22
export class Mysql implements LazyLoadedIntegration < MysqlConnection > {
14
23
/**
@@ -48,6 +57,32 @@ export class Mysql implements LazyLoadedIntegration<MysqlConnection> {
48
57
return ;
49
58
}
50
59
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
+
51
86
// The original function will have one of these signatures:
52
87
// function (callback) => void
53
88
// function (options, callback) => void
@@ -60,6 +95,7 @@ export class Mysql implements LazyLoadedIntegration<MysqlConnection> {
60
95
description : typeof options === 'string' ? options : ( options as { sql : string } ) . sql ,
61
96
op : 'db' ,
62
97
data : {
98
+ ...spanDataFromConfig ( ) ,
63
99
'db.system' : 'mysql' ,
64
100
} ,
65
101
} ) ;
0 commit comments