Phase 8: widgets and polish

- Live job card widget (progress bar, in-widget cancel via widget_query)
  and live invoice widget (QR drawn from a bitmap, countdown, paid state),
  Bot API 10.4 sendWidget, private chats only
- Automatic fallback to the ordinary message when sendWidget fails, with
  a breaker that pauses widgets after repeated failures or on servers
  without the method; TELEGRAM_WIDGETS
- Prometheus metrics on the core (/metrics) and on every worker: queue
  depth and wait, slots, heartbeats, job outcomes, errors per site,
  deliveries, cache, invoices, Lightning, ledger check, widgets
- Example Prometheus config and alert rules, Caddyfile, dashboard notes
- Load test: cmd/loadtest and deploy/loadtest (make loadtest)
- Browser check of the widgets' HTML, including decoding a drawn QR
- docs/DEPLOY.md (production in under an hour) and docs/MONITORING.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 18:24:36 +00:00
co-authored by Claude Opus 5.5
parent f1f9c6156c
commit 3c153af97f
45 changed files with 3158 additions and 20 deletions
+102
View File
@@ -0,0 +1,102 @@
package metrics
import (
"context"
"errors"
"io"
"net/http/httptest"
"strings"
"testing"
"time"
)
func get(t *testing.T, serve func(*httptest.ResponseRecorder)) string {
t.Helper()
rec := httptest.NewRecorder()
serve(rec)
body, _ := io.ReadAll(rec.Result().Body)
if rec.Code != 200 {
t.Fatalf("status %d: %s", rec.Code, body)
}
return string(body)
}
func TestCoreMetrics(t *testing.T) {
books := 0
m := New(Sources{
Queue: func(context.Context) (Queue, error) {
return Queue{Paid: 2, Free: 5, OldestWait: 90 * time.Second, Running: 3, Capacity: 4}, nil
},
Workers: func(context.Context) ([]Worker, error) {
return []Worker{{Name: "eu-1", State: "active", LastSeen: time.Now().Add(-7 * time.Second)}, {Name: "new", State: "offline"}}, nil
},
Cache: func() (int64, int64, int64) { return 8, 2, 1 << 20 },
Invoices: func() (int64, int64) { return 5, 3 },
Lightning: func() (bool, bool) { return true, false },
Books: func(context.Context) (int, error) { books++; return 0, nil },
})
m.QueueWait(12 * time.Second)
m.JobFinished("done", "paid")
m.DownloadError("", "network")
m.Widget("job", "fallback")
body := get(t, func(r *httptest.ResponseRecorder) {
m.Handler().ServeHTTP(r, httptest.NewRequest("GET", "/metrics", nil))
})
for _, want := range []string{
`dlbot_queue_jobs{lane="paid"} 2`, `dlbot_queue_jobs{lane="free"} 5`, "dlbot_queue_oldest_wait_seconds 90",
"dlbot_jobs_running 3", "dlbot_worker_slots 4",
`dlbot_worker_heartbeat_age_seconds{state="active",worker="eu-1"} 7`,
`dlbot_worker_heartbeat_age_seconds{state="offline",worker="new"} -1`,
"dlbot_cache_hits_total 8", "dlbot_cache_misses_total 2", "dlbot_cache_bytes_saved_total 1.048576e+06",
"dlbot_invoices_created_total 5", "dlbot_invoices_settled_total 3", "dlbot_lightning_up 0",
`dlbot_queue_wait_seconds_bucket{le="15"} 1`, `dlbot_jobs_finished_total{lane="paid",outcome="done"} 1`,
`dlbot_download_errors_total{code="network",site="unknown"} 1`, `dlbot_widgets_total{outcome="fallback",widget="job"} 1`,
"dlbot_wallets_mismatched 0",
} {
if !strings.Contains(body, want) {
t.Errorf("missing %q", want)
}
}
// The ledger check is expensive and is not repeated on every scrape.
get(t, func(r *httptest.ResponseRecorder) {
m.Handler().ServeHTTP(r, httptest.NewRequest("GET", "/metrics", nil))
})
if books != 1 {
t.Fatalf("the ledger was checked %d times for two scrapes", books)
}
// A source that fails does not break the scrape; it is reported.
broken := New(Sources{
Queue: func(context.Context) (Queue, error) { return Queue{}, errors.New("db down") },
Lightning: func() (bool, bool) { return false, false },
})
body = get(t, func(r *httptest.ResponseRecorder) {
broken.Handler().ServeHTTP(r, httptest.NewRequest("GET", "/metrics", nil))
})
if !strings.Contains(body, `dlbot_scrape_errors{source="queue"} 1`) || strings.Contains(body, "dlbot_lightning_up") {
t.Fatalf("broken sources: %s", body)
}
var none *Metrics
none.QueueWait(time.Second)
none.Widget("x", "y") // nil is a no-op
}
func TestWorkerMetrics(t *testing.T) {
h := WorkerHandler(WorkerSource{
Active: func() int { return 1 }, Capacity: func() int { return 2 },
LastContact: func() (time.Time, bool) { return time.Now().Add(-3 * time.Second), false },
Outcomes: func() map[string]int64 { return map[string]int64{"done": 4, "failed": 1} },
YtDlp: func() string { return "2026.10.26" },
})
body := get(t, func(r *httptest.ResponseRecorder) { h.ServeHTTP(r, httptest.NewRequest("GET", "/metrics", nil)) })
for _, want := range []string{
"dlbot_worker_active_jobs 1", "dlbot_worker_capacity 2", "dlbot_worker_core_contact_age_seconds 3",
"dlbot_worker_token_rejected 0", `dlbot_worker_jobs_total{outcome="done"} 4`, `dlbot_worker_ytdlp_info{version="2026.10.26"} 1`,
} {
if !strings.Contains(body, want) {
t.Errorf("missing %q", want)
}
}
}