|
14 | 14 | package prometheus
|
15 | 15 |
|
16 | 16 | import (
|
| 17 | + "reflect" |
17 | 18 | "testing"
|
18 | 19 |
|
| 20 | + "google.golang.org/protobuf/proto" |
| 21 | + |
19 | 22 | dto "github.com/prometheus/client_model/go"
|
20 | 23 | )
|
21 | 24 |
|
@@ -52,6 +55,54 @@ func TestTimerObserve(t *testing.T) {
|
52 | 55 | }
|
53 | 56 | }
|
54 | 57 |
|
| 58 | +func TestTimerObserveWithExemplar(t *testing.T) { |
| 59 | + var ( |
| 60 | + exemplar = Labels{"foo": "bar"} |
| 61 | + his = NewHistogram(HistogramOpts{Name: "test_histogram"}) |
| 62 | + sum = NewSummary(SummaryOpts{Name: "test_summary"}) |
| 63 | + gauge = NewGauge(GaugeOpts{Name: "test_gauge"}) |
| 64 | + ) |
| 65 | + |
| 66 | + func() { |
| 67 | + hisTimer := NewTimer(his) |
| 68 | + sumTimer := NewTimer(sum) |
| 69 | + gaugeTimer := NewTimer(ObserverFunc(gauge.Set)) |
| 70 | + defer hisTimer.ObserveDurationWithExemplar(exemplar) |
| 71 | + // Gauges and summaries does not implement ExemplarObserver, so we expect them to ignore exemplar. |
| 72 | + defer sumTimer.ObserveDurationWithExemplar(exemplar) |
| 73 | + defer gaugeTimer.ObserveDurationWithExemplar(exemplar) |
| 74 | + }() |
| 75 | + |
| 76 | + m := &dto.Metric{} |
| 77 | + his.Write(m) |
| 78 | + if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got { |
| 79 | + t.Errorf("want %d observations for histogram, got %d", want, got) |
| 80 | + } |
| 81 | + var got []*dto.LabelPair |
| 82 | + for _, b := range m.GetHistogram().GetBucket() { |
| 83 | + if b.Exemplar != nil { |
| 84 | + got = b.Exemplar.GetLabel() |
| 85 | + break |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + want := []*dto.LabelPair{{Name: proto.String("foo"), Value: proto.String("bar")}} |
| 90 | + if !reflect.DeepEqual(got, want) { |
| 91 | + t.Errorf("expected %v exemplar labels, got %v", want, got) |
| 92 | + } |
| 93 | + |
| 94 | + m.Reset() |
| 95 | + sum.Write(m) |
| 96 | + if want, got := uint64(1), m.GetSummary().GetSampleCount(); want != got { |
| 97 | + t.Errorf("want %d observations for summary, got %d", want, got) |
| 98 | + } |
| 99 | + m.Reset() |
| 100 | + gauge.Write(m) |
| 101 | + if got := m.GetGauge().GetValue(); got <= 0 { |
| 102 | + t.Errorf("want value > 0 for gauge, got %f", got) |
| 103 | + } |
| 104 | +} |
| 105 | + |
55 | 106 | func TestTimerEmpty(t *testing.T) {
|
56 | 107 | emptyTimer := NewTimer(nil)
|
57 | 108 | emptyTimer.ObserveDuration()
|
|
0 commit comments