"""The relay face's metrics, as declarations.
`relayserver.render_metrics` built these eight by hand, with the names, HELP
and TYPE typed inline and every reading wrapped in `hasattr` plus a bare
`except: pass`. They were the only metrics the node exposed.
Declaring them changes nothing an operator scrapes, and that is the point.
Every exposed name and every TYPE is reproduced exactly, because a metric
name is an interface: a dashboard, an alert rule and a recording rule are all
written against it, and renaming one to suit a convention breaks them
silently. Two of these are not what their names suggest - `fleetmesh_blobs_total`
is a gauge despite the counter suffix, and `fleetmesh_events_stored_total`
counts what is in the store now and can go down - and both are kept as they
are rather than corrected. The place to fix an exposition name is a release
note, not a refactor.
What does change: the readings come through `Monitors`, so a sampler that
raises is counted rather than swallowed by `except: pass`, and the face's
numbers sit in the same census as every compartment's.
"""
from __future__ import annotations
from typing import Any, Callable, Mapping
from . import COUNTER, GAUGE, Metric
def _try(fn: Callable[[], Any], default: Any = 0) -> Any:
"""A reading, or the default.
The old code caught everything and moved on, which is why a store that
could not be counted reported zero and nobody knew. `Monitors.read`
counts a sampler that raises, so a sampler here lets a real error through
and only guards the shape - an object that does not implement `count` at
all, which is a configuration and not a fault.
"""
try:
return fn()
except (AttributeError, TypeError):
return default
[docs]def face_metrics(face: Any) -> tuple[Metric, ...]:
"""The eight the relay face has always exposed, bound to this server.
`face` is the RelayServer. The samplers close over it rather than being
handed values, so each scrape reads the live number.
"""
def stored() -> int:
return int(_try(lambda: face.store.count()))
def conns() -> int:
with face._lock:
return len(face._conns)
def subs() -> int:
with face._lock:
return sum(len(s) for s in face._subs.values())
def blob_count() -> int:
if face.blob_store is None:
return 0
return int(_try(lambda: face.blob_store.count()))
def blob_bytes() -> int:
if face.blob_store is None:
return 0
return int(_try(lambda: face.blob_store.total_bytes(), 0) or 0)
def by_tier() -> Mapping[str, int]:
"""Peers per trust tier, which is the one labelled metric here."""
out: dict[str, int] = {}
if face.grants is None:
return out
for g in _try(lambda: list(face.grants.list()), []):
payload = g.get('payload')
t = payload.get('tier') if isinstance(payload, dict) else g.get('tier', 'unknown')
out[str(t)] = out.get(str(t), 0) + 1
return out
return (
# Named `_total` and typed counter since it was written, though it
# counts what is in the store now and can fall. Kept as it is: the
# name is what alerts are written against.
Metric(name='events_stored', description='Total events stored in local store.',
kind=COUNTER, sample=stored, tags=('face', 'store')),
Metric(name='active_connections', description='Current active WebSocket connections.',
kind=GAUGE, sample=conns, tags=('face', 'connections')),
Metric(name='active_subscriptions', description='Current active event subscriptions.',
kind=GAUGE, sample=subs, tags=('face', 'subscriptions')),
Metric(name='events_accepted', description='Total valid events accepted through relay face.',
kind=COUNTER, sample=lambda: int(getattr(face, 'accepted', 0)),
tags=('face', 'events')),
Metric(name='events_refused_future',
description='Total events refused due to future timestamps.',
kind=COUNTER, sample=lambda: int(getattr(face, 'refused_future', 0)),
tags=('face', 'events', 'refusals')),
# A gauge wearing a counter's suffix, preserved deliberately.
Metric(name='blobs_total', description='Total content-addressed blobs in Blossom CAS.',
kind=GAUGE, sample=blob_count, tags=('face', 'blobs')),
Metric(name='blobs_bytes_total',
description='Total size in bytes of blobs in Blossom CAS.',
kind=GAUGE, sample=blob_bytes, tags=('face', 'blobs')),
Metric(name='peers_by_tier',
description='Number of peers categorized by trust tier.',
kind=GAUGE, labels=('tier',), sample=by_tier, tags=('face', 'peering')),
)
__all__ = ['face_metrics']