epok
View as Markdown

MongoDB

The OpenTelemetry Collector reads MongoDB metrics with its MongoDB receiver and tails the server log with the filelog receiver, then sends both to Epok over OTLP/HTTP. Run it on the database host or next to it. Tested with MongoDB 7.0 over TLS (CA-verified) and collector 0.162.0.

1. Create a monitoring user

clusterMonitor reads serverStatus, dbStats and replica set status. It cannot read documents.

javascript
db.getSiblingDB("admin").createUser({
  user: "epok_monitor", pwd: passwordPrompt(),
  roles: [{ role: "clusterMonitor", db: "admin" }]
})

2. Turn on the logs you want

The config reads the JSON log at systemLog.path. Operations slower than slowOpThresholdMs (default 100 ms) are logged as "Slow query" with duration_ms, mongodb_namespace and mongodb_op_type.

yaml
systemLog:
  destination: file
  path: /var/log/mongodb/mongod.log
  logAppend: true
operationProfiling:
  slowOpThresholdMs: 100

The collector's user must be able to read the log files. On most installs that means adding it to the database's group. Change the include: paths if your logs live elsewhere.

Personal data: statement text can contain email addresses, names or tokens. The file replaces every literal in a logged statement with ? on your host, before anything is sent. Values that the database writes into an error message itself are not rewritten, so leave statement logging off where that matters.

3. Run the collector

Install otel/opentelemetry-collector-contrib:0.162.0 (or the matching otelcol-contrib package). Put EPOK_INGEST_KEY (an ingest-only key from Settings, then API Keys) and MONGODB_MONITOR_PASSWORDin the collector's environment file, mode 0600, typed at a prompt that does not echo. Neither goes in the config file or on a command line.

bash
read -rsp 'Epok ingest key: ' K; echo; read -rsp 'Monitoring password: ' P; echo
printf 'EPOK_INGEST_KEY=%s\nMONGODB_MONITOR_PASSWORD=%s\n' "$K" "$P" | sudo tee -a /etc/otelcol-contrib/otelcol-contrib.conf >/dev/null
unset K P
sudo chmod 600 /etc/otelcol-contrib/otelcol-contrib.conf

The connection is TLS with the server certificate verified: set endpoint to the server name in its certificate and ca_file to the CA that signed it. Plain TCP is only for a collector on the database host; the file shows those localhost lines as a comment. The same file is in the app under Connect.

yaml
# otelcol-mongodb.yaml: MongoDB metrics + server log (incl. slow operations) to Epok
# Collector: otel/opentelemetry-collector-contrib:0.162.0 (pinned; tested with MongoDB 7.0)
#
# Secrets never go in this file or on a command line. Put them in the collector's
# environment file (otelcol-contrib package: /etc/otelcol-contrib/otelcol-contrib.conf,
# or an EnvironmentFile= of your own, mode 0600):
#   read -rsp 'Epok ingest key: ' K; echo; read -rsp 'Monitoring password: ' P; echo
#   printf 'EPOK_INGEST_KEY=%s\nMONGODB_MONITOR_PASSWORD=%s\n' "$K" "$P" | sudo tee -a /etc/otelcol-contrib/otelcol-contrib.conf >/dev/null
#   unset K P
#
# Monitoring user (no data access); mongosh prompts for the password:
#   db.getSiblingDB("admin").createUser({user: "epok_monitor", pwd: passwordPrompt(),
#     roles: [{role: "clusterMonitor", db: "admin"}]})
#
# The log pipeline keeps the message, namespace, op type and duration of slow operations
# and DROPS the rest of `attr` (filters, sort keys and plan summaries carry your data).

receivers:
  mongodb:
    hosts:
      - endpoint: db.example.internal:27017   # the server's name as it appears in its TLS certificate
    username: epok_monitor
    password: ${env:MONGODB_MONITOR_PASSWORD}
    collection_interval: 30s
    tls:
      insecure: false            # TLS on and the server certificate verified
      ca_file: /etc/otelcol-contrib/db-ca.pem   # the CA that signed the server certificate
      # LOCALHOST ONLY (collector on the database host, server without TLS):
      #   endpoint: localhost:27017
      #   tls: {insecure: true}
    metrics:
      mongodb.uptime: {enabled: true}
      mongodb.operation.latency.time: {enabled: true}
      mongodb.operation.repl.count: {enabled: true}
      mongodb.lock.acquire.count: {enabled: true}
      mongodb.active.reads: {enabled: true}
      mongodb.active.writes: {enabled: true}
      mongodb.wtcache.bytes.read: {enabled: true}
      mongodb.page_faults: {enabled: true}
    resource_attributes:          # tell instances apart when one host runs several
      server.address: {enabled: true}
      server.port: {enabled: true}

  # Server log (systemLog.path; JSON since MongoDB 4.4). Slow operations are
  # logged with msg "Slow query" once they exceed operationProfiling.slowOpThresholdMs (default 100).
  filelog/mongodb:
    include: [/var/log/mongodb/mongod.log]
    start_at: end
    operators:
      - type: json_parser
        on_error: send
        severity:
          parse_from: attributes.s
          mapping:
            fatal: F
            error: E
            warn: W
            info: I
            debug: [D1, D2, D3, D4, D5]
      - type: move
        if: 'attributes.attr?.durationMillis != nil'
        from: attributes.attr.durationMillis
        to: attributes.duration_ms
      - type: move
        if: 'attributes.attr?.ns != nil'
        from: attributes.attr.ns
        to: attributes.mongodb_namespace
      - type: move
        if: 'attributes.attr?.type != nil'
        from: attributes.attr.type
        to: attributes.mongodb_op_type
      - type: move
        if: 'attributes.msg != nil'
        from: attributes.msg
        to: body
      - type: move
        if: 'attributes.c != nil'
        from: attributes.c
        to: attributes.mongodb_component
      - type: move
        if: 'attributes.id != nil'
        from: attributes.id
        to: attributes.mongodb_log_id
      - type: remove
        if: 'attributes.attr != nil'
        field: attributes.attr
      - type: remove
        if: 'attributes.t != nil'
        field: attributes.t
      - type: remove
        if: 'attributes.s != nil'
        field: attributes.s
      - type: add
        field: attributes.db_system
        value: mongodb

processors:
  batch:
    send_batch_max_size: 10000   # at or under Epok's 10,000 entries per batch
  resourcedetection:
    detectors: [env, system]
    system:
      hostname_sources: [os]
  resource/mongodb:
    attributes:
      - key: service.name
        value: mongodb
        action: upsert

exporters:
  otlphttp/epok:
    metrics_endpoint: https://ingest.epok.cloud/v1/metrics
    logs_endpoint: https://ingest.epok.cloud/v1/logs
    compression: gzip
    encoding: json
    headers:
      x-api-key: ${env:EPOK_INGEST_KEY}

service:
  pipelines:
    metrics:
      receivers: [mongodb]
      processors: [resourcedetection, resource/mongodb, batch]
      exporters: [otlphttp/epok]
    logs:
      receivers: [filelog/mongodb]
      processors: [resourcedetection, resource/mongodb, batch]
      exporters: [otlphttp/epok]

4. Check that data arrives

Metrics arrive within a minute (one point every 30 seconds). In Metrics, search for any of these names:

mongodb_connection_count, mongodb_operation_count, mongodb_operation_time, mongodb_cache_operations, mongodb_document_operation_count, mongodb_memory_usage, mongodb_cursor_count

Logs are searchable in Explore with these queries:

  • service:mongodb -record_type:metric "Slow query": slow operations with duration_ms and the namespace
  • service:mongodb -record_type:metric level:(error OR fatal OR warn): errors and warnings
  • service:mongodb -record_type:metric: every server log line (-record_type:metric leaves out the metric points, which are also stored with service:mongodb)

Dashboard

When database boards are on for your workspace, Settings, then Setup, then Quickstarts installs the MongoDB board: connections, throughput, latency or slow queries, cache hit ratio, replication and errors. Without it, every metric and log above is searchable and can be charted on your own dashboards.

Limits

  • Replica set lag is not on the board: the receiver reports replicated operations, not lag.
  • On a standalone server the receiver logs that replica set status is unavailable each scrape; metrics are still collected.
  • No query samples, normalized query statistics or explain plans. Epok reads server metrics and logs only.

OpenTelemetry CollectorAll install guides