Skip to content

Commit

Permalink
refactor(instr-cassandra-driver): use exported strings for attributes (
Browse files Browse the repository at this point in the history
…#2139)

Also, correct component-label mapping for instrumentation-cassandra-driver package

Co-authored-by: Trent Mick <trentm@gmail.com>
Refs: #2025
  • Loading branch information
david-luna and trentm committed May 1, 2024
1 parent 585b833 commit e74cee4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/component-label-map.yml
Expand Up @@ -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/**
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions plugins/node/opentelemetry-instrumentation-cassandra/README.md
Expand Up @@ -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: <https://opentelemetry.io/>
Expand Down
Expand Up @@ -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"
}
Expand Up @@ -18,7 +18,7 @@ import {
context,
trace,
Span,
SpanAttributes,
Attributes,
SpanKind,
SpanStatusCode,
} from '@opentelemetry/api';
Expand All @@ -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';
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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}`, {
Expand Down
Expand Up @@ -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';
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -98,22 +103,22 @@ 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 = [
{
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,
},
Expand Down

0 comments on commit e74cee4

Please sign in to comment.