# plone.observability

> Kubernetes-style health probes, a pluggable Prometheus and JSON metrics endpoint, and
> optional OpenTelemetry tracing for Plone running in containers.

plone.observability gives a container orchestrator the signals it needs to run Plone:
separate liveness, readiness, and startup probes on a dedicated port, a `@@metrics`
endpoint, and OpenTelemetry spans. Everything is registered through the Zope Component
Architecture and can be extended or replaced.

## Key Facts

- Package: plone.observability
- License: GPL-2.0-only
- Python: 3.10 or later
- Plone: 6.2
- Repository: https://github.com/plone/plone.observability
- Docs: https://plone.github.io/plone.observability/

## Health probes

Run on a dedicated port (default 8081) in a background daemon thread, separate from the
Zope WSGI server, so they stay answerable when all worker threads are busy. Started by the
`egg:plone.observability#healthserver` WSGI filter, not on Zope process startup.

- `/live`: liveness; must not touch ZODB or block. No check is registered by default, so
  it returns `{"status": "ok", "checks": {}}`. Failure means restart.
- `/ready`: readiness; runs the readiness checks, by default ZODB connectivity through
  `db.open()`. Failure means drain traffic.
- `/startup`: evaluates the readiness checks itself and latches on first success, because
  Kubernetes does not run the readiness probe until startup has succeeded.

Readiness runs single-flight: one run at a time, results reused for
PLONE_OBSERVABILITY_READINESS_CACHE_TTL seconds (default 2); a run older than
PLONE_OBSERVABILITY_READINESS_STUCK_AFTER seconds (default 10) answers 503.
All endpoints return JSON, 200 on success and 503 on failure.

## Metrics

`@@metrics` is a browser view on `OFS.interfaces.IApplication`. It collects from all
`IMetricProvider` adapters and serializes with an `IMetricFormatter`. Default format is
Prometheus text 0.0.4; `?format=json` or `Accept: application/json` gives JSON. Request
metrics require the `egg:plone.observability#observability` WSGI filter.

Metrics carry a `scope` label: `global` (same across instances sharing a ZODB, read from
one instance) or `instance` (per process, sum across instances). Request metrics also
carry `auth="authenticated"|"anonymous"`. User identity is never a metric label, only a
span attribute, to keep label cardinality bounded.

Built-in metrics:

- Runtime: plone_uptime_seconds, plone_info (gauge, value 1, version labels),
  plone_threads_active, plone_process_rss_bytes, plone_process_rss_peak_bytes,
  plone_process_rss_anon_bytes, plone_process_rss_file_bytes, plone_process_cpu_seconds.
- Python: plone_python_allocated_blocks, plone_python_interned_strings (3.12+),
  plone_gc_collections_total / collected_total / uncollectable_total (label generation),
  plone_gc_pause_seconds histogram.
- Allocators: plone_malloc_inuse/free/mmap/arena_bytes (glibc mallinfo2, opt-in with
  PLONE_OBSERVABILITY_MALLINFO=1, cached; left out when jemalloc replaces malloc);
  plone_jemalloc_allocated/active/metadata/resident/mapped/retained_bytes (automatic when
  jemalloc is preloaded; resident is an upper bound).
- Requests: plone_requests_total, plone_request_duration_seconds (sum/count/bucket/max),
  plone_request_errors; opt-in plone_request_rss_growth_bytes histogram
  (PLONE_OBSERVABILITY_REQUEST_RSS=1).
- ZODB: plone_zodb_object_count, plone_zodb_db_size_bytes (global, cached),
  plone_zodb_connections, plone_zodb_connections_idle, plone_zodb_connections_alive
  (connections still in memory, incl. released ones; a lasting excess over
  plone_zodb_connections means pinned connections), plone_zodb_pool_size,
  plone_zodb_cache_size, plone_zodb_cache_size_bytes, plone_zodb_loads_total,
  plone_zodb_stores_total, plone_zodb_conflicts_total (label retry).
- Content: plone_content_total, plone_content_by_state (global, cached). Catalog backends
  that do not keep data per site (plone-pgcatalog) replace the `content` provider in
  their overrides.zcml.

## OpenTelemetry tracing (optional extra)

Install `plone.observability[opentelemetry]` and add the
`egg:plone.observability#opentelemetry` WSGI filter; without the filter no spans are
exported. OTel-native: honors `OTEL_*` env vars, activates when an OTLP endpoint is set,
exports OTLP over gRPC by default or over HTTP with
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf.

Spans: root WSGI span, ZPublisher.publish, catalog.searchResults /
unrestrictedSearchResults, transaction.commit, transformchain + transform.<name>,
subrequest <segment>, viewletmanager / viewlet / portletcolumn / portlet render spans.
Publish, transformchain and commit spans come from Zope events and hooks; catalog, render,
subrequest and ZODB load timing use reversible wrappers, installed only when tracing is
enabled. Per-span ZODB load counts (plone.zodb.*) on publish, subrequest and render spans;
opt-in plone.process.rss_* attributes on the request and subrequest spans.

The `@@metrics` scrape is excluded by default; exclude more paths with
`OTEL_PYTHON_WSGI_EXCLUDED_URLS`. Open custom spans with
`from plone.observability.spans import start_span` (no-op without the extra).

## Configuration (environment variables)

- PLONE_OBSERVABILITY_HEALTH_HOST (default 0.0.0.0)
- PLONE_OBSERVABILITY_HEALTH_PORT (default 8081; 0 disables)
- PLONE_OBSERVABILITY_READINESS_CACHE_TTL (default 2)
- PLONE_OBSERVABILITY_READINESS_STUCK_AFTER (default 10)
- PLONE_OBSERVABILITY_METRICS_ALLOWLIST (default empty/open; comma-separated CIDRs)
- PLONE_OBSERVABILITY_TRUSTED_PROXIES (default 127.0.0.1,::1)
- PLONE_OBSERVABILITY_METRICS_CACHE_TTL (default 60)
- PLONE_OBSERVABILITY_MALLINFO (default 0)
- PLONE_OBSERVABILITY_REQUEST_RSS (default 0)
- PLONE_OBSERVABILITY_REQUEST_RSS_WARN_MIB (default 20; 0 disables the warning)
- PLONE_OBSERVABILITY_ZODB_ACTIVITY_MONITOR (default 1; 0 disables)
- OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
  OTEL_EXPORTER_OTLP_PROTOCOL (grpc default, or http/protobuf), OTEL_SERVICE_NAME,
  OTEL_TRACES_SAMPLER, OTEL_PYTHON_WSGI_EXCLUDED_URLS
- PLONE_OBSERVABILITY_OTEL_ENABLED (1/0 master override)
- PLONE_OBSERVABILITY_OTEL_EXCLUDE_DEFAULTS (default 1)
- PLONE_OBSERVABILITY_OTEL_INSTRUMENTORS (default 0; botocore, requests, urllib3, httpx,
  psycopg via the opentelemetry-io / opentelemetry-db extras)
- PLONE_OBSERVABILITY_OTEL_RENDER (default 1)
- PLONE_OBSERVABILITY_OTEL_USER_ID (default off; adds enduser.id PII to spans)

## Extensibility (ZCA)

- ILivenessCheck / IReadinessCheck: named utilities returning (ok, message)
- IMetricProvider: named adapter on IApplication, `collect()` yields
  Metric(name, value, type, scope, help, labels) from `plone.observability.metric`;
  type is gauge or counter (an `info` type breaks the Prometheus scrape). A built-in
  provider is replaced by registering an adapter with the same name in overrides.zcml.
- IMetricFormatter: named utility with `content_type` and `format(metrics)`, selected via
  `?format=<name>`
