@@ -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' ;
@@ -437,6 +438,52 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
437438 } ;
438439 }
439440
441+ private _setPoolConnectEventListeners ( pgPool : PgPoolExtended ) {
442+ if ( pgPool [ EVENT_LISTENERS_SET ] ) return ;
443+ const poolName = utils . getPoolName ( pgPool . options ) ;
444+
445+ pgPool . on ( 'connect' , ( ) => {
446+ this . _connectionsCounter = utils . updateCounter (
447+ poolName ,
448+ pgPool ,
449+ this . _connectionsCount ,
450+ this . _connectionPendingRequests ,
451+ this . _connectionsCounter
452+ ) ;
453+ } ) ;
454+
455+ pgPool . on ( 'acquire' , ( ) => {
456+ this . _connectionsCounter = utils . updateCounter (
457+ poolName ,
458+ pgPool ,
459+ this . _connectionsCount ,
460+ this . _connectionPendingRequests ,
461+ this . _connectionsCounter
462+ ) ;
463+ } ) ;
464+
465+ pgPool . on ( 'remove' , ( ) => {
466+ this . _connectionsCounter = utils . updateCounter (
467+ poolName ,
468+ pgPool ,
469+ this . _connectionsCount ,
470+ this . _connectionPendingRequests ,
471+ this . _connectionsCounter
472+ ) ;
473+ } ) ;
474+
475+ pgPool . on ( 'release' as any , ( ) => {
476+ this . _connectionsCounter = utils . updateCounter (
477+ poolName ,
478+ pgPool ,
479+ this . _connectionsCount ,
480+ this . _connectionPendingRequests ,
481+ this . _connectionsCounter
482+ ) ;
483+ } ) ;
484+ pgPool [ EVENT_LISTENERS_SET ] = true ;
485+ }
486+
440487 private _getPoolConnectPatch ( ) {
441488 const plugin = this ;
442489 return ( originalConnect : typeof pgPoolTypes . prototype . connect ) => {
@@ -451,41 +498,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
451498 attributes : utils . getSemanticAttributesFromPool ( this . options ) ,
452499 } ) ;
453500
454- this . on ( 'connect' , ( ) => {
455- plugin . _connectionsCounter = utils . updateCounter (
456- this ,
457- plugin . _connectionsCount ,
458- plugin . _connectionPendingRequests ,
459- plugin . _connectionsCounter
460- ) ;
461- } ) ;
462-
463- this . on ( 'acquire' , ( ) => {
464- plugin . _connectionsCounter = utils . updateCounter (
465- this ,
466- plugin . _connectionsCount ,
467- plugin . _connectionPendingRequests ,
468- plugin . _connectionsCounter
469- ) ;
470- } ) ;
471-
472- this . on ( 'remove' , ( ) => {
473- plugin . _connectionsCounter = utils . updateCounter (
474- this ,
475- plugin . _connectionsCount ,
476- plugin . _connectionPendingRequests ,
477- plugin . _connectionsCounter
478- ) ;
479- } ) ;
480-
481- this . on ( 'release' as any , ( ) => {
482- plugin . _connectionsCounter = utils . updateCounter (
483- this ,
484- plugin . _connectionsCount ,
485- plugin . _connectionPendingRequests ,
486- plugin . _connectionsCounter
487- ) ;
488- } ) ;
501+ plugin . _setPoolConnectEventListeners ( this ) ;
489502
490503 if ( callback ) {
491504 const parentSpan = trace . getSpan ( context . active ( ) ) ;
0 commit comments