Skip to content
Last9
Book demo

MongoDB Atlas

Monitor MongoDB Atlas clusters with metrics, logs, slow query extraction, and memory analysis using OpenTelemetry with Last9

Use OpenTelemetry to instrument your MongoDB Atlas databases and send telemetry data to Last9. This integration collects metrics, logs, events, and extracts slow query details into structured attributes for analysis.

Prerequisites

Before setting up MongoDB Atlas monitoring, ensure you have:

MongoDB Atlas API Access

The OpenTelemetry Collector uses the Atlas Administration API (not database credentials) to pull metrics and logs. You need to create a programmatic API key pair:

  1. In MongoDB Atlas, go to ProjectAccess ManagerAPI KeysCreate API Key
  2. Set a description (e.g., last9-monitoring) and select the Project Data Access Read Only role

Create API Key with Project Read Only role

  1. Click Next to generate the key pair. Copy both keys immediately — the private key is shown only once

Copy the Public Key and Private Key

  1. Optionally add your collector’s IP to the API Access List (or leave empty to allow all IPs)
  2. Click Done

Required Permissions by feature:

FeatureMinimum Role
Metrics onlyProject Data Access Read Only + Project Observability Viewer
Metrics + host logsProject Data Access Read Only + Project Observability Viewer
Project eventsProject Read Only
Organization eventsOrganization Member
Access logsProject Owner or Organization Owner
Audit logsProject Data Access Read Only + audit logging enabled

Enable Required Features

  1. Database Auditing (optional): Enable in MongoDB Atlas Project Settings if you want to collect audit logs
  2. Monitoring API: Ensure monitoring APIs are accessible for your project
  3. Project Configuration: Note your Project Names and Cluster Names for configuration
  1. Install OpenTelemetry Collector

    sudo apt-get update
    sudo apt-get -y install wget systemctl
    wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.128.0/otelcol-contrib_0.128.0_linux_amd64.deb
    sudo dpkg -i otelcol-contrib_0.128.0_linux_amd64.deb
  2. Configure OpenTelemetry Collector

    Use the following configuration for collecting MongoDB Atlas metrics:

    receivers:
    mongodbatlas:
    public_key: "${env:MONGODB_ATLAS_PUBLIC_KEY}"
    private_key: "${env:MONGODB_ATLAS_PRIVATE_KEY}"
    granularity: "PT1M"
    collection_interval: 3m
    projects:
    - name: "YOUR_PROJECT_NAME"
    include_clusters: ["YOUR_CLUSTER_NAME"]
    metrics:
    mongodbatlas.process.cache.ratio:
    enabled: true
    retry_on_failure:
    enabled: true
    initial_interval: 5s
    max_interval: 30s
    max_elapsed_time: 5m
    processors:
    batch:
    timeout: 5s
    send_batch_size: 10000
    send_batch_max_size: 10000
    resourcedetection/system:
    detectors: ["system"]
    system:
    hostname_sources: ["os"]
    resourcedetection/cloud:
    detectors: ["gcp", "ec2", "azure"]
    exporters:
    otlp/last9:
    endpoint: "{{ .Logs.WriteURL }}"
    headers:
    "Authorization": "{{ .Logs.AuthValue }}"
    debug:
    verbosity: detailed
    service:
    pipelines:
    metrics:
    receivers: [mongodbatlas]
    processors: [batch, resourcedetection/system, resourcedetection/cloud]
    exporters: [otlp/last9]
  3. Environment Variables

    Set the following environment variables before starting the collector:

    export MONGODB_ATLAS_PUBLIC_KEY="your_public_key"
    export MONGODB_ATLAS_PRIVATE_KEY="your_private_key"
  4. Start Collector

    otelcol-contrib --config /etc/otelcol-contrib/config.yaml

Memory Metrics

The Atlas receiver collects memory metrics at both the MongoDB process and system level. These OTLP metrics are converted to Prometheus format in Last9:

OTLP MetricLast9 MetricDescriptionLabels
mongodbatlas.process.memory.usagemongodbatlas_process_memory_usage_bytesMongoDB process memorymemory_state: resident, virtual, mapped, computed
mongodbatlas.system.memory.usage.averagemongodbatlas_system_memory_usage_average_kibibytesSystem memory averagememory_status: used, free, available, buffers, cached, shared
mongodbatlas.system.memory.usage.maxmongodbatlas_system_memory_usage_max_kibibytesPeak system memorymemory_status: same as above
mongodbatlas.process.cache.sizemongodbatlas_process_cache_size_bytesWiredTiger cachecache_status: dirty, used
mongodbatlas.process.cache.iomongodbatlas_process_cache_io_bytesWiredTiger cache throughputcache_direction: read_into, written_from
mongodbatlas.process.cache.ratiomongodbatlas_process_cache_ratio_percentWiredTiger cache fill and dirty ratios (requires explicit enable)cache_ratio_type: cache_fill, dirty_fill

Calculating memory usage percentage in Last9:

# MongoDB process memory as % of total system memory
mongodbatlas_process_memory_usage_bytes{memory_state="resident"}
/ on()
(mongodbatlas_system_memory_usage_average_kibibytes{memory_status="used"} + mongodbatlas_system_memory_usage_average_kibibytes{memory_status="free"})
/ 1024 * 100

Slow Query Monitoring

MongoDB Atlas host logs contain slow query entries in structured JSON format (MongoDB 4.4+). The transform/slow_queries processor extracts these fields into structured log attributes:

AttributeSource FieldDescription
db.namespaceattr.nsDatabase and collection (e.g., mydb.users)
db.operation.duration_msattr.durationMillisTotal execution time in milliseconds
db.plan_summaryattr.planSummaryExecution plan (COLLSCAN, IXSCAN, etc.)
db.keys_examinedattr.keysExaminedNumber of index keys scanned
db.docs_examinedattr.docsExaminedNumber of documents scanned
db.rows_affectedattr.nreturnedNumber of documents returned
db.query_hashattr.queryHashHash identifying the query shape
db.systemAlways set to mongodb
slow_queryAlways set to true for slow query logs

Slow queries (> 100ms) are automatically elevated to WARN severity. To adjust the slow query threshold in Atlas, go to Project Settings → Advanced and set the Slow Operation Threshold (default: 100ms).

Configuration Parameters

Required Parameters

  • public_key: MongoDB Atlas API public key
  • private_key: MongoDB Atlas API private key

Optional Parameters

  • base_url: (default: https://cloud.mongodb.com/) MongoDB Atlas base URL
  • granularity: (default: PT1M) Metrics granularity (see MongoDB Atlas documentation)
  • collection_interval: (default: 3m) How often to collect metrics
  • storage: Component ID of storage extension for preventing data duplication

Projects Configuration

  • name: Name of the MongoDB Atlas project
  • include_clusters: List of specific clusters to monitor (optional)
  • exclude_clusters: List of clusters to exclude from monitoring (optional)

Logs Configuration

  • enabled: Enable log collection
  • collect_host_logs: (default: true) Collect MongoDB host logs
  • collect_audit_logs: (default: false) Collect audit logs (requires enabled audit logging)

Events Configuration

  • projects: List of projects to collect events from
  • organizations: List of organization IDs to collect events from
  • poll_interval: (default: 1m) How often to poll for events
  • page_size: (default: 100) Number of events per API request
  • max_pages: (default: 25) Maximum pages to request per poll

Access Logs Configuration

  • enabled: Enable access log collection
  • auth_result: Filter by authentication result (true/false/unspecified)
  • page_size: (default: 20000) Number of access logs per API request
  • max_pages: (default: 10) Maximum pages to request per poll
  • poll_interval: (default: 5m) How often to poll for access logs

Important Notes

  1. Collector Version: Pin the collector to v0.128.0. Versions 0.129.0 through 0.144.0 have a regression where the metrics scraper silently fails to start — logs work fine, but no metrics are collected. This is due to an internal API change (scraperhelper.AddScraperscraperhelper.AddMetricsScraper).

  2. API Key Roles: For metrics collection, your API key needs both Project Data Access Read Only (for log downloads) and Project Observability Viewer (for metrics). Without the Observability Viewer role, metrics collection silently produces nothing.

  3. Atlas Tier: The free tier (M0) does not support host log download or full monitoring APIs. Use M10+ clusters for complete metrics and log collection.

  4. API Rate Limits: MongoDB Atlas has API rate limits. The recommended polling interval is 5 minutes for logs and metrics.

  5. Storage Extension: Use a storage extension to prevent data duplication after collector restarts, especially for events, alerts, and access logs.

  6. Project and Cluster Names: Replace YOUR_PROJECT_NAME, YOUR_CLUSTER_NAME, and YOUR_ORGANIZATION_ID with your actual MongoDB Atlas project names, cluster names, and organization ID.

  7. Audit Logs: Audit logging must be explicitly enabled in your MongoDB Atlas project settings to collect audit logs.

  8. Access Logs: Access log collection requires Project Owner or Organization Owner permissions.

Verification

To verify the setup is working:

  1. Check that the collector service is running
  2. Review the collector logs for any errors
  3. Log into your Last9 account to confirm MongoDB Atlas metrics are being received

Need Help?

If you encounter any issues or have questions: