Skip to content

Commit 33c093d

Browse files
authored
fix(instrumentation-pg): not add duplicate listeners to pg pool (#2484)
1 parent b043ffb commit 33c093d

File tree

4 files changed

+321
-36
lines changed

4 files changed

+321
-36
lines changed

plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
PostgresCallback,
4040
PgPoolExtended,
4141
PgPoolCallback,
42+
EVENT_LISTENERS_SET,
4243
} from './internal-types';
4344
import { PgInstrumentationConfig } from './types';
4445
import * 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());

plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ export interface PgPoolOptionsParams {
5555
maxClient: number; // maximum size of the pool
5656
}
5757

58+
export const EVENT_LISTENERS_SET = Symbol(
59+
'opentelemetry.instrumentation.pg.eventListenersSet'
60+
);
61+
5862
export interface PgPoolExtended extends pgPoolTypes<pgTypes.Client> {
5963
options: PgPoolOptionsParams;
64+
[EVENT_LISTENERS_SET]?: boolean; // flag to identify if the event listeners for instrumentation have been set
6065
}
6166

6267
export type PgClientConnect = (callback?: Function) => Promise<void> | void;

plugins/node/opentelemetry-instrumentation-pg/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@ export interface poolConnectionsCounter {
282282
}
283283

284284
export function updateCounter(
285+
poolName: string,
285286
pool: PgPoolExtended,
286287
connectionCount: UpDownCounter,
287288
connectionPendingRequests: UpDownCounter,
288289
latestCounter: poolConnectionsCounter
289290
): poolConnectionsCounter {
290-
const poolName = getPoolName(pool.options);
291291
const all = pool.totalCount;
292292
const pending = pool.waitingCount;
293293
const idle = pool.idleCount;

0 commit comments

Comments
 (0)