MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestSendSumOfHistogramsWithLabels

Function TestSendSumOfHistogramsWithLabels

pkg/util/metrics_helper_test.go:244–317  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

242}
243
244func TestSendSumOfHistogramsWithLabels(t *testing.T) {
245 buckets := []float64{1, 2, 3}
246 user1Metric := prometheus.NewHistogramVec(prometheus.HistogramOpts{Name: "test_metric", Buckets: buckets}, []string{"label_one", "label_two"})
247 user2Metric := prometheus.NewHistogramVec(prometheus.HistogramOpts{Name: "test_metric", Buckets: buckets}, []string{"label_one", "label_two"})
248 user1Metric.WithLabelValues("a", "b").Observe(1)
249 user1Metric.WithLabelValues("a", "c").Observe(2)
250 user2Metric.WithLabelValues("a", "b").Observe(3)
251 user2Metric.WithLabelValues("a", "c").Observe(4)
252
253 user1Reg := prometheus.NewRegistry()
254 user2Reg := prometheus.NewRegistry()
255 user1Reg.MustRegister(user1Metric)
256 user2Reg.MustRegister(user2Metric)
257
258 regs := NewUserRegistries()
259 regs.AddUserRegistry("user-1", user1Reg)
260 regs.AddUserRegistry("user-2", user2Reg)
261 mf := regs.BuildMetricFamiliesPerUser()
262
263 {
264 desc := prometheus.NewDesc("test_metric", "", []string{"label_one"}, nil)
265 actual := collectMetrics(t, func(out chan prometheus.Metric) {
266 mf.SendSumOfHistogramsWithLabels(out, desc, "test_metric", "label_one")
267 })
268 expected := []*dto.Metric{
269 {Label: makeLabels("label_one", "a"), Histogram: &dto.Histogram{SampleCount: uint64p(4), SampleSum: float64p(10), Bucket: []*dto.Bucket{
270 {UpperBound: float64p(1), CumulativeCount: uint64p(1)},
271 {UpperBound: float64p(2), CumulativeCount: uint64p(2)},
272 {UpperBound: float64p(3), CumulativeCount: uint64p(3)},
273 }}},
274 }
275 require.ElementsMatch(t, expected, actual)
276 }
277
278 {
279 desc := prometheus.NewDesc("test_metric", "", []string{"label_two"}, nil)
280 actual := collectMetrics(t, func(out chan prometheus.Metric) {
281 mf.SendSumOfHistogramsWithLabels(out, desc, "test_metric", "label_two")
282 })
283 expected := []*dto.Metric{
284 {Label: makeLabels("label_two", "b"), Histogram: &dto.Histogram{SampleCount: uint64p(2), SampleSum: float64p(4), Bucket: []*dto.Bucket{
285 {UpperBound: float64p(1), CumulativeCount: uint64p(1)},
286 {UpperBound: float64p(2), CumulativeCount: uint64p(1)},
287 {UpperBound: float64p(3), CumulativeCount: uint64p(2)},
288 }}},
289 {Label: makeLabels("label_two", "c"), Histogram: &dto.Histogram{SampleCount: uint64p(2), SampleSum: float64p(6), Bucket: []*dto.Bucket{
290 {UpperBound: float64p(1), CumulativeCount: uint64p(0)},
291 {UpperBound: float64p(2), CumulativeCount: uint64p(1)},
292 {UpperBound: float64p(3), CumulativeCount: uint64p(1)},
293 }}},
294 }
295 require.ElementsMatch(t, expected, actual)
296 }
297
298 {
299 desc := prometheus.NewDesc("test_metric", "", []string{"label_one", "label_two"}, nil)
300 actual := collectMetrics(t, func(out chan prometheus.Metric) {
301 mf.SendSumOfHistogramsWithLabels(out, desc, "test_metric", "label_one", "label_two")

Callers

nothing calls this directly

Calls 8

AddUserRegistryMethod · 0.95
NewUserRegistriesFunction · 0.85
collectMetricsFunction · 0.85
uint64pFunction · 0.85
float64pFunction · 0.85
makeLabelsFunction · 0.70

Tested by

no test coverage detected