Skip to content

Commit

Permalink
refactor(instr-knex): use exported strings for attributes (#2109)
Browse files Browse the repository at this point in the history
* refactor(instr-knex): use exported strings for attributes

* chore(instr-knex): update semantic conventions

---------

Co-authored-by: Marc Pichler <[email protected]>
  • Loading branch information
david-luna and pichlermarc authored Apr 18, 2024
1 parent d5e0d65 commit df2fa5a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 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.

18 changes: 18 additions & 0 deletions plugins/node/opentelemetry-instrumentation-knex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <https://opentelemetry.io/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';

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

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
);
Expand Down
9 changes: 6 additions & 3 deletions plugins/node/opentelemetry-instrumentation-knex/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit df2fa5a

Please sign in to comment.