Skip to content

Commit 3a70ffe

Browse files
committed
feat(instrumentation-knex): Use newer semantic conventions
1 parent ce62d8c commit 3a70ffe

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@ import {
2525
isWrapped,
2626
} from '@opentelemetry/instrumentation';
2727
import {
28-
SEMATTRS_DB_NAME,
29-
SEMATTRS_DB_OPERATION,
30-
SEMATTRS_DB_SQL_TABLE,
31-
SEMATTRS_DB_STATEMENT,
32-
SEMATTRS_DB_SYSTEM,
33-
SEMATTRS_DB_USER,
34-
SEMATTRS_NET_PEER_NAME,
35-
SEMATTRS_NET_PEER_PORT,
36-
SEMATTRS_NET_TRANSPORT,
37-
} from '@opentelemetry/semantic-conventions';
28+
ATTR_DB_COLLECTION_NAME,
29+
ATTR_DB_NAMESPACE,
30+
ATTR_DB_OPERATION_NAME,
31+
ATTR_DB_QUERY_TEXT,
32+
ATTR_DB_SYSTEM,
33+
ATTR_DB_USER,
34+
} from './semconv';
3835
import * as utils from './utils';
3936
import { KnexInstrumentationConfig } from './types';
37+
import {
38+
ATTR_NETWORK_TRANSPORT,
39+
ATTR_SERVER_ADDRESS,
40+
ATTR_SERVER_PORT,
41+
} from '@opentelemetry/semantic-conventions';
4042

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

137-
const attributes: api.SpanAttributes = {
139+
const attributes: api.Attributes = {
138140
'knex.version': moduleVersion,
139-
[SEMATTRS_DB_SYSTEM]: utils.mapSystem(config.client),
140-
[SEMATTRS_DB_SQL_TABLE]: table,
141-
[SEMATTRS_DB_OPERATION]: operation,
142-
[SEMATTRS_DB_USER]: config?.connection?.user,
143-
[SEMATTRS_DB_NAME]: name,
144-
[SEMATTRS_NET_PEER_NAME]: config?.connection?.host,
145-
[SEMATTRS_NET_PEER_PORT]: config?.connection?.port,
146-
[SEMATTRS_NET_TRANSPORT]:
141+
[ATTR_DB_SYSTEM]: utils.mapSystem(config.client),
142+
[ATTR_DB_COLLECTION_NAME]: table,
143+
[ATTR_DB_OPERATION_NAME]: operation,
144+
[ATTR_DB_USER]: config?.connection?.user,
145+
[ATTR_DB_NAMESPACE]: name,
146+
[ATTR_SERVER_ADDRESS]: config?.connection?.host,
147+
[ATTR_SERVER_PORT]: config?.connection?.port,
148+
[ATTR_NETWORK_TRANSPORT]:
147149
config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
148150
};
149151
if (maxQueryLength) {
150152
// filters both undefined and 0
151-
attributes[SEMATTRS_DB_STATEMENT] = utils.limitLength(
153+
attributes[ATTR_DB_QUERY_TEXT] = utils.limitLength(
152154
query?.sql,
153155
maxQueryLength
154156
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export const ATTR_DB_NAMESPACE = 'db.namespace';
18+
export const ATTR_DB_OPERATION_NAME = 'db.operation.name';
19+
export const ATTR_DB_SYSTEM = 'db.system';
20+
export const ATTR_DB_COLLECTION_NAME = 'db.collection.name';
21+
export const ATTR_DB_USER = 'db.user';
22+
export const ATTR_DB_QUERY_TEXT = 'db.query.text';

plugins/node/opentelemetry-instrumentation-knex/test/index.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe('Knex instrumentation', () => {
149149
await client.raw(statement);
150150

151151
const [span] = memoryExporter.getFinishedSpans();
152-
const limitedStatement = span?.attributes?.['db.statement'] as string;
152+
const limitedStatement = span?.attributes?.['db.query.text'] as string;
153153
assert.strictEqual(limitedStatement.length, 52);
154154
assert.ok(statement.startsWith(limitedStatement.substring(0, 50)));
155155
});
@@ -614,15 +614,15 @@ const assertSpans = (
614614
span.attributes['db.system'],
615615
customAssertOptions.dbSystem
616616
);
617-
assert.strictEqual(span.attributes['db.name'], ':memory:');
618-
assert.strictEqual(span.attributes['db.sql.table'], expected.table);
619-
assert.strictEqual(span.attributes['db.statement'], expected.statement);
617+
assert.strictEqual(span.attributes['db.namespace'], ':memory:');
618+
assert.strictEqual(span.attributes['db.collection.name'], expected.table);
619+
assert.strictEqual(span.attributes['db.query.text'], expected.statement);
620620
assert.strictEqual(
621621
typeof span.attributes['knex.version'],
622622
'string',
623623
'knex.version not specified'
624624
);
625-
assert.strictEqual(span.attributes['db.operation'], expected.op);
625+
assert.strictEqual(span.attributes['db.operation.name'], expected.op);
626626
assert.strictEqual(
627627
span.parentSpanId,
628628
expected.parentSpan?.spanContext().spanId

0 commit comments

Comments
 (0)