From e74cee49a7f8dde2e34a5a6e37d87e3b7d1bd949 Mon Sep 17 00:00:00 2001 From: David Luna Date: Wed, 1 May 2024 05:30:19 +0200 Subject: [PATCH] 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 Refs: #2025 --- .github/component-label-map.yml | 2 +- package-lock.json | 4 +-- .../README.md | 15 +++++++++++ .../package.json | 2 +- .../src/instrumentation.ts | 25 ++++++++++------- .../test/cassandra-driver.test.ts | 27 +++++++++++-------- 6 files changed, 50 insertions(+), 25 deletions(-) diff --git a/.github/component-label-map.yml b/.github/component-label-map.yml index 5a9fc3af56..c766a864d0 100644 --- a/.github/component-label-map.yml +++ b/.github/component-label-map.yml @@ -71,7 +71,7 @@ pkg:instrumentation-bunyan: - changed-files: - any-glob-to-any-file: - plugins/node/opentelemetry-instrumentation-bunyan/** -pkg:instrumentation-cassandra: +pkg:instrumentation-cassandra-driver: - changed-files: - any-glob-to-any-file: - plugins/node/opentelemetry-instrumentation-cassandra/** diff --git a/package-lock.json b/package-lock.json index 4c8e6c2a3b..72cbe122ef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38193,7 +38193,7 @@ "license": "Apache-2.0", "dependencies": { "@opentelemetry/instrumentation": "^0.51.0", - "@opentelemetry/semantic-conventions": "^1.0.0" + "@opentelemetry/semantic-conventions": "^1.22.0" }, "devDependencies": { "@opentelemetry/api": "^1.3.0", @@ -47132,7 +47132,7 @@ "@opentelemetry/instrumentation": "^0.51.0", "@opentelemetry/sdk-trace-base": "^1.8.0", "@opentelemetry/sdk-trace-node": "^1.8.0", - "@opentelemetry/semantic-conventions": "^1.0.0", + "@opentelemetry/semantic-conventions": "^1.22.0", "@types/mocha": "7.0.2", "@types/node": "18.6.5", "@types/semver": "7.5.3", diff --git a/plugins/node/opentelemetry-instrumentation-cassandra/README.md b/plugins/node/opentelemetry-instrumentation-cassandra/README.md index 637bcf4fa2..8743ebffaf 100644 --- a/plugins/node/opentelemetry-instrumentation-cassandra/README.md +++ b/plugins/node/opentelemetry-instrumentation-cassandra/README.md @@ -49,6 +49,21 @@ await client.execute('select * from foo'); `>=4.4 <5.0` +## Semantic Conventions + +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) + +Attributes collected: + +| Attribute | Short Description | +| ----------------------- | ------------------------------------------------------------------------------ | +| `db.name` | This attribute is used to report the name of the database being accessed. | +| `db.statement` | The database statement being executed. | +| `db.system` | An identifier for the database management system (DBMS) product being used. | +| `db.user` | Username for accessing the database. | +| `net.peer.name` | Remote hostname or similar. | +| `net.peer.port` | Remote port number. | + ## Useful links * For more information on OpenTelemetry, visit: diff --git a/plugins/node/opentelemetry-instrumentation-cassandra/package.json b/plugins/node/opentelemetry-instrumentation-cassandra/package.json index 2529936322..73ffc747d6 100644 --- a/plugins/node/opentelemetry-instrumentation-cassandra/package.json +++ b/plugins/node/opentelemetry-instrumentation-cassandra/package.json @@ -62,7 +62,7 @@ }, "dependencies": { "@opentelemetry/instrumentation": "^0.51.0", - "@opentelemetry/semantic-conventions": "^1.0.0" + "@opentelemetry/semantic-conventions": "^1.22.0" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-cassandra#readme" } diff --git a/plugins/node/opentelemetry-instrumentation-cassandra/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-cassandra/src/instrumentation.ts index 754277310f..f11ffab83e 100644 --- a/plugins/node/opentelemetry-instrumentation-cassandra/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-cassandra/src/instrumentation.ts @@ -18,7 +18,7 @@ import { context, trace, Span, - SpanAttributes, + Attributes, SpanKind, SpanStatusCode, } from '@opentelemetry/api'; @@ -31,8 +31,13 @@ import { } from '@opentelemetry/instrumentation'; import { CassandraDriverInstrumentationConfig, ResultSet } from './types'; import { - SemanticAttributes, - DbSystemValues, + DBSYSTEMVALUES_CASSANDRA, + SEMATTRS_DB_NAME, + SEMATTRS_DB_STATEMENT, + SEMATTRS_DB_SYSTEM, + SEMATTRS_DB_USER, + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, } from '@opentelemetry/semantic-conventions'; import { VERSION } from './version'; import { EventEmitter } from 'events'; @@ -172,10 +177,10 @@ export class CassandraDriverInstrumentation extends InstrumentationBase { if (span !== undefined && conn !== undefined) { const port = parseInt(conn.port, 10); - span.setAttribute(SemanticAttributes.NET_PEER_NAME, conn.address); + span.setAttribute(SEMATTRS_NET_PEER_NAME, conn.address); if (!isNaN(port)) { - span.setAttribute(SemanticAttributes.NET_PEER_PORT, port); + span.setAttribute(SEMATTRS_NET_PEER_PORT, port); } } @@ -302,24 +307,24 @@ export class CassandraDriverInstrumentation extends InstrumentationBase { { op, query }: { op: string; query?: unknown }, client: CassandraDriver.Client ): Span { - const attributes: SpanAttributes = { - [SemanticAttributes.DB_SYSTEM]: DbSystemValues.CASSANDRA, + const attributes: Attributes = { + [SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA, }; if (this._shouldIncludeDbStatement() && query !== undefined) { const statement = truncateQuery(query, this._getMaxQueryLength()); - attributes[SemanticAttributes.DB_STATEMENT] = statement; + attributes[SEMATTRS_DB_STATEMENT] = statement; } // eslint-disable-next-line @typescript-eslint/no-explicit-any const user = (client as any).options?.credentials?.username; if (user) { - attributes[SemanticAttributes.DB_USER] = user; + attributes[SEMATTRS_DB_USER] = user; } if (client.keyspace) { - attributes[SemanticAttributes.DB_NAME] = client.keyspace; + attributes[SEMATTRS_DB_NAME] = client.keyspace; } return this.tracer.startSpan(`cassandra-driver.${op}`, { diff --git a/plugins/node/opentelemetry-instrumentation-cassandra/test/cassandra-driver.test.ts b/plugins/node/opentelemetry-instrumentation-cassandra/test/cassandra-driver.test.ts index c7a802d4b8..c663ab0abe 100644 --- a/plugins/node/opentelemetry-instrumentation-cassandra/test/cassandra-driver.test.ts +++ b/plugins/node/opentelemetry-instrumentation-cassandra/test/cassandra-driver.test.ts @@ -30,8 +30,13 @@ import { import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { - SemanticAttributes, - DbSystemValues, + DBSYSTEMVALUES_CASSANDRA, + SEMATTRS_DB_STATEMENT, + SEMATTRS_DB_SYSTEM, + SEMATTRS_DB_USER, + SEMATTRS_EXCEPTION_MESSAGE, + SEMATTRS_EXCEPTION_STACKTRACE, + SEMATTRS_EXCEPTION_TYPE, } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; import * as testUtils from '@opentelemetry/contrib-test-utils'; @@ -60,13 +65,13 @@ function assertSpan( customAttributes?: Attributes ) { const attributes: Attributes = { - [SemanticAttributes.DB_SYSTEM]: DbSystemValues.CASSANDRA, - [SemanticAttributes.DB_USER]: 'cassandra', + [SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA, + [SEMATTRS_DB_USER]: 'cassandra', ...customAttributes, }; if (query !== undefined) { - attributes[SemanticAttributes.DB_STATEMENT] = query; + attributes[SEMATTRS_DB_STATEMENT] = query; } const spanStatus = @@ -98,12 +103,12 @@ function assertErrorSpan( const [span] = spans; const attributes: Attributes = { - [SemanticAttributes.DB_SYSTEM]: DbSystemValues.CASSANDRA, - [SemanticAttributes.DB_USER]: 'cassandra', + [SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_CASSANDRA, + [SEMATTRS_DB_USER]: 'cassandra', }; if (query !== undefined) { - attributes[SemanticAttributes.DB_STATEMENT] = query; + attributes[SEMATTRS_DB_STATEMENT] = query; } const events = [ @@ -111,9 +116,9 @@ function assertErrorSpan( name: 'exception', droppedAttributesCount: 0, attributes: { - [SemanticAttributes.EXCEPTION_STACKTRACE]: error.stack, - [SemanticAttributes.EXCEPTION_MESSAGE]: error.message, - [SemanticAttributes.EXCEPTION_TYPE]: String(error.code), + [SEMATTRS_EXCEPTION_STACKTRACE]: error.stack, + [SEMATTRS_EXCEPTION_MESSAGE]: error.message, + [SEMATTRS_EXCEPTION_TYPE]: String(error.code), }, time: span.events[0].time, },