Skip to content

Commit

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

* refactor(instr-mongodb): update README

---------

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
david-luna and pichlermarc authored Apr 10, 2024
1 parent a172f8a commit 66d9cea
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 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/opentelemetry-instrumentation-mongodb/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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"
}
Original file line number Diff line number Diff line change
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

0 comments on commit 66d9cea

Please sign in to comment.