Skip to content

Commit 69630eb

Browse files
committed
Basic functional options support to metrics assertion in integration tests
Signed-off-by: Marco Pracucci <[email protected]>
1 parent b404a95 commit 69630eb

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
lines changed

integration/e2e/composite_service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (s *CompositeHTTPService) WaitSumMetrics(isExpected func(sums ...float64) b
4545
)
4646

4747
for s.retryBackoff.Reset(); s.retryBackoff.Ongoing(); {
48-
sums, err = s.SumMetrics(metricNames...)
48+
sums, err = s.SumMetrics(metricNames)
4949
if err != nil {
5050
return err
5151
}
@@ -80,11 +80,11 @@ func (s *CompositeHTTPService) WaitSumMetricWithLabels(isExpected func(sums floa
8080
}
8181

8282
// SumMetrics returns the sum of the values of each given metric names.
83-
func (s *CompositeHTTPService) SumMetrics(metricNames ...string) ([]float64, error) {
83+
func (s *CompositeHTTPService) SumMetrics(metricNames []string, opts ...MetricsOption) ([]float64, error) {
8484
sums := make([]float64, len(metricNames))
8585

8686
for _, service := range s.services {
87-
partials, err := service.SumMetrics(metricNames...)
87+
partials, err := service.SumMetrics(metricNames, opts...)
8888
if err != nil {
8989
return nil, err
9090
}

integration/e2e/metrics.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
io_prometheus_client "github.com/prometheus/client_model/go"
77
)
88

9-
func getValue(m *io_prometheus_client.Metric) float64 {
9+
func getMetricValue(m *io_prometheus_client.Metric) float64 {
1010
if m.GetGauge() != nil {
1111
return m.GetGauge().GetValue()
1212
} else if m.GetCounter() != nil {
@@ -20,10 +20,28 @@ func getValue(m *io_prometheus_client.Metric) float64 {
2020
}
2121
}
2222

23-
func sumValues(family *io_prometheus_client.MetricFamily) float64 {
24-
sum := 0.0
23+
func getMetricCount(m *io_prometheus_client.Metric) float64 {
24+
if m.GetHistogram() != nil {
25+
return float64(m.GetHistogram().GetSampleCount())
26+
} else if m.GetSummary() != nil {
27+
return float64(m.GetSummary().GetSampleCount())
28+
} else {
29+
return 0
30+
}
31+
}
32+
33+
func getValues(family *io_prometheus_client.MetricFamily, get GetMetricValueFunc) []float64 {
34+
values := make([]float64, 0, len(family.Metric))
2535
for _, m := range family.Metric {
26-
sum += getValue(m)
36+
values = append(values, get(m))
37+
}
38+
return values
39+
}
40+
41+
func sumValues(values []float64) float64 {
42+
sum := 0.0
43+
for _, v := range values {
44+
sum += v
2745
}
2846
return sum
2947
}

integration/e2e/metrics_options.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package e2e
2+
3+
import io_prometheus_client "github.com/prometheus/client_model/go"
4+
5+
var (
6+
DefaultMetricsOptions = MetricsOptions{
7+
GetValue: getMetricValue,
8+
}
9+
)
10+
11+
// GetMetricValueFunc defined the signature of a function used to get the metric value.
12+
type GetMetricValueFunc func(m *io_prometheus_client.Metric) float64
13+
14+
// MetricsOption defined the signature of a function used to manipulate options.
15+
type MetricsOption func(*MetricsOptions)
16+
17+
// MetricsOptions is the structure holding all options.
18+
type MetricsOptions struct {
19+
GetValue GetMetricValueFunc
20+
}
21+
22+
// WithMetricCount is an option to get the histogram/summary count as metric value.
23+
func WithMetricCount(opts *MetricsOptions) {
24+
opts.GetValue = getMetricCount
25+
}
26+
27+
func buildMetricsOptions(opts []MetricsOption) MetricsOptions {
28+
result := DefaultMetricsOptions
29+
for _, opt := range opts {
30+
opt(&result)
31+
}
32+
return result
33+
}

integration/e2e/service.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,17 @@ func (s *HTTPService) NetworkHTTPEndpointFor(networkName string) string {
522522
// WaitSumMetrics waits for at least one instance of each given metric names to be present and their sums, returning true
523523
// when passed to given isExpected(...).
524524
func (s *HTTPService) WaitSumMetrics(isExpected func(sums ...float64) bool, metricNames ...string) error {
525+
return s.WaitSumMetricsWithOptions(isExpected, metricNames)
526+
}
527+
528+
func (s *HTTPService) WaitSumMetricsWithOptions(isExpected func(sums ...float64) bool, metricNames []string, opts ...MetricsOption) error {
525529
var (
526530
sums []float64
527531
err error
528532
)
529533

530534
for s.retryBackoff.Reset(); s.retryBackoff.Ongoing(); {
531-
sums, err = s.SumMetrics(metricNames...)
535+
sums, err = s.SumMetrics(metricNames, opts...)
532536
if err != nil {
533537
return err
534538
}
@@ -544,7 +548,8 @@ func (s *HTTPService) WaitSumMetrics(isExpected func(sums ...float64) bool, metr
544548
}
545549

546550
// SumMetrics returns the sum of the values of each given metric names.
547-
func (s *HTTPService) SumMetrics(metricNames ...string) ([]float64, error) {
551+
func (s *HTTPService) SumMetrics(metricNames []string, opts ...MetricsOption) ([]float64, error) {
552+
options := buildMetricsOptions(opts)
548553
sums := make([]float64, len(metricNames))
549554

550555
metrics, err := s.Metrics()
@@ -563,7 +568,7 @@ func (s *HTTPService) SumMetrics(metricNames ...string) ([]float64, error) {
563568

564569
// Check if the metric is exported.
565570
if mf, ok := families[m]; ok {
566-
sums[i] = sumValues(mf)
571+
sums[i] = sumValues(getValues(mf, options.GetValue))
567572
continue
568573
}
569574
return nil, errors.Errorf("metric %s not found in %s metric page", m, s.name)
@@ -582,7 +587,7 @@ func (s *HTTPService) WaitForMetricWithLabels(okFn func(v float64) bool, metricN
582587
}
583588

584589
for _, m := range ms {
585-
if okFn(getValue(m)) {
590+
if okFn(getMetricValue(m)) {
586591
return nil
587592
}
588593
}
@@ -602,7 +607,7 @@ func (s *HTTPService) SumMetricWithLabels(metricName string, expectedLabels map[
602607
}
603608

604609
for _, m := range ms {
605-
sum += getValue(m)
610+
sum += getMetricValue(m)
606611
}
607612
return sum, nil
608613
}

integration/query_frontend_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ func runQueryFrontendTest(t *testing.T, testMissingMetricName bool, setup queryF
203203
}
204204
require.NoError(t, queryFrontend.WaitSumMetrics(e2e.Equals(numUsers*numQueriesPerUser+extra), "cortex_query_frontend_queries_total"))
205205

206+
// The number of received request is greater then the query requests because include
207+
// requests to /metrics and /ready.
208+
require.NoError(t, queryFrontend.WaitSumMetricsWithOptions(e2e.Greater(numUsers*numQueriesPerUser), []string{"cortex_request_duration_seconds"}, e2e.WithMetricCount))
209+
require.NoError(t, querier.WaitSumMetricsWithOptions(e2e.Greater(numUsers*numQueriesPerUser), []string{"cortex_request_duration_seconds"}, e2e.WithMetricCount))
210+
206211
// Ensure no service-specific metrics prefix is used by the wrong service.
207212
assertServiceMetricsPrefixes(t, Distributor, distributor)
208213
assertServiceMetricsPrefixes(t, Ingester, ingester)

0 commit comments

Comments
 (0)