Skip to content

Commit 97a2956

Browse files
authored
fix(instrumentation-pg): ensure db.client.operation.duration metric is recorded for Promises API usage of pg (#2480)
Refs: #2380
1 parent 25e53d6 commit 97a2956

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
414414
// Return a pass-along promise which ends the span and then goes to user's orig resolvers
415415
return new Promise(resolve => {
416416
utils.handleExecutionResult(plugin.getConfig(), span, result);
417+
recordDuration();
417418
span.end();
418419
resolve(result);
419420
});
@@ -424,6 +425,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
424425
code: SpanStatusCode.ERROR,
425426
message: error.message,
426427
});
428+
recordDuration();
427429
span.end();
428430
reject(error);
429431
});

plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ import {
5151
SEMATTRS_DB_STATEMENT,
5252
} from '@opentelemetry/semantic-conventions';
5353
import {
54+
ATTR_DB_CLIENT_CONNECTION_STATE,
5455
METRIC_DB_CLIENT_CONNECTION_COUNT,
5556
METRIC_DB_CLIENT_CONNECTION_PENDING_REQUESTS,
56-
ATTR_DB_CLIENT_CONNECTION_STATE,
57+
METRIC_DB_CLIENT_OPERATION_DURATION,
5758
} from '@opentelemetry/semantic-conventions/incubating';
5859

5960
const memoryExporter = new InMemorySpanExporter();
@@ -524,6 +525,11 @@ describe('pg-pool', () => {
524525
);
525526

526527
const metrics = resourceMetrics.scopeMetrics[0].metrics;
528+
assert.strictEqual(
529+
metrics[0].descriptor.name,
530+
METRIC_DB_CLIENT_OPERATION_DURATION
531+
);
532+
527533
assert.strictEqual(
528534
metrics[1].descriptor.name,
529535
METRIC_DB_CLIENT_CONNECTION_COUNT
@@ -573,6 +579,40 @@ describe('pg-pool', () => {
573579
});
574580
});
575581

582+
it('should generate `db.client.*` metrics (Promises-style)', async (...args) => {
583+
const client = await pool.connect();
584+
585+
try {
586+
const ret = await client.query('SELECT NOW()');
587+
assert.ok(ret);
588+
} finally {
589+
client.release();
590+
}
591+
592+
const { resourceMetrics, errors } = await metricReader.collect();
593+
assert.deepEqual(
594+
errors,
595+
[],
596+
'expected no errors from the callback during metric collection'
597+
);
598+
599+
// We just test the expected metric *names* here. The particulars of the
600+
// metric values are already tested in other test cases.
601+
const metrics = resourceMetrics.scopeMetrics[0].metrics;
602+
assert.strictEqual(
603+
metrics[0].descriptor.name,
604+
METRIC_DB_CLIENT_OPERATION_DURATION
605+
);
606+
assert.strictEqual(
607+
metrics[1].descriptor.name,
608+
METRIC_DB_CLIENT_CONNECTION_COUNT
609+
);
610+
assert.strictEqual(
611+
metrics[2].descriptor.name,
612+
METRIC_DB_CLIENT_CONNECTION_PENDING_REQUESTS
613+
);
614+
});
615+
576616
it('should not add duplicate event listeners to PgPool events', done => {
577617
const poolAux: pgPool<pg.Client> = new pgPool(CONFIG);
578618
let completed = 0;

0 commit comments

Comments
 (0)