Skip to content

Commit e9c7162

Browse files
committed
Have a function that can emulate old GatherAndCompare
Follow up to prometheus/client_golang#1424 Signed-off-by: György Krajcsovits <[email protected]>
1 parent b48666f commit e9c7162

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

pkg/ingester/ingester_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -8948,7 +8948,7 @@ func TestIngesterActiveSeriesConfigChanges(t *testing.T) {
89488948
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="bool_is_true_flagbased",user="test_user"} 16
89498949
`
89508950
// Check tracked Prometheus metrics
8951-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
8951+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
89528952

89538953
// Add new runtime configs
89548954
activeSeriesTenantOverride := new(TenantLimitsMock)
@@ -8989,7 +8989,7 @@ func TestIngesterActiveSeriesConfigChanges(t *testing.T) {
89898989
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="bool_is_false_flagbased",user="other_test_user"} 16
89908990
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="bool_is_true_flagbased",user="other_test_user"} 16
89918991
`
8992-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
8992+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
89938993

89948994
// Saving time before second push to avoid purging it before exposing.
89958995
currentTime = time.Now()
@@ -9032,7 +9032,7 @@ func TestIngesterActiveSeriesConfigChanges(t *testing.T) {
90329032
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="team_a",user="test_user"} 16
90339033
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="team_b",user="test_user"} 16
90349034
`
9035-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
9035+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
90369036
},
90379037
},
90389038
"remove runtime overwrite and revert to flag based config": {
@@ -9082,7 +9082,7 @@ func TestIngesterActiveSeriesConfigChanges(t *testing.T) {
90829082
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="team_b",user="test_user"} 16
90839083
`
90849084
// Check tracked Prometheus metrics
9085-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
9085+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
90869086

90879087
// Remove runtime configs
90889088
limits := defaultLimitsTestConfig()
@@ -9118,7 +9118,7 @@ func TestIngesterActiveSeriesConfigChanges(t *testing.T) {
91189118
# TYPE cortex_ingester_active_series_loading gauge
91199119
cortex_ingester_active_series_loading{user="test_user"} 1
91209120
`
9121-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
9121+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
91229122

91239123
// Saving time before second push to avoid purging it before exposing.
91249124
currentTime = time.Now()
@@ -9161,7 +9161,7 @@ func TestIngesterActiveSeriesConfigChanges(t *testing.T) {
91619161
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="bool_is_false_flagbased",user="test_user"} 16
91629162
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="bool_is_true_flagbased",user="test_user"} 16
91639163
`
9164-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
9164+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
91659165
},
91669166
},
91679167
"changing runtime override should result in new metrics": {
@@ -9199,7 +9199,7 @@ func TestIngesterActiveSeriesConfigChanges(t *testing.T) {
91999199
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="bool_is_true_flagbased",user="test_user"} 16
92009200
`
92019201
// Check tracked Prometheus metrics
9202-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
9202+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
92039203

92049204
// Change runtime configs
92059205
activeSeriesTenantOverride := new(TenantLimitsMock)
@@ -9221,7 +9221,7 @@ func TestIngesterActiveSeriesConfigChanges(t *testing.T) {
92219221
# TYPE cortex_ingester_active_series_loading gauge
92229222
cortex_ingester_active_series_loading{user="test_user"} 1
92239223
`
9224-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
9224+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
92259225

92269226
// Saving time before second push to avoid purging it before exposing.
92279227
currentTime = time.Now()
@@ -9260,7 +9260,7 @@ func TestIngesterActiveSeriesConfigChanges(t *testing.T) {
92609260
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="team_c",user="test_user"} 16
92619261
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="team_d",user="test_user"} 16
92629262
`
9263-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
9263+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
92649264
},
92659265
},
92669266
"should cleanup loading metric at close": {
@@ -9310,7 +9310,7 @@ func TestIngesterActiveSeriesConfigChanges(t *testing.T) {
93109310
cortex_ingester_active_native_histogram_buckets_custom_tracker{name="team_b",user="test_user"} 16
93119311
`
93129312
// Check tracked Prometheus metrics
9313-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
9313+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
93149314

93159315
// Remove all configs
93169316
limits := defaultLimitsTestConfig()
@@ -9324,11 +9324,11 @@ func TestIngesterActiveSeriesConfigChanges(t *testing.T) {
93249324
cortex_ingester_active_series_loading{user="test_user"} 1
93259325
cortex_ingester_active_series_loading{user="other_test_user"} 1
93269326
`
9327-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
9327+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
93289328
ingester.closeAllTSDB()
93299329
expectedMetrics = `
93309330
`
9331-
require.NoError(t, testutil.GatherAndCompare(gatherer, strings.NewReader(expectedMetrics), metricNames...))
9331+
util_test.AssertGatherAndCompare(t, gatherer, expectedMetrics, metricNames...)
93329332
},
93339333
},
93349334
}

pkg/util/test/metrics.go

+32
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
package test
44

55
import (
6+
"bufio"
67
"fmt"
78
"io"
89
"strings"
10+
"testing"
11+
12+
"github.com/prometheus/client_golang/prometheus"
13+
"github.com/prometheus/client_golang/prometheus/testutil"
14+
"github.com/stretchr/testify/require"
915
)
1016

1117
type ExpectedMetricsContext struct {
@@ -60,3 +66,29 @@ func (m *ExpectedMetrics) GetOutput() io.Reader {
6066
func (m *ExpectedMetrics) GetNames() []string {
6167
return m.Names
6268
}
69+
70+
func AssertGatherAndCompare(t *testing.T, g prometheus.Gatherer, expectedText string, metrics ...string) {
71+
sc := bufio.NewScanner(strings.NewReader(expectedText))
72+
absent := make([]string, len(metrics))
73+
copy(absent, metrics)
74+
required := make([]string, 0, len(metrics))
75+
c := 0
76+
for sc.Scan() {
77+
c++
78+
line := sc.Text()
79+
for i, metric := range absent {
80+
if strings.HasPrefix(strings.TrimSpace(line), metric+"{") {
81+
absent = append(absent[:i], absent[i+1:]...)
82+
required = append(required, metric)
83+
break
84+
}
85+
}
86+
}
87+
require.Equal(t, len(metrics), len(required)+len(absent)) // Sanity check.
88+
if len(required) > 0 {
89+
require.NoError(t, testutil.GatherAndCompare(g, strings.NewReader(expectedText), required...))
90+
}
91+
if len(absent) > 0 {
92+
require.ErrorContains(t, testutil.GatherAndCompare(g, strings.NewReader(""), absent...), "expected metric name(s) not found")
93+
}
94+
}

0 commit comments

Comments
 (0)