About metrics and cardinality#

The metrics endpoint is small on the surface and opinionated underneath. This page explains the choices that shape what it measures and how. It covers label cardinality, metric scope, the cache in front of the expensive metrics, the memory metrics, and the two ZODB monitors.

Why user identity is never a label#

Prometheus stores one time series per unique combination of label values. A label can draw its value from an unbounded set, such as a user id, a session id, or a full URL with query string. Such a label multiplies the series count without bound. This is the single most common way to make a Prometheus server fall over. It is hard to undo once the data is in.

To avoid that, plone.observability keeps labels low-cardinality on purpose. Request metrics split only by auth="authenticated" or auth="anonymous", which is two values. User identity, when you want it at all, belongs on a trace span. Each span is a separate record. High cardinality is the norm there. That is why enduser.id is a span attribute gated behind PLONE_OBSERVABILITY_OTEL_USER_ID, and never a metric label.

Global versus instance scope#

Several Plone instances usually share one ZODB. That makes some numbers per-process and others shared, and aggregating them the same way gives wrong answers.

A metric tagged scope="instance" is local to one process: its request count, its resident memory, its open connections. To get a fleet total you sum across instances.

A metric tagged scope="global" is a property of the shared database: the object count, the content totals. Every instance reports the same value. Summing across instances multiplies it by the number of instances. For global metrics you pick one instance to read from instead.

The scope label exists so that whoever writes the PromQL can tell the two apart without memorizing which metric is which.

Why some metrics are cached#

Most metrics are cheap: read a counter, read a gauge, done. A few are not. The database-wide object count and the database size are full-table operations on a Postgres-backed storage. The content counts walk the catalog indexes of every Plone site. Running them on every scrape, every fifteen or thirty seconds, forever, would put a steady, pointless load on the database.

These metrics sit behind a short time-to-live cache instead, sixty seconds by default, tunable with PLONE_OBSERVABILITY_METRICS_CACHE_TTL. The cache lives at module level, keyed by database name or site id, because @@metrics builds its providers afresh on every scrape. The numbers move slowly enough that a minute of staleness is invisible. The database is spared the repeated full scans.

Where memory grows#

plone_process_rss_bytes tells you that a process grows. It does not tell you where. Finding out used to mean inspecting the heap of a live process, which is slow and risky. It was also only possible after someone noticed the growth. The memory metrics split the resident memory into parts that point at different causes, cheaply enough to collect on every scrape.

plone_process_rss_anon_bytes is the memory the process allocated itself. plone_process_rss_file_bytes is the resident part of mapped files, mostly shared libraries. Growth almost always shows up in the anonymous part.

Inside the anonymous part, Python serves small objects from its own allocator. plone_python_allocated_blocks counts them. Growth there means more Python objects are alive: a cache that does not evict, or objects pinned by a reference nobody expected. Larger objects and most C extensions allocate through malloc, which plone_malloc_inuse_bytes measures. plone_malloc_free_bytes is memory that malloc has taken from the system but does not use. When it grows while the memory in use stays flat, the heap is fragmented. The process is not leaking.

Two leak detectors watch for specific patterns. plone_python_interned_strings grows when code keeps creating new identifiers, for example a template that is compiled again on every request. plone_gc_uncollectable_total counts reference cycles that the garbage collector cannot free. plone_gc_pause_seconds shows how long collections stop the process, which adds directly to request latency.

Connections that outlive the pool#

The ZODB connection pool keeps a few connections, each with its own object cache. When the pool has more than it needs, it releases one and forgets it. Normally nothing else references that connection, and the garbage collector frees it.

If something still references the released connection, it stays in memory. So does every object it loaded, because each object points back to its connection. A stored traceback is enough, since its frames hold the objects a request was working on. The pool no longer counts such a connection. plone_zodb_connections does not show it.

plone_zodb_connections_alive counts every connection that is still in memory. Above plone_zodb_connections, it shows released connections that have not been freed. A short excess after a release is normal. A connection references itself. Only the cyclic garbage collector frees it, which can take a while on a large heap. A leak shows as an excess that persists for hours or keeps growing.

Why the malloc metrics are opt-in#

mallinfo2() is the only way to read the glibc allocator’s numbers. It is not cheap. It walks every free memory chunk while holding the allocator lock. On a heap fragmented into a million free chunks, a call takes about 100 milliseconds. Threads that need memory wait for it. The cost grows with fragmentation, the very condition the metric is meant to show. The package collects these metrics only when you set PLONE_OBSERVABILITY_MALLINFO=1. It caches them like the other expensive metrics.

They also have a blind spot. They cover only glibc malloc. A Rust extension may use its own allocator, such as mimalloc. The anonymous memory that no other metric accounts for is then the part these allocators hold. A jemalloc loaded with LD_PRELOAD replaces malloc entirely. glibc then reports zero for everything, which looks plausible and is wrong. The package detects jemalloc and leaves the malloc metrics out instead.

jemalloc has statistics of its own, and the package reports them as plone_jemalloc_*. Reading them costs well under a millisecond. They need no opt-in. plone_jemalloc_allocated_bytes is what the application asked for. plone_jemalloc_active_bytes is the pages that hold those allocations. The difference is fragmentation inside pages.

Memory growth per request#

The metrics above show that a process grows, and roughly what grows. They do not show which request made it grow. Step-wise growth is the hardest to explain from the outside, and a single page or tile often causes it.

With PLONE_OBSERVABILITY_REQUEST_RSS=1, the package reads the resident set size before and after each request and each tile subrequest. The histogram plone_request_rss_growth_bytes shows whether large steps happen. The span attributes show which page or tile caused them. A trace backend can filter for spans above a threshold. A warning in the log names the path of a request that grew the process by more than a threshold.

The readings are hints, for two reasons. The resident set size belongs to the whole process. With several worker threads, one request’s allocations show up on another request that runs at the same time. The resident set size also counts memory pages. It does not count allocations. A request served from memory that the allocator already holds reads as zero growth. The next request that needs a new page gets the step. For the question of which request grows the heap, that is the right measure. A single large value can be a neighbor’s doing. Repeated large values on the same path or tile are the signal.

The histogram records growth only. A negative observation would make its _sum decrease, and Prometheus treats that as a counter reset. The span attributes keep the sign.

Why the request maximum resets on every scrape#

A histogram bounds a latency only to its bucket edges. Requests of 5.5 and 9.5 seconds land in the same bucket, between 5 and 10 seconds. plone_request_duration_seconds_max tracks the slowest request directly. Reading it resets it. Each scrape reports the worst request of its own window. Two scrapers would split the window between them. Each would miss the peaks the other one read.

Content counts and catalog backends#

The content provider asks each Plone site’s catalog for its portal_type and review_state counts through the ZCatalog index API. That gives per-site numbers only when each site keeps its own catalog data. plone-pgcatalog keeps the data of all sites in one PostgreSQL table. It emulates the index API. The generic provider would still answer. Every site would then report the counts of the whole database under its own label. A backend like that replaces the content provider with one that counts per site, as plone-pgcatalog does.

The ZODB activity monitor#

plone_zodb_loads_total and plone_zodb_stores_total count objects loaded from and stored to the database. They come from a minimal activity monitor that the package installs into the database’s activity-monitor slot on the first metrics scrape. It is storage-agnostic and works the same on FileStorage, RelStorage, and zodb-pgjsonb. It is O(1) in memory.

These two counters let you catch a “loads per request” regression early. A rising rate(plone_zodb_loads_total) / rate(plone_requests_total) usually means a new view is waking up far more objects than it needs.

The monitor is installed only if no activity monitor is already configured. The package never overrides a pre-existing monitor. It logs a warning and leaves the two counters unavailable instead. Set PLONE_OBSERVABILITY_ZODB_ACTIVITY_MONITOR=0 to skip installation entirely.

Conflict metrics#

plone_zodb_conflicts_total counts ZODB ConflictErrors raised during request publication.

A write conflict means two transactions changed the same object concurrently, which points at a write hotspot. A read conflict means an object a transaction required to stay current was changed underneath it, which points at long transactions or readCurrent invariants. Both are counted.

The retry label separates the two outcomes that matter operationally. retry="true" is a conflict that was retried, which usually recovers and is invisible to the user. retry="false" is the final attempt that gave up and surfaced an error. Watching the second one tells you when contention is actually hurting users.

See also