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

     1  package e2e
     2  
     3  import (
     4  	"math"
     5  
     6  	io_prometheus_client "github.com/prometheus/client_model/go"
     7  )
     8  
     9  func getMetricValue(m *io_prometheus_client.Metric) float64 {
    10  	if m.GetGauge() != nil {
    11  		return m.GetGauge().GetValue()
    12  	} else if m.GetCounter() != nil {
    13  		return m.GetCounter().GetValue()
    14  	} else if m.GetHistogram() != nil {
    15  		return m.GetHistogram().GetSampleSum()
    16  	} else if m.GetSummary() != nil {
    17  		return m.GetSummary().GetSampleSum()
    18  	} else {
    19  		return 0
    20  	}
    21  }
    22  
    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(metrics []*io_prometheus_client.Metric, opts MetricsOptions) []float64 {
    34  	values := make([]float64, 0, len(metrics))
    35  	for _, m := range metrics {
    36  		values = append(values, opts.GetValue(m))
    37  	}
    38  	return values
    39  }
    40  
    41  func filterMetrics(metrics []*io_prometheus_client.Metric, opts MetricsOptions) []*io_prometheus_client.Metric {
    42  	// If no label matcher is configured, then no filtering should be done.
    43  	if len(opts.LabelMatchers) == 0 {
    44  		return metrics
    45  	}
    46  	if len(metrics) == 0 {
    47  		return metrics
    48  	}
    49  
    50  	filtered := make([]*io_prometheus_client.Metric, 0, len(metrics))
    51  
    52  	for _, m := range metrics {
    53  		metricLabels := map[string]string{}
    54  		for _, lp := range m.GetLabel() {
    55  			metricLabels[lp.GetName()] = lp.GetValue()
    56  		}
    57  
    58  		matches := true
    59  		for _, matcher := range opts.LabelMatchers {
    60  			if !matcher.Matches(metricLabels[matcher.Name]) {
    61  				matches = false
    62  				break
    63  			}
    64  		}
    65  
    66  		if !matches {
    67  			continue
    68  		}
    69  
    70  		filtered = append(filtered, m)
    71  	}
    72  
    73  	return filtered
    74  }
    75  
    76  func SumValues(values []float64) float64 {
    77  	sum := 0.0
    78  	for _, v := range values {
    79  		sum += v
    80  	}
    81  	return sum
    82  }
    83  
    84  func EqualsSingle(expected float64) func(float64) bool {
    85  	return func(v float64) bool {
    86  		return v == expected || (math.IsNaN(v) && math.IsNaN(expected))
    87  	}
    88  }
    89  
    90  // Equals is an isExpected function for WaitSumMetrics that returns true if given single sum is equals to given value.
    91  func Equals(value float64) func(sums ...float64) bool {
    92  	return func(sums ...float64) bool {
    93  		if len(sums) != 1 {
    94  			panic("equals: expected one value")
    95  		}
    96  		return sums[0] == value || math.IsNaN(sums[0]) && math.IsNaN(value)
    97  	}
    98  }
    99  
   100  // Greater is an isExpected function for WaitSumMetrics that returns true if given single sum is greater than given value.
   101  func Greater(value float64) func(sums ...float64) bool {
   102  	return func(sums ...float64) bool {
   103  		if len(sums) != 1 {
   104  			panic("greater: expected one value")
   105  		}
   106  		return sums[0] > value
   107  	}
   108  }
   109  
   110  // GreaterOrEqual is an isExpected function for WaitSumMetrics that returns true if given single sum is greater or equal than given value.
   111  func GreaterOrEqual(value float64) func(sums ...float64) bool {
   112  	return func(sums ...float64) bool {
   113  		if len(sums) != 1 {
   114  			panic("greater: expected one value")
   115  		}
   116  		return sums[0] >= value
   117  	}
   118  }
   119  
   120  // Less is an isExpected function for WaitSumMetrics that returns true if given single sum is less than given value.
   121  func Less(value float64) func(sums ...float64) bool {
   122  	return func(sums ...float64) bool {
   123  		if len(sums) != 1 {
   124  			panic("less: expected one value")
   125  		}
   126  		return sums[0] < value
   127  	}
   128  }
   129  
   130  // EqualsAmongTwo is an isExpected function for WaitSumMetrics that returns true if first sum is equal to the second.
   131  // NOTE: Be careful on scrapes in between of process that changes two metrics. Those are
   132  // usually not atomic.
   133  func EqualsAmongTwo(sums ...float64) bool {
   134  	if len(sums) != 2 {
   135  		panic("equalsAmongTwo: expected two values")
   136  	}
   137  	return sums[0] == sums[1]
   138  }
   139  
   140  // GreaterAmongTwo is an isExpected function for WaitSumMetrics that returns true if first sum is greater than second.
   141  // NOTE: Be careful on scrapes in between of process that changes two metrics. Those are
   142  // usually not atomic.
   143  func GreaterAmongTwo(sums ...float64) bool {
   144  	if len(sums) != 2 {
   145  		panic("greaterAmongTwo: expected two values")
   146  	}
   147  	return sums[0] > sums[1]
   148  }
   149  
   150  // LessAmongTwo is an isExpected function for WaitSumMetrics that returns true if first sum is smaller than second.
   151  // NOTE: Be careful on scrapes in between of process that changes two metrics. Those are
   152  // usually not atomic.
   153  func LessAmongTwo(sums ...float64) bool {
   154  	if len(sums) != 2 {
   155  		panic("lessAmongTwo: expected two values")
   156  	}
   157  	return sums[0] < sums[1]
   158  }