MongoDB
Integrating MongoDB Metrics and Logs to KubeSense
KubeSense ingests MongoDB telemetry through the OpenTelemetry (OTEL) Collector, letting you visualize database performance and logs alongside the rest of your infrastructure.
Prerequisites
Before you begin, ensure you have:
- A MongoDB deployment running version 4.4 or newer (standalone, replica set, or sharded cluster)
- KubeSense installed with an OTEL Collector that can reach the MongoDB instances
- A MongoDB monitoring user with
clusterMonitorpermissions - File access to the MongoDB logs (structured logging recommended) if you plan to collect logs
- Access to modify the KubeSense Helm values file
Collecting MongoDB Metrics
Step 1: Update KubeSense Helm Values File
Add the MongoDB receiver configuration to your KubeSense Helm values file inside the kubesensor section:
otel-collector:
config:
receivers:
mongodb:
hosts:
- endpoint: <MONGODB_HOST>:27017
# Provide credentials for the monitoring user if authentication is enabled
username: <username>
password: <password>
collection_interval: 60s
tls:
insecure: true
insecure_skip_verify: true
metrics:
mongodb.lock.acquire.count:
enabled: true
mongodb.lock.acquire.time:
enabled: true
mongodb.lock.deadlock.count:
enabled: true
mongodb.operation.latency.time:
enabled: true
appendToPipelines:
metrics:
receivers:
- mongodbNote: If you are monitoring a replica set or sharded deployment, list each endpoint under hosts. For TLS-enabled clusters, replace the tls block with your CA and client certificate paths.
Step 2: Apply the Configuration
Upgrade your KubeSense deployment to load the updated values:
helm upgrade kubesense kubesense/kubesense \
-f values.yaml \
--namespace kubesenseCollecting MongoDB Logs
Step 1: Enable Log Collection in Helm Values
Configure the filelog receiver to parse structured MongoDB logs:
otel-collector:
config:
receivers:
filelog/mongodb:
include: ["/var/log/mongodb/mongodb.log"]
operators:
- type: json_parser
if: body matches '^{.*}$'
parse_from: body
parse_to: attributes
timestamp:
parse_from: attributes.t.$$date
layout: '2006-01-02T15:04:05.000-07:00'
layout_type: gotime
severity:
parse_from: attributes.s
overwrite_text: true
mapping:
debug: [D1, D2, D3, D4, D5]
info: I
warn: W
error: E
fatal: F
- type: flatten
if: attributes.attr != nil
field: attributes.attr
- type: move
if: attributes.msg != nil
from: attributes.msg
to: body
- type: move
if: attributes.c != nil
from: attributes.c
to: attributes.component
- type: move
if: attributes.id != nil
from: attributes.id
to: attributes.mongo_log_id
- type: remove
if: attributes.t != nil
field: attributes.t
- type: remove
if: attributes.s != nil
field: attributes.s
- type: add
field: attributes.source
value: mongodb
processors:
batch/mongodb:
send_batch_size: 10000
send_batch_max_size: 11000
timeout: 10s
service:
pipelines:
logs/mongodb:
receivers:
- filelog/mongodb
processors:
- batch
- resource
exporters:
- otlphttp/kubesense_logsNote: Update the include path to match your MongoDB log file location. Structured logging must be enabled (systemLog.component.control: { verbosity: 1 }) for the parser to work correctly.
Step 2: Apply the Configuration
Redeploy KubeSense with the updated values:
helm upgrade kubesense kubesense/kubesense \
-f values.yaml \
--namespace kubesenseMongoDB Metrics Collected
KubeSense captures the following key MongoDB metrics:
| Metric Name | Type | Unit | Description |
|---|---|---|---|
| mongodb_connection_count | sum | number | Total number of client connections |
| mongodb_operation_count | sum | number | Total operations executed |
| mongodb_operation_latency_time | gauge | microseconds | Operation latency |
| mongodb_lock_acquire_count | sum | number | Number of lock acquisitions |
| mongodb_lock_acquire_wait_count | sum | number | Lock waits due to contention |
| mongodb_lock_acquire_time | sum | microseconds | Time spent waiting for locks |
| mongodb_memory_usage | sum | bytes | MongoDB process memory usage |
| mongodb_data_size | sum | bytes | Data size across databases |
| mongodb_index_size | sum | bytes | Total index size |
| mongodb_uptime | sum | milliseconds | MongoDB server uptime |
MongoDB Log Attributes
| Name | Path | Type | Description |
|---|---|---|---|
| Timestamp | timestamp | timestamp | Log entry timestamp |
| Severity Text | severity_text | string | Parsed severity level |
| Severity Number | severity_number | number | Numeric severity |
| Component | attributes.component | string | MongoDB subsystem |
| Message | body | string | Log message content |
| Log ID | attributes.mongo_log_id | string | MongoDB log identifier |
Visualization in KubeSense
Once the pipelines are active, MongoDB metrics and logs appear in KubeSense dashboards and the log explorer, enabling you to:
- Track connections, latency, and lock contention
- Monitor storage growth and memory usage
- Investigate operational and audit events from structured logs
- Build alerts for key MongoDB health indicators
Configuration Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
hosts[].endpoint | string | Yes | MongoDB host and port |
username | string | Conditional | Monitoring user name when auth is enabled |
password | string | Conditional | Monitoring user password when auth is enabled |
collection_interval | string | No | Metrics polling interval (60s default) |
tls.* | string/boolean | No | TLS connection options |
include (logs) | array | Yes | Log file paths accessible to the collector |
Troubleshooting
Issue: Metrics not collected
- Verify the OTEL collector can reach each MongoDB host
- Ensure the monitoring user has
clusterMonitorprivileges - Check OTEL collector logs for authentication or TLS errors
Issue: Logs missing or unparsed
- Confirm MongoDB structured logging is enabled
- Verify the log file path and permissions inside the collector pod
- Review the OTEL collector logs for
filelogparser errors
Issue: High lock wait counts
- Investigate workload contention and slow operations in MongoDB
- Consider building alerts on
mongodb_lock_acquire_wait_countto detect bottlenecks
Best Practices
- Isolate MongoDB metrics pipelines if monitoring multiple clusters
- Rotate MongoDB log files and ensure the collector can follow rotations
- Use resource attributes to label clusters (
kubesense.cluster,kubesense.env_type) - Regularly review collector logs for API or parser warnings
Conclusion
Integrating MongoDB with KubeSense provides unified visibility into database performance, capacity, and operational events, helping teams troubleshoot faster and maintain healthy clusters.