@@ -39,6 +39,7 @@ import {
3939 PostgresCallback ,
4040 PgPoolExtended ,
4141 PgPoolCallback ,
42+ EVENT_LISTENERS_SET ,
4243} from './internal-types' ;
4344import { PgInstrumentationConfig } from './types' ;
4445import * as utils from './utils' ;
@@ -435,6 +436,52 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
435436 } ;
436437 }
437438
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+
438485 private _getPoolConnectPatch ( ) {
439486 const plugin = this ;
440487 return ( originalConnect : typeof pgPoolTypes . prototype . connect ) => {
@@ -449,41 +496,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
449496 attributes : utils . getSemanticAttributesFromPool ( this . options ) ,
450497 } ) ;
451498
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 ) ;
487500
488501 if ( callback ) {
489502 const parentSpan = trace . getSpan ( context . active ( ) ) ;
0 commit comments