ML-11: Improve performance of Telemetry.Storage with in-memory buffer

This commit is contained in:
Claudio Ortolina
2026-04-24 10:30:28 +01:00
parent 2a688e2309
commit 252caf31ce
6 changed files with 375 additions and 53 deletions
+5 -1
View File
@@ -33,7 +33,7 @@ MusicLibrary.Application (one_for_one)
├── MusicLibrary.BackgroundRepo # Oban SQLite repo (separate DB)
├── MusicLibrary.TelemetryRepo # Telemetry metrics SQLite repo
├── MusicLibraryWeb.Telemetry # Telemetry supervisor
│ ├── Telemetry.Storage # Metrics storage (SQLite-backed, persistent)
│ ├── Telemetry.Storage # Buffered metrics storage (in-memory, 5s flush to SQLite, force-flush on read)
│ └── :telemetry_poller # 30s periodic measurements
├── Oban # Background job engine
├── Ecto.Migrator # Migrations (skipped in release; run by Coolify post-deploy)
@@ -420,3 +420,7 @@ All events are namespaced with `music_library:` prefix.
- **`use MusicLibraryWeb, :live_view`** imports: Phoenix.LiveView, Fluxon, Gettext, LiveToast,
CoreComponents, Phoenix.HTML, verified routes, JS alias
- **`use MusicLibraryWeb, :live_component`** additionally imports `put_toast!/2`
- **Telemetry buffering**: `Telemetry.Storage` keeps incoming datapoints in an in-memory map
keyed by metric and flushes to SQLite on a 5 s timer, on shutdown, and synchronously when
`metrics_history/1` is called (only for the requested metric). Keeps the cast path O(1) and
the dashboard read path consistent.
+6 -1
View File
@@ -205,7 +205,12 @@ checks and post-deploy verification.
### Telemetry
SQLite-backed persistent metrics (`MusicLibraryWeb.Telemetry.Storage`) with 30-second
polling interval. Tracks:
polling interval. Events are buffered in GenServer state keyed by metric and flushed to
SQLite every 5 seconds inside a single transaction; reads via `metrics_history/1`
force-flush only the requested metric so the dashboard sees fresh data without waiting
for the next tick. Per-metric retention is capped at 32 768 rows
(`:retention_limit`), pruned after each flush. Flush failures are logged at `:warning`
and the offending batch is dropped. Tracks:
- Database query times (total, query, queue)
- External API request latency (Finch)