MongoDB Metrics and Logs

Overview

This integration helps you monitor key MongoDB metrics and logs, view them with an out-of-the-box dashboard, and parse MongoDB logs for better querying and aggregation.

Prerequisites

Before you begin, ensure you have:

  1. A MongoDB server running version 4.4 or newer
  2. An OpenTelemetry (OTEL) Collector with access to the MongoDB server
    • Install the OTEL Collector (v0.88.0 or newer) if not done already
    • Ensure you can provide config files and set environment variables for the collector
    • Ensure the OTEL collector has access to the MongoDB server for metrics collection and can read the MongoDB log file for logs collection

Collecting MongoDB Metrics

Step 1: Create the Collector Config File

Create a file named mongodb-metrics-collection-config.yaml with the following content:

receivers:
  mongodb:
    # - 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.
    hosts:
      - endpoint: ${env:MONGODB_ENDPOINT}
    # If authentication is required, the user can with clusterMonitor permissions can be provided here
    username: ${env:MONGODB_USERNAME}
    # If authentication is required, the password can be provided here.
    password: ${env:MONGODB_PASSWORD}
    collection_interval: 60s
    # If TLS is enabled, the following fields can be used to configure the connection
    tls:
      insecure: true
      insecure_skip_verify: true
    #   ca_file: /etc/ssl/certs/ca-certificates.crt
    #   cert_file: /etc/ssl/certs/mongodb.crt
    #   key_file: /etc/ssl/certs/mongodb.key
    metrics:
      mongodb.lock.acquire.count:
        enabled: true
      mongodb.lock.acquire.time:
        enabled: true
      mongodb.lock.acquire.wait_count:
        enabled: true
      mongodb.lock.deadlock.count:
        enabled: true
      mongodb.operation.latency.time:
        enabled: true

processors:
  # enriches the data with additional host information
  # see https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/resourcedetectionprocessor#resource-detection-processor
  resourcedetection/system:
    # add additional detectors if needed
    detectors: ["system"]
    system:
      hostname_sources: ["os"]

exporters:
  # export to SigNoz cloud
  otlp/mongodb:
    endpoint: "${env:OTLP_DESTINATION_ENDPOINT}"
    tls:
      insecure: false
    headers:
      "signoz-ingestion-key": "${env:SIGNOZ_INGESTION_KEY}"

  # export to local collector
  # otlp/mongodb:
  #   endpoint: "localhost:4317"
  #   tls:
  #     insecure: true

service:
  pipelines:
    metrics/mongodb:
      receivers: [mongodb]
      # note: remove this processor if the collector host is not running on the same host as the mongo instance
      processors: [resourcedetection/system]
      exporters: [otlp/mongodb]

Step 2: Set Environment Variables

Set the following environment variables:

# MongoDB endpoint reachable from the otel collector
export MONGODB_ENDPOINT="host:port"

# MongoDB monitoring user credentials
export MONGODB_USERNAME="monitoring"
export MONGODB_PASSWORD="your_secure_password"

# region specific SigNoz cloud ingestion endpoint
export OTLP_DESTINATION_ENDPOINT="ingest.us.signoz.cloud:443"

# your SigNoz ingestion key
export SIGNOZ_INGESTION_KEY="signoz-ingestion-key"

You can find more details about ingestion keys and Regions here

Step 3: Use the Collector Config File

Add the following flag to your collector run command:

--config mongodb-metrics-collection-config.yaml

Note: The collector can use multiple config files by specifying multiple --config flags.

Collecting MongoDB Logs

Step 1: Create the Collector Config File

Create a file named mongodb-logs-collection-config.yaml with the following content:

receivers:
  filelog/mongodb:
    include: ["${env:MONGODB_LOG_FILE}"]
    operators:
      # Parse structured mongodb logs
      # For more details, see https://www.mongodb.com/docs/manual/reference/log-messages/#structured-logging
      - type: json_parser
        if: body matches '^\\s*{\\s*".*}\\s*$'
        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:
    send_batch_size: 10000
    send_batch_max_size: 11000
    timeout: 10s

exporters:
  # export to SigNoz cloud
  otlp/mongodb-logs:
    endpoint: "${env:OTLP_DESTINATION_ENDPOINT}"
    tls:
      insecure: false
    headers:
      "signoz-ingestion-key": "${env:SIGNOZ_INGESTION_KEY}"

  # export to local collector
  # otlp/mongodb-logs:
  #   endpoint: "localhost:4317"
  #   tls:
  #     insecure: true


service:
  pipelines:
    logs/mongodb:
      receivers: [filelog/mongodb]
      processors: [batch]
      exporters: [otlp/mongodb-logs]

Step 2: Set Environment Variables

Set the following environment variables:

# path of MongoDB server log file. must be accessible by the otel collector
export MONGODB_LOG_FILE="/var/log/mongodb/mongodb.log"

# region specific SigNoz cloud ingestion endpoint
export OTLP_DESTINATION_ENDPOINT="ingest.us.signoz.cloud:443"

# your SigNoz ingestion key
export SIGNOZ_INGESTION_KEY="signoz-ingestion-key"

You can find more details about ingestion keys and Regions here

Step 3: Use the Collector Config File

Add the following flag to your collector run command:

--config mongodb-logs-collection-config.yaml

Note: The collector can use multiple config files by specifying multiple --config flags in the collector run command.

Connect MongoDB

Once you're done with setting up MongoDB for collecting metrics and logs, head over to the integrations tab in SigNoz and search for the MongoDB integration.

Search for MongoDB in Integrations tab
Search for MongoDB in Integrations tab

Click on the Connect Mongo Button, and select I have already configured, this will start listening for data from your MongoDB instance. To stop this, you can select the Remove from SigNoz button.

Connect MongoDB
Connect MongoDB

 

Listening for data from MongoDB
Listening for data from MongoDB

MongoDB Dashboard

Once SigNoz has started listening to your MongoDB data, head over to the Dashboards tab and search for Mongo. This will show you a newly created dashboard which displays various MongoDB metrics.

Dashboard for monitoring MongoDB Metrics
Dashboard for monitoring MongoDB Metrics

Dashboard asset

You can also manually create the above MongoDB Dashboard by importing the JSON file available here. To learn how to create Dashboards, checkout this documentation.

Data Collected

When you switch to the Data Collected tab of your MongoDB Integration, it shows you details about the different logs attributes and the metrics types that you can monitor for your MongoDB instance. The tables below give you a list of the different logs attributes and metrics available.

Dashboard for monitoring MongoDB Metrics
Dashboard for monitoring MongoDB Metrics

MongoDB log attributes

  • Name: The name of the log attribute.
  • Path: The specific location or attribute within a log entry where the corresponding data can be found.
  • Type: The data type of the log attribute.
NamePathType
Timestamptimestamptimestamp
Severity Textseverity_textstring
Severity Numberseverity_numbernumber
MongoDB Componentattributes.componentstring

MongoDB metrics

  • Name: The name of the metric.
  • Type: The type of the metric (e.g., Sum, Gauge).
  • Unit: The unit of measurement for the metric.
  • Description: A brief description of what the metric represents.
NameTypeUnitDescription
mongodb_cache_operationsSumnumberThe number of cache operations of the instance.
mongodb_collection_countSumnumberThe number of collections.
mongodb_data_sizeSumBytesThe size of the collection. Data compression does not affect this value.
mongodb_connection_countSumnumberThe number of connections.
mongodb_extent_countSumnumberThe number of extents.
mongodb_global_lock_timeSummillisecondsThe time the global lock has been held.
mongodb_index_countSumnumberThe number of indexes.
mongodb_index_sizeSumBytesSum of the space allocated to all indexes in the database, including free index space.
mongodb_memory_usageSumBytesThe amount of memory used.
mongodb_object_countSumnumberThe number of objects.
mongodb_operation_latency_timeGaugemicrosecondsThe latency of operations.
mongodb_operation_countSumnumberThe number of operations executed.
mongodb_operation_repl_countSumnumberThe number of replicated operations executed.
mongodb_storage_sizeSumBytesThe total amount of storage allocated to this collection.
mongodb_database_countSumnumberThe number of existing databases.
mongodb_index_access_countSumnumberThe number of times an index has been accessed.
mongodb_document_operation_countSumnumberThe number of document operations executed.
mongodb_network_io_receiveSumBytesThe number of bytes received.
mongodb_network_io_transmitSumBytesThe number of bytes transmitted.
mongodb_network_request_countSumnumberThe number of requests received by the server.
mongodb_operation_timeSummillisecondsThe total time spent performing operations.
mongodb_session_countSumnumberThe total number of active sessions.
mongodb_cursor_countSumnumberThe number of open cursors maintained for clients.
mongodb_cursor_timeout_countSumnumberThe number of cursors that have timed out.
mongodb_lock_acquire_countSumnumberNumber of times the lock was acquired in the specified mode.
mongodb_lock_acquire_wait_countSumnumberNumber of times the lock acquisitions encountered waits because the locks were held in a conflicting mode.
mongodb_lock_acquire_timeSummicrosecondsCumulative wait time for the lock acquisitions.
mongodb_lock_deadlock_countSumnumberNumber of times the lock acquisitions encountered deadlocks.
mongodb_healthGaugenumberThe health status of the server.
mongodb_uptimeSummillisecondsThe amount of time that the server has been running.

Was this page helpful?