Skip to content

Commit

Permalink
fix(instrumentation-pg): not add duplicate listeners to pg pool (#2484)
Browse files Browse the repository at this point in the history
  • Loading branch information
maryliag authored Oct 16, 2024
1 parent b043ffb commit 33c093d
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
PostgresCallback,
PgPoolExtended,
PgPoolCallback,
EVENT_LISTENERS_SET,
} from './internal-types';
import { PgInstrumentationConfig } from './types';
import * as utils from './utils';
Expand Down Expand Up @@ -435,6 +436,52 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
};
}

private _setPoolConnectEventListeners(pgPool: PgPoolExtended) {
if (pgPool[EVENT_LISTENERS_SET]) return;
const poolName = utils.getPoolName(pgPool.options);

pgPool.on('connect', () => {
this._connectionsCounter = utils.updateCounter(
poolName,
pgPool,
this._connectionsCount,
this._connectionPendingRequests,
this._connectionsCounter
);
});

pgPool.on('acquire', () => {
this._connectionsCounter = utils.updateCounter(
poolName,
pgPool,
this._connectionsCount,
this._connectionPendingRequests,
this._connectionsCounter
);
});

pgPool.on('remove', () => {
this._connectionsCounter = utils.updateCounter(
poolName,
pgPool,
this._connectionsCount,
this._connectionPendingRequests,
this._connectionsCounter
);
});

pgPool.on('release' as any, () => {
this._connectionsCounter = utils.updateCounter(
poolName,
pgPool,
this._connectionsCount,
this._connectionPendingRequests,
this._connectionsCounter
);
});
pgPool[EVENT_LISTENERS_SET] = true;
}

private _getPoolConnectPatch() {
const plugin = this;
return (originalConnect: typeof pgPoolTypes.prototype.connect) => {
Expand All @@ -449,41 +496,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
attributes: utils.getSemanticAttributesFromPool(this.options),
});

this.on('connect', () => {
plugin._connectionsCounter = utils.updateCounter(
this,
plugin._connectionsCount,
plugin._connectionPendingRequests,
plugin._connectionsCounter
);
});

this.on('acquire', () => {
plugin._connectionsCounter = utils.updateCounter(
this,
plugin._connectionsCount,
plugin._connectionPendingRequests,
plugin._connectionsCounter
);
});

this.on('remove', () => {
plugin._connectionsCounter = utils.updateCounter(
this,
plugin._connectionsCount,
plugin._connectionPendingRequests,
plugin._connectionsCounter
);
});

this.on('release' as any, () => {
plugin._connectionsCounter = utils.updateCounter(
this,
plugin._connectionsCount,
plugin._connectionPendingRequests,
plugin._connectionsCounter
);
});
plugin._setPoolConnectEventListeners(this);

if (callback) {
const parentSpan = trace.getSpan(context.active());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ export interface PgPoolOptionsParams {
maxClient: number; // maximum size of the pool
}

export const EVENT_LISTENERS_SET = Symbol(
'opentelemetry.instrumentation.pg.eventListenersSet'
);

export interface PgPoolExtended extends pgPoolTypes<pgTypes.Client> {
options: PgPoolOptionsParams;
[EVENT_LISTENERS_SET]?: boolean; // flag to identify if the event listeners for instrumentation have been set
}

export type PgClientConnect = (callback?: Function) => Promise<void> | void;
2 changes: 1 addition & 1 deletion plugins/node/opentelemetry-instrumentation-pg/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ export interface poolConnectionsCounter {
}

export function updateCounter(
poolName: string,
pool: PgPoolExtended,
connectionCount: UpDownCounter,
connectionPendingRequests: UpDownCounter,
latestCounter: poolConnectionsCounter
): poolConnectionsCounter {
const poolName = getPoolName(pool.options);
const all = pool.totalCount;
const pending = pool.waitingCount;
const idle = pool.idleCount;
Expand Down
Loading

0 comments on commit 33c093d

Please sign in to comment.