# Semantic conventions for MongoDB client operations

Site [llms.txt](/llms.txt)

---

**Status**: [Development][DocumentStatus]

<!-- START doctoc -->

- [Spans](#spans)
  - [Example](#example)
- [Context propagation](#context-propagation)
- [Metrics](#metrics)

<!-- END doctoc -->

The Semantic Conventions for [MongoDB](https://www.mongodb.com/) extend and override the [Database Semantic Conventions](README.md).

## Spans

<!-- semconv span.db.mongodb.client -->
<!-- NOTE: THIS TEXT IS AUTOGENERATED. DO NOT EDIT BY HAND. -->
<!-- see templates/registry/markdown/snippet.md.j2 -->
<!-- prettier-ignore-start -->

**Status:** ![Development](https://img.shields.io/badge/-development-blue)

Spans representing calls to MongoDB adhere to the general [Semantic Conventions for Database Client Spans](/docs/specs/semconv/db/database-spans.md).

**Span name** SHOULD follow the general [database span name convention](/docs/specs/semconv/db/database-spans.md#name)

**Span kind** SHOULD be `CLIENT`.

**Span status** SHOULD follow the [Recording Errors](/docs/specs/semconv/general/recording-errors.md) document.

**Attributes:**

| Key | Stability | [Requirement Level](/docs/specs/semconv/general/attribute-requirement-level/) | Value Type | Description | Example Values |
| --- | --- | --- | --- | --- | --- |
| [`db.collection.name`](/docs/specs/semconv/registry/attributes/db.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Required` | string | The MongoDB collection being accessed within the database stated in `db.namespace`. [1] | `public.users`; `customers` |
| [`db.operation.name`](/docs/specs/semconv/registry/attributes/db.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Required` | string | The name of the [MongoDB command](https://www.mongodb.com/docs/manual/reference/command/) being executed. | `findAndModify`; `getMore`; `insertMany`; `bulkWrite` |
| [`db.system.name`](/docs/specs/semconv/registry/attributes/db.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Required` | string | The database management system (DBMS) product as identified by the client instrumentation. [2] | `mongodb` |
| [`db.namespace`](/docs/specs/semconv/registry/attributes/db.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Conditionally Required` If available. | string | The MongoDB database name. | `customers`; `test.users` |
| [`db.response.status_code`](/docs/specs/semconv/registry/attributes/db.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Conditionally Required` [3] | string | [MongoDB error code](https://www.mongodb.com/docs/manual/reference/error-codes/) represented as a string. [4] | `36`; `11602` |
| [`error.type`](/docs/specs/semconv/registry/attributes/error.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Conditionally Required` If and only if the operation failed. | string | Describes a class of error the operation ended with. [5] | `timeout`; `java.net.UnknownHostException`; `server_certificate_invalid`; `500` |
| [`server.port`](/docs/specs/semconv/registry/attributes/server.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Conditionally Required` [6] | int | Server port number. [7] | `80`; `8080`; `443` |
| [`db.operation.batch.size`](/docs/specs/semconv/registry/attributes/db.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Recommended` | int | The number of database operations included in a batch operation. [8] | `2`; `3`; `4` |
| [`server.address`](/docs/specs/semconv/registry/attributes/server.md) | ![Stable](https://img.shields.io/badge/-stable-lightgreen) | `Recommended` | string | The 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`](https://redis.io/docs/latest/commands/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.

> [!WARNING]
>
> `server.address` MUST NOT contain credentials or other sensitive information.

See [Database server address](/docs/specs/semconv/db/database-spans.md#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):

* [`db.collection.name`](/docs/specs/semconv/registry/attributes/db.md)
* [`db.namespace`](/docs/specs/semconv/registry/attributes/db.md)
* [`db.operation.name`](/docs/specs/semconv/registry/attributes/db.md)
* [`db.system.name`](/docs/specs/semconv/registry/attributes/db.md)
* [`server.address`](/docs/specs/semconv/registry/attributes/server.md)
* [`server.port`](/docs/specs/semconv/registry/attributes/server.md)

<!-- prettier-ignore-end -->
<!-- END AUTOGENERATED TEXT -->
<!-- endsemconv -->

### Example

| Key                    | Value                      |
| :--------------------- | :------------------------- |
| Span name              | `"findAndModify products"` |
| `db.system.name`       | `"mongodb"`                |
| `server.address`       | `"mongodb0.example.com"`   |
| `server.port`          | `27017`                    |
| `db.collection.name`   | `"products"`               |
| `db.namespace`         | `"shopDb"`                 |
| `db.query.text`        | not set                    |
| `db.operation.name`    | `"findAndModify"`          |

## Context propagation

**Status**: [Development][DocumentStatus]

Instrumentations MAY propagate context to MongoDB servers and compatible
servers through the [`comment` command field](https://www.mongodb.com/docs/manual/reference/command/find/).
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:

```json
{
  "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](database-metrics.md).

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

[DocumentStatus]: /docs/specs/otel/document-status/
