Skip to content

Commit df2fa5a

Browse files
refactor(instr-knex): use exported strings for attributes (#2109)
* refactor(instr-knex): use exported strings for attributes * chore(instr-knex): update semantic conventions --------- Co-authored-by: Marc Pichler <[email protected]>
1 parent d5e0d65 commit df2fa5a

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/node/opentelemetry-instrumentation-knex/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ registerInstrumentations({
4848
| ------- | ---- | ------- | ----------- |
4949
| `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. |
5050

51+
## Semantic Conventions
52+
53+
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)
54+
55+
Attributes collected:
56+
57+
| Attribute | Short Description |
58+
| ----------------------- | ------------------------------------------------------------------------------ |
59+
| `db.name` | This attribute is used to report the name of the database being accessed. |
60+
| `db.operation` | The name of the operation being executed. |
61+
| `db.sql.table` | The name of the primary table that the operation is acting upon. |
62+
| `db.statement` | The database statement being executed. |
63+
| `db.system` | An identifier for the database management system (DBMS) product being used. |
64+
| `db.user` | Username for accessing the database. |
65+
| `net.peer.name` | Remote hostname or similar. |
66+
| `net.peer.port` | Remote port number. |
67+
| `net.transport` | Transport protocol used. |
68+
5169
## Useful links
5270

5371
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>

plugins/node/opentelemetry-instrumentation-knex/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
},
5959
"dependencies": {
6060
"@opentelemetry/instrumentation": "^0.50.0",
61-
"@opentelemetry/semantic-conventions": "^1.0.0"
61+
"@opentelemetry/semantic-conventions": "^1.22.0"
6262
},
6363
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-knex#readme"
6464
}

plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ import {
2323
InstrumentationNodeModuleFile,
2424
isWrapped,
2525
} from '@opentelemetry/instrumentation';
26-
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
26+
import {
27+
SEMATTRS_DB_NAME,
28+
SEMATTRS_DB_OPERATION,
29+
SEMATTRS_DB_SQL_TABLE,
30+
SEMATTRS_DB_STATEMENT,
31+
SEMATTRS_DB_SYSTEM,
32+
SEMATTRS_DB_USER,
33+
SEMATTRS_NET_PEER_NAME,
34+
SEMATTRS_NET_PEER_PORT,
35+
SEMATTRS_NET_TRANSPORT,
36+
} from '@opentelemetry/semantic-conventions';
2737
import * as utils from './utils';
2838
import * as types from './types';
2939

@@ -144,18 +154,18 @@ export class KnexInstrumentation extends InstrumentationBase<any> {
144154

145155
const attributes: api.SpanAttributes = {
146156
'knex.version': moduleVersion,
147-
[SemanticAttributes.DB_SYSTEM]: utils.mapSystem(config.client),
148-
[SemanticAttributes.DB_SQL_TABLE]: table,
149-
[SemanticAttributes.DB_OPERATION]: operation,
150-
[SemanticAttributes.DB_USER]: config?.connection?.user,
151-
[SemanticAttributes.DB_NAME]: name,
152-
[SemanticAttributes.NET_PEER_NAME]: config?.connection?.host,
153-
[SemanticAttributes.NET_PEER_PORT]: config?.connection?.port,
154-
[SemanticAttributes.NET_TRANSPORT]:
157+
[SEMATTRS_DB_SYSTEM]: utils.mapSystem(config.client),
158+
[SEMATTRS_DB_SQL_TABLE]: table,
159+
[SEMATTRS_DB_OPERATION]: operation,
160+
[SEMATTRS_DB_USER]: config?.connection?.user,
161+
[SEMATTRS_DB_NAME]: name,
162+
[SEMATTRS_NET_PEER_NAME]: config?.connection?.host,
163+
[SEMATTRS_NET_PEER_PORT]: config?.connection?.port,
164+
[SEMATTRS_NET_TRANSPORT]:
155165
config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
156166
};
157167
if (maxLen !== 0) {
158-
attributes[SemanticAttributes.DB_STATEMENT] = utils.limitLength(
168+
attributes[SEMATTRS_DB_STATEMENT] = utils.limitLength(
159169
query?.sql,
160170
maxLen
161171
);

plugins/node/opentelemetry-instrumentation-knex/src/utils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { DbSystemValues } from '@opentelemetry/semantic-conventions';
17+
import {
18+
DBSYSTEMVALUES_SQLITE,
19+
DBSYSTEMVALUES_POSTGRESQL,
20+
} from '@opentelemetry/semantic-conventions';
1821

1922
type Exception = {
2023
new (message: string): Exception;
@@ -52,8 +55,8 @@ export const cloneErrorWithNewMessage = (err: Exception, message: string) => {
5255
};
5356

5457
const systemMap = new Map([
55-
['sqlite3', DbSystemValues.SQLITE],
56-
['pg', DbSystemValues.POSTGRESQL],
58+
['sqlite3', DBSYSTEMVALUES_SQLITE],
59+
['pg', DBSYSTEMVALUES_POSTGRESQL],
5760
]);
5861
export const mapSystem = (knexSystem: string) => {
5962
return systemMap.get(knexSystem) || knexSystem;

0 commit comments

Comments
 (0)