Skip to content

Commit

Permalink
refactor(instr-tedious): use exported strings for attributes (#2144)
Browse files Browse the repository at this point in the history
Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
  • Loading branch information
david-luna and blumamir committed Apr 29, 2024
1 parent 56392f4 commit b40926e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
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.

16 changes: 16 additions & 0 deletions plugins/node/instrumentation-tedious/README.md
Expand Up @@ -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: <https://opentelemetry.io/>
Expand Down
2 changes: 1 addition & 1 deletion plugins/node/instrumentation-tedious/package.json
Expand Up @@ -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"
Expand Down
24 changes: 15 additions & 9 deletions plugins/node/instrumentation-tedious/src/instrumentation.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
},
}
);
Expand Down
40 changes: 17 additions & 23 deletions plugins/node/instrumentation-tedious/test/instrumentation.test.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit b40926e

Please sign in to comment.