From 913ef67aca1ceb7de54c3cff92ddd2e76c6c26bd Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Fri, 25 Sep 2026 20:36:42 +0100 Subject: [PATCH] Federation dashboard (phase 1, step 7) A fifth provisioned Grafana dashboard: fediverse lookups by kind and result, sharing switched on/off, federation endpoint rates and p95 latency, and inbox deliveries. Dashboard tests now know federation's own metrics, and a new test rejects PromQL string escapes Prometheus can't parse (the endpoint filter first used \. and failed live). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW --- deploy/grafana/dashboards/federation.json | 327 ++++++++++++++++++++++ tests/test_dashboards.py | 23 +- 2 files changed, 347 insertions(+), 3 deletions(-) create mode 100644 deploy/grafana/dashboards/federation.json diff --git a/deploy/grafana/dashboards/federation.json b/deploy/grafana/dashboards/federation.json new file mode 100644 index 0000000..f075cb5 --- /dev/null +++ b/deploy/grafana/dashboards/federation.json @@ -0,0 +1,327 @@ +{ + "uid": "scrobbler-federation", + "title": "Scrobbler: Federation", + "description": "ActivityPub: how the fediverse finds and talks to users sharing their listening. Empty unless FEDERATION_ENABLED is on.", + "tags": [ + "scrobbler" + ], + "timezone": "browser", + "editable": true, + "graphTooltip": 1, + "refresh": "30s", + "time": { + "from": "now-6h", + "to": "now" + }, + "schemaVersion": 41, + "version": 1, + "templating": { + "list": [] + }, + "annotations": { + "list": [] + }, + "links": [ + { + "type": "dashboards", + "tags": [ + "scrobbler" + ], + "asDropdown": true, + "title": "Scrobbler", + "includeVars": false + } + ], + "panels": [ + { + "type": "row", + "title": "Discovery", + "id": 1, + "collapsed": false, + "gridPos": { + "x": 0, + "y": 0, + "w": 24, + "h": 1 + }, + "panels": [] + }, + { + "type": "timeseries", + "title": "Lookups / min by kind", + "description": "WebFinger, actor, NodeInfo and profile-page lookups. 'not_found' covers both unknown users and users not sharing.", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "expr": "sum by (kind, result) (rate(scrobbler_federation_lookups_total[$__rate_interval])) * 60", + "legendFormat": "{{kind}} {{result}}", + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "unit": "short", + "custom": { + "lineWidth": 2, + "fillOpacity": 40, + "showPoints": "never", + "spanNulls": true, + "stacking": { + "mode": "normal", + "group": "A" + } + }, + "min": 0 + }, + "overrides": [] + }, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "id": 2, + "gridPos": { + "x": 0, + "y": 1, + "w": 12, + "h": 8 + } + }, + { + "type": "timeseries", + "title": "Sharing switched on / off", + "description": "", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "expr": "sum by (change) (increase(scrobbler_federation_sharing_changes_total[$__rate_interval]))", + "legendFormat": "{{change}}", + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "unit": "short", + "custom": { + "lineWidth": 1, + "fillOpacity": 70, + "showPoints": "never", + "spanNulls": true, + "drawStyle": "bars" + }, + "min": 0 + }, + "overrides": [] + }, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "id": 3, + "gridPos": { + "x": 12, + "y": 1, + "w": 12, + "h": 8 + } + }, + { + "type": "row", + "title": "Traffic", + "id": 4, + "collapsed": false, + "gridPos": { + "x": 0, + "y": 9, + "w": 24, + "h": 1 + }, + "panels": [] + }, + { + "type": "timeseries", + "title": "Federation requests / s by endpoint", + "description": "", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "expr": "sum by (endpoint) (rate(scrobbler_http_requests_total{endpoint=~\"(federation|Federation)[.].*\"}[$__rate_interval]))", + "legendFormat": "{{endpoint}}", + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "unit": "reqps", + "custom": { + "lineWidth": 2, + "fillOpacity": 40, + "showPoints": "never", + "spanNulls": true, + "stacking": { + "mode": "normal", + "group": "A" + } + }, + "min": 0 + }, + "overrides": [] + }, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "id": 5, + "gridPos": { + "x": 0, + "y": 10, + "w": 12, + "h": 8 + } + }, + { + "type": "timeseries", + "title": "p95 latency by endpoint", + "description": "", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "expr": "histogram_quantile(0.95, sum by (le, endpoint) (rate(scrobbler_http_request_duration_seconds_bucket{endpoint=~\"(federation|Federation)[.].*\"}[$__rate_interval])))", + "legendFormat": "{{endpoint}}", + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "unit": "s", + "custom": { + "lineWidth": 2, + "fillOpacity": 10, + "showPoints": "never", + "spanNulls": true + }, + "min": 0 + }, + "overrides": [] + }, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "id": 6, + "gridPos": { + "x": 12, + "y": 10, + "w": 12, + "h": 8 + } + }, + { + "type": "timeseries", + "title": "Inbox deliveries / min", + "description": "Activities other servers send us (follows, likes, ...). Accepted but not yet processed until following is implemented.", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "expr": "sum by (inbox) (rate(scrobbler_federation_inbox_requests_total[$__rate_interval])) * 60", + "legendFormat": "{{inbox}}", + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "unit": "short", + "custom": { + "lineWidth": 2, + "fillOpacity": 10, + "showPoints": "never", + "spanNulls": true + }, + "min": 0 + }, + "overrides": [] + }, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "id": 7, + "gridPos": { + "x": 0, + "y": 18, + "w": 24, + "h": 7 + } + } + ] +} diff --git a/tests/test_dashboards.py b/tests/test_dashboards.py index f2cbc02..339138a 100644 --- a/tests/test_dashboards.py +++ b/tests/test_dashboards.py @@ -7,6 +7,7 @@ import yaml from prometheus_client.metrics import MetricWrapperBase from scrobbler import metrics +from scrobbler.federation import metrics as federation_metrics DEPLOY = Path(__file__).resolve().parents[1] / "deploy" DASHBOARDS = sorted((DEPLOY / "grafana" / "dashboards").glob("*.json")) @@ -16,7 +17,8 @@ METRIC_NAME = re.compile(r"\bscrobbler_[a-z0-9_]+") def defined_series() -> set[str]: """Every series name the service can export.""" names = set(metrics.COLLECTED.values()) - for value in vars(metrics).values(): + modules = (metrics, federation_metrics) + for value in (v for module in modules for v in vars(module).values()): if not isinstance(value, MetricWrapperBase): continue name, kind = value._name, value._type @@ -41,8 +43,14 @@ def exprs(node): yield from exprs(item) -def test_there_are_four_dashboards(): - assert {p.stem for p in DASHBOARDS} == {"api-overview", "lastfm-api", "usage", "database"} +def test_the_expected_dashboards_exist(): + assert {p.stem for p in DASHBOARDS} == { + "api-overview", + "lastfm-api", + "usage", + "database", + "federation", + } def test_dashboard_uids_are_unique(): @@ -75,3 +83,12 @@ def test_alert_rules_only_use_defined_metrics(): for name in METRIC_NAME.findall(rule["expr"]) } assert used <= defined_series(), f"undefined metrics: {sorted(used - defined_series())}" + + +@pytest.mark.parametrize("path", DASHBOARDS, ids=lambda p: p.stem) +def test_queries_have_no_invalid_string_escapes(path): + """PromQL rejects escapes like \\. inside double-quoted strings (Grafana would show a + query error); use a character class such as [.] instead.""" + invalid = re.compile(r'(?