| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package metrix |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | func TestForEachSeriesIdentityScenarios(t *testing.T) { |
| 13 | tests := map[string]struct { |
| 14 | run func(t *testing.T) |
| 15 | }{ |
| 16 | "returns stable id/hash and deterministic order": { |
| 17 | run: func(t *testing.T) { |
| 18 | s := NewCollectorStore() |
| 19 | cc := cycleController(t, s) |
| 20 | sm := s.Write().SnapshotMeter("svc") |
| 21 | g := sm.Gauge("requests") |
| 22 | lsA := sm.LabelSet(Label{Key: "instance", Value: "a"}) |
| 23 | lsB := sm.LabelSet(Label{Key: "instance", Value: "b"}) |
| 24 | |
| 25 | cc.BeginCycle() |
| 26 | g.Observe(20, lsB) |
| 27 | g.Observe(10, lsA) |
| 28 | cc.CommitCycleSuccess() |
| 29 | |
| 30 | type gotSeries struct { |
| 31 | id SeriesID |
| 32 | hash uint64 |
| 33 | name string |
| 34 | value SampleValue |
| 35 | } |
| 36 | got := make([]gotSeries, 0) |
| 37 | |
| 38 | s.Read().ForEachSeriesIdentity(func(identity SeriesIdentity, _ SeriesMeta, name string, _ LabelView, v SampleValue) { |
| 39 | got = append(got, gotSeries{ |
| 40 | id: identity.ID, |
| 41 | hash: identity.Hash64, |
| 42 | name: name, |
| 43 | value: v, |
| 44 | }) |
| 45 | }) |
| 46 | |
| 47 | require.Len(t, got, 2) |
| 48 | assert.Equal(t, "svc.requests", got[0].name) |
| 49 | assert.Equal(t, "svc.requests", got[1].name) |
| 50 | assert.Contains(t, string(got[0].id), "instance\xffa") |
| 51 | assert.Contains(t, string(got[1].id), "instance\xffb") |
| 52 | assert.Equal(t, seriesIDHash(got[0].id), got[0].hash) |
| 53 | assert.Equal(t, seriesIDHash(got[1].id), got[1].hash) |
| 54 | assert.Equal(t, SampleValue(10), got[0].value) |
| 55 | assert.Equal(t, SampleValue(20), got[1].value) |
| 56 | }, |
| 57 | }, |
| 58 | "raw iterator matches identity/value order": { |
| 59 | run: func(t *testing.T) { |
| 60 | s := NewCollectorStore() |
| 61 | cc := cycleController(t, s) |
| 62 | sm := s.Write().SnapshotMeter("svc") |
| 63 | g := sm.Gauge("requests") |
| 64 | lsA := sm.LabelSet(Label{Key: "instance", Value: "a"}) |
| 65 | lsB := sm.LabelSet(Label{Key: "instance", Value: "b"}) |
| 66 | |
| 67 | cc.BeginCycle() |
| 68 | g.Observe(20, lsB) |
| 69 | g.Observe(10, lsA) |
| 70 | cc.CommitCycleSuccess() |
| 71 | |
| 72 | r := s.Read() |
| 73 | rawIter, ok := r.(SeriesIdentityRawIterator) |
| 74 | require.True(t, ok, "reader should expose SeriesIdentityRawIterator") |
| 75 | |
| 76 | type gotSeries struct { |
| 77 | id SeriesID |
| 78 | hash uint64 |
| 79 | name string |
| 80 | instance string |
| 81 | value SampleValue |
| 82 | labelKeySeen bool |
| 83 | } |
| 84 | got := make([]gotSeries, 0, 2) |
| 85 | rawIter.ForEachSeriesIdentityRaw(func(identity SeriesIdentity, _ SeriesMeta, name string, labels []Label, v SampleValue) { |
| 86 | instance := "" |
| 87 | labelKeySeen := false |
| 88 | for _, lbl := range labels { |
| 89 | if lbl.Key == "instance" { |
| 90 | instance = lbl.Value |
| 91 | labelKeySeen = true |
| 92 | break |
| 93 | } |
| 94 | } |
| 95 | got = append(got, gotSeries{ |
| 96 | id: identity.ID, |
| 97 | hash: identity.Hash64, |
| 98 | name: name, |
| 99 | instance: instance, |
| 100 | value: v, |
| 101 | labelKeySeen: labelKeySeen, |
| 102 | }) |
| 103 | }) |
| 104 | |
| 105 | require.Len(t, got, 2) |
| 106 | assert.Equal(t, "svc.requests", got[0].name) |
| 107 | assert.Equal(t, "svc.requests", got[1].name) |
| 108 | assert.Equal(t, "a", got[0].instance) |
| 109 | assert.Equal(t, "b", got[1].instance) |
| 110 | assert.True(t, got[0].labelKeySeen) |
| 111 | assert.True(t, got[1].labelKeySeen) |
| 112 | assert.Equal(t, seriesIDHash(got[0].id), got[0].hash) |
| 113 | assert.Equal(t, seriesIDHash(got[1].id), got[1].hash) |
| 114 | assert.Equal(t, SampleValue(10), got[0].value) |
| 115 | assert.Equal(t, SampleValue(20), got[1].value) |
| 116 | }, |
| 117 | }, |
| 118 | } |
| 119 | |
| 120 | for name, tc := range tests { |
| 121 | t.Run(name, tc.run) |
| 122 | } |
| 123 | } |