Skip to content

Commit 8e2f6b4

Browse files
authored
chore(instrumentation-cassandra-driver): update semconv usage to ATTR_ exports (#3058)
Refs: #2377
1 parent 8198baa commit 8e2f6b4

File tree

4 files changed

+151
-52
lines changed

4 files changed

+151
-52
lines changed

packages/instrumentation-cassandra-driver/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@opentelemetry/contrib-test-utils": "^0.51.0",
5858
"@opentelemetry/sdk-trace-base": "^2.0.0",
5959
"@opentelemetry/sdk-trace-node": "^2.0.0",
60+
"@opentelemetry/semantic-conventions": "^1.27.0",
6061
"@types/mocha": "10.0.10",
6162
"@types/node": "18.18.14",
6263
"@types/semver": "7.5.8",
@@ -69,8 +70,7 @@
6970
"typescript": "5.0.4"
7071
},
7172
"dependencies": {
72-
"@opentelemetry/instrumentation": "^0.205.0",
73-
"@opentelemetry/semantic-conventions": "^1.27.0"
73+
"@opentelemetry/instrumentation": "^0.205.0"
7474
},
7575
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation-cassandra-driver#readme"
7676
}

packages/instrumentation-cassandra-driver/src/instrumentation.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ import {
3131
} from '@opentelemetry/instrumentation';
3232
import { CassandraDriverInstrumentationConfig, ResultSet } from './types';
3333
import {
34-
DBSYSTEMVALUES_CASSANDRA,
35-
SEMATTRS_DB_NAME,
36-
SEMATTRS_DB_STATEMENT,
37-
SEMATTRS_DB_SYSTEM,
38-
SEMATTRS_DB_USER,
39-
SEMATTRS_NET_PEER_NAME,
40-
SEMATTRS_NET_PEER_PORT,
41-
} from '@opentelemetry/semantic-conventions';
34+
DB_SYSTEM_VALUE_CASSANDRA,
35+
ATTR_DB_NAME,
36+
ATTR_DB_STATEMENT,
37+
ATTR_DB_SYSTEM,
38+
ATTR_DB_USER,
39+
ATTR_NET_PEER_NAME,
40+
ATTR_NET_PEER_PORT,
41+
} from './semconv';
4242
/** @knipignore */
4343
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
4444
import { EventEmitter } from 'events';
@@ -174,10 +174,10 @@ export class CassandraDriverInstrumentation extends InstrumentationBase<Cassandr
174174
if (span !== undefined && conn !== undefined) {
175175
const port = parseInt(conn.port, 10);
176176

177-
span.setAttribute(SEMATTRS_NET_PEER_NAME, conn.address);
177+
span.setAttribute(ATTR_NET_PEER_NAME, conn.address);
178178

179179
if (!isNaN(port)) {
180-
span.setAttribute(SEMATTRS_NET_PEER_PORT, port);
180+
span.setAttribute(ATTR_NET_PEER_PORT, port);
181181
}
182182
}
183183

@@ -305,23 +305,23 @@ export class CassandraDriverInstrumentation extends InstrumentationBase<Cassandr
305305
client: CassandraDriver.Client
306306
): Span {
307307
const attributes: Attributes = {
308-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA,
308+
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_CASSANDRA,
309309
};
310310

311311
if (this._shouldIncludeDbStatement() && query !== undefined) {
312312
const statement = truncateQuery(query, this._getMaxQueryLength());
313-
attributes[SEMATTRS_DB_STATEMENT] = statement;
313+
attributes[ATTR_DB_STATEMENT] = statement;
314314
}
315315

316316
// eslint-disable-next-line @typescript-eslint/no-explicit-any
317317
const user = (client as any).options?.credentials?.username;
318318

319319
if (user) {
320-
attributes[SEMATTRS_DB_USER] = user;
320+
attributes[ATTR_DB_USER] = user;
321321
}
322322

323323
if (client.keyspace) {
324-
attributes[SEMATTRS_DB_NAME] = client.keyspace;
324+
attributes[ATTR_DB_NAME] = client.keyspace;
325325
}
326326

327327
return this.tracer.startSpan(`cassandra-driver.${op}`, {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
/*
18+
* This file contains a copy of unstable semantic convention definitions
19+
* used by this package.
20+
* @see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv
21+
*/
22+
23+
/**
24+
* Deprecated, use `db.namespace` instead.
25+
*
26+
* @example customers
27+
* @example main
28+
*
29+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
30+
*
31+
* @deprecated Replaced by `db.namespace`.
32+
*/
33+
export const ATTR_DB_NAME = 'db.name' as const;
34+
35+
/**
36+
* The database statement being executed.
37+
*
38+
* @example SELECT * FROM wuser_table
39+
* @example SET mykey "WuValue"
40+
*
41+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
42+
*
43+
* @deprecated Replaced by `db.query.text`.
44+
*/
45+
export const ATTR_DB_STATEMENT = 'db.statement' as const;
46+
47+
/**
48+
* Deprecated, use `db.system.name` instead.
49+
*
50+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
51+
*
52+
* @deprecated Replaced by `db.system.name`.
53+
*/
54+
export const ATTR_DB_SYSTEM = 'db.system' as const;
55+
56+
/**
57+
* Deprecated, no replacement at this time.
58+
*
59+
* @example readonly_user
60+
* @example reporting_user
61+
*
62+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
63+
*
64+
* @deprecated Removed, no replacement at this time.
65+
*/
66+
export const ATTR_DB_USER = 'db.user' as const;
67+
68+
/**
69+
* Deprecated, use `server.address` on client spans and `client.address` on server spans.
70+
*
71+
* @example example.com
72+
*
73+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
74+
*
75+
* @deprecated Replaced by `server.address` on client spans and `client.address` on server spans.
76+
*/
77+
export const ATTR_NET_PEER_NAME = 'net.peer.name' as const;
78+
79+
/**
80+
* Deprecated, use `server.port` on client spans and `client.port` on server spans.
81+
*
82+
* @example 8080
83+
*
84+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
85+
*
86+
* @deprecated Replaced by `server.port` on client spans and `client.port` on server spans.
87+
*/
88+
export const ATTR_NET_PEER_PORT = 'net.peer.port' as const;
89+
90+
/**
91+
* Enum value "cassandra" for attribute {@link ATTR_DB_SYSTEM}.
92+
*
93+
* Apache Cassandra
94+
*
95+
* @experimental This enum value is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
96+
*/
97+
export const DB_SYSTEM_VALUE_CASSANDRA = 'cassandra' as const;

packages/instrumentation-cassandra-driver/test/cassandra-driver.test.ts

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ import {
3030
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
3131
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
3232
import {
33-
DBSYSTEMVALUES_CASSANDRA,
34-
SEMATTRS_DB_STATEMENT,
35-
SEMATTRS_DB_SYSTEM,
36-
SEMATTRS_DB_USER,
37-
SEMATTRS_EXCEPTION_MESSAGE,
38-
SEMATTRS_EXCEPTION_STACKTRACE,
39-
SEMATTRS_EXCEPTION_TYPE,
40-
SEMATTRS_NET_PEER_NAME,
41-
SEMATTRS_NET_PEER_PORT,
33+
ATTR_EXCEPTION_MESSAGE,
34+
ATTR_EXCEPTION_STACKTRACE,
35+
ATTR_EXCEPTION_TYPE,
4236
} from '@opentelemetry/semantic-conventions';
37+
import {
38+
DB_SYSTEM_VALUE_CASSANDRA,
39+
ATTR_DB_STATEMENT,
40+
ATTR_DB_SYSTEM,
41+
ATTR_DB_USER,
42+
ATTR_NET_PEER_NAME,
43+
ATTR_NET_PEER_PORT,
44+
} from '../src/semconv';
4345
import * as assert from 'assert';
4446
import * as testUtils from '@opentelemetry/contrib-test-utils';
4547
import type * as CassandraDriver from 'cassandra-driver';
@@ -69,13 +71,13 @@ function assertSpan(
6971
customAttributes?: Attributes
7072
) {
7173
const attributes: Attributes = {
72-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA,
73-
[SEMATTRS_DB_USER]: 'cassandra',
74+
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_CASSANDRA,
75+
[ATTR_DB_USER]: 'cassandra',
7476
...customAttributes,
7577
};
7678

7779
if (query !== undefined) {
78-
attributes[SEMATTRS_DB_STATEMENT] = query;
80+
attributes[ATTR_DB_STATEMENT] = query;
7981
}
8082

8183
const spanStatus =
@@ -113,23 +115,23 @@ function assertErrorSpan(
113115
const [span] = spans;
114116

115117
const attributes: Attributes = {
116-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA,
117-
[SEMATTRS_DB_USER]: 'cassandra',
118+
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_CASSANDRA,
119+
[ATTR_DB_USER]: 'cassandra',
118120
...customAttributes,
119121
};
120122

121123
if (query !== undefined) {
122-
attributes[SEMATTRS_DB_STATEMENT] = query;
124+
attributes[ATTR_DB_STATEMENT] = query;
123125
}
124126

125127
const events = [
126128
{
127129
name: 'exception',
128130
droppedAttributesCount: 0,
129131
attributes: {
130-
[SEMATTRS_EXCEPTION_STACKTRACE]: error.stack,
131-
[SEMATTRS_EXCEPTION_MESSAGE]: error.message,
132-
[SEMATTRS_EXCEPTION_TYPE]: String(error.code),
132+
[ATTR_EXCEPTION_STACKTRACE]: error.stack,
133+
[ATTR_EXCEPTION_MESSAGE]: error.message,
134+
[ATTR_EXCEPTION_TYPE]: String(error.code),
133135
},
134136
time: span.events[0].time,
135137
},
@@ -193,16 +195,16 @@ describe('CassandraDriverInstrumentation', () => {
193195
it('creates a span for promise based execute', async () => {
194196
await client.execute('select * from ot.test');
195197
assertSingleSpan('cassandra-driver.execute', undefined, undefined, {
196-
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
197-
[SEMATTRS_NET_PEER_PORT]: 9042,
198+
[ATTR_NET_PEER_NAME]: cassandraContactPoint,
199+
[ATTR_NET_PEER_PORT]: 9042,
198200
});
199201
});
200202

201203
it('creates a span for callback based execute', done => {
202204
client.execute('select * from ot.test', () => {
203205
assertSingleSpan('cassandra-driver.execute', undefined, undefined, {
204-
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
205-
[SEMATTRS_NET_PEER_PORT]: 9042,
206+
[ATTR_NET_PEER_NAME]: cassandraContactPoint,
207+
[ATTR_NET_PEER_PORT]: 9042,
206208
});
207209
done();
208210
});
@@ -213,8 +215,8 @@ describe('CassandraDriverInstrumentation', () => {
213215
await client.execute('selec * from');
214216
} catch (e: any) {
215217
assertErrorSpan('cassandra-driver.execute', e, undefined, {
216-
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
217-
[SEMATTRS_NET_PEER_PORT]: 9042,
218+
[ATTR_NET_PEER_NAME]: cassandraContactPoint,
219+
[ATTR_NET_PEER_PORT]: 9042,
218220
});
219221
return;
220222
}
@@ -243,17 +245,17 @@ describe('CassandraDriverInstrumentation', () => {
243245
const query = 'select * from ot.test';
244246
await client.execute(query);
245247
assertSingleSpan('cassandra-driver.execute', query, undefined, {
246-
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
247-
[SEMATTRS_NET_PEER_PORT]: 9042,
248+
[ATTR_NET_PEER_NAME]: cassandraContactPoint,
249+
[ATTR_NET_PEER_PORT]: 9042,
248250
});
249251
});
250252

251253
it('truncates long queries', async () => {
252254
const query = 'select userid, count from ot.test';
253255
await client.execute(query);
254256
const customAttributes = {
255-
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
256-
[SEMATTRS_NET_PEER_PORT]: 9042,
257+
[ATTR_NET_PEER_NAME]: cassandraContactPoint,
258+
[ATTR_NET_PEER_PORT]: 9042,
257259
};
258260
assertSingleSpan(
259261
'cassandra-driver.execute',
@@ -299,8 +301,8 @@ describe('CassandraDriverInstrumentation', () => {
299301
assertAttributeInSingleSpan('cassandra-driver.execute', {
300302
[customAttributeName]: customAttributeValue,
301303
[responseAttributeName]: 2,
302-
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
303-
[SEMATTRS_NET_PEER_PORT]: 9042,
304+
[ATTR_NET_PEER_NAME]: cassandraContactPoint,
305+
[ATTR_NET_PEER_PORT]: 9042,
304306
});
305307
});
306308

@@ -322,8 +324,8 @@ describe('CassandraDriverInstrumentation', () => {
322324

323325
assertAttributeInSingleSpan('cassandra-driver.execute', {
324326
[hookAttributeName]: hookAttributeValue,
325-
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
326-
[SEMATTRS_NET_PEER_PORT]: 9042,
327+
[ATTR_NET_PEER_NAME]: cassandraContactPoint,
328+
[ATTR_NET_PEER_PORT]: 9042,
327329
});
328330
});
329331
});
@@ -346,8 +348,8 @@ describe('CassandraDriverInstrumentation', () => {
346348
it('creates a span for callback based batch', done => {
347349
client.batch([q1, q2], () => {
348350
assertSingleSpan('cassandra-driver.batch', undefined, undefined, {
349-
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
350-
[SEMATTRS_NET_PEER_PORT]: 9042,
351+
[ATTR_NET_PEER_NAME]: cassandraContactPoint,
352+
[ATTR_NET_PEER_PORT]: 9042,
351353
});
352354
done();
353355
});
@@ -399,8 +401,8 @@ describe('CassandraDriverInstrumentation', () => {
399401
// stream internally uses execute
400402
assert.strictEqual(spans.length, 2);
401403
assertSpan(spans[0], 'cassandra-driver.execute', undefined, undefined, {
402-
[SEMATTRS_NET_PEER_NAME]: cassandraContactPoint,
403-
[SEMATTRS_NET_PEER_PORT]: 9042,
404+
[ATTR_NET_PEER_NAME]: cassandraContactPoint,
405+
[ATTR_NET_PEER_PORT]: 9042,
404406
});
405407
assertSpan(spans[1], 'cassandra-driver.stream');
406408
}

0 commit comments

Comments
 (0)