Declared-label check is vacuous: Metric families have no _labelnames
The finding
registry.collect() returns prometheus_client.metrics_core.Metric objects. These have only name, documentation, type, unit and samples. _labelnames is an attribute of the collector objects (Counter, Gauge, ...), not of the collected Metric families. So getattr(family, "_labelnames", ()) always returns () and declared is always empty. The comment says this catches label names "even for a family with no samples", but it does not. A labelled metric that declares a target label (e.g. instance or job) and has not been observed yet produces no samples. It therefore passes both the registry loop and the /metrics text loop. The test gives false assurance about the contract it claims to enforce.
1| from __future__ import annotations
2|
3| from datetime import UTC, date, datetime, timedelta
4| from pathlib import Path
5|
6| import pytest
7| from fastapi.testclient import TestClient
8| from prometheus_client.parser import text_string_to_metric_families
9|
10| from argus_scout.budget import DailyBudget
11| from argus_scout.config import parse_config
12| from argus_scout.events import StatusEvent
13| from argus_scout.recommendations import Recommendation, Status
14| from argus_scout.server.app import create_app
15| from argus_scout.severity import Severity
16| from argus_scout.sources import Sources
17| from argus_scout.store import SqliteStore
18|
19| EXAMPLES = Path(__file__).resolve().parent.parent / "examples"
20| NOW = datetime(2026, 6, 1, 16, 30, tzinfo=UTC) # 16:30 UTC: the example tariff's peak period
21| The work pack
Recommendation. Get declared label names from the collectors themselves. For example, iterate registry._collector_to_names keys, or keep references to the metric objects on the runtime, and read collector._labelnames. Alternatively, call .labels(...) on every labelled family before collecting so samples exist. Assert that the declared-label set is non-empty for at least one family, so the check cannot silently degrade to a no-op.
Acceptance criteria
- Address: Declared-label check is vacuous: Metric families have no
_labelnames - Keep the change scoped to the cited file/symbols unless impact requires otherwise
- Get declared label names from the collectors themselves. For example, iterate
registry._collector_to_nameskeys, or keep references to the metric objects on the runtime, and readcollector._labelnames. Alternatively, call.labels(...) - Re-check
tests/test_metrics.pyafter the fix
Status
- claimed
- wontfix
A related change in the same PR
a9bdf3a310 test: the metrics target-label check insists it sees declared labels.
@@ -162,6 +162,9 @@ def test_no_family_uses_a_prometheus_target_label(store, tmp_path): families = list(registry.collect()) text = client.get("/metrics").text assert {f.name for f in families} >= {"scout_job_runs", "scout_deliveries", "scout_tariff_rate", "scout_usage_used"}+ # The collector yields *MetricFamily objects, whose _labelnames hold the declared labels; if that ever+ # stops being true the declared check would go quiet, so insist it sees some.+ assert any(getattr(family, "_labelnames", ()) for family in families) for family in families: declared = set(getattr(family, "_labelnames", ())) # the label names, even for a family with no samples sampled = {name for sample in family.samples for name in sample.labels}How it was judged
The agent checked the finding against the code and found it wrong. No fix, no test run: the pack was closed with this note, which a person can read and overturn.
Wrong: ScoutCollector yields GaugeMetricFamily/CounterMetricFamily objects, which do carry _labelnames (checked: scout_deliveries declares channel,result with 0 samples, exactly the case covered). Added a one-line assertion on the branch anyway (some family must declare labels) so the check can't silently go vacuous.