-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
metric_matcher.go
70 lines (54 loc) · 1.63 KB
/
metric_matcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package libmonitoring
import (
"fmt"
"github.com/machadovilaca/operator-observability/pkg/operatormetrics"
"github.com/onsi/gomega/format"
)
type MetricMatcher struct {
Metric operatormetrics.Metric
Labels map[string]string
}
func (matcher *MetricMatcher) FailureMessage(actual interface{}) (message string) {
msg := format.Message(actual, "to contain metric", matcher.Metric.GetOpts().Name)
if matcher.Labels != nil {
msg += fmt.Sprintf(" with labels %v", matcher.Labels)
}
return msg
}
func (matcher *MetricMatcher) NegatedFailureMessage(actual interface{}) (message string) {
msg := format.Message(actual, "not to contain metric", matcher.Metric.GetOpts().Name)
if matcher.Labels != nil {
msg += fmt.Sprintf(" with labels %v", matcher.Labels)
}
return msg
}
func (matcher *MetricMatcher) Match(actual interface{}) (success bool, err error) {
actualMetric, ok := actual.(promResult)
if !ok {
return false, fmt.Errorf("metric matcher requires a libmonitoring.PromResult")
}
actualName, ok := actualMetric.Metric["__name__"]
if !ok {
return false, fmt.Errorf("metric matcher requires a map with __name__ key")
}
nameToMatch := matcher.Metric.GetOpts().Name
if matcher.Metric.GetType() == operatormetrics.HistogramType || matcher.Metric.GetType() == operatormetrics.HistogramVecType {
nameToMatch = nameToMatch + "_bucket"
}
if actualName != nameToMatch {
return false, nil
}
if matcher.Labels == nil {
return true, nil
}
for k, v := range matcher.Labels {
actualValue, ok := actualMetric.Metric[k]
if !ok {
return false, nil
}
if actualValue != v {
return false, nil
}
}
return true, nil
}