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(); + }); + }); }); });