Introducing a new way to review your PRs.

Read the announcement

zipstack / unstract

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

View on GitHub

28 documents · Updated Nov 23, 2026

docs / platform-metrics

Platform metrics

Platform metrics provide a platform-wide Prometheus view of queue work, workflow execution, and batch recovery health. The domain turns bounded database snapshots into scrape data that backend operators, dashboards, and autoscalers can use without making each backend replica query the database independently. See the backend API platform page for the wider backend service, and the related backend API v2, workflow management, agent KV extraction, platform audit, and inbox domain pages for adjacent domains.

Observability domain

Metric groups

Metric groups are the main units of collection and failure isolation. Each group describes one operational area and produces stable Prometheus series from a database snapshot.

The platform currently exposes these groups:

GroupOperational viewMain use
queuesMessage count by queue and claim state, plus oldest message ageDetect backlog and support queue-based scaling
executionsActive workflow executions and terminal executions in a recent time windowShow current load and recent completion outcomes
batchesFile-batch fan-outs in progress and fan-outs that are stuck or expiredShow work that may need recovery

Known label values are emitted as zero when no matching work exists. This keeps a healthy idle system distinct from a missing series. Queue metrics cover the PostgreSQL queue transport; work held in RabbitMQ is outside this group.

References: backend/…/groups.py• L1-14 backend/…/groups.py• L95-107 backend/…/groups.py• L163-181 backend/…/groups.py• L252-261

Operational meaning

The metric definitions align with the work state used by the queue reaper and workflow execution system. Queue depth counts all undelivered and in-flight PostgreSQL queue messages. Batch stuck counts use the same stranded-work rule as batch recovery, so the metric describes work that the recovery process would act on.

Recent execution counts use completion time rather than later record changes. A bounded lookback limits the amount of execution history that collection must inspect while preserving the recent operational view.

References: backend/…/groups.py• L98-107 backend/…/groups.py• L166-180 backend/…/groups.py• L252-260 backend/…/test_groups_db.py• L95-154

Snapshot collection

Database snapshots

Collection separates reading platform state from rendering Prometheus data. A group reads one JSON-safe snapshot inside a transaction with a database statement limit. The exporter then turns that snapshot into metric families without reading the database again.

The separation gives every backend replica the same metric shape and lets the cache share the result. It also bounds a slow database query instead of allowing it to hold a scrape open without limit.

flowchart LR
    S[Prometheus scrape] --> E[Group exporter]
    E --> C[Snapshot cache]
    C -->|fresh snapshot| R[Metric families]
    C -->|refresh needed| D[Database query]
    D --> C
    R --> S

References: backend/…/groups.py• L53-92 backend/…/exporter.py• L49-91 backend/…/test_groups_db.py• L157-176

Shared cache

The snapshot cache is a cluster-wide coordination layer for scrape-time collection. Redis keeps each group snapshot beyond its refresh time, and a non-blocking group lock allows one backend pod to refresh stale data while other pods serve the last usable snapshot.

The cache has a local copy for the case where Redis is unavailable. A short Redis failure breaker prevents every group from paying the Redis timeout during the same outage. Groups refresh independently, so a slow or broken queue query does not block execution or batch metrics.

References: backend/…/cache.py• L1-29 backend/…/cache.py• L199-225 backend/…/cache.py• L246-366 backend/…/test_cache.py• L42-184

Freshness and failure state

A failed refresh serves the last successful snapshot when one exists and records the failure. If no snapshot exists, that group is absent from the scrape rather than represented by an invented value. Queue data has an explicit maximum age; once it is too old, the exporter leaves it out so an autoscaler does not act on frozen backlog data.

Refresh failure totals are shared through Redis. The endpoint also reports the last successful refresh time and whether the metrics cache was reachable, which lets an operator distinguish a real zero from stale or unavailable observability data.

References: backend/…/cache.py• L11-29 backend/…/cache.py• L411-477 backend/…/exporter.py• L52-115 backend/…/test_exporter.py• L61-117

Prometheus exposure

Export behavior

The exporter builds one Prometheus text response for the selected groups. It handles each group separately, so one collection or render failure does not remove healthy groups from the same scrape.

The response includes the domain's health series alongside business-facing platform series:

  • unstract_metrics_last_successful_refresh_timestamp shows snapshot age by group.
  • unstract_metrics_refresh_failures_total shows shared refresh failures by group.
  • unstract_metrics_cache_up shows whether the exporter reached its Redis cache during the scrape.

A caller can select one or more groups with the repeatable group query parameter. This supports clients with a narrow need, such as an autoscaler that only reads queue depth.

References: backend/…/exporter.py• L1-13 backend/…/exporter.py• L49-116 backend/…/exporter.py• L139-161 backend/…/test_exporter.py• L46-137

Metrics endpoint

GET /metrics is a root-level backend endpoint that returns Prometheus text. It sits beside the health endpoint and outside the public API prefixes, which keeps platform-wide operational data on the in-cluster backend service.

The endpoint does not use session authentication. An environment can add a bearer token check with PLATFORM_METRICS_TOKEN. It accepts only GET, returns 401 for a bad token, and returns 400 for an unknown group request.

References: backend/…/views.py• L1-16 backend/…/views.py• L35-62 backend/…/urls.py• L1-5 backend/…/base_urls.py• L40-45 backend/…/test_views.py• L24-79

Runtime boundaries

Configuration surface

Configuration sets the collection limits and the operational meaning of time-based metrics. The main controls are grouped below.

ConcernConfiguration
AccessPLATFORM_METRICS_TOKEN
Refresh and database costPLATFORM_METRICS_CACHE_TTL_SECONDS, PLATFORM_METRICS_QUERY_TIMEOUT_MS, PLATFORM_METRICS_REDIS_TIMEOUT_SECONDS
Execution and batch windowsPLATFORM_METRICS_RECENT_WINDOW_SECONDS, PLATFORM_METRICS_RECENT_LOOKBACK_SECONDS, PLATFORM_METRICS_BATCH_STUCK_TIMEOUT_SECONDS
Queue safetyPLATFORM_METRICS_QUEUE_MAX_STALENESS_SECONDS, PLATFORM_METRICS_QUEUES

The batch timeout follows the worker reaper setting. The configured queue list defines queues that remain visible at zero, while queues seen in the database are also reported. Changes to these values can alter alert thresholds, autoscaler behavior, or the meaning of a missing series.

References: backend/…/base.py• L1151-1197 backend/…/groups.py• L111-115 backend/…/groups.py• L268-276

Test boundaries

The test boundary covers the contracts that make observability safe to depend on. Fast group tests check metric names, labels, zero filling, and group registration. Database tests check that counts match queue, execution, and recovery state. Cache and exporter tests check sharing, lock behavior, fallbacks, failure totals, freshness limits, and group selection. Endpoint tests check routing, access control, query selection, and response status.

Redis client tests cover bounded waits for both direct Redis and Sentinel-backed Redis. Together these tests protect the boundary between application state, shared cache state, and the Prometheus scrape contract.

References: backend/…/test_groups.py• L1-107 backend/…/test_groups_db.py• L1-176 backend/…/test_cache.py• L1-288 backend/…/test_exporter.py• L1-251 backend/…/test_views.py• L1-79 backend/…/test_redis_client.py• L1-109

Change checklist

When these files or sections are changed, remember to consider these:

  • A new or renamed metric group must be registered with the exporter, included in group selection, and represented in shared failure totals.
  • A change to a snapshot shape must account for Redis entries written by older backend processes and for the render path that consumes the shape.
  • A change to queue or batch state rules must stay aligned with the worker reaper and with the meaning of autoscaler inputs.
  • A change to freshness, query, or Redis time limits must preserve the scrape timeout budget and the fallback path.
  • A change to the root route or whitelist must preserve in-cluster reachability without exposing the platform endpoint through public API prefixes.

Important failure modes

TriggerConsequenceGuard
Redis is unavailable or stallsThe scrape uses process-local snapshots; shared failure totals are omitted until Redis returnsBounded Redis timeouts, a shared breaker, and unstract_metrics_cache_up
A group refresh failsThe group serves its last good snapshot or disappears if it has never succeededPer-group locks, refresh backoff, failure totals, and refresh timestamps
Queue data exceeds its freshness limitQueue series are omitted instead of showing a frozen valuePLATFORM_METRICS_QUEUE_MAX_STALENESS_SECONDS
A cached snapshot no longer matches the current groupThat group's series are dropped while other groups continuePer-group render isolation and refresh-failure accounting
A caller requests an unknown groupThe endpoint returns 400 and does not produce a partial selectionExporter group-name validation

Key files

FileWhy to read it
backend/…/groups.pyDefines the platform views and the registered metric groups
backend/…/cache.pyExplains shared snapshots, refresh coordination, fallbacks, and failure totals
backend/…/exporter.pyShows how groups and health signals become one Prometheus response
backend/…/views.pyDefines endpoint access, group selection, and response behavior
backend/…/base.pyDefines the runtime limits and queue visibility settings
backend/…/test_groups_db.pyConfirms metric meaning against real queue, execution, and batch state
backend/…/test_exporter.pyConfirms failure isolation, freshness handling, and shared health output