| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package metrix |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func TestSeededHelperScenarios(t *testing.T) { |
| 11 | tests := map[string]struct { |
| 12 | run func(t *testing.T) |
| 13 | }{ |
| 14 | "SeededGauge creates visible zero-valued series": { |
| 15 | run: func(t *testing.T) { |
| 16 | s := NewRuntimeStore() |
| 17 | m := s.Write().StatefulMeter("runtime") |
| 18 | _ = SeededGauge(m, "queue_depth") |
| 19 | |
| 20 | mustValue(t, s.Read(ReadRaw()), "runtime.queue_depth", nil, 0) |
| 21 | }, |
| 22 | }, |
| 23 | "SeededCounter creates visible zero-valued series without initial delta": { |
| 24 | run: func(t *testing.T) { |
| 25 | s := NewRuntimeStore() |
| 26 | m := s.Write().StatefulMeter("runtime") |
| 27 | _ = SeededCounter(m, "jobs_total") |
| 28 | |
| 29 | mustValue(t, s.Read(ReadRaw()), "runtime.jobs_total", nil, 0) |
| 30 | mustNoDelta(t, s.Read(ReadRaw()), "runtime.jobs_total", nil) |
| 31 | }, |
| 32 | }, |
| 33 | "SeededCounter accumulates normally after seed": { |
| 34 | run: func(t *testing.T) { |
| 35 | s := NewRuntimeStore() |
| 36 | m := s.Write().StatefulMeter("runtime") |
| 37 | c := SeededCounter(m, "events_total") |
| 38 | |
| 39 | c.Add(5) |
| 40 | mustValue(t, s.Read(ReadRaw()), "runtime.events_total", nil, 5) |
| 41 | mustDelta(t, s.Read(ReadRaw()), "runtime.events_total", nil, 5) |
| 42 | }, |
| 43 | }, |
| 44 | "Seeded helpers preserve meter labels": { |
| 45 | run: func(t *testing.T) { |
| 46 | s := NewRuntimeStore() |
| 47 | m := s.Write().StatefulMeter("runtime").WithLabels(Label{Key: "component", Value: "functions"}) |
| 48 | _ = SeededGauge(m, "invocations_active") |
| 49 | _ = SeededCounter(m, "calls_total") |
| 50 | |
| 51 | labels := Labels{"component": "functions"} |
| 52 | mustValue(t, s.Read(ReadRaw()), "runtime.invocations_active", labels, 0) |
| 53 | mustValue(t, s.Read(ReadRaw()), "runtime.calls_total", labels, 0) |
| 54 | }, |
| 55 | }, |
| 56 | "Seeded series can be evicted by TTL when compaction is triggered later": { |
| 57 | run: func(t *testing.T) { |
| 58 | s := NewRuntimeStore() |
| 59 | view := runtimeStoreViewForTest(t, s) |
| 60 | now := time.Unix(1_700_000_000, 0) |
| 61 | view.backend.now = func() time.Time { return now } |
| 62 | view.backend.retention = runtimeRetentionPolicy{ |
| 63 | ttl: 5 * time.Second, |
| 64 | maxSeries: 0, |
| 65 | } |
| 66 | view.backend.compaction = runtimeCompactionPolicy{ |
| 67 | maxOverlayDepth: 1, |
| 68 | maxOverlayWrites: 1, |
| 69 | } |
| 70 | |
| 71 | m := s.Write().StatefulMeter("runtime") |
| 72 | _ = SeededCounter(m, "stale_total") |
| 73 | mustValue(t, s.Read(ReadRaw()), "runtime.stale_total", nil, 0) |
| 74 | |
| 75 | now = now.Add(6 * time.Second) |
| 76 | SeededGauge(m, "trigger") |
| 77 | mustNoValue(t, s.Read(ReadRaw()), "runtime.stale_total", nil) |
| 78 | }, |
| 79 | }, |
| 80 | } |
| 81 | |
| 82 | for name, tc := range tests { |
| 83 | t.Run(name, tc.run) |
| 84 | } |
| 85 | } |