# How to scrape and query metrics with Prometheus This guide shows you how to scrape the `@@metrics` endpoint with Prometheus and query the result with PromQL. It assumes the `observability` filter is wired. See {doc}`/how-to/install`. (scrape-with-prometheus-add-a-scrape-job)= ## Add a scrape job Point Prometheus at the main Zope port with the `@@metrics` path. ```yaml scrape_configs: - job_name: plone static_configs: - targets: ["plone-host:8080"] metrics_path: /@@metrics ``` If you restrict access with `PLONE_OBSERVABILITY_METRICS_ALLOWLIST`, add the Prometheus server's CIDR to the allowlist. See {doc}`/reference/configuration`. (scrape-with-prometheus-query-the-metrics)= ## Query the metrics Total requests across all instances: ```promql sum(plone_requests_total{job="plone"}) ``` Request rate per instance over a five-minute window: ```promql rate(plone_requests_total{job="plone"}[5m]) ``` Approximate median request duration from the histogram: ```promql histogram_quantile(0.5, sum(rate(plone_request_duration_seconds_bucket[5m])) by (le, instance) ) ``` Memory per instance in megabytes: ```promql plone_process_rss_bytes{job="plone"} / 1024 / 1024 ``` (scrape-with-prometheus-read-global-metrics-from-one-instance)= ## Read global metrics from one instance Global metrics report the same value on every instance that shares the ZODB. Summing them double-counts. Query a single instance instead: ```promql plone_zodb_object_count{instance="plone-0:8080", scope="global"} ``` (scrape-with-prometheus-watch-zodb-contention)= ## Watch ZODB contention Overall conflict rate: ```promql rate(plone_zodb_conflicts_total[5m]) ``` Conflicts that gave up after the final retry, the ones users actually felt: ```promql rate(plone_zodb_conflicts_total{retry="false"}[5m]) ``` A loads-per-request smell test that catches views waking up too many objects: ```promql rate(plone_zodb_loads_total[5m]) / rate(plone_requests_total[5m]) ``` (scrape-with-prometheus-find-where-memory-grows)= ## Find where memory grows Growth of the anonymous memory, the part that leaks, over six hours: ```promql delta(plone_process_rss_anon_bytes{job="plone"}[6h]) ``` Growth of live Python objects over the same window: ```promql delta(plone_python_allocated_blocks{job="plone"}[6h]) ``` Interned strings that keep growing point at code that is compiled again and again: ```promql deriv(plone_python_interned_strings{job="plone"}[1h]) > 0 ``` Time spent in garbage collection, as a share of wall-clock time: ```promql rate(plone_gc_pause_seconds_sum{job="plone"}[5m]) ``` With `PLONE_OBSERVABILITY_REQUEST_RSS=1`, the rate of requests that grew the process by more than 16 MiB: ```promql rate(plone_request_rss_growth_bytes_count[5m]) - ignoring(le) rate(plone_request_rss_growth_bytes_bucket{le="16777216"}[5m]) ``` Released ZODB connections that stayed in memory for a whole hour, a sign of connections pinned by a leftover reference: ```promql min_over_time(plone_zodb_connections_alive{database="main"}[1h]) - max_over_time(plone_zodb_connections{database="main"}[1h]) > 0 ``` With `PLONE_OBSERVABILITY_MALLINFO=1`, the share of the glibc heap that is free but not returned to the system: ```promql plone_malloc_free_bytes / (plone_malloc_free_bytes + plone_malloc_inuse_bytes) ``` With jemalloc preloaded, the share of its active pages that holds no allocation: ```promql 1 - plone_jemalloc_allocated_bytes / plone_jemalloc_active_bytes ``` ```{seealso} - {doc}`/reference/metrics` for the full metric list and the scope label. - {doc}`/explanation/metrics-design` for what global versus instance scope means, why the conflict and load counters exist, and how to read the memory metrics. ```