Skip to content

Commit

Permalink
Merge pull request #803 from havlicekj/histogram_zero_fix
Browse files Browse the repository at this point in the history
Histogram zero fix
  • Loading branch information
jaqx0r committed Feb 6, 2024
2 parents 9f0a27a + 9acb5da commit 5622bb9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion internal/metrics/datum/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d *Buckets) Observe(v float64, ts time.Time) {
defer d.Unlock()

for i, b := range d.Buckets {
if b.Range.Contains(v) {
if v <= b.Range.Max {
d.Buckets[i].Count++
break
}
Expand Down
9 changes: 2 additions & 7 deletions internal/runtime/compiler/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,12 @@ func (c *codegen) VisitBefore(node ast.Node) (ast.Visitor, ast.Node) {
c.errorf(n.Pos(), "a histogram need at least two boundaries")
return nil, n
}
if n.Buckets[0] >= n.Buckets[1] {
c.errorf(n.Pos(), "buckets boundaries must be sorted")
return nil, n
}

if n.Buckets[0] > 0 {
m.Buckets = append(m.Buckets, datum.Range{0, n.Buckets[0]})
}
m.Buckets = append(m.Buckets, datum.Range{n.Buckets[0], n.Buckets[1]})
min := n.Buckets[1]
for _, max := range n.Buckets[2:] {
min := n.Buckets[0]
for _, max := range n.Buckets[1:] {
if max <= min {
c.errorf(n.Pos(), "buckets boundaries must be sorted")
return nil, n
Expand Down
22 changes: 16 additions & 6 deletions internal/runtime/runtime_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ histogram hist3 by f buckets -1, 0, 1
`b 3
b 3
b 3
b 0
`,
0,
metrics.MetricSlice{
Expand All @@ -84,7 +85,10 @@ b 3
{
Value: &datum.Buckets{
Buckets: []datum.BucketCount{
{Range: datum.Range{Min: 0, Max: 1}},
{
Range: datum.Range{Min: 0, Max: 1},
Count: 1,
},
{Range: datum.Range{Min: 1, Max: 2}},
{
Range: datum.Range{Min: 2, Max: 4},
Expand All @@ -93,7 +97,7 @@ b 3
{Range: datum.Range{Min: 4, Max: 8}},
{Range: datum.Range{Min: 8, Max: math.Inf(+1)}},
},
Count: 3,
Count: 4,
Sum: 9,
},
},
Expand All @@ -111,7 +115,10 @@ b 3
Labels: []string{"b"},
Value: &datum.Buckets{
Buckets: []datum.BucketCount{
{Range: datum.Range{Min: 0, Max: 1}},
{
Range: datum.Range{Min: 0, Max: 1},
Count: 1,
},
{Range: datum.Range{Min: 1, Max: 2}},
{
Range: datum.Range{Min: 2, Max: 4},
Expand All @@ -120,7 +127,7 @@ b 3
{Range: datum.Range{Min: 4, Max: 8}},
{Range: datum.Range{Min: 8, Max: math.Inf(+1)}},
},
Count: 3,
Count: 4,
Sum: 9,
},
},
Expand All @@ -139,14 +146,17 @@ b 3
Labels: []string{"b"},
Value: &datum.Buckets{
Buckets: []datum.BucketCount{
{Range: datum.Range{Min: -1, Max: 0}},
{
Range: datum.Range{Min: -1, Max: 0},
Count: 1,
},
{Range: datum.Range{Min: 0, Max: 1}},
{
Range: datum.Range{Min: 1, Max: math.Inf(+1)},
Count: 3,
},
},
Count: 3,
Count: 4,
Sum: 9,
},
},
Expand Down

0 comments on commit 5622bb9

Please sign in to comment.