MCPcopy Index your code
hub / github.com/cortexproject/cortex / compareMatrix

Function compareMatrix

tools/querytee/response_comparator.go:76–129  ·  view source on GitHub ↗
(expectedRaw, actualRaw json.RawMessage, tolerance float64)

Source from the content-addressed store, hash-verified

74}
75
76func compareMatrix(expectedRaw, actualRaw json.RawMessage, tolerance float64) error {
77 var expected, actual model.Matrix
78
79 err := json.Unmarshal(expectedRaw, &expected)
80 if err != nil {
81 return err
82 }
83 err = json.Unmarshal(actualRaw, &actual)
84 if err != nil {
85 return err
86 }
87
88 if len(expected) != len(actual) {
89 return fmt.Errorf("expected %d metrics but got %d", len(expected),
90 len(actual))
91 }
92
93 metricFingerprintToIndexMap := make(map[model.Fingerprint]int, len(expected))
94 for i, actualMetric := range actual {
95 metricFingerprintToIndexMap[actualMetric.Metric.Fingerprint()] = i
96 }
97
98 for _, expectedMetric := range expected {
99 actualMetricIndex, ok := metricFingerprintToIndexMap[expectedMetric.Metric.Fingerprint()]
100 if !ok {
101 return fmt.Errorf("expected metric %s missing from actual response", expectedMetric.Metric)
102 }
103
104 actualMetric := actual[actualMetricIndex]
105 expectedMetricLen := len(expectedMetric.Values)
106 actualMetricLen := len(actualMetric.Values)
107
108 if expectedMetricLen != actualMetricLen {
109 err := fmt.Errorf("expected %d samples for metric %s but got %d", expectedMetricLen,
110 expectedMetric.Metric, actualMetricLen)
111 if expectedMetricLen > 0 && actualMetricLen > 0 {
112 level.Error(util_log.Logger).Log("msg", err.Error(), "oldest-expected-ts", expectedMetric.Values[0].Timestamp,
113 "newest-expected-ts", expectedMetric.Values[expectedMetricLen-1].Timestamp,
114 "oldest-actual-ts", actualMetric.Values[0].Timestamp, "newest-actual-ts", actualMetric.Values[actualMetricLen-1].Timestamp)
115 }
116 return err
117 }
118
119 for i, expectedSamplePair := range expectedMetric.Values {
120 actualSamplePair := actualMetric.Values[i]
121 err := compareSamplePair(expectedSamplePair, actualSamplePair, tolerance)
122 if err != nil {
123 return errors.Wrapf(err, "sample pair not matching for metric %s", expectedMetric.Metric)
124 }
125 }
126 }
127
128 return nil
129}
130
131func compareVector(expectedRaw, actualRaw json.RawMessage, tolerance float64) error {
132 var expected, actual model.Vector

Callers 1

TestCompareMatrixFunction · 0.85

Calls 4

compareSamplePairFunction · 0.85
UnmarshalMethod · 0.45
LogMethod · 0.45
ErrorMethod · 0.45

Tested by 1

TestCompareMatrixFunction · 0.68