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 (#2480)

Refs: #2380
  • Loading branch information
trentm authored Oct 23, 2024
1 parent 25e53d6 commit 97a2956
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,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 @@ -424,6 +425,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
code: SpanStatusCode.ERROR,
message: error.message,
});
recordDuration();
span.end();
reject(error);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -524,6 +525,11 @@ describe('pg-pool', () => {
);

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
Expand Down Expand Up @@ -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<pg.Client> = new pgPool(CONFIG);
let completed = 0;
Expand Down

0 comments on commit 97a2956

Please sign in to comment.