Skip to content

Commit d79075a

Browse files
committed
Merge branch 'main' of github.com:open-telemetry/opentelemetry-js-contrib into db-error
2 parents ca67d83 + 33c093d commit d79075a

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';
@@ -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());

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
@@ -289,12 +289,12 @@ export interface poolConnectionsCounter {
289289
}
290290

291291
export function updateCounter(
292+
poolName: string,
292293
pool: PgPoolExtended,
293294
connectionCount: UpDownCounter,
294295
connectionPendingRequests: UpDownCounter,
295296
latestCounter: poolConnectionsCounter
296297
): poolConnectionsCounter {
297-
const poolName = getPoolName(pool.options);
298298
const all = pool.totalCount;
299299
const pending = pool.waitingCount;
300300
const idle = pool.idleCount;

0 commit comments

Comments
 (0)