Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(instr-knex): use exported strings for attributes #2109

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading