Semantic conventions for MongoDB client operations

Status: Development

The Semantic Conventions for MongoDB extend and override the Database Semantic Conventions.

Spans

Status: Development

Spans representing calls to MongoDB adhere to the general Semantic Conventions for Database Client Spans.

Span name SHOULD follow the general database span name convention

Span kind SHOULD be CLIENT.

Span status SHOULD follow the Recording Errors document.

Attributes:

KeyStabilityRequirement LevelValue TypeDescriptionExample Values
db.collection.nameStableRequiredstringThe MongoDB collection being accessed within the database stated in db.namespace. [1]public.users; customers
db.operation.nameStableRequiredstringThe name of the MongoDB command being executed.findAndModify; getMore; insertMany; bulkWrite
db.system.nameStableRequiredstringThe database management system (DBMS) product as identified by the client instrumentation. [2]mongodb
db.namespaceStableConditionally Required If available.stringThe MongoDB database name.customers; test.users
db.response.status_codeStableConditionally Required [3]stringMongoDB error code represented as a string. [4]36; 11602
error.typeStableConditionally Required If and only if the operation failed.stringDescribes a class of error the operation ended with. [5]timeout; java.net.UnknownHostException; server_certificate_invalid; 500
server.portStableConditionally Required [6]intServer port number. [7]80; 8080; 443
db.operation.batch.sizeStableRecommendedintThe number of database operations included in a batch operation. [8]2; 3; 4
server.addressStableRecommendedstringThe database address specified in the client configuration. [9]example.com; 10.1.2.80; /tmp/my.sock

[1] db.collection.name: It is RECOMMENDED to capture the value as provided by the application without attempting to do any case normalization. For batch operations, if the individual operations would all have the same db.collection.name when executed as non-batch operations, then that collection name SHOULD be used.

[2] db.system.name: MUST be set to "mongodb".

[3] db.response.status_code: If the operation failed and error code is available.

[4] db.response.status_code: All MongoDB error codes SHOULD be considered errors.

[5] error.type: The error.type SHOULD match the db.response.status_code returned by the database or the client library, or the canonical name of exception that occurred. When using canonical exception type name, instrumentation SHOULD do the best effort to report the most relevant type. For example, if the original exception is wrapped into a generic one, the original exception SHOULD be preferred. Instrumentations SHOULD document how error.type is populated.

[6] server.port: If server.address is set and the client is configured with a single database server endpoint that uses a non-default port.

[7] server.port: server.port SHOULD reflect the database port specified in the client configuration. It SHOULD NOT be derived from the server selected for an operation.

[8] db.operation.batch.size: Except for empty batch requests described below, a batch operation contains two or more database operations explicitly submitted as separate operations in a single client call, protocol message, or database command.

Requests to batch APIs that contain only one operation SHOULD be modeled as single operations, not as batch operations.

A database call is not a batch operation solely because one operation accepts multiple operands, such as keys, rows, documents, points, or other data elements, including Redis MGET with multiple keys.

In batch APIs that execute the same parameterized operation with parameter sets, each parameter set represents one database operation for determining whether the request is a batch operation. Requests with only one parameter set SHOULD be modeled as single operations, not as batch operations.

db.operation.batch.size SHOULD be set to the number of operations in the batch. It SHOULD NOT be set for non-batch operations.

A request to execute a batch operation with no operations SHOULD also be treated as a batch operation, and db.operation.batch.size SHOULD be set to 0.

[9] server.address: Instrumentation SHOULD set server.address to the database address specified in the client configuration. It SHOULD NOT derive the value from the server selected for an operation.

See Database server address for guidance and examples.

The following attributes can be important for making sampling decisions and SHOULD be provided at span creation time (if provided at all):

Example

KeyValue
Span name"findAndModify products"
db.system.name"mongodb"
server.address"mongodb0.example.com"
server.port27017
db.collection.name"products"
db.namespace"shopDb"
db.query.textnot set
db.operation.name"findAndModify"

Context propagation

Status: Development

Instrumentations MAY propagate context to MongoDB servers and compatible servers through the comment command field. This context propagation mechanism SHOULD NOT be enabled by default, but instrumentation MAY allow users to opt into it.

Before injecting context, instrumentation MUST determine that the target server accepts a BSON document as the value of comment. MongoDB 4.4 and later accept all BSON types for comment, while earlier versions accept only strings. Compatible servers may expose support through different versions or capabilities. If document-valued comment support is unknown or unavailable, instrumentation MUST skip context injection for the operation.

When this mechanism is enabled and the target server supports document-valued comment, instrumentation MUST use the selected propagator to inject string key-value pairs into the top-level fields of a BSON document in comment, unless doing so would exceed an applicable size limit.

The instrumentation SHOULD allow users to pass a propagator to overwrite the global propagator. If no propagator is provided by the user, instrumentation SHOULD use the global propagator. The override applies only to this instrumentation.

The optional originalComment field is reserved for the application-provided comment, which can be any BSON type. When the application has set comment, instrumentation MUST preserve its value and BSON type unchanged in originalComment, including an explicitly set BSON null. When the application has not set comment, instrumentation MUST omit originalComment. Instrumentation MUST prevent propagators from setting originalComment.

For example, with a W3C Trace Context propagator and an application-provided document comment:

{
  "comment": {
    "traceparent": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01",
    "tracestate": "congo=t61rcWkgMzE",
    "originalComment": {
      "job": "monthly rollup"
    }
  }
}

Servers that extract this context read the top-level string fields of a document-valued comment and need a propagator compatible with the one used for injection.

MongoDB servers and compatible servers may expose or persist comment in logs, profiling data, or diagnostic interfaces. Instrumentations SHOULD document that every field injected into comment may be visible to users with access to those outputs, and that wrapping an application-provided comment may change the structure exposed in those outputs.

The propagation document counts toward applicable BSON document and wire message size limits. Instrumentations SHOULD keep it compact and MUST NOT assume that the entire size limit is available for comment.

If context injection would cause an otherwise valid command to exceed an applicable size limit, instrumentation MUST skip injection and send the original command unchanged. This includes preserving the value and BSON type of any application-provided comment, including explicit BSON null, or leaving comment absent if it was not provided. Instrumentation MUST NOT truncate an application-provided comment or fail the operation because context injection would exceed a size limit.

Metrics

MongoDB client instrumentations SHOULD collect metrics according to the general Semantic Conventions for Database Client Metrics.

db.system.name MUST be set to "mongodb".