Skip to content

Commit

Permalink
fix(instrumentation-pg): ensure db.client.operation.duration metric i…
Browse files Browse the repository at this point in the history
…s recorded for Promises API usage of pg

Refs: open-telemetry#2380
  • Loading branch information
trentm committed Oct 11, 2024
1 parent d11efb3 commit 5a7e89a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
// Return a pass-along promise which ends the span and then goes to user's orig resolvers
return new Promise(resolve => {
utils.handleExecutionResult(plugin.getConfig(), span, result);
recordDuration();
span.end();
resolve(result);
});
Expand All @@ -416,6 +417,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
code: SpanStatusCode.ERROR,
message: error.message,
});
recordDuration();
span.end();
reject(error);
});
Expand Down
40 changes: 40 additions & 0 deletions plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ describe('pg-pool', () => {
await client.query('SELECT NOW()');
} finally {
client.release();
await newPool.end();
}
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 0);
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
);
});
});
});

0 comments on commit 5a7e89a

Please sign in to comment.