Skip to content

Commit 860fd75

Browse files
committed
test: add basic otlp histogram test case
1 parent 7ef3093 commit 860fd75

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

otlp_histogram.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"sync"
88
)
99

10-
var defaultBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
10+
var defaultUpperBounds = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
1111

1212
// OTLP Histogram is a histogram for non-negative values with pre-defined buckets
1313
//
@@ -70,8 +70,9 @@ func (h *OTLPHistogram) Update(v float64) {
7070
}
7171
h.mu.Lock()
7272
h.sum += v
73+
h.count++
7374
if bucketIdx == -1 {
74-
// +Inf, nothing to do
75+
// +Inf, nothing to do, already accounted for in the total count
7576
}
7677
h.buckets[bucketIdx]++
7778
h.mu.Unlock()
@@ -125,18 +126,39 @@ func GetOrCreateOTLPHistogram(name string) *OTLPHistogram {
125126
return defaultSet.GetOrCreateOTLPHistogram(name)
126127
}
127128

129+
func newOTLPHistogram(upperBounds []float64) *OTLPHistogram {
130+
validateBuckets(&upperBounds)
131+
oh := OTLPHistogram{
132+
upperBounds: upperBounds,
133+
buckets: make([]uint64, len(upperBounds)),
134+
}
135+
136+
return &oh
137+
}
138+
139+
func validateBuckets(upperBounds *[]float64) {
140+
// TODO
141+
if len(*upperBounds) == 0 {
142+
panic("not good")
143+
}
144+
}
145+
128146
func (h *OTLPHistogram) marshalTo(prefix string, w io.Writer) {
129147
cumulativeSum := uint64(0)
130148
h.mu.Lock()
149+
count := h.count
150+
sum := h.sum
151+
if count == 0 {
152+
h.mu.Unlock()
153+
return
154+
}
131155
for i, ub := range h.upperBounds {
132156
cumulativeSum += h.buckets[i]
133157
tag := fmt.Sprintf(`le="%v"`, ub)
134158
metricName := addTag(prefix, tag)
135159
name, labels := splitMetricName(metricName)
136160
fmt.Fprintf(w, "%s_bucket%s %d\n", name, labels, cumulativeSum)
137161
}
138-
count := h.count
139-
sum := h.sum
140162
h.mu.Unlock()
141163

142164
tag := fmt.Sprintf("le=%q", "+Inf")

otlp_histogram_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package metrics
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestOTLPHistogramSerial(t *testing.T) {
10+
name := "TestOTLPHistogramSerial"
11+
h := NewOTLPHistogram(name)
12+
13+
// Verify that the histogram is invisible in the output of WritePrometheus when it has no data.
14+
var bb bytes.Buffer
15+
WritePrometheus(&bb, false)
16+
result := bb.String()
17+
if strings.Contains(result, name) {
18+
t.Fatalf("histogram %s shouldn't be visible in the WritePrometheus output; got\n%s", name, result)
19+
}
20+
21+
// Write data to histogram
22+
for i := 98; i < 218; i++ {
23+
h.Update(float64(i)*1e-4)
24+
}
25+
26+
// Make sure the histogram prints <prefix>_bucket on marshalTo call
27+
testMarshalTo(t, h, "prefix", `prefix_bucket{le="0.005"} 0
28+
prefix_bucket{le="0.01"} 3
29+
prefix_bucket{le="0.025"} 120
30+
prefix_bucket{le="0.05"} 120
31+
prefix_bucket{le="0.1"} 120
32+
prefix_bucket{le="0.25"} 120
33+
prefix_bucket{le="0.5"} 120
34+
prefix_bucket{le="1"} 120
35+
prefix_bucket{le="2.5"} 120
36+
prefix_bucket{le="5"} 120
37+
prefix_bucket{le="10"} 120
38+
prefix_bucket{le="+Inf"} 120
39+
prefix_sum 1.8900000000000003
40+
prefix_count 120
41+
`)
42+
}

set.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (s *Set) GetOrCreateHistogram(name string) *Histogram {
139139
//
140140
// The returned histogram is safe to use from concurrent goroutines.
141141
func (s *Set) NewOTLPHistogram(name string) *OTLPHistogram {
142-
h := &OTLPHistogram{}
142+
h := newOTLPHistogram(defaultUpperBounds)
143143
s.registerMetric(name, h)
144144
return h
145145
}
@@ -168,7 +168,7 @@ func (s *Set) GetOrCreateOTLPHistogram(name string) *OTLPHistogram {
168168
}
169169
nmNew := &namedMetric{
170170
name: name,
171-
metric: &OTLPHistogram{},
171+
metric: newOTLPHistogram(defaultUpperBounds),
172172
}
173173
s.mu.Lock()
174174
nm = s.m[name]

0 commit comments

Comments
 (0)