From 9564380782164c8d8da6cac675d76d2fb4cdf20b Mon Sep 17 00:00:00 2001 From: Marylia Gutierrez Date: Fri, 25 Oct 2024 13:17:44 +0200 Subject: [PATCH] feat(instrumentation-pg): add error type to db duration metric (#2476) Co-authored-by: Marc Pichler --- .../src/instrumentation.ts | 2 + .../src/utils.ts | 7 +++ .../test/pg.test.ts | 47 +++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts index b123214fa5..9b8175038c 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts @@ -326,6 +326,7 @@ export class PgInstrumentation extends InstrumentationBase { 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).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(); + }); + }); }); });