Skip to content

Commit e74cee4

Browse files
david-lunatrentm
andauthored
refactor(instr-cassandra-driver): use exported strings for attributes (#2139)
Also, correct component-label mapping for instrumentation-cassandra-driver package Co-authored-by: Trent Mick <[email protected]> Refs: #2025
1 parent 585b833 commit e74cee4

File tree

6 files changed

+50
-25
lines changed

6 files changed

+50
-25
lines changed

.github/component-label-map.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pkg:instrumentation-bunyan:
7171
- changed-files:
7272
- any-glob-to-any-file:
7373
- plugins/node/opentelemetry-instrumentation-bunyan/**
74-
pkg:instrumentation-cassandra:
74+
pkg:instrumentation-cassandra-driver:
7575
- changed-files:
7676
- any-glob-to-any-file:
7777
- plugins/node/opentelemetry-instrumentation-cassandra/**

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/node/opentelemetry-instrumentation-cassandra/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ await client.execute('select * from foo');
4949

5050
`>=4.4 <5.0`
5151

52+
## Semantic Conventions
53+
54+
This package uses `@opentelemetry/semantic-conventions` version `1.22+`, which implements Semantic Convention [Version 1.7.0](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/semantic_conventions/README.md)
55+
56+
Attributes collected:
57+
58+
| Attribute | Short Description |
59+
| ----------------------- | ------------------------------------------------------------------------------ |
60+
| `db.name` | This attribute is used to report the name of the database being accessed. |
61+
| `db.statement` | The database statement being executed. |
62+
| `db.system` | An identifier for the database management system (DBMS) product being used. |
63+
| `db.user` | Username for accessing the database. |
64+
| `net.peer.name` | Remote hostname or similar. |
65+
| `net.peer.port` | Remote port number. |
66+
5267
## Useful links
5368

5469
* For more information on OpenTelemetry, visit: <https://opentelemetry.io/>

plugins/node/opentelemetry-instrumentation-cassandra/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
},
6363
"dependencies": {
6464
"@opentelemetry/instrumentation": "^0.51.0",
65-
"@opentelemetry/semantic-conventions": "^1.0.0"
65+
"@opentelemetry/semantic-conventions": "^1.22.0"
6666
},
6767
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-cassandra#readme"
6868
}

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
context,
1919
trace,
2020
Span,
21-
SpanAttributes,
21+
Attributes,
2222
SpanKind,
2323
SpanStatusCode,
2424
} from '@opentelemetry/api';
@@ -31,8 +31,13 @@ import {
3131
} from '@opentelemetry/instrumentation';
3232
import { CassandraDriverInstrumentationConfig, ResultSet } from './types';
3333
import {
34-
SemanticAttributes,
35-
DbSystemValues,
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,
3641
} from '@opentelemetry/semantic-conventions';
3742
import { VERSION } from './version';
3843
import { EventEmitter } from 'events';
@@ -172,10 +177,10 @@ export class CassandraDriverInstrumentation extends InstrumentationBase {
172177
if (span !== undefined && conn !== undefined) {
173178
const port = parseInt(conn.port, 10);
174179

175-
span.setAttribute(SemanticAttributes.NET_PEER_NAME, conn.address);
180+
span.setAttribute(SEMATTRS_NET_PEER_NAME, conn.address);
176181

177182
if (!isNaN(port)) {
178-
span.setAttribute(SemanticAttributes.NET_PEER_PORT, port);
183+
span.setAttribute(SEMATTRS_NET_PEER_PORT, port);
179184
}
180185
}
181186

@@ -302,24 +307,24 @@ export class CassandraDriverInstrumentation extends InstrumentationBase {
302307
{ op, query }: { op: string; query?: unknown },
303308
client: CassandraDriver.Client
304309
): Span {
305-
const attributes: SpanAttributes = {
306-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.CASSANDRA,
310+
const attributes: Attributes = {
311+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA,
307312
};
308313

309314
if (this._shouldIncludeDbStatement() && query !== undefined) {
310315
const statement = truncateQuery(query, this._getMaxQueryLength());
311-
attributes[SemanticAttributes.DB_STATEMENT] = statement;
316+
attributes[SEMATTRS_DB_STATEMENT] = statement;
312317
}
313318

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

317322
if (user) {
318-
attributes[SemanticAttributes.DB_USER] = user;
323+
attributes[SEMATTRS_DB_USER] = user;
319324
}
320325

321326
if (client.keyspace) {
322-
attributes[SemanticAttributes.DB_NAME] = client.keyspace;
327+
attributes[SEMATTRS_DB_NAME] = client.keyspace;
323328
}
324329

325330
return this.tracer.startSpan(`cassandra-driver.${op}`, {

plugins/node/opentelemetry-instrumentation-cassandra/test/cassandra-driver.test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ import {
3030
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
3131
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
3232
import {
33-
SemanticAttributes,
34-
DbSystemValues,
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,
3540
} from '@opentelemetry/semantic-conventions';
3641
import * as assert from 'assert';
3742
import * as testUtils from '@opentelemetry/contrib-test-utils';
@@ -60,13 +65,13 @@ function assertSpan(
6065
customAttributes?: Attributes
6166
) {
6267
const attributes: Attributes = {
63-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.CASSANDRA,
64-
[SemanticAttributes.DB_USER]: 'cassandra',
68+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA,
69+
[SEMATTRS_DB_USER]: 'cassandra',
6570
...customAttributes,
6671
};
6772

6873
if (query !== undefined) {
69-
attributes[SemanticAttributes.DB_STATEMENT] = query;
74+
attributes[SEMATTRS_DB_STATEMENT] = query;
7075
}
7176

7277
const spanStatus =
@@ -98,22 +103,22 @@ function assertErrorSpan(
98103
const [span] = spans;
99104

100105
const attributes: Attributes = {
101-
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.CASSANDRA,
102-
[SemanticAttributes.DB_USER]: 'cassandra',
106+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA,
107+
[SEMATTRS_DB_USER]: 'cassandra',
103108
};
104109

105110
if (query !== undefined) {
106-
attributes[SemanticAttributes.DB_STATEMENT] = query;
111+
attributes[SEMATTRS_DB_STATEMENT] = query;
107112
}
108113

109114
const events = [
110115
{
111116
name: 'exception',
112117
droppedAttributesCount: 0,
113118
attributes: {
114-
[SemanticAttributes.EXCEPTION_STACKTRACE]: error.stack,
115-
[SemanticAttributes.EXCEPTION_MESSAGE]: error.message,
116-
[SemanticAttributes.EXCEPTION_TYPE]: String(error.code),
119+
[SEMATTRS_EXCEPTION_STACKTRACE]: error.stack,
120+
[SEMATTRS_EXCEPTION_MESSAGE]: error.message,
121+
[SEMATTRS_EXCEPTION_TYPE]: String(error.code),
117122
},
118123
time: span.events[0].time,
119124
},

0 commit comments

Comments
 (0)