-
Notifications
You must be signed in to change notification settings - Fork 837
Basic functional options support to metrics assertion in integration tests #2522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
69630eb
dc1db59
96735fb
9ef91ea
3df92f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ import ( | |
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/pkg/errors" | ||
|
|
||
| "github.com/cortexproject/cortex/pkg/util" | ||
| ) | ||
|
|
||
|
|
@@ -39,52 +41,41 @@ func (s *CompositeHTTPService) Instances() []*HTTPService { | |
| // WaitSumMetrics waits for at least one instance of each given metric names to be present and their sums, returning true | ||
| // when passed to given isExpected(...). | ||
| func (s *CompositeHTTPService) WaitSumMetrics(isExpected func(sums ...float64) bool, metricNames ...string) error { | ||
| return s.WaitSumMetricsWithOptions(isExpected, metricNames) | ||
| } | ||
|
|
||
| func (s *CompositeHTTPService) WaitSumMetricsWithOptions(isExpected func(sums ...float64) bool, metricNames []string, opts ...MetricsOption) error { | ||
| var ( | ||
| sums []float64 | ||
| err error | ||
| sums []float64 | ||
| err error | ||
| options = buildMetricsOptions(opts) | ||
| ) | ||
|
|
||
| for s.retryBackoff.Reset(); s.retryBackoff.Ongoing(); { | ||
| sums, err = s.SumMetrics(metricNames...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if isExpected(sums...) { | ||
| return nil | ||
| sums, err = s.SumMetrics(metricNames, opts...) | ||
| if options.WaitMissingMetrics && errors.Is(err, errMissingMetric) { | ||
| continue | ||
| } | ||
|
|
||
| s.retryBackoff.Wait() | ||
| } | ||
|
|
||
| return fmt.Errorf("unable to find metrics %s with expected values. Last values: %v", metricNames, sums) | ||
| } | ||
|
|
||
| func (s *CompositeHTTPService) WaitSumMetricWithLabels(isExpected func(sums float64) bool, metricName string, expectedLabels map[string]string) error { | ||
| lastSum := 0.0 | ||
|
|
||
| for s.retryBackoff.Reset(); s.retryBackoff.Ongoing(); { | ||
| lastSum, err := s.SumMetricWithLabels(metricName, expectedLabels) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if isExpected(lastSum) { | ||
| if isExpected(sums...) { | ||
| return nil | ||
| } | ||
|
|
||
| s.retryBackoff.Wait() | ||
| } | ||
|
|
||
| return fmt.Errorf("unable to find metric %s with labels %v with expected value. Last value: %v", metricName, expectedLabels, lastSum) | ||
| return fmt.Errorf("unable to find metrics %s with expected values. Last error: %v. Last values: %v", metricNames, err, sums) | ||
| } | ||
|
|
||
| // SumMetrics returns the sum of the values of each given metric names. | ||
| func (s *CompositeHTTPService) SumMetrics(metricNames ...string) ([]float64, error) { | ||
| func (s *CompositeHTTPService) SumMetrics(metricNames []string, opts ...MetricsOption) ([]float64, error) { | ||
|
||
| sums := make([]float64, len(metricNames)) | ||
|
|
||
| for _, service := range s.services { | ||
| partials, err := service.SumMetrics(metricNames...) | ||
| partials, err := service.SumMetrics(metricNames, opts...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -100,19 +91,3 @@ func (s *CompositeHTTPService) SumMetrics(metricNames ...string) ([]float64, err | |
|
|
||
| return sums, nil | ||
| } | ||
|
|
||
| // SumMetricWithLabels returns the sum of the values of metric with matching labels across all services. | ||
| func (s *CompositeHTTPService) SumMetricWithLabels(metricName string, expectedLabels map[string]string) (float64, error) { | ||
| sum := 0.0 | ||
|
|
||
| for _, service := range s.services { | ||
| s, err := service.SumMetricWithLabels(metricName, expectedLabels) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| sum += s | ||
| } | ||
|
|
||
| return sum, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package e2e | ||
|
|
||
| import ( | ||
| io_prometheus_client "github.com/prometheus/client_model/go" | ||
| "github.com/prometheus/prometheus/pkg/labels" | ||
| ) | ||
|
|
||
| var ( | ||
| DefaultMetricsOptions = MetricsOptions{ | ||
| GetValue: getMetricValue, | ||
| WaitMissingMetrics: false, | ||
| } | ||
| ) | ||
|
|
||
| // GetMetricValueFunc defined the signature of a function used to get the metric value. | ||
| type GetMetricValueFunc func(m *io_prometheus_client.Metric) float64 | ||
|
|
||
| // MetricsOption defined the signature of a function used to manipulate options. | ||
| type MetricsOption func(*MetricsOptions) | ||
|
|
||
| // MetricsOptions is the structure holding all options. | ||
| type MetricsOptions struct { | ||
| GetValue GetMetricValueFunc | ||
| LabelMatchers []*labels.Matcher | ||
| WaitMissingMetrics bool | ||
| } | ||
|
|
||
| // WithMetricCount is an option to get the histogram/summary count as metric value. | ||
| func WithMetricCount(opts *MetricsOptions) { | ||
| opts.GetValue = getMetricCount | ||
| } | ||
|
|
||
| // WithLabelMatchers is an option to filter only matching series. | ||
| func WithLabelMatchers(matchers ...*labels.Matcher) MetricsOption { | ||
| return func(opts *MetricsOptions) { | ||
| opts.LabelMatchers = matchers | ||
| } | ||
| } | ||
|
|
||
| // WithWaitMissingMetrics is an option to wait whenever an expected metric is missing. If this | ||
| // option is not enabled, will return error on missing metrics. | ||
| func WaitMissingMetrics(opts *MetricsOptions) { | ||
| opts.WaitMissingMetrics = true | ||
| } | ||
|
|
||
| func buildMetricsOptions(opts []MetricsOption) MetricsOptions { | ||
| result := DefaultMetricsOptions | ||
| for _, opt := range opts { | ||
| opt(&result) | ||
| } | ||
| return result | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!