Skip to main content

Mongodb Receiver

Status Available in: contrib Maintainers: @justinianvoss22, @dyl10s, @ishleenk17 Source: opentelemetry-collector-contrib

Supported Telemetry

Metrics

Overview

Purpose

The purpose of this receiver is to allow users to monitor metrics from standalone MongoDB clusters. This includes non-Atlas managed MongoDB Servers.

Prerequisites

This receiver supports MongoDB versions:
  • 4.0+
  • 5.0
  • 6.0
  • 7.0
Mongodb recommends to set up a least privilege user (LPU) with a clusterMonitor role in order to collect metrics. Please refer to lpu.sh for an example of how to configure these permissions.

Configuration

The following settings are optional:
  • hosts (default: [localhost:27017]): list of host:port or unix domain socket endpoints.The transport option is no longer available.
    • For standalone MongoDB deployments this is the hostname and port of the mongod instance
    • For replica sets specify the hostnames and ports of the mongod instances that are in the replica set configuration. If the replica_set field is specified, nodes will be autodiscovered.
    • For a sharded MongoDB deployment, please specify a list of the mongos hosts.
  • scheme (default: mongodb): connection scheme. Use mongodb+srv for clusters that use SRV DNS records (e.g. MongoDB Atlas). When using mongodb+srv, exactly one host must be specified.
  • username: If authentication is required, the user can with clusterMonitor permissions can be provided here.
  • password: If authentication is required, the password can be provided here.
  • auth_mechanism: (optional) The authentication mechanism to use. Common values include SCRAM-SHA-1, SCRAM-SHA-256, MONGODB-X509, GSSAPI, MONGODB-AWS, etc. If not specified, MongoDB will use the default mechanism.
  • auth_source: (optional) The database name to use for authentication. If not specified, MongoDB will use the default authentication database (usually admin).
  • auth_mechanism_properties: (optional) A map of key-value pairs specifying additional properties for the authentication mechanism. For example, when using GSSAPI (Kerberos), you may need to set SERVICE_NAME. For MONGODB-AWS, you may need to set AWS_SESSION_TOKEN when using temporary AWS credentials.
  • collection_interval: (default = 1m): This receiver collects metrics on an interval. This value must be a string readable by Golang’s time.ParseDuration. Valid time units are ns, us (or µs), ms, s, m, h.
  • initial_delay (default = 1s): defines how long this receiver waits before starting.
  • replica_set: If the deployment of MongoDB is a replica set then this allows users to specify the replica set name which allows for autodiscovery of other nodes in the replica set.
  • timeout: (default = 1m) The timeout of running commands against mongo.
  • tls: TLS control. By default, insecure settings are rejected and certificate verification is on.
  • direct_connection: If true, then the driver will not try to autodiscover other nodes, and perform instead a direct connection o the host.

Example Configuration

receivers:
  mongodb:
    hosts:
      - endpoint: localhost:27017
    username: otel
    password: ${env:MONGODB_PASSWORD}
    collection_interval: 60s
    initial_delay: 1s
    tls:
      insecure: true
      insecure_skip_verify: true

Example Configuration (MongoDB Atlas / SRV)

receivers:
  mongodb:
    hosts:
      - endpoint: cluster0.example.mongodb.net
    scheme: mongodb+srv
    username: otel
    password: ${env:MONGODB_PASSWORD}
    collection_interval: 60s
The full list of settings exposed for this receiver are documented in config.go with detailed sample configurations in testdata/config.yaml.

Authentication

The receiver supports several MongoDB authentication mechanisms. The auth_mechanism, auth_source, and auth_mechanism_properties fields are passed directly to the MongoDB Go driver’s options.Credential struct.

SCRAM (Default)

SCRAM-SHA-256 is the default authentication mechanism for MongoDB 4.0+. If auth_mechanism is not specified and username/password are provided, the driver will negotiate the strongest SCRAM mechanism supported by the server (SCRAM-SHA-256, falling back to SCRAM-SHA-1).
receivers:
  mongodb:
    hosts:
      - endpoint: localhost:27017
    username: otel
    password: ${env:MONGODB_PASSWORD}
    collection_interval: 60s
    initial_delay: 1s
    tls:
      insecure: true
To explicitly specify the mechanism:
receivers:
  mongodb:
    hosts:
      - endpoint: localhost:27017
    username: otel
    password: ${env:MONGODB_PASSWORD}
    auth_mechanism: SCRAM-SHA-256
    auth_source: admin
    collection_interval: 60s
    initial_delay: 1s
    tls:
      insecure: true

X.509 Certificate Authentication

X.509 authentication uses TLS client certificates instead of username and password. The certificate’s subject DN is used as the identity.
receivers:
  mongodb:
    hosts:
      - endpoint: localhost:27018
    auth_mechanism: MONGODB-X509
    auth_source: $external
    collection_interval: 60s
    initial_delay: 1s
    tls:
      ca_file: /path/to/ca.pem
      cert_file: /path/to/client-cert.pem
      key_file: /path/to/client-key.pem
The MongoDB Go driver typically uses a single combined PEM file (tlsCertificateKeyFile) containing both the certificate and private key. However, the receiver uses the OpenTelemetry Collector’s standard TLS configuration, which requires separate cert_file and key_file entries. If you have a combined PEM file, split it into separate files:

openssl x509 -in client.pem -out client-cert.pem

# Extract the private key
openssl pkey -in client.pem -out client-key.pem

MONGODB-AWS (IAM Authentication)

MONGODB-AWS authenticates using AWS IAM credentials. This mechanism is available in MongoDB 4.4+ and requires server-side support (e.g., MongoDB Atlas or Percona Server for MongoDB). It is not supported by the MongoDB Community Edition. Using explicit IAM credentials:
receivers:
  mongodb:
    hosts:
      - endpoint: mongodb-host:27017
    username: ${env:AWS_ACCESS_KEY_ID}
    password: ${env:AWS_SECRET_ACCESS_KEY}
    auth_mechanism: MONGODB-AWS
    auth_source: $external
    auth_mechanism_properties:
      AWS_SESSION_TOKEN: ${env:AWS_SESSION_TOKEN}
    collection_interval: 60s
    initial_delay: 1s
    tls:
      insecure: true
When running on AWS infrastructure (EC2, ECS, Lambda), the driver can automatically discover credentials from environment variables, the ECS task role endpoint, or the EC2 instance metadata endpoint. In that case, username, password, and auth_mechanism_properties can be omitted:
receivers:
  mongodb:
    hosts:
      - endpoint: mongodb-host:27017
    auth_mechanism: MONGODB-AWS
    auth_source: $external
    collection_interval: 60s
    initial_delay: 1s

GSSAPI (Kerberos)

GSSAPI/Kerberos is available for MongoDB Enterprise deployments only. It is not supported by MongoDB Community Edition. Using this mechanism requires:
  • The collector to be compiled with the gssapi build tag and cgo support (CGO_ENABLED=1).
  • On Linux, the libkrb5 library must be installed.
Note: The default otelcontribcol binary is built without the gssapi build tag and with CGO_ENABLED=0, so GSSAPI authentication will not work out of the box. You will need to build a custom collector with CGO_ENABLED=1 and the gssapi build tag to use this mechanism.
receivers:
  mongodb:
    hosts:
      - endpoint: mongodb-host:27017
    username: [email protected]
    auth_mechanism: GSSAPI
    auth_source: $external
    auth_mechanism_properties:
      SERVICE_NAME: mongodb
    collection_interval: 60s
    initial_delay: 1s
The default Kerberos service name is mongodb. Users can authenticate with an explicit password or by storing authentication keys in keytab files initialized with the kinit utility.

Metrics

The following metric are available with versions:
  • mongodb.extent.count < 4.4 with mmapv1 storage engine
Details about the metrics produced by this receiver can be found in metadata.yaml

Feature gate configurations

See the Collector feature gates for an overview of feature gates in the collector. STABLE: receiver.mongodb.removeDatabaseAttr The feature gate receiver.mongodb.removeDatabaseAttr will remove the database name attribute from data points because it is already found on the resource. This feature gate cannot be changed and will be removed soon.

Metrics

Metric NameDescriptionUnitTypeAttributes
mongodb.active.readsThe number of read operations currently being processed.{reads}UpDownCounter
mongodb.active.writesThe number of write operations currently being processed.{writes}UpDownCounter
mongodb.cache.operationsThe number of cache operations of the instance.{operations}Countertype
mongodb.collection.countThe number of collections.{collections}UpDownCounterdb.namespace
mongodb.commands.rateThe number of commands executed per second.{command}/sGauge
mongodb.connection.countThe number of connections.{connections}UpDownCounterconnection_type, db.namespace
mongodb.cursor.countThe number of open cursors maintained for clients.{cursors}UpDownCounter
mongodb.cursor.timeout.countThe number of cursors that have timed out.{cursors}UpDownCounter
mongodb.data.sizeThe size of the collection. Data compression does not affect this value.ByUpDownCounterdb.namespace
mongodb.database.countThe number of existing databases.{databases}UpDownCounter
mongodb.deletes.rateThe number of deletes executed per second.{delete}/sGauge
mongodb.document.operation.countThe number of document operations executed.{documents}UpDownCounteroperation, db.namespace
mongodb.extent.countThe number of extents.{extents}UpDownCounterdb.namespace
mongodb.flushes.rateThe number of flushes executed per second.{flush}/sGauge
mongodb.getmores.rateThe number of getmores executed per second.{getmore}/sGauge
mongodb.global_lock.timeThe time the global lock has been held.msCounter
mongodb.healthThe health status of the server.1Gauge
mongodb.index.access.countThe number of times an index has been accessed.{accesses}UpDownCountercollection, db.namespace
mongodb.index.countThe number of indexes.{indexes}UpDownCounterdb.namespace
mongodb.index.sizeSum of the space allocated to all indexes in the database, including free index space.ByUpDownCounterdb.namespace
mongodb.inserts.rateThe number of insertions executed per second.{insert}/sGauge
mongodb.lock.acquire.countNumber of times the lock was acquired in the specified mode.{count}Counterlock_type, lock_mode, db.namespace
mongodb.lock.acquire.timeCumulative wait time for the lock acquisitions.microsecondsCounterlock_type, lock_mode, db.namespace
mongodb.lock.acquire.wait_countNumber of times the lock acquisitions encountered waits because the locks were held in a conflicting mode.{count}Counterlock_type, lock_mode, db.namespace
mongodb.lock.deadlock.countNumber of times the lock acquisitions encountered deadlocks.{count}Counterlock_type, lock_mode, db.namespace
mongodb.memory.usageThe amount of memory used.ByUpDownCountermemory_type, db.namespace
mongodb.network.io.receiveThe number of bytes received.ByUpDownCounter
mongodb.network.io.transmitThe number of by transmitted.ByUpDownCounter
mongodb.network.request.countThe number of requests received by the server.{requests}UpDownCounter
mongodb.object.countThe number of objects.{objects}UpDownCounterdb.namespace
mongodb.operation.countThe number of operations executed.{operations}Counteroperation
mongodb.operation.latency.timeThe latency of operations.usGaugeoperation_latency
mongodb.operation.repl.countThe number of replicated operations executed.{operations}Counteroperation
mongodb.operation.timeThe total time spent performing operations.msCounteroperation
mongodb.page_faultsThe number of page faults.{faults}Counter
mongodb.queries.rateThe number of queries executed per second.{query}/sGauge
mongodb.repl_commands_per_secThe number of replicated commands executed per second.{command}/sGauge
mongodb.repl_deletes_per_secThe number of replicated deletes executed per second.{delete}/sGauge
mongodb.repl_getmores_per_secThe number of replicated getmores executed per second.{getmore}/sGauge
mongodb.repl_inserts_per_secThe number of replicated insertions executed per second.{insert}/sGauge
mongodb.repl_queries_per_secThe number of replicated queries executed per second.{query}/sGauge
mongodb.repl_updates_per_secThe number of replicated updates executed per second.{update}/sGauge
mongodb.session.countThe total number of active sessions.{sessions}UpDownCounter
mongodb.storage.sizeThe total amount of storage allocated to this collection.ByCounterdb.namespace
mongodb.updates.rateThe number of updates executed per second.{update}/sGauge
mongodb.uptimeThe amount of time that the server has been running.msCounter
mongodb.wtcache.bytes.readThe number of bytes read into the WiredTiger cache.ByCounter

Attributes

Attribute NameDescriptionTypeValues
collectionThe name of a collection.string
typeThe status of the connection.stringactive, available, current
db.namespaceThe name of a database.string
lock_modeThe mode of Lock which denotes the degree of accessstringshared, exclusive, intent_shared, intent_exclusive
lock_typeThe Resource over which the Lock controls accessstringparallel_batch_write_mode, replication_state_transition, global, database, collection, mutex, metadata, oplog
typeThe type of memory used.stringresident, virtual
operationThe MongoDB operation being counted.stringinsert, query, update, delete, getmore, command
operationThe MongoDB operation with regards to latencystringread, write, command
typeThe result of a cache request.stringhit, miss

Resource Attributes

Attribute NameDescriptionTypeEnabled
server.addressThe address of the MongoDB host.string
server.portThe port of the MongoDB host.int
service.instance.idA unique identifier of the MongoDB resource as a UUID v5, derived from server address and port.string

Configuration

Example Configuration

mongodb:
  hosts:
    - endpoint: localhost:27017
  username: otel
  password: ${env:MONGO_PASSWORD}
  auth_mechanism: SCRAM-SHA-256
  auth_source: admin
  auth_mechanism_properties:
    SERVICE_NAME: mongodb
  collection_interval: 60s

mongodb/srv:
  hosts:
    - endpoint: cluster0.example.mongodb.net
  scheme: mongodb+srv
  username: otel
  password: ${env:MONGO_PASSWORD}
  collection_interval: 60s

Last generated: 2026-04-13