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-mongodb): use exported strings for attributes #2088

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.
Jump to
Jump to file
Failed to load files.
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.

16 changes: 16 additions & 0 deletions plugins/node/opentelemetry-instrumentation-mongodb/README.md
Expand Up @@ -55,6 +55,22 @@ Mongodb instrumentation has few options available to choose from. You can set th
| `responseHook` | `MongoDBInstrumentationExecutionResponseHook` (function) | Function for adding custom attributes from db response |
| `dbStatementSerializer` | `DbStatementSerializer` (function) | Custom serializer function for the db.statement tag |

## 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.system` | An identifier for the database management system (DBMS) product being used. |
| `db.connection_string` | The connection string used to connect to the database. |
| `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.mongodb.collection` | The collection being accessed within the database stated in `db.name`. |
| `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
Expand Up @@ -68,7 +68,7 @@
"dependencies": {
"@opentelemetry/instrumentation": "^0.50.0",
"@opentelemetry/sdk-metrics": "^1.9.1",
"@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-mongodb#readme"
}
Expand Up @@ -29,8 +29,15 @@ import {
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import {
DbSystemValues,
SemanticAttributes,
DBSYSTEMVALUES_MONGODB,
SEMATTRS_DB_CONNECTION_STRING,
SEMATTRS_DB_MONGODB_COLLECTION,
SEMATTRS_DB_NAME,
SEMATTRS_DB_OPERATION,
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
} from '@opentelemetry/semantic-conventions';
import { MongoDBInstrumentationConfig, CommandResult } from './types';
import {
Expand Down Expand Up @@ -898,18 +905,18 @@ export class MongoDBInstrumentation extends InstrumentationBase {
) {
// add database related attributes
span.setAttributes({
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.MONGODB,
[SemanticAttributes.DB_NAME]: dbName,
[SemanticAttributes.DB_MONGODB_COLLECTION]: dbCollection,
[SemanticAttributes.DB_OPERATION]: operation,
[SemanticAttributes.DB_CONNECTION_STRING]: `mongodb://${host}:${port}/${dbName}`,
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MONGODB,
[SEMATTRS_DB_NAME]: dbName,
[SEMATTRS_DB_MONGODB_COLLECTION]: dbCollection,
[SEMATTRS_DB_OPERATION]: operation,
[SEMATTRS_DB_CONNECTION_STRING]: `mongodb://${host}:${port}/${dbName}`,
});

if (host && port) {
span.setAttribute(SemanticAttributes.NET_PEER_NAME, host);
span.setAttribute(SEMATTRS_NET_PEER_NAME, host);
const portNumber = parseInt(port, 10);
if (!isNaN(portNumber)) {
span.setAttribute(SemanticAttributes.NET_PEER_PORT, portNumber);
span.setAttribute(SEMATTRS_NET_PEER_PORT, portNumber);
}
}
if (!commandObj) return;
Expand All @@ -921,7 +928,7 @@ export class MongoDBInstrumentation extends InstrumentationBase {
safeExecuteInTheMiddle(
() => {
const query = dbStatementSerializer(commandObj);
span.setAttribute(SemanticAttributes.DB_STATEMENT, query);
span.setAttribute(SEMATTRS_DB_STATEMENT, query);
},
err => {
if (err) {
Expand Down