clone

a self-hosted multitenant nostr relay with sites, file storage, git hosting, and inbox and outbox delivery

feat/tinyagent
tinyrelay / internal / daemon / queue_metrics_test.go
queue_metrics_test.go 2.1K
1package daemon23import (4 "context"5 "testing"67 "github.com/FelineStateMachine/tinyrelay/internal/storage"8 "github.com/prometheus/client_golang/prometheus"9 dto "github.com/prometheus/client_model/go"10)1112func TestQueueCollectorBoundsKindsAndAccumulatesScrapeFailures(t *testing.T) {13 store, err := storage.Open(context.Background(), t.TempDir()+"/relay.db")14 if err != nil {15 t.Fatal(err)16 }17 app := &App{tenants: map[string]*Tenant{"one": {store: store}}}18 collector := app.QueueCollector()19 registry := prometheus.NewPedanticRegistry()20 if err := registry.Register(collector); err != nil {21 t.Fatal(err)22 }23 if _, err := store.DB().Exec(`INSERT INTO work_intents(id,kind,event_id,target,payload,next_at,created_at,updated_at) VALUES('id','unbounded','event','target','{}',0,0,0)`); err != nil {24 t.Fatal(err)25 }26 families, err := registry.Gather()27 if err != nil {28 t.Fatal(err)29 }30 if got := metricValue(families, "relay_work_pending", "kind", "other"); got != 1 {31 t.Fatalf("other pending = %v", got)32 }33 if err := store.Close(); err != nil {34 t.Fatal(err)35 }36 first := gatherMetric(t, registry.Gather, "relay_work_collection_errors_total")37 second := gatherMetric(t, registry.Gather, "relay_work_collection_errors_total")38 if first < 1 || second <= first {39 t.Fatalf("scrape errors are not cumulative: first=%v second=%v", first, second)40 }41}4243func metricValue(families []*dto.MetricFamily, name, labelName, labelValue string) float64 {44 for _, family := range families {45 if family.GetName() != name {46 continue47 }48 for _, metric := range family.GetMetric() {49 for _, label := range metric.GetLabel() {50 if label.GetName() == labelName && label.GetValue() == labelValue {51 return metric.GetGauge().GetValue()52 }53 }54 }55 }56 return 057}5859func gatherMetric(t *testing.T, gather func() ([]*dto.MetricFamily, error), name string) float64 {60 t.Helper()61 families, err := gather()62 if err != nil {63 t.Fatal(err)64 }65 for _, family := range families {66 if family.GetName() == name && len(family.GetMetric()) > 0 {67 return family.GetMetric()[0].GetCounter().GetValue()68 }69 }70 return 071}