From 31e9d82b213bd24d7761fc4c8901d4c1ba6b8b7b Mon Sep 17 00:00:00 2001 From: Joel Takvorian Date: Wed, 2 Aug 2023 12:08:12 +0200 Subject: [PATCH] Add test for missing labels (#463) --- pkg/pipeline/encode/encode_prom_test.go | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/pkg/pipeline/encode/encode_prom_test.go b/pkg/pipeline/encode/encode_prom_test.go index 2a9f977ee..9840eda30 100644 --- a/pkg/pipeline/encode/encode_prom_test.go +++ b/pkg/pipeline/encode/encode_prom_test.go @@ -328,6 +328,50 @@ func Test_MetricTTL(t *testing.T) { require.NotContains(t, exposed, `test_bytes_total{dstIP="30.0.0.3",srcIP="10.0.0.1"}`) } +func Test_MissingLabels(t *testing.T) { + metrics := []config.GenericMap{ + { + "namespace": "A", + "IP": "10.0.0.1", + "bytes": 7, + }, + { + "namespace": "A", + "IP": "10.0.0.2", + "bytes": 1, + }, + { + "IP": "10.0.0.3", + "bytes": 4, + }, + } + params := api.PromEncode{ + ExpiryTime: api.Duration{ + Duration: time.Duration(60 * time.Second), + }, + Metrics: []api.PromMetricsItem{ + { + Name: "my_counter", + Type: "counter", + ValueKey: "bytes", + Labels: []string{"namespace"}, + }, + }, + } + + encodeProm, err := initProm(¶ms) + require.NoError(t, err) + for _, metric := range metrics { + encodeProm.Encode(metric) + } + time.Sleep(100 * time.Millisecond) + + exposed := test.ReadExposedMetrics(t) + + require.Contains(t, exposed, `my_counter{namespace="A"} 8`) + require.Contains(t, exposed, `my_counter{namespace=""} 4`) +} + func buildFlow() config.GenericMap { return config.GenericMap{ "srcIP": "10.0.0." + strconv.Itoa(rand.Intn(20)),