Skip to content

Commit

Permalink
feat(instrumentation-pg): add error type to db duration metric (#2476)
Browse files Browse the repository at this point in the history
Co-authored-by: Marc Pichler <[email protected]>
  • Loading branch information
maryliag and pichlermarc authored Oct 25, 2024
1 parent 9b43ccb commit 9564380
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
instrumentationConfig,
span,
args[args.length - 1] as PostgresCallback, // nb: not type safe.
attributes,
recordDuration
);

Expand All @@ -342,6 +343,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
plugin.getConfig(),
span,
queryConfig.callback as PostgresCallback, // nb: not type safe.
attributes,
recordDuration
);

Expand Down
7 changes: 7 additions & 0 deletions plugins/node/opentelemetry-instrumentation-pg/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import {
SpanKind,
diag,
UpDownCounter,
Attributes,
} from '@opentelemetry/api';
import { AttributeNames } from './enums/AttributeNames';
import {
ATTR_ERROR_TYPE,
SEMATTRS_DB_SYSTEM,
SEMATTRS_DB_NAME,
SEMATTRS_DB_CONNECTION_STRING,
Expand Down Expand Up @@ -244,6 +246,7 @@ export function patchCallback(
instrumentationConfig: PgInstrumentationConfig,
span: Span,
cb: PostgresCallback,
attributes: Attributes,
recordDuration: { (): void }
): PostgresCallback {
return function patchedCallback(
Expand All @@ -252,6 +255,10 @@ export function patchCallback(
res: object
) {
if (err) {
if (Object.prototype.hasOwnProperty.call(err, 'code')) {
attributes[ATTR_ERROR_TYPE] = (err as any)['code'];
}

span.setStatus({
code: SpanStatusCode.ERROR,
message: err.message,
Expand Down
47 changes: 47 additions & 0 deletions plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1021,5 +1021,52 @@ describe('pg', () => {
done();
});
});

it('should generate db.client.operation.duration metric with error attribute', done => {
client.query('SELECT foo from bar', async (err, ret) => {
assert.notEqual(err, null);
const { resourceMetrics, errors } = await metricReader.collect();
assert.deepEqual(
errors,
[],
'expected no errors from the callback during metric collection'
);

const metrics = resourceMetrics.scopeMetrics[0].metrics;
assert.strictEqual(
metrics[0].descriptor.name,
METRIC_DB_CLIENT_OPERATION_DURATION
);
assert.strictEqual(
metrics[0].descriptor.description,
'Duration of database client operations.'
);
const dataPoint = metrics[0].dataPoints[0];
assert.strictEqual(
dataPoint.attributes[SEMATTRS_DB_SYSTEM],
DBSYSTEMVALUES_POSTGRESQL
);
assert.strictEqual(
dataPoint.attributes[ATTR_DB_OPERATION_NAME],
'SELECT'
);
assert.strictEqual(dataPoint.attributes[ATTR_ERROR_TYPE], '42P01');

const v = (dataPoint as DataPoint<Histogram>).value;
v.min = v.min ? v.min : 0;
v.max = v.max ? v.max : 0;
assert.equal(
v.min > 0,
true,
'expect min value for Histogram to be greater than 0'
);
assert.equal(
v.max > 0,
true,
'expect max value for Histogram to be greater than 0'
);
done();
});
});
});
});

0 comments on commit 9564380

Please sign in to comment.