Files
Scrobbler/src/scrobbler/metrics/__init__.py
T
mattandClaude Opus 5.5 098eab025f
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Failing after 38s
CI / test (push) Successful in 46s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s
Generic background worker (phase 0, step 2)
scrobbler/worker.py runs tasks that modules register: queue tasks
(called while they report work) and periodic tasks. A failing task is
logged, counted, rolled back and doesn't stop the others. Imports are
now just a registered task plus a periodic stall check.

- `flask worker` replaces `flask imports worker` (kept as an alias);
  compose uses the new command
- scrobbler_worker_tasks_total{task,outcome} and a Worker tasks panel
  on the Usage dashboard
- WORKER_METRICS_PORT (IMPORT_WORKER_METRICS_PORT still honoured)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
2026-09-25 19:57:25 +01:00

154 lines
4.2 KiB
Python

"""All Prometheus metrics for the service, defined in one place.
Labels are deliberately low-cardinality: Flask endpoint names (never raw paths),
Last.fm method names from the handler registry, and small fixed enums. Never add
user, api_key or track labels.
"""
import platform
from prometheus_client import Counter, Gauge, Histogram
from scrobbler import __version__
LATENCY_BUCKETS = (0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10)
# HTTP (RED)
http_requests_total = Counter(
"scrobbler_http_requests_total",
"HTTP requests handled",
["endpoint", "http_method", "status"],
)
http_request_duration_seconds = Histogram(
"scrobbler_http_request_duration_seconds",
"HTTP request latency",
["endpoint", "http_method"],
buckets=LATENCY_BUCKETS,
)
http_requests_in_progress = Gauge(
"scrobbler_http_requests_in_progress",
"HTTP requests currently being handled",
["endpoint"],
multiprocess_mode="livesum",
)
# Last.fm API
lastfm_requests_total = Counter(
"scrobbler_lastfm_requests_total",
"Last.fm API calls",
["lastfm_method", "format", "outcome"],
)
lastfm_request_duration_seconds = Histogram(
"scrobbler_lastfm_request_duration_seconds",
"Last.fm API call latency",
["lastfm_method"],
buckets=LATENCY_BUCKETS,
)
lastfm_errors_total = Counter(
"scrobbler_lastfm_errors_total",
"Last.fm API errors by error code",
["lastfm_method", "code"],
)
# Scrobbling
scrobbles_total = Counter(
"scrobbler_scrobbles_total",
"Scrobbles submitted, by result",
["result"],
)
scrobbles_ignored_total = Counter(
"scrobbler_scrobbles_ignored_total",
"Scrobbles ignored, by Last.fm ignored-message code",
["reason"],
)
scrobble_batch_size = Histogram(
"scrobbler_scrobble_batch_size",
"Tracks per track.scrobble call",
buckets=(1, 2, 5, 10, 25, 50),
)
scrobble_lag_seconds = Histogram(
"scrobbler_scrobble_lag_seconds",
"Delay between a track being played and its scrobble arriving",
buckets=(10, 60, 300, 900, 3600, 6 * 3600, 86400, 7 * 86400, 14 * 86400),
)
now_playing_updates_total = Counter(
"scrobbler_now_playing_updates_total",
"track.updateNowPlaying calls that were stored",
)
# Auth
auth_sessions_created_total = Counter(
"scrobbler_auth_sessions_created_total",
"Last.fm session keys issued",
["flow"],
)
auth_failures_total = Counter(
"scrobbler_auth_failures_total",
"Authentication failures",
["reason"],
)
ui_logins_total = Counter(
"scrobbler_ui_logins_total",
"Web UI login attempts",
["result"],
)
registrations_total = Counter(
"scrobbler_registrations_total",
"User registrations",
)
# History imports (counted by the import worker)
imports_total = Counter(
"scrobbler_imports_total",
"Import jobs by source and lifecycle event (created, completed, failed, cancelled)",
["source", "status"],
)
imported_scrobbles_total = Counter(
"scrobbler_imported_scrobbles_total",
"Rows read by imports, by outcome",
["source", "result"],
)
import_duration_seconds = Histogram(
"scrobbler_import_duration_seconds",
"Time to process an import job",
["source"],
buckets=(1, 5, 15, 30, 60, 120, 300, 600, 1800, 3600, 7200),
)
# Background worker
worker_tasks_total = Counter(
"scrobbler_worker_tasks_total",
"Worker task runs that did work (queue tasks) or ran (periodic tasks), and failures",
["task", "outcome"],
)
# Database
db_query_duration_seconds = Histogram(
"scrobbler_db_query_duration_seconds",
"Database statement latency",
["operation"],
buckets=(0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5),
)
db_pool_connections = Gauge(
"scrobbler_db_pool_connections",
"Database connection pool state",
["state"],
multiprocess_mode="livesum",
)
build_info = Gauge(
"scrobbler_build_info",
"Build information",
["version", "python_version"],
multiprocess_mode="max",
)
build_info.labels(version=__version__, python_version=platform.python_version()).set(1)
# Gauges computed from the database by metrics.collectors.DatabaseStatsCollector.
COLLECTED = {
"users": "scrobbler_users",
"active_users": "scrobbler_active_users",
"now_playing_active": "scrobbler_now_playing_active",
"import_jobs": "scrobbler_import_jobs",
}