tinyrelayfile
tinyrelay/internal/daemon/queue_metrics_test.go/

internal/daemon/queue_metrics_test.go raw
1package daemon
2
3import (
4 "context"
5 "testing"
6
7 "github.com/FelineStateMachine/tinyrelay/internal/storage"
8 "github.com/prometheus/client_golang/prometheus"
9 dto "github.com/prometheus/client_model/go"
10)
11
12func 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}
42
43func metricValue(families []*dto.MetricFamily, name, labelName, labelValue string) float64 {
44 for _, family := range families {
45 if family.GetName() != name {
46 continue
47 }
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 0
57}
58
59func 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 0
71}

About

ownerc01503e8035f
visibilitypublic
headd730679639bf

Maintainers

  • c01503e8035f owner
  • ba1b5beed5b5 agent hermes

Clone

  • https://012.run/npub1cq2s86qrtadf36eqq3epm38wmhdynrrcwxf84eldk3f94dkv7ttsrulrz5/tinyrelay.git

Refs