Q&A: Phase 5 Prometheus metrics — cardinality, duplicate registration, and histogram pitfalls #223
Unanswered
web3guru888
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Question
We are adding 20+ Prometheus metrics for Phase 5 (Issue #220), several with labels (e.g.,
phase5_rollback_active{scenario},phase5_hot_reload_total{status},phase5_kg_writes_total{tier}). What are the prometheus_client pitfalls to watch out for, and how do we keep cardinality under control?Why cardinality matters
Prometheus stores one time series per unique label combination. High cardinality (many label values) inflates storage and query time. Phase 5 labels are all low-cardinality by design:
phase5_rollback_activescenariophase5_hot_reload_totalstatusphase5_kg_writes_totaltierphase5_consolidation_runs_totalstatusTotal cardinality from labels: 13 extra series across 4 metrics. Well within Prometheus best practice (<10K series for a single job).
Pitfall 1: Duplicate metric registration on restart
Problem: If
Phase5MetricsExporter.__init__()is called twice (e.g., in a test that re-creates the object),prometheus_clientraisesValueError: Duplicated timeseries.Solution: Use a
CollectorRegistryinstance per exporter, not the global default:In tests:
Pitfall 2: Gauge vs. Counter confusion for rollback state
Problem: Using a Counter for
phase5_rollback_activeis tempting ("increment when rollback starts, decrement when it ends"), but Prometheus counters must be monotonically increasing.Solution: Use a Gauge:
The separate
phase5_rollback_triggered_totalCounter tracks cumulative occurrences.Pitfall 3: Histogram bucket boundary bleed
Problem: If a weight delta norm of 0.85 is observed but your highest bucket is 0.8, Prometheus places it in
+Inf. This makes the P95 calculation look like+Infwhen the system is at its critical threshold.Solution: Always include at least one bucket above your critical threshold:
Pitfall 4: Missing pre-initialization of label combinations
Problem:
phase5_rollback_activewithscenariolabel will have no time series until a rollback actually occurs. Prometheus alert rules that check "is rollback_active == 0" will return no-data (not 0) until the first rollback.Solution: Pre-initialize all label combinations to 0 in
__init__:Summary: Phase 5 prometheus_client checklist
registry=CollectorRegistry()in tests to prevent duplicate registration__init__phase5_— no exceptionsRelated
Phase5MetricsExporterdesign decisionsAll reactions