liveBook · Manning

Prometheus

As explained in Learn Docker in a Month of Lunches, Second Edition

Prometheus is an open-source monitoring system and time-series database that collects and queries numerical measurements.

Containers can appear, disappear, and move, so a fixed list of servers is not enough to monitor an application. Prometheus runs in a container and collects numerical metrics from application containers and the Docker Engine. Its web UI lets you find metrics and query them; Grafana can turn those queries into dashboards. section 9.1 section 9.4

To get metrics flowing:

  • Expose them. Add a Prometheus client library and a metrics HTTP endpoint to each application component. Clients can report runtime measurements; application code can also record custom counters and gauges. section 9.2
  • Configure scraping. Prometheus pulls metrics from named endpoints on a schedule. In Docker Compose, targets can use service names resolved through Docker DNS. section 9.3
  • Check the results. Open the application’s metrics endpoint, then use the Prometheus UI to find measurements and run queries before adding them to a Grafana dashboard. section 9.2 section 9.4
Listing 9.2 Configuring Prometheus to scrape application metrics
global:
  scrape_interval: 10s

scrape_configs:
  - job_name: "image-gallery"
    metrics_path: /metrics
    static_configs:
      - targets: ["image-gallery"]

  - job_name: "iotd-api"
    metrics_path: /actuator/prometheus
    static_configs:
      - targets: ["iotd"]

  - job_name: "access-log"
    metrics_path: /metrics
    dns_sd_configs:
      - names:
          - accesslog
        type: A
        port: 80

This sample configuration scrapes three services every 10 seconds. It shows both static targets and DNS service discovery, plus a component whose metrics use /actuator/prometheus rather than /metrics. section 9.3

Count requests across containers

Scale the access-log service to three containers and send requests to the application. Query `sum(access_log_total) without(hostname, instance)` to see their combined count. For scaled services, use DNS service discovery so Prometheus scrapes every container: a static target resolves only one address and can give misleading results. section 9.3

For hands-on practice, run an application with Prometheus and Grafana in Docker Compose. Configure scraping, then build a dashboard for tasks created, total HTTP requests, and requests currently in progress. section 9.6

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Walks through metrics endpoints, scheduled scraping, target discovery and basic queries; leaves application-specific metric choices and client-library mechanics to the runtime.
Examples
Uses an image gallery with runtime and request metrics, a scaled access-log service, and a to-do application monitoring lab.
What it assumes
Assumes familiarity with running containers and Docker Compose while introducing metrics exposure and scraping through examples.
What you can do after
Operate/configure it with Docker Compose to scrape application metrics, check queries and supply Grafana dashboards.
How it connects
Connects Docker Compose, service discovery, Prometheus client libraries, PromQL and Grafana dashboards.

Also explained in

Compare how 8 books explain Prometheus →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.