From b40926e9686598a991ed1ca0c4fdf034ae07bec6 Mon Sep 17 00:00:00 2001 From: David Luna Date: Mon, 29 Apr 2024 07:55:19 +0200 Subject: [PATCH] refactor(instr-tedious): use exported strings for attributes (#2144) Co-authored-by: Amir Blum --- package-lock.json | 4 +- .../node/instrumentation-tedious/README.md | 16 ++++++++ .../node/instrumentation-tedious/package.json | 2 +- .../src/instrumentation.ts | 24 ++++++----- .../test/instrumentation.test.ts | 40 ++++++++----------- 5 files changed, 51 insertions(+), 35 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4663055e9d..3ec369f264 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37361,7 +37361,7 @@ "license": "Apache-2.0", "dependencies": { "@opentelemetry/instrumentation": "^0.51.0", - "@opentelemetry/semantic-conventions": "^1.0.0", + "@opentelemetry/semantic-conventions": "^1.22.0", "@types/tedious": "^4.0.10" }, "devDependencies": { @@ -47180,7 +47180,7 @@ "@opentelemetry/contrib-test-utils": "^0.39.0", "@opentelemetry/instrumentation": "^0.51.0", "@opentelemetry/sdk-trace-base": "^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/tedious": "^4.0.10", diff --git a/plugins/node/instrumentation-tedious/README.md b/plugins/node/instrumentation-tedious/README.md index ee8a9ee535..48863d1a3c 100644 --- a/plugins/node/instrumentation-tedious/README.md +++ b/plugins/node/instrumentation-tedious/README.md @@ -40,6 +40,22 @@ registerInstrumentations({ }) ``` +## 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.sql.table` | The name of the primary table that the operation is acting upon. | +| `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/instrumentation-tedious/package.json b/plugins/node/instrumentation-tedious/package.json index c123a3a90b..8d2948bf55 100644 --- a/plugins/node/instrumentation-tedious/package.json +++ b/plugins/node/instrumentation-tedious/package.json @@ -63,7 +63,7 @@ }, "dependencies": { "@opentelemetry/instrumentation": "^0.51.0", - "@opentelemetry/semantic-conventions": "^1.0.0", + "@opentelemetry/semantic-conventions": "^1.22.0", "@types/tedious": "^4.0.10" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-tedious#readme" diff --git a/plugins/node/instrumentation-tedious/src/instrumentation.ts b/plugins/node/instrumentation-tedious/src/instrumentation.ts index d9c23eeb86..24935a32c3 100644 --- a/plugins/node/instrumentation-tedious/src/instrumentation.ts +++ b/plugins/node/instrumentation-tedious/src/instrumentation.ts @@ -22,8 +22,14 @@ import { isWrapped, } from '@opentelemetry/instrumentation'; import { - DbSystemValues, - SemanticAttributes, + DBSYSTEMVALUES_MSSQL, + SEMATTRS_DB_NAME, + SEMATTRS_DB_SQL_TABLE, + SEMATTRS_DB_STATEMENT, + SEMATTRS_DB_SYSTEM, + SEMATTRS_DB_USER, + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, } from '@opentelemetry/semantic-conventions'; import type * as tedious from 'tedious'; import { TediousInstrumentationConfig } from './types'; @@ -155,16 +161,16 @@ export class TediousInstrumentation extends InstrumentationBase { { kind: api.SpanKind.CLIENT, attributes: { - [SemanticAttributes.DB_SYSTEM]: DbSystemValues.MSSQL, - [SemanticAttributes.DB_NAME]: databaseName, - [SemanticAttributes.NET_PEER_PORT]: this.config?.options?.port, - [SemanticAttributes.NET_PEER_NAME]: this.config?.server, + [SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MSSQL, + [SEMATTRS_DB_NAME]: databaseName, + [SEMATTRS_NET_PEER_PORT]: this.config?.options?.port, + [SEMATTRS_NET_PEER_NAME]: this.config?.server, // >=4 uses `authentication` object, older versions just userName and password pair - [SemanticAttributes.DB_USER]: + [SEMATTRS_DB_USER]: this.config?.userName ?? this.config?.authentication?.options?.userName, - [SemanticAttributes.DB_STATEMENT]: sql, - [SemanticAttributes.DB_SQL_TABLE]: request.table, + [SEMATTRS_DB_STATEMENT]: sql, + [SEMATTRS_DB_SQL_TABLE]: request.table, }, } ); diff --git a/plugins/node/instrumentation-tedious/test/instrumentation.test.ts b/plugins/node/instrumentation-tedious/test/instrumentation.test.ts index 9313281838..5a531e2815 100644 --- a/plugins/node/instrumentation-tedious/test/instrumentation.test.ts +++ b/plugins/node/instrumentation-tedious/test/instrumentation.test.ts @@ -17,8 +17,14 @@ import { context, trace, SpanStatusCode, SpanKind } from '@opentelemetry/api'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { - DbSystemValues, - SemanticAttributes, + DBSYSTEMVALUES_MSSQL, + SEMATTRS_DB_NAME, + SEMATTRS_DB_SQL_TABLE, + SEMATTRS_DB_STATEMENT, + SEMATTRS_DB_SYSTEM, + SEMATTRS_DB_USER, + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, } from '@opentelemetry/semantic-conventions'; import * as util from 'util'; import * as testUtils from '@opentelemetry/contrib-test-utils'; @@ -335,17 +341,14 @@ function assertSpan(span: ReadableSpan, expected: any) { assert(span); assert.strictEqual(span.name, expected.name); assert.strictEqual(span.kind, SpanKind.CLIENT); + assert.strictEqual(span.attributes[SEMATTRS_DB_SYSTEM], DBSYSTEMVALUES_MSSQL); assert.strictEqual( - span.attributes[SemanticAttributes.DB_SYSTEM], - DbSystemValues.MSSQL - ); - assert.strictEqual( - span.attributes[SemanticAttributes.DB_NAME], + span.attributes[SEMATTRS_DB_NAME], expected.database ?? database ); - assert.strictEqual(span.attributes[SemanticAttributes.NET_PEER_PORT], port); - assert.strictEqual(span.attributes[SemanticAttributes.NET_PEER_NAME], host); - assert.strictEqual(span.attributes[SemanticAttributes.DB_USER], user); + assert.strictEqual(span.attributes[SEMATTRS_NET_PEER_PORT], port); + assert.strictEqual(span.attributes[SEMATTRS_NET_PEER_NAME], host); + assert.strictEqual(span.attributes[SEMATTRS_DB_USER], user); assert.strictEqual( span.attributes['tedious.procedure_count'], expected.procCount ?? 1, @@ -362,27 +365,18 @@ function assertSpan(span: ReadableSpan, expected: any) { expected.parentSpan.spanContext().spanId ); } - assert.strictEqual( - span.attributes[SemanticAttributes.DB_SQL_TABLE], - expected.table - ); + assert.strictEqual(span.attributes[SEMATTRS_DB_SQL_TABLE], expected.table); if (expected.sql) { if (expected.sql instanceof RegExp) { assertMatch( - span.attributes[SemanticAttributes.DB_STATEMENT] as string | undefined, + span.attributes[SEMATTRS_DB_STATEMENT] as string | undefined, expected.sql ); } else { - assert.strictEqual( - span.attributes[SemanticAttributes.DB_STATEMENT], - expected.sql - ); + assert.strictEqual(span.attributes[SEMATTRS_DB_STATEMENT], expected.sql); } } else { - assert.strictEqual( - span.attributes[SemanticAttributes.DB_STATEMENT], - undefined - ); + assert.strictEqual(span.attributes[SEMATTRS_DB_STATEMENT], undefined); } if (expected.error) { assert(