github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/integration/e2e/metrics_options.go (about)

     1  package e2e
     2  
     3  import (
     4  	io_prometheus_client "github.com/prometheus/client_model/go"
     5  	"github.com/prometheus/prometheus/pkg/labels"
     6  )
     7  
     8  var (
     9  	DefaultMetricsOptions = MetricsOptions{
    10  		GetValue:           getMetricValue,
    11  		WaitMissingMetrics: false,
    12  	}
    13  )
    14  
    15  // GetMetricValueFunc defined the signature of a function used to get the metric value.
    16  type GetMetricValueFunc func(m *io_prometheus_client.Metric) float64
    17  
    18  // MetricsOption defined the signature of a function used to manipulate options.
    19  type MetricsOption func(*MetricsOptions)
    20  
    21  // MetricsOptions is the structure holding all options.
    22  type MetricsOptions struct {
    23  	GetValue           GetMetricValueFunc
    24  	LabelMatchers      []*labels.Matcher
    25  	WaitMissingMetrics bool
    26  	SkipMissingMetrics bool
    27  }
    28  
    29  // WithMetricCount is an option to get the histogram/summary count as metric value.
    30  func WithMetricCount(opts *MetricsOptions) {
    31  	opts.GetValue = getMetricCount
    32  }
    33  
    34  // WithLabelMatchers is an option to filter only matching series.
    35  func WithLabelMatchers(matchers ...*labels.Matcher) MetricsOption {
    36  	return func(opts *MetricsOptions) {
    37  		opts.LabelMatchers = matchers
    38  	}
    39  }
    40  
    41  // WithWaitMissingMetrics is an option to wait whenever an expected metric is missing. If this
    42  // option is not enabled, will return error on missing metrics.
    43  func WaitMissingMetrics(opts *MetricsOptions) {
    44  	opts.WaitMissingMetrics = true
    45  }
    46  
    47  // SkipWaitMissingMetrics is an option to skip/ignore whenever an expected metric is missing.
    48  func SkipMissingMetrics(opts *MetricsOptions) {
    49  	opts.SkipMissingMetrics = true
    50  }
    51  
    52  func buildMetricsOptions(opts []MetricsOption) MetricsOptions {
    53  	result := DefaultMetricsOptions
    54  	for _, opt := range opts {
    55  		opt(&result)
    56  	}
    57  	return result
    58  }