MCPcopy Index your code
hub / github.com/coder/coder / verifyCollectedMetrics

Function verifyCollectedMetrics

coderd/prometheusmetrics/aggregator_test.go:173–224  ·  view source on GitHub ↗
(t *testing.T, expected []*agentproto.Stats_Metric, actual []prometheus.Metric)

Source from the content-addressed store, hash-verified

171}
172
173func verifyCollectedMetrics(t *testing.T, expected []*agentproto.Stats_Metric, actual []prometheus.Metric) bool {
174 if len(expected) != len(actual) {
175 t.Logf("expected %d metrics, got %d", len(expected), len(actual))
176 return false
177 }
178
179 // ensure stable iteration order
180 sort.Slice(expected, func(i, j int) bool {
181 return expected[i].Name < expected[j].Name
182 })
183
184 sort.Slice(actual, func(i, j int) bool {
185 m1 := prometheusMetricToString(t, actual[i])
186 m2 := prometheusMetricToString(t, actual[j])
187 return m1 < m2
188 })
189
190 for i, e := range expected {
191 desc := actual[i].Desc()
192 assert.Contains(t, desc.String(), e.Name)
193
194 var d dto.Metric
195 err := actual[i].Write(&d)
196 assert.NoError(t, err)
197
198 switch e.Type {
199 case agentproto.Stats_Metric_COUNTER:
200 if e.Value != d.Counter.GetValue() {
201 return false
202 }
203 case agentproto.Stats_Metric_GAUGE:
204 if e.Value != d.Gauge.GetValue() {
205 return false
206 }
207 default:
208 assert.Failf(t, "unsupported type: %s", string(e.Type))
209 }
210
211 expectedLabels := make([]*agentproto.Stats_Metric_Label, len(e.Labels))
212 copy(expectedLabels, e.Labels)
213
214 dtoLabels := asMetricAgentLabels(d.GetLabel())
215 // dto labels are sorted in alphabetical order.
216 sortFn := func(i, j int) bool {
217 return expectedLabels[i].Name < expectedLabels[j].Name
218 }
219 sort.Slice(expectedLabels, sortFn)
220 sort.Slice(dtoLabels, sortFn)
221 assert.Equal(t, expectedLabels, dtoLabels, d.String())
222 }
223 return true
224}
225
226func prometheusMetricToString(t *testing.T, m prometheus.Metric) string {
227 var sb strings.Builder

Callers 2

TestLabelsAggregationFunction · 0.85

Calls 9

StringMethod · 0.95
prometheusMetricToStringFunction · 0.85
asMetricAgentLabelsFunction · 0.85
LogfMethod · 0.65
WriteMethod · 0.65
ContainsMethod · 0.45
StringMethod · 0.45
GetValueMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected