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 operation name attribute to db duration metric #2475

Merged
merged 4 commits into from
Oct 15, 2024
Merged
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 @@ -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
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
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