Skip to content

Commit

Permalink
feat(instrumentation-knex): Use newer semantic conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
deejay1 committed Feb 9, 2025
1 parent ce62d8c commit 3a70ffe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ import {
isWrapped,
} from '@opentelemetry/instrumentation';
import {
SEMATTRS_DB_NAME,
SEMATTRS_DB_OPERATION,
SEMATTRS_DB_SQL_TABLE,
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,
SEMATTRS_DB_USER,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
SEMATTRS_NET_TRANSPORT,
} from '@opentelemetry/semantic-conventions';
ATTR_DB_COLLECTION_NAME,
ATTR_DB_NAMESPACE,
ATTR_DB_OPERATION_NAME,
ATTR_DB_QUERY_TEXT,
ATTR_DB_SYSTEM,
ATTR_DB_USER,
} from './semconv';
import * as utils from './utils';
import { KnexInstrumentationConfig } from './types';
import {
ATTR_NETWORK_TRANSPORT,
ATTR_SERVER_ADDRESS,
ATTR_SERVER_PORT,
} from '@opentelemetry/semantic-conventions';

const contextSymbol = Symbol('opentelemetry.instrumentation-knex.context');
const DEFAULT_CONFIG: KnexInstrumentationConfig = {
Expand Down Expand Up @@ -134,21 +136,21 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
config?.connection?.filename || config?.connection?.database;
const { maxQueryLength } = instrumentation.getConfig();

const attributes: api.SpanAttributes = {
const attributes: api.Attributes = {
'knex.version': moduleVersion,
[SEMATTRS_DB_SYSTEM]: utils.mapSystem(config.client),
[SEMATTRS_DB_SQL_TABLE]: table,
[SEMATTRS_DB_OPERATION]: operation,
[SEMATTRS_DB_USER]: config?.connection?.user,
[SEMATTRS_DB_NAME]: name,
[SEMATTRS_NET_PEER_NAME]: config?.connection?.host,
[SEMATTRS_NET_PEER_PORT]: config?.connection?.port,
[SEMATTRS_NET_TRANSPORT]:
[ATTR_DB_SYSTEM]: utils.mapSystem(config.client),
[ATTR_DB_COLLECTION_NAME]: table,
[ATTR_DB_OPERATION_NAME]: operation,
[ATTR_DB_USER]: config?.connection?.user,
[ATTR_DB_NAMESPACE]: name,
[ATTR_SERVER_ADDRESS]: config?.connection?.host,
[ATTR_SERVER_PORT]: config?.connection?.port,
[ATTR_NETWORK_TRANSPORT]:
config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
};
if (maxQueryLength) {
// filters both undefined and 0
attributes[SEMATTRS_DB_STATEMENT] = utils.limitLength(
attributes[ATTR_DB_QUERY_TEXT] = utils.limitLength(
query?.sql,
maxQueryLength
);
Expand Down
22 changes: 22 additions & 0 deletions plugins/node/opentelemetry-instrumentation-knex/src/semconv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export const ATTR_DB_NAMESPACE = 'db.namespace';
export const ATTR_DB_OPERATION_NAME = 'db.operation.name';
export const ATTR_DB_SYSTEM = 'db.system';
export const ATTR_DB_COLLECTION_NAME = 'db.collection.name';
export const ATTR_DB_USER = 'db.user';
export const ATTR_DB_QUERY_TEXT = 'db.query.text';
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('Knex instrumentation', () => {
await client.raw(statement);

const [span] = memoryExporter.getFinishedSpans();
const limitedStatement = span?.attributes?.['db.statement'] as string;
const limitedStatement = span?.attributes?.['db.query.text'] as string;
assert.strictEqual(limitedStatement.length, 52);
assert.ok(statement.startsWith(limitedStatement.substring(0, 50)));
});
Expand Down Expand Up @@ -614,15 +614,15 @@ const assertSpans = (
span.attributes['db.system'],
customAssertOptions.dbSystem
);
assert.strictEqual(span.attributes['db.name'], ':memory:');
assert.strictEqual(span.attributes['db.sql.table'], expected.table);
assert.strictEqual(span.attributes['db.statement'], expected.statement);
assert.strictEqual(span.attributes['db.namespace'], ':memory:');
assert.strictEqual(span.attributes['db.collection.name'], expected.table);
assert.strictEqual(span.attributes['db.query.text'], expected.statement);
assert.strictEqual(
typeof span.attributes['knex.version'],
'string',
'knex.version not specified'
);
assert.strictEqual(span.attributes['db.operation'], expected.op);
assert.strictEqual(span.attributes['db.operation.name'], expected.op);
assert.strictEqual(
span.parentSpanId,
expected.parentSpan?.spanContext().spanId
Expand Down

0 comments on commit 3a70ffe

Please sign in to comment.