From 97a2956f2703ebe62c16863decada93fef4574ec Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 23 Oct 2024 14:08:41 -0700 Subject: [PATCH] fix(instrumentation-pg): ensure db.client.operation.duration metric is recorded for Promises API usage of pg (#2480) Refs: #2380 --- .../src/instrumentation.ts | 2 + .../test/pg-pool.test.ts | 42 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts index 2905e783a8..856a21ab26 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts @@ -414,6 +414,7 @@ export class PgInstrumentation extends InstrumentationBase { utils.handleExecutionResult(plugin.getConfig(), span, result); + recordDuration(); span.end(); resolve(result); }); @@ -424,6 +425,7 @@ export class PgInstrumentation extends InstrumentationBase { ); const metrics = resourceMetrics.scopeMetrics[0].metrics; + assert.strictEqual( + metrics[0].descriptor.name, + METRIC_DB_CLIENT_OPERATION_DURATION + ); + assert.strictEqual( metrics[1].descriptor.name, METRIC_DB_CLIENT_CONNECTION_COUNT @@ -573,6 +579,40 @@ describe('pg-pool', () => { }); }); + it('should generate `db.client.*` metrics (Promises-style)', async (...args) => { + const client = await pool.connect(); + + try { + const ret = await client.query('SELECT NOW()'); + assert.ok(ret); + } finally { + client.release(); + } + + const { resourceMetrics, errors } = await metricReader.collect(); + assert.deepEqual( + errors, + [], + 'expected no errors from the callback during metric collection' + ); + + // We just test the expected metric *names* here. The particulars of the + // metric values are already tested in other test cases. + const metrics = resourceMetrics.scopeMetrics[0].metrics; + assert.strictEqual( + metrics[0].descriptor.name, + METRIC_DB_CLIENT_OPERATION_DURATION + ); + assert.strictEqual( + metrics[1].descriptor.name, + METRIC_DB_CLIENT_CONNECTION_COUNT + ); + assert.strictEqual( + metrics[2].descriptor.name, + METRIC_DB_CLIENT_CONNECTION_PENDING_REQUESTS + ); + }); + it('should not add duplicate event listeners to PgPool events', done => { const poolAux: pgPool = new pgPool(CONFIG); let completed = 0;