mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Fix HistogramChunk's AtFloatHistogram()
Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>
This commit is contained in:
parent
3362bf6d79
commit
507bfa46fd
|
@ -363,6 +363,7 @@ func (m *Exemplar) GetTimestamp() int64 {
|
||||||
// integer histogram as well as a float histogram.
|
// integer histogram as well as a float histogram.
|
||||||
type Histogram struct {
|
type Histogram struct {
|
||||||
// Types that are valid to be assigned to Count:
|
// Types that are valid to be assigned to Count:
|
||||||
|
//
|
||||||
// *Histogram_CountInt
|
// *Histogram_CountInt
|
||||||
// *Histogram_CountFloat
|
// *Histogram_CountFloat
|
||||||
Count isHistogram_Count `protobuf_oneof:"count"`
|
Count isHistogram_Count `protobuf_oneof:"count"`
|
||||||
|
@ -377,6 +378,7 @@ type Histogram struct {
|
||||||
Schema int32 `protobuf:"zigzag32,4,opt,name=schema,proto3" json:"schema,omitempty"`
|
Schema int32 `protobuf:"zigzag32,4,opt,name=schema,proto3" json:"schema,omitempty"`
|
||||||
ZeroThreshold float64 `protobuf:"fixed64,5,opt,name=zero_threshold,json=zeroThreshold,proto3" json:"zero_threshold,omitempty"`
|
ZeroThreshold float64 `protobuf:"fixed64,5,opt,name=zero_threshold,json=zeroThreshold,proto3" json:"zero_threshold,omitempty"`
|
||||||
// Types that are valid to be assigned to ZeroCount:
|
// Types that are valid to be assigned to ZeroCount:
|
||||||
|
//
|
||||||
// *Histogram_ZeroCountInt
|
// *Histogram_ZeroCountInt
|
||||||
// *Histogram_ZeroCountFloat
|
// *Histogram_ZeroCountFloat
|
||||||
ZeroCount isHistogram_ZeroCount `protobuf_oneof:"zero_count"`
|
ZeroCount isHistogram_ZeroCount `protobuf_oneof:"zero_count"`
|
||||||
|
|
|
@ -738,6 +738,7 @@ func (it *histogramIterator) Next() ValueType {
|
||||||
}
|
}
|
||||||
it.sum = math.Float64frombits(sum)
|
it.sum = math.Float64frombits(sum)
|
||||||
|
|
||||||
|
var current int64
|
||||||
for i := range it.pBuckets {
|
for i := range it.pBuckets {
|
||||||
v, err := readVarbitInt(&it.br)
|
v, err := readVarbitInt(&it.br)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -745,8 +746,10 @@ func (it *histogramIterator) Next() ValueType {
|
||||||
return ValNone
|
return ValNone
|
||||||
}
|
}
|
||||||
it.pBuckets[i] = v
|
it.pBuckets[i] = v
|
||||||
it.pFloatBuckets[i] = float64(v)
|
current += it.pBuckets[i]
|
||||||
|
it.pFloatBuckets[i] = float64(current)
|
||||||
}
|
}
|
||||||
|
current = 0
|
||||||
for i := range it.nBuckets {
|
for i := range it.nBuckets {
|
||||||
v, err := readVarbitInt(&it.br)
|
v, err := readVarbitInt(&it.br)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -754,7 +757,8 @@ func (it *histogramIterator) Next() ValueType {
|
||||||
return ValNone
|
return ValNone
|
||||||
}
|
}
|
||||||
it.nBuckets[i] = v
|
it.nBuckets[i] = v
|
||||||
it.nFloatBuckets[i] = float64(v)
|
current += it.nBuckets[i]
|
||||||
|
it.nFloatBuckets[i] = float64(current)
|
||||||
}
|
}
|
||||||
|
|
||||||
it.numRead++
|
it.numRead++
|
||||||
|
|
|
@ -330,3 +330,140 @@ func TestHistoChunkAppendable(t *testing.T) {
|
||||||
require.True(t, cr)
|
require.True(t, cr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAtFloatHistogram(t *testing.T) {
|
||||||
|
input := []histogram.Histogram{
|
||||||
|
{
|
||||||
|
Schema: 0,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1234.5,
|
||||||
|
ZeroThreshold: 0.001,
|
||||||
|
ZeroCount: 4,
|
||||||
|
PositiveSpans: []histogram.Span{
|
||||||
|
{Offset: 0, Length: 4},
|
||||||
|
{Offset: 0, Length: 0},
|
||||||
|
{Offset: 0, Length: 3},
|
||||||
|
},
|
||||||
|
PositiveBuckets: []int64{1, 1, -1, 0, 0, 0, 0},
|
||||||
|
NegativeSpans: []histogram.Span{
|
||||||
|
{Offset: 1, Length: 4},
|
||||||
|
{Offset: 2, Length: 0},
|
||||||
|
{Offset: 2, Length: 3},
|
||||||
|
},
|
||||||
|
NegativeBuckets: []int64{1, 1, -1, 1, 0, 0, 0},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Schema: 0,
|
||||||
|
Count: 36,
|
||||||
|
Sum: 2345.6,
|
||||||
|
ZeroThreshold: 0.001,
|
||||||
|
ZeroCount: 5,
|
||||||
|
PositiveSpans: []histogram.Span{
|
||||||
|
{Offset: 0, Length: 4},
|
||||||
|
{Offset: 0, Length: 0},
|
||||||
|
{Offset: 0, Length: 3},
|
||||||
|
},
|
||||||
|
PositiveBuckets: []int64{1, 2, -2, 1, -1, 0, 0},
|
||||||
|
NegativeSpans: []histogram.Span{
|
||||||
|
{Offset: 1, Length: 4},
|
||||||
|
{Offset: 2, Length: 0},
|
||||||
|
{Offset: 2, Length: 3},
|
||||||
|
},
|
||||||
|
NegativeBuckets: []int64{1, 3, -2, 5, -2, 0, -3},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Schema: 0,
|
||||||
|
Count: 36,
|
||||||
|
Sum: 1111.1,
|
||||||
|
ZeroThreshold: 0.001,
|
||||||
|
ZeroCount: 5,
|
||||||
|
PositiveSpans: []histogram.Span{
|
||||||
|
{Offset: 0, Length: 4},
|
||||||
|
{Offset: 0, Length: 0},
|
||||||
|
{Offset: 0, Length: 3},
|
||||||
|
},
|
||||||
|
PositiveBuckets: []int64{1, 2, -2, 2, -1, 0, 0},
|
||||||
|
NegativeSpans: []histogram.Span{
|
||||||
|
{Offset: 1, Length: 4},
|
||||||
|
{Offset: 2, Length: 0},
|
||||||
|
{Offset: 2, Length: 3},
|
||||||
|
},
|
||||||
|
NegativeBuckets: []int64{1, 3, -2, 5, -1, 0, -3},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expOutput := []*histogram.FloatHistogram{
|
||||||
|
{
|
||||||
|
Schema: 0,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1234.5,
|
||||||
|
ZeroThreshold: 0.001,
|
||||||
|
ZeroCount: 4,
|
||||||
|
PositiveSpans: []histogram.Span{
|
||||||
|
{Offset: 0, Length: 4},
|
||||||
|
{Offset: 0, Length: 0},
|
||||||
|
{Offset: 0, Length: 3},
|
||||||
|
},
|
||||||
|
PositiveBuckets: []float64{1, 2, 1, 1, 1, 1, 1},
|
||||||
|
NegativeSpans: []histogram.Span{
|
||||||
|
{Offset: 1, Length: 4},
|
||||||
|
{Offset: 2, Length: 0},
|
||||||
|
{Offset: 2, Length: 3},
|
||||||
|
},
|
||||||
|
NegativeBuckets: []float64{1, 2, 1, 2, 2, 2, 2},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Schema: 0,
|
||||||
|
Count: 36,
|
||||||
|
Sum: 2345.6,
|
||||||
|
ZeroThreshold: 0.001,
|
||||||
|
ZeroCount: 5,
|
||||||
|
PositiveSpans: []histogram.Span{
|
||||||
|
{Offset: 0, Length: 4},
|
||||||
|
{Offset: 0, Length: 0},
|
||||||
|
{Offset: 0, Length: 3},
|
||||||
|
},
|
||||||
|
PositiveBuckets: []float64{1, 3, 1, 2, 1, 1, 1},
|
||||||
|
NegativeSpans: []histogram.Span{
|
||||||
|
{Offset: 1, Length: 4},
|
||||||
|
{Offset: 2, Length: 0},
|
||||||
|
{Offset: 2, Length: 3},
|
||||||
|
},
|
||||||
|
NegativeBuckets: []float64{1, 4, 2, 7, 5, 5, 2},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Schema: 0,
|
||||||
|
Count: 36,
|
||||||
|
Sum: 1111.1,
|
||||||
|
ZeroThreshold: 0.001,
|
||||||
|
ZeroCount: 5,
|
||||||
|
PositiveSpans: []histogram.Span{
|
||||||
|
{Offset: 0, Length: 4},
|
||||||
|
{Offset: 0, Length: 0},
|
||||||
|
{Offset: 0, Length: 3},
|
||||||
|
},
|
||||||
|
PositiveBuckets: []float64{1, 3, 1, 3, 2, 2, 2},
|
||||||
|
NegativeSpans: []histogram.Span{
|
||||||
|
{Offset: 1, Length: 4},
|
||||||
|
{Offset: 2, Length: 0},
|
||||||
|
{Offset: 2, Length: 3},
|
||||||
|
},
|
||||||
|
NegativeBuckets: []float64{1, 4, 2, 7, 6, 6, 3},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
chk := NewHistogramChunk()
|
||||||
|
app, err := chk.Appender()
|
||||||
|
require.NoError(t, err)
|
||||||
|
for i := range input {
|
||||||
|
app.AppendHistogram(int64(i), &input[i])
|
||||||
|
}
|
||||||
|
it := chk.Iterator(nil)
|
||||||
|
i := int64(0)
|
||||||
|
for it.Next() != ValNone {
|
||||||
|
ts, h := it.AtFloatHistogram()
|
||||||
|
require.Equal(t, i, ts)
|
||||||
|
require.Equal(t, expOutput[i], h, "histogram %d unequal", i)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue