| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package metrix |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/require" |
| 9 | ) |
| 10 | |
| 11 | func TestFlattenSnapshotScenarios(t *testing.T) { |
| 12 | tests := map[string]struct { |
| 13 | run func(t *testing.T) |
| 14 | }{ |
| 15 | "malformed histogram cumulative length is skipped safely": { |
| 16 | run: func(t *testing.T) { |
| 17 | src := &readSnapshot{ |
| 18 | series: map[string]*committedSeries{ |
| 19 | "svc.latency": { |
| 20 | key: "svc.latency", |
| 21 | name: "svc.latency", |
| 22 | desc: &instrumentDescriptor{name: "svc.latency", kind: kindHistogram, mode: modeSnapshot, freshness: FreshnessCycle, histogram: &histogramSchema{bounds: []float64{1, 2}}}, |
| 23 | meta: SeriesMeta{LastSeenSuccessSeq: 1}, |
| 24 | value: 0, |
| 25 | histogramCount: 2, |
| 26 | histogramSum: 3, |
| 27 | histogramCumulative: []SampleValue{1}, // malformed: len 1, bounds len 2 |
| 28 | }, |
| 29 | }, |
| 30 | } |
| 31 | |
| 32 | flat := flattenSnapshot(src) |
| 33 | r := &storeReader{snap: flat} |
| 34 | |
| 35 | _, ok := r.Value("svc.latency_bucket", Labels{"le": "1"}) |
| 36 | require.False(t, ok, "expected malformed histogram bucket series to be skipped") |
| 37 | _, ok = r.Value("svc.latency_count", nil) |
| 38 | require.False(t, ok, "expected malformed histogram count series to be skipped") |
| 39 | _, ok = r.Value("svc.latency_sum", nil) |
| 40 | require.False(t, ok, "expected malformed histogram sum series to be skipped") |
| 41 | }, |
| 42 | }, |
| 43 | "malformed summary quantile length skips quantile series but keeps count sum": { |
| 44 | run: func(t *testing.T) { |
| 45 | src := &readSnapshot{ |
| 46 | series: map[string]*committedSeries{ |
| 47 | "svc.latency": { |
| 48 | key: "svc.latency", |
| 49 | name: "svc.latency", |
| 50 | desc: &instrumentDescriptor{name: "svc.latency", kind: kindSummary, mode: modeSnapshot, freshness: FreshnessCycle, summary: &summarySchema{quantiles: []float64{0.5, 0.9}}}, |
| 51 | meta: SeriesMeta{LastSeenSuccessSeq: 1}, |
| 52 | value: 0, |
| 53 | |
| 54 | summaryCount: 2, |
| 55 | summarySum: 1.2, |
| 56 | summaryQuantiles: []SampleValue{0.4}, // malformed: len 1, quantiles len 2 |
| 57 | }, |
| 58 | }, |
| 59 | } |
| 60 | |
| 61 | flat := flattenSnapshot(src) |
| 62 | require.Contains(t, flat.series, "svc.latency_count") |
| 63 | require.Contains(t, flat.series, "svc.latency_sum") |
| 64 | r := &storeReader{snap: flat, raw: true} |
| 65 | |
| 66 | mustValue(t, r, "svc.latency_count", nil, 2) |
| 67 | mustValue(t, r, "svc.latency_sum", nil, 1.2) |
| 68 | _, ok := r.Value("svc.latency", Labels{"quantile": "0.5"}) |
| 69 | require.False(t, ok, "expected malformed summary quantile series to be skipped") |
| 70 | }, |
| 71 | }, |
| 72 | } |
| 73 | |
| 74 | for name, tc := range tests { |
| 75 | t.Run(name, tc.run) |
| 76 | } |
| 77 | } |