Skip to content

Commit

Permalink
feat(instrumentation-pg): add operation name attribute to db duration…
Browse files Browse the repository at this point in the history
… metric (#2475)
  • Loading branch information
maryliag authored Oct 15, 2024
1 parent a558044 commit b043ffb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
METRIC_DB_CLIENT_CONNECTION_PENDING_REQUESTS,
METRIC_DB_CLIENT_OPERATION_DURATION,
ATTR_DB_NAMESPACE,
ATTR_DB_OPERATION_NAME,
} from '@opentelemetry/semantic-conventions/incubating';

export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConfig> {
Expand Down Expand Up @@ -228,6 +229,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
ATTR_ERROR_TYPE,
ATTR_SERVER_PORT,
ATTR_SERVER_ADDRESS,
ATTR_DB_OPERATION_NAME,
];

keysToCopy.forEach(key => {
Expand Down Expand Up @@ -282,6 +284,11 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
[ATTR_SERVER_ADDRESS]: this.connectionParameters.host,
};

if (queryConfig?.text) {
attributes[ATTR_DB_OPERATION_NAME] =
utils.parseNormalizedOperationName(queryConfig?.text);
}

const recordDuration = () => {
plugin.recordOperationDuration(attributes, startTime);
};
Expand Down
2 changes: 1 addition & 1 deletion plugins/node/opentelemetry-instrumentation-pg/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function getQuerySpanName(
return `${SpanNames.QUERY_PREFIX}:${command}${dbName ? ` ${dbName}` : ''}`;
}

function parseNormalizedOperationName(queryText: string) {
export function parseNormalizedOperationName(queryText: string) {
const indexOfFirstSpace = queryText.indexOf(' ');
let sqlCommand =
indexOfFirstSpace === -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ import {
SEMATTRS_DB_USER,
SEMATTRS_DB_STATEMENT,
} from '@opentelemetry/semantic-conventions';
import { ATTR_DB_CLIENT_CONNECTION_STATE } from '@opentelemetry/semantic-conventions/incubating';
import {
METRIC_DB_CLIENT_CONNECTION_COUNT,
METRIC_DB_CLIENT_CONNECTION_PENDING_REQUESTS,
ATTR_DB_CLIENT_CONNECTION_STATE,
} from '@opentelemetry/semantic-conventions/incubating';

const memoryExporter = new InMemorySpanExporter();

Expand Down Expand Up @@ -522,7 +526,7 @@ describe('pg-pool', () => {
const metrics = resourceMetrics.scopeMetrics[0].metrics;
assert.strictEqual(
metrics[1].descriptor.name,
'db.client.connection.count'
METRIC_DB_CLIENT_CONNECTION_COUNT
);
assert.strictEqual(
metrics[1].descriptor.description,
Expand Down Expand Up @@ -553,7 +557,7 @@ describe('pg-pool', () => {

assert.strictEqual(
metrics[2].descriptor.name,
'db.client.connection.pending_requests'
METRIC_DB_CLIENT_CONNECTION_PENDING_REQUESTS
);
assert.strictEqual(
metrics[2].descriptor.description,
Expand Down
10 changes: 9 additions & 1 deletion plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ import {
DBSYSTEMVALUES_POSTGRESQL,
ATTR_ERROR_TYPE,
} from '@opentelemetry/semantic-conventions';
import {
METRIC_DB_CLIENT_OPERATION_DURATION,
ATTR_DB_OPERATION_NAME,
} from '@opentelemetry/semantic-conventions/incubating';
import { addSqlCommenterComment } from '@opentelemetry/sql-common';

const memoryExporter = new InMemorySpanExporter();
Expand Down Expand Up @@ -984,7 +988,7 @@ describe('pg', () => {
const metrics = resourceMetrics.scopeMetrics[0].metrics;
assert.strictEqual(
metrics[0].descriptor.name,
'db.client.operation.duration'
METRIC_DB_CLIENT_OPERATION_DURATION
);
assert.strictEqual(
metrics[0].descriptor.description,
Expand All @@ -995,6 +999,10 @@ describe('pg', () => {
dataPoint.attributes[SEMATTRS_DB_SYSTEM],
DBSYSTEMVALUES_POSTGRESQL
);
assert.strictEqual(
dataPoint.attributes[ATTR_DB_OPERATION_NAME],
'SELECT'
);
assert.strictEqual(dataPoint.attributes[ATTR_ERROR_TYPE], undefined);

const v = (dataPoint as DataPoint<Histogram>).value;
Expand Down

0 comments on commit b043ffb

Please sign in to comment.