MCPcopy
hub / github.com/prometheus/client_golang / TestHistogramExemplar

Function TestHistogramExemplar

prometheus/histogram_test.go:413–470  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

411}
412
413func TestHistogramExemplar(t *testing.T) {
414 now := time.Now()
415
416 histogram := NewHistogram(HistogramOpts{
417 Name: "test",
418 Help: "test help",
419 Buckets: []float64{1, 2, 3, 4},
420 now: func() time.Time { return now },
421 }).(*histogram)
422
423 ts := timestamppb.New(now)
424 if err := ts.CheckValid(); err != nil {
425 t.Fatal(err)
426 }
427 expectedExemplars := []*dto.Exemplar{
428 nil,
429 {
430 Label: []*dto.LabelPair{
431 {Name: proto.String("id"), Value: proto.String("2")},
432 },
433 Value: proto.Float64(1.6),
434 Timestamp: ts,
435 },
436 nil,
437 {
438 Label: []*dto.LabelPair{
439 {Name: proto.String("id"), Value: proto.String("3")},
440 },
441 Value: proto.Float64(4),
442 Timestamp: ts,
443 },
444 {
445 Label: []*dto.LabelPair{
446 {Name: proto.String("id"), Value: proto.String("4")},
447 },
448 Value: proto.Float64(4.5),
449 Timestamp: ts,
450 },
451 }
452
453 histogram.ObserveWithExemplar(1.5, Labels{"id": "1"})
454 histogram.ObserveWithExemplar(1.6, Labels{"id": "2"}) // To replace exemplar in bucket 0.
455 histogram.ObserveWithExemplar(4, Labels{"id": "3"})
456 histogram.ObserveWithExemplar(4.5, Labels{"id": "4"}) // Should go to +Inf bucket.
457
458 for i, ex := range histogram.exemplars {
459 var got, expected string
460 if val := ex.Load(); val != nil {
461 got = val.(*dto.Exemplar).String()
462 }
463 if expectedExemplars[i] != nil {
464 expected = expectedExemplars[i].String()
465 }
466 if got != expected {
467 t.Errorf("expected exemplar %s, got %s.", expected, got)
468 }
469 }
470}

Callers

nothing calls this directly

Calls 3

NewHistogramFunction · 0.70
ObserveWithExemplarMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected