Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instrumentation-pg): add error type to db duration metric #2476

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
instrumentationConfig,
span,
args[args.length - 1] as PostgresCallback, // nb: not type safe.
attributes,
recordDuration
);

Expand All @@ -341,6 +342,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();
});
});
});
});