From d6fa055d0ecef333f952598c5885a2eccdd309de Mon Sep 17 00:00:00 2001 From: David Luna Date: Wed, 17 Apr 2024 17:27:45 +0200 Subject: [PATCH 1/2] refactor(instr-knex): use exported strings for attributes --- .../README.md | 18 +++++++++++ .../src/instrumentation.ts | 30 ++++++++++++------- .../src/utils.ts | 9 ++++-- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-knex/README.md b/plugins/node/opentelemetry-instrumentation-knex/README.md index 05537459cc..ba3e97846e 100644 --- a/plugins/node/opentelemetry-instrumentation-knex/README.md +++ b/plugins/node/opentelemetry-instrumentation-knex/README.md @@ -48,6 +48,24 @@ registerInstrumentations({ | ------- | ---- | ------- | ----------- | | `maxQueryLength` | `number` | `100` | Truncate `db.statement` attribute to a maximum length. If the statement is truncated `'..'` is added to it's end. Default `1022`. `-1` leaves `db.statement` untouched. | +## 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.operation` | The name of the operation being executed. | +| `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. | +| `net.transport` | Transport protocol used. | + ## Useful links - For more information on OpenTelemetry, visit: diff --git a/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts index 257232f278..bb6e6114b5 100644 --- a/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts @@ -23,7 +23,17 @@ import { InstrumentationNodeModuleFile, isWrapped, } from '@opentelemetry/instrumentation'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_DB_NAME, + SEMATTRS_DB_OPERATION, + SEMATTRS_DB_SQL_TABLE, + SEMATTRS_DB_STATEMENT, + SEMATTRS_DB_SYSTEM, + SEMATTRS_DB_USER, + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, + SEMATTRS_NET_TRANSPORT, +} from '@opentelemetry/semantic-conventions'; import * as utils from './utils'; import * as types from './types'; @@ -144,18 +154,18 @@ export class KnexInstrumentation extends InstrumentationBase { const attributes: api.SpanAttributes = { 'knex.version': moduleVersion, - [SemanticAttributes.DB_SYSTEM]: utils.mapSystem(config.client), - [SemanticAttributes.DB_SQL_TABLE]: table, - [SemanticAttributes.DB_OPERATION]: operation, - [SemanticAttributes.DB_USER]: config?.connection?.user, - [SemanticAttributes.DB_NAME]: name, - [SemanticAttributes.NET_PEER_NAME]: config?.connection?.host, - [SemanticAttributes.NET_PEER_PORT]: config?.connection?.port, - [SemanticAttributes.NET_TRANSPORT]: + [SEMATTRS_DB_SYSTEM]: utils.mapSystem(config.client), + [SEMATTRS_DB_SQL_TABLE]: table, + [SEMATTRS_DB_OPERATION]: operation, + [SEMATTRS_DB_USER]: config?.connection?.user, + [SEMATTRS_DB_NAME]: name, + [SEMATTRS_NET_PEER_NAME]: config?.connection?.host, + [SEMATTRS_NET_PEER_PORT]: config?.connection?.port, + [SEMATTRS_NET_TRANSPORT]: config?.connection?.filename === ':memory:' ? 'inproc' : undefined, }; if (maxLen !== 0) { - attributes[SemanticAttributes.DB_STATEMENT] = utils.limitLength( + attributes[SEMATTRS_DB_STATEMENT] = utils.limitLength( query?.sql, maxLen ); diff --git a/plugins/node/opentelemetry-instrumentation-knex/src/utils.ts b/plugins/node/opentelemetry-instrumentation-knex/src/utils.ts index 6c7e6fbdba..da640ceae3 100644 --- a/plugins/node/opentelemetry-instrumentation-knex/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-knex/src/utils.ts @@ -14,7 +14,10 @@ * limitations under the License. */ -import { DbSystemValues } from '@opentelemetry/semantic-conventions'; +import { + DBSYSTEMVALUES_SQLITE, + DBSYSTEMVALUES_POSTGRESQL, +} from '@opentelemetry/semantic-conventions'; type Exception = { new (message: string): Exception; @@ -52,8 +55,8 @@ export const cloneErrorWithNewMessage = (err: Exception, message: string) => { }; const systemMap = new Map([ - ['sqlite3', DbSystemValues.SQLITE], - ['pg', DbSystemValues.POSTGRESQL], + ['sqlite3', DBSYSTEMVALUES_SQLITE], + ['pg', DBSYSTEMVALUES_POSTGRESQL], ]); export const mapSystem = (knexSystem: string) => { return systemMap.get(knexSystem) || knexSystem; From 8859d6567a6eae766cf6d25bb91e4daa7984cc17 Mon Sep 17 00:00:00 2001 From: David Luna Date: Wed, 17 Apr 2024 18:24:03 +0200 Subject: [PATCH 2/2] chore(instr-knex): update semantic conventions --- package-lock.json | 4 ++-- plugins/node/opentelemetry-instrumentation-knex/package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 080efc40e3..0d35dc047e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37894,7 +37894,7 @@ "license": "Apache-2.0", "dependencies": { "@opentelemetry/instrumentation": "^0.50.0", - "@opentelemetry/semantic-conventions": "^1.0.0" + "@opentelemetry/semantic-conventions": "^1.22.0" }, "devDependencies": { "@opentelemetry/api": "^1.3.0", @@ -46335,7 +46335,7 @@ "@opentelemetry/instrumentation": "^0.50.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", "knex": "0.95.9", diff --git a/plugins/node/opentelemetry-instrumentation-knex/package.json b/plugins/node/opentelemetry-instrumentation-knex/package.json index 0248695c58..5a8b7078c6 100644 --- a/plugins/node/opentelemetry-instrumentation-knex/package.json +++ b/plugins/node/opentelemetry-instrumentation-knex/package.json @@ -58,7 +58,7 @@ }, "dependencies": { "@opentelemetry/instrumentation": "^0.50.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-knex#readme" }