diff --git a/deploy/grafana/dashboards/federation.json b/deploy/grafana/dashboards/federation.json index 760cd9b..d7616af 100644 --- a/deploy/grafana/dashboards/federation.json +++ b/deploy/grafana/dashboards/federation.json @@ -956,6 +956,239 @@ "w": 12, "h": 8 } + }, + { + "type": "row", + "title": "Posts", + "id": 20, + "collapsed": false, + "gridPos": { + "x": 0, + "y": 50, + "w": 24, + "h": 1 + }, + "panels": [] + }, + { + "type": "timeseries", + "title": "Posts / min by kind and visibility", + "description": "Weekly summaries and milestones actually published.", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "expr": "sum by (kind, visibility) (rate(scrobbler_federation_posts_total[$__rate_interval])) * 60", + "legendFormat": "{{kind}} {{visibility}}", + "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": 21, + "gridPos": { + "x": 0, + "y": 51, + "w": 12, + "h": 8 + } + }, + { + "type": "timeseries", + "title": "Post deletions / min by reason", + "description": "'manual' from the web UI, 'sharing_off' when a user turns sharing off.", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "expr": "sum by (reason) (rate(scrobbler_federation_post_deletions_total[$__rate_interval])) * 60", + "legendFormat": "{{reason}}", + "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": 22, + "gridPos": { + "x": 12, + "y": 51, + "w": 12, + "h": 8 + } + }, + { + "type": "timeseries", + "title": "Weekly summary checks / min by result", + "description": "disabled, skipped_empty, already_posted or posted.", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "expr": "sum by (result) (rate(scrobbler_federation_weekly_total[$__rate_interval])) * 60", + "legendFormat": "{{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": 23, + "gridPos": { + "x": 0, + "y": 59, + "w": 12, + "h": 8 + } + }, + { + "type": "timeseries", + "title": "Milestone checks / min by result", + "description": "disabled, none or posted.", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "expr": "sum by (result) (rate(scrobbler_federation_milestone_checks_total[$__rate_interval])) * 60", + "legendFormat": "{{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": 24, + "gridPos": { + "x": 12, + "y": 59, + "w": 12, + "h": 8 + } } ] } diff --git a/deploy/prometheus/alerts.yml b/deploy/prometheus/alerts.yml index 81f451d..1a14936 100644 --- a/deploy/prometheus/alerts.yml +++ b/deploy/prometheus/alerts.yml @@ -89,6 +89,14 @@ groups: summary: Received fediverse activities haven't been processed for 15 minutes description: The worker is probably stopped, or stuck on something. + - alert: FederationWeeklySummariesMissing + expr: sum(increase(scrobbler_worker_tasks_total{task="federation.weekly"}[30m])) == 0 + for: 5m + labels: {severity: warning} + annotations: + summary: The weekly summary check hasn't run in 30 minutes + description: federation.weekly runs every 15 minutes regardless of whether anyone is due a post; the worker is probably stopped or stuck. + - alert: FederationSignatureFailures expr: | sum(rate(scrobbler_federation_signatures_total{direction="in",result!="ok"}[15m])) diff --git a/src/scrobbler/federation/metrics.py b/src/scrobbler/federation/metrics.py index 1aeea3d..75016ec 100644 --- a/src/scrobbler/federation/metrics.py +++ b/src/scrobbler/federation/metrics.py @@ -51,3 +51,23 @@ followers = Gauge( ["state"], multiprocess_mode="max", ) +posts_total = Counter( + "scrobbler_federation_posts_total", + "Posts published: weekly summaries and milestones", + ["kind", "visibility"], +) +post_deletions_total = Counter( + "scrobbler_federation_post_deletions_total", + "Posts deleted, by reason", + ["reason"], +) +milestone_checks_total = Counter( + "scrobbler_federation_milestone_checks_total", + "Milestone checks, by result: disabled, none or posted", + ["result"], +) +weekly_total = Counter( + "scrobbler_federation_weekly_total", + "Weekly summary checks, by result: disabled, skipped_empty, already_posted or posted", + ["result"], +) diff --git a/src/scrobbler/federation/publishing/base.py b/src/scrobbler/federation/publishing/base.py index 6a6f47f..0e0c6a7 100644 --- a/src/scrobbler/federation/publishing/base.py +++ b/src/scrobbler/federation/publishing/base.py @@ -14,6 +14,7 @@ from sqlalchemy.exc import IntegrityError from scrobbler.extensions import db from scrobbler.federation import activities, followers, ids, sharing +from scrobbler.federation import metrics as fed_metrics from scrobbler.federation.models import FederationActivity, FederationPost from scrobbler.federation.protocol import addressing, vocab @@ -141,6 +142,7 @@ def publish( except IntegrityError: # posted with this key by a concurrent pass db.session.rollback() return None + fed_metrics.posts_total.labels(kind=kind, visibility=settings.visibility).inc() return post @@ -150,7 +152,7 @@ def delete_post(user_id: int, username: str, post: FederationPost) -> Federation None if it's already deleted.""" if post.deleted_at is not None: return None - return _delete(user_id, username, post) + return _delete(user_id, username, post, reason="manual") def delete_all_for(user_id: int, username: str) -> int: @@ -161,11 +163,13 @@ def delete_all_for(user_id: int, username: str) -> int: db.select(FederationPost).filter_by(user_id=user_id, deleted_at=None) ).all() for post in posts: - _delete(user_id, username, post) + _delete(user_id, username, post, reason="sharing_off") return len(posts) -def _delete(user_id: int, username: str, post: FederationPost) -> FederationActivity: +def _delete( + user_id: int, username: str, post: FederationPost, *, reason: str +) -> FederationActivity: # The post's own visibility at the time it was posted, not the user's current # setting: a Delete has to reach whoever could have seen the Create. note_id = post.activity.document["object"]["id"] @@ -179,4 +183,5 @@ def _delete(user_id: int, username: str, post: FederationPost) -> FederationActi if inboxes: activities.queue(activity, inboxes) post.deleted_at = _now() + fed_metrics.post_deletions_total.labels(reason=reason).inc() return activity diff --git a/src/scrobbler/federation/publishing/milestones.py b/src/scrobbler/federation/publishing/milestones.py index d21efb0..e28ae30 100644 --- a/src/scrobbler/federation/publishing/milestones.py +++ b/src/scrobbler/federation/publishing/milestones.py @@ -12,6 +12,7 @@ never gets a burst of "congratulations" for plays from years ago. import logging from scrobbler.extensions import db +from scrobbler.federation import metrics as fed_metrics from scrobbler.federation import publishing, sharing from scrobbler.federation.models import FederationMilestoneMark, FederationPendingCheck from scrobbler.federation.protocol import content @@ -132,6 +133,12 @@ def check_and_post(user_id: int) -> str: """For one user, post at most one milestone: the first of scrobble-count, top-10 entry, or artist-plays (in that priority) that has a new, unmarked threshold to report. Returns a result: disabled, none or posted.""" + result = _check_and_post(user_id) + fed_metrics.milestone_checks_total.labels(result=result).inc() + return result + + +def _check_and_post(user_id: int) -> str: settings = sharing.settings_for(user_id) if not (settings.enabled and settings.post_milestones): return "disabled" diff --git a/src/scrobbler/federation/publishing/weekly.py b/src/scrobbler/federation/publishing/weekly.py index 7f1e273..8ed2515 100644 --- a/src/scrobbler/federation/publishing/weekly.py +++ b/src/scrobbler/federation/publishing/weekly.py @@ -11,6 +11,7 @@ import logging from datetime import UTC, datetime from scrobbler.extensions import db +from scrobbler.federation import metrics as fed_metrics from scrobbler.federation import publishing, sharing from scrobbler.federation.models import FederationSettings from scrobbler.federation.protocol import content, schedule @@ -61,6 +62,12 @@ def build_content(count: int, top_artists: list, top_tracks: list) -> tuple[str, def check_and_post(settings: FederationSettings, now_utc: datetime) -> str: """For one user's settings, post their most recent eligible week if it's due and not already posted. Returns a result: disabled, already_posted, skipped_empty or posted.""" + result = _check_and_post(settings, now_utc) + fed_metrics.weekly_total.labels(result=result).inc() + return result + + +def _check_and_post(settings: FederationSettings, now_utc: datetime) -> str: if not settings.post_weekly_summary: return "disabled" user = accounts.get_user(settings.user_id) diff --git a/tests/test_federation_milestones.py b/tests/test_federation_milestones.py index 4387b2e..a33ad1c 100644 --- a/tests/test_federation_milestones.py +++ b/tests/test_federation_milestones.py @@ -52,15 +52,19 @@ def test_nothing_reached_posts_nothing(fed_ctx, user, sharing_settings): assert posts_for(user.id) == [] -def test_a_scrobble_count_threshold_is_posted(fed_ctx, user, sharing_settings, monkeypatch): +def test_a_scrobble_count_threshold_is_posted( + fed_ctx, user, sharing_settings, monkeypatch, metric_delta +): monkeypatch.setattr(milestones, "SCROBBLE_THRESHOLDS", [3]) played(user, "Radiohead", "Reckoner", n=3) + posted = metric_delta("scrobbler_federation_milestone_checks_total", result="posted") outcome = milestones.check_and_post(user.id) assert outcome == "posted" [post] = posts_for(user.id) assert post.key == "milestone:scrobbles:3" assert "3 plays scrobbled" in post.content_text assert marks_for(user.id) == {"scrobbles:3": True} + assert posted.delta == 1 def test_checking_twice_only_posts_once(fed_ctx, user, sharing_settings, monkeypatch): diff --git a/tests/test_federation_publishing.py b/tests/test_federation_publishing.py index e2a6351..cea45ac 100644 --- a/tests/test_federation_publishing.py +++ b/tests/test_federation_publishing.py @@ -32,12 +32,14 @@ def approved_follower(fed_client, remote, sharing_user, name): return account -def test_publish_creates_a_post_and_a_create_activity(fed_ctx, user): +def test_publish_creates_a_post_and_a_create_activity(fed_ctx, user, metric_delta): sharing.update(user.id, {"enabled": True}) + posted = metric_delta("scrobbler_federation_posts_total", kind="weekly", visibility="followers") post = publishing.publish( user.id, "alice", "weekly", "2026-W01", text="312 plays", html="
312 plays
" ) assert post is not None + assert posted.delta == 1 assert (post.kind, post.key, post.content_text) == ("weekly", "weekly:2026-W01", "312 plays") assert post.visibility == "followers" assert post.deleted_at is None @@ -138,12 +140,13 @@ def test_publish_with_no_followers_still_creates_the_post(fed_ctx, user): assert deliveries_for(post.activity_id) == [] -def test_delete_post_sends_delete_and_tombstones(fed_client, remote, sharing_user): +def test_delete_post_sends_delete_and_tombstones(fed_client, remote, sharing_user, metric_delta): bob = approved_follower(fed_client, remote, sharing_user, "bob") post = publishing.publish( sharing_user.id, "alice", "weekly", "2026-W01", text="x", html="x
" ) note_id = db.session.get(FederationActivity, post.activity_id).document["object"]["id"] + deleted = metric_delta("scrobbler_federation_post_deletions_total", reason="manual") activity = publishing.delete_post(sharing_user.id, "alice", post) db.session.commit() @@ -152,6 +155,7 @@ def test_delete_post_sends_delete_and_tombstones(fed_client, remote, sharing_use assert activity.document["object"] == {"id": note_id, "type": "Tombstone"} assert db.session.get(FederationPost, post.id).deleted_at is not None assert [d.inbox for d in deliveries_for(activity.id)] == [bob.server.url("/inbox")] + assert deleted.delta == 1 def test_deleting_an_already_deleted_post_is_a_noop(fed_ctx, user): @@ -175,7 +179,7 @@ def test_delete_uses_the_posts_own_visibility_not_the_current_setting(fed_ctx, u assert activity.document["to"] == [PUBLIC] # still addressed as it was posted -def test_delete_all_for_deletes_every_live_post(fed_ctx, user): +def test_delete_all_for_deletes_every_live_post(fed_ctx, user, metric_delta): sharing.update(user.id, {"enabled": True}) a = publishing.publish(user.id, "alice", "weekly", "2026-W01", text="a", html="a") b = publishing.publish(user.id, "alice", "milestone", "scrobbles:1000", text="b", html="b") @@ -183,10 +187,12 @@ def test_delete_all_for_deletes_every_live_post(fed_ctx, user): publishing.delete_post(user.id, "alice", already_deleted) db.session.commit() + sharing_off = metric_delta("scrobbler_federation_post_deletions_total", reason="sharing_off") count = publishing.delete_all_for(user.id, "alice") db.session.commit() assert count == 2 # not the one already deleted + assert sharing_off.delta == 2 assert db.session.get(FederationPost, a.id).deleted_at is not None assert db.session.get(FederationPost, b.id).deleted_at is not None # 3 Deletes total: one from the already_deleted post above, plus a and b just now diff --git a/tests/test_federation_weekly.py b/tests/test_federation_weekly.py index 4f1f7af..892da5e 100644 --- a/tests/test_federation_weekly.py +++ b/tests/test_federation_weekly.py @@ -42,9 +42,10 @@ def test_an_empty_week_is_not_posted(fed_ctx, user, sharing_settings): assert posts_for(user.id) == [] -def test_a_week_with_plays_is_posted(fed_ctx, user, sharing_settings): +def test_a_week_with_plays_is_posted(fed_ctx, user, sharing_settings, metric_delta): a_scrobble_in_the_target_week(user) a_scrobble_in_the_target_week(user, hour=13) + posted = metric_delta("scrobbler_federation_weekly_total", result="posted") outcome = weekly.check_and_post(sharing_settings, GATE) assert outcome == "posted" [post] = posts_for(user.id) @@ -52,6 +53,7 @@ def test_a_week_with_plays_is_posted(fed_ctx, user, sharing_settings): assert "2 plays" in post.content_text activity = db.session.get(FederationActivity, post.activity_id) assert activity.activity_type == "Create" + assert posted.delta == 1 def test_checking_twice_only_posts_once(fed_ctx, user, sharing_settings):