@@ -39,6 +39,7 @@ import {
39
39
PostgresCallback ,
40
40
PgPoolExtended ,
41
41
PgPoolCallback ,
42
+ EVENT_LISTENERS_SET ,
42
43
} from './internal-types' ;
43
44
import { PgInstrumentationConfig } from './types' ;
44
45
import * as utils from './utils' ;
@@ -435,6 +436,52 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
435
436
} ;
436
437
}
437
438
439
+ private _setPoolConnectEventListeners ( pgPool : PgPoolExtended ) {
440
+ if ( pgPool [ EVENT_LISTENERS_SET ] ) return ;
441
+ const poolName = utils . getPoolName ( pgPool . options ) ;
442
+
443
+ pgPool . on ( 'connect' , ( ) => {
444
+ this . _connectionsCounter = utils . updateCounter (
445
+ poolName ,
446
+ pgPool ,
447
+ this . _connectionsCount ,
448
+ this . _connectionPendingRequests ,
449
+ this . _connectionsCounter
450
+ ) ;
451
+ } ) ;
452
+
453
+ pgPool . on ( 'acquire' , ( ) => {
454
+ this . _connectionsCounter = utils . updateCounter (
455
+ poolName ,
456
+ pgPool ,
457
+ this . _connectionsCount ,
458
+ this . _connectionPendingRequests ,
459
+ this . _connectionsCounter
460
+ ) ;
461
+ } ) ;
462
+
463
+ pgPool . on ( 'remove' , ( ) => {
464
+ this . _connectionsCounter = utils . updateCounter (
465
+ poolName ,
466
+ pgPool ,
467
+ this . _connectionsCount ,
468
+ this . _connectionPendingRequests ,
469
+ this . _connectionsCounter
470
+ ) ;
471
+ } ) ;
472
+
473
+ pgPool . on ( 'release' as any , ( ) => {
474
+ this . _connectionsCounter = utils . updateCounter (
475
+ poolName ,
476
+ pgPool ,
477
+ this . _connectionsCount ,
478
+ this . _connectionPendingRequests ,
479
+ this . _connectionsCounter
480
+ ) ;
481
+ } ) ;
482
+ pgPool [ EVENT_LISTENERS_SET ] = true ;
483
+ }
484
+
438
485
private _getPoolConnectPatch ( ) {
439
486
const plugin = this ;
440
487
return ( originalConnect : typeof pgPoolTypes . prototype . connect ) => {
@@ -449,41 +496,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
449
496
attributes : utils . getSemanticAttributesFromPool ( this . options ) ,
450
497
} ) ;
451
498
452
- this . on ( 'connect' , ( ) => {
453
- plugin . _connectionsCounter = utils . updateCounter (
454
- this ,
455
- plugin . _connectionsCount ,
456
- plugin . _connectionPendingRequests ,
457
- plugin . _connectionsCounter
458
- ) ;
459
- } ) ;
460
-
461
- this . on ( 'acquire' , ( ) => {
462
- plugin . _connectionsCounter = utils . updateCounter (
463
- this ,
464
- plugin . _connectionsCount ,
465
- plugin . _connectionPendingRequests ,
466
- plugin . _connectionsCounter
467
- ) ;
468
- } ) ;
469
-
470
- this . on ( 'remove' , ( ) => {
471
- plugin . _connectionsCounter = utils . updateCounter (
472
- this ,
473
- plugin . _connectionsCount ,
474
- plugin . _connectionPendingRequests ,
475
- plugin . _connectionsCounter
476
- ) ;
477
- } ) ;
478
-
479
- this . on ( 'release' as any , ( ) => {
480
- plugin . _connectionsCounter = utils . updateCounter (
481
- this ,
482
- plugin . _connectionsCount ,
483
- plugin . _connectionPendingRequests ,
484
- plugin . _connectionsCounter
485
- ) ;
486
- } ) ;
499
+ plugin . _setPoolConnectEventListeners ( this ) ;
487
500
488
501
if ( callback ) {
489
502
const parentSpan = trace . getSpan ( context . active ( ) ) ;
0 commit comments