diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts index e7c3da7a5f..9914b67e8a 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts @@ -406,6 +406,7 @@ export class PgInstrumentation extends InstrumentationBase { utils.handleExecutionResult(plugin.getConfig(), span, result); + recordDuration(); span.end(); resolve(result); }); @@ -416,6 +417,7 @@ export class PgInstrumentation extends InstrumentationBase { await client.query('SELECT NOW()'); } finally { client.release(); + await newPool.end(); } const spans = memoryExporter.getFinishedSpans(); assert.strictEqual(spans.length, 0); @@ -519,6 +520,11 @@ describe('pg-pool', () => { ); const metrics = resourceMetrics.scopeMetrics[0].metrics; + assert.strictEqual( + metrics[0].descriptor.name, + 'db.client.operation.duration' + ); + assert.strictEqual( metrics[1].descriptor.name, 'db.client.connection.count' @@ -567,5 +573,39 @@ 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, + 'db.client.operation.duration' + ); + assert.strictEqual( + metrics[1].descriptor.name, + 'db.client.connection.count' + ); + assert.strictEqual( + metrics[2].descriptor.name, + 'db.client.connection.pending_requests' + ); + }); }); });