Kubesense

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:

  1. A MongoDB deployment running version 4.4 or newer (standalone, replica set, or sharded cluster)
  2. KubeSense installed with an OTEL Collector that can reach the MongoDB instances
  3. A MongoDB monitoring user with clusterMonitor permissions
  4. File access to the MongoDB logs (structured logging recommended) if you plan to collect logs
  5. 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:
        - mongodb

Note: 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 kubesense

Collecting 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_logs

Note: 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 kubesense

MongoDB Metrics Collected

KubeSense captures the following key MongoDB metrics:

Metric NameTypeUnitDescription
mongodb_connection_countsumnumberTotal number of client connections
mongodb_operation_countsumnumberTotal operations executed
mongodb_operation_latency_timegaugemicrosecondsOperation latency
mongodb_lock_acquire_countsumnumberNumber of lock acquisitions
mongodb_lock_acquire_wait_countsumnumberLock waits due to contention
mongodb_lock_acquire_timesummicrosecondsTime spent waiting for locks
mongodb_memory_usagesumbytesMongoDB process memory usage
mongodb_data_sizesumbytesData size across databases
mongodb_index_sizesumbytesTotal index size
mongodb_uptimesummillisecondsMongoDB server uptime

MongoDB Log Attributes

NamePathTypeDescription
TimestamptimestamptimestampLog entry timestamp
Severity Textseverity_textstringParsed severity level
Severity Numberseverity_numbernumberNumeric severity
Componentattributes.componentstringMongoDB subsystem
MessagebodystringLog message content
Log IDattributes.mongo_log_idstringMongoDB 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

ParameterTypeRequiredDescription
hosts[].endpointstringYesMongoDB host and port
usernamestringConditionalMonitoring user name when auth is enabled
passwordstringConditionalMonitoring user password when auth is enabled
collection_intervalstringNoMetrics polling interval (60s default)
tls.*string/booleanNoTLS connection options
include (logs)arrayYesLog 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 clusterMonitor privileges
  • 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 filelog parser errors

Issue: High lock wait counts

  • Investigate workload contention and slow operations in MongoDB
  • Consider building alerts on mongodb_lock_acquire_wait_count to 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.