From 5a7e89a948d0001780872fabbf51f7294efff6a9 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Fri, 11 Oct 2024 13:45:58 -0700 Subject: [PATCH 1/4] fix(instrumentation-pg): ensure db.client.operation.duration metric is recorded for Promises API usage of pg Refs: #2380 --- .../src/instrumentation.ts | 2 + .../test/pg-pool.test.ts | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+) 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' + ); + }); }); }); From 2120edf41b622b05e97f022e62a17f0add978e45 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 15 Oct 2024 09:22:01 -0700 Subject: [PATCH 2/4] use semconv constants --- .../test/pg-pool.test.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts index 83c48dae1b..11e27da8aa 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts @@ -51,9 +51,10 @@ import { SEMATTRS_DB_STATEMENT, } from '@opentelemetry/semantic-conventions'; import { + ATTR_DB_CLIENT_CONNECTION_STATE, METRIC_DB_CLIENT_CONNECTION_COUNT, METRIC_DB_CLIENT_CONNECTION_PENDING_REQUESTS, - ATTR_DB_CLIENT_CONNECTION_STATE, + METRIC_DB_CLIENT_OPERATION_DURATION, } from '@opentelemetry/semantic-conventions/incubating'; const memoryExporter = new InMemorySpanExporter(); @@ -600,15 +601,15 @@ describe('pg-pool', () => { const metrics = resourceMetrics.scopeMetrics[0].metrics; assert.strictEqual( metrics[0].descriptor.name, - 'db.client.operation.duration' + METRIC_DB_CLIENT_OPERATION_DURATION, ); assert.strictEqual( metrics[1].descriptor.name, - 'db.client.connection.count' + METRIC_DB_CLIENT_CONNECTION_COUNT, ); assert.strictEqual( metrics[2].descriptor.name, - 'db.client.connection.pending_requests' + METRIC_DB_CLIENT_CONNECTION_PENDING_REQUESTS, ); }); }); From 21bebd32fef899515ccf0b1e71ba4f3ad5e2abe8 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 15 Oct 2024 12:09:53 -0700 Subject: [PATCH 3/4] fix lint --- .../opentelemetry-instrumentation-pg/test/pg-pool.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts index 11e27da8aa..adb4fbf1b4 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts @@ -601,15 +601,15 @@ describe('pg-pool', () => { const metrics = resourceMetrics.scopeMetrics[0].metrics; assert.strictEqual( metrics[0].descriptor.name, - METRIC_DB_CLIENT_OPERATION_DURATION, + METRIC_DB_CLIENT_OPERATION_DURATION ); assert.strictEqual( metrics[1].descriptor.name, - METRIC_DB_CLIENT_CONNECTION_COUNT, + METRIC_DB_CLIENT_CONNECTION_COUNT ); assert.strictEqual( metrics[2].descriptor.name, - METRIC_DB_CLIENT_CONNECTION_PENDING_REQUESTS, + METRIC_DB_CLIENT_CONNECTION_PENDING_REQUESTS ); }); }); From fd844d650a4f28cebdb8444a4d2df457616bca98 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 15 Oct 2024 12:31:17 -0700 Subject: [PATCH 4/4] another semconv const --- .../node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts index adb4fbf1b4..2e99408bad 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts @@ -527,7 +527,7 @@ describe('pg-pool', () => { const metrics = resourceMetrics.scopeMetrics[0].metrics; assert.strictEqual( metrics[0].descriptor.name, - 'db.client.operation.duration' + METRIC_DB_CLIENT_OPERATION_DURATION ); assert.strictEqual(