Perform integer/float histogram type checking on conversions, and use a consistent method for determining integer vs float histogram

Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
This commit is contained in:
Jeanette Tan 2023-04-22 02:27:15 +08:00
parent 8f1dc4a70f
commit e9a1e26ab7
6 changed files with 24 additions and 13 deletions

View file

@ -20,6 +20,11 @@ import (
func (m Sample) T() int64 { return m.Timestamp } func (m Sample) T() int64 { return m.Timestamp }
func (m Sample) V() float64 { return m.Value } func (m Sample) V() float64 { return m.Value }
func (h Histogram) IsFloatHistogram() bool {
_, ok := h.GetCount().(*Histogram_CountFloat)
return ok
}
func (r *ChunkedReadResponse) PooledMarshal(p *sync.Pool) ([]byte, error) { func (r *ChunkedReadResponse) PooledMarshal(p *sync.Pool) ([]byte, error) {
size := r.Size() size := r.Size()
data, ok := p.Get().(*[]byte) data, ok := p.Get().(*[]byte)

View file

@ -455,10 +455,10 @@ func (c *concreteSeriesIterator) Seek(t int64) chunkenc.ValueType {
} }
func getHistogramValType(h *prompb.Histogram) chunkenc.ValueType { func getHistogramValType(h *prompb.Histogram) chunkenc.ValueType {
if _, isInt := h.GetCount().(*prompb.Histogram_CountInt); isInt { if h.IsFloatHistogram() {
return chunkenc.ValHistogram return chunkenc.ValFloatHistogram
} }
return chunkenc.ValFloatHistogram return chunkenc.ValHistogram
} }
// At implements chunkenc.Iterator. // At implements chunkenc.Iterator.
@ -624,9 +624,11 @@ func exemplarProtoToExemplar(ep prompb.Exemplar) exemplar.Exemplar {
} }
// HistogramProtoToHistogram extracts a (normal integer) Histogram from the // HistogramProtoToHistogram extracts a (normal integer) Histogram from the
// provided proto message. The caller has to make sure that the proto message // provided proto message.
// represents an integer histogram and not a float histogram.
func HistogramProtoToHistogram(hp prompb.Histogram) *histogram.Histogram { func HistogramProtoToHistogram(hp prompb.Histogram) *histogram.Histogram {
if hp.IsFloatHistogram() {
panic("don't call HistogramProtoToHistogram on a float histogram")
}
return &histogram.Histogram{ return &histogram.Histogram{
CounterResetHint: histogram.CounterResetHint(hp.ResetHint), CounterResetHint: histogram.CounterResetHint(hp.ResetHint),
Schema: hp.Schema, Schema: hp.Schema,
@ -642,9 +644,11 @@ func HistogramProtoToHistogram(hp prompb.Histogram) *histogram.Histogram {
} }
// FloatHistogramProtoToFloatHistogram extracts a float Histogram from the // FloatHistogramProtoToFloatHistogram extracts a float Histogram from the
// provided proto message to a Float Histogram. The caller has to make sure that // provided proto message to a Float Histogram.
// the proto message represents a float histogram and not an integer histogram.
func FloatHistogramProtoToFloatHistogram(hp prompb.Histogram) *histogram.FloatHistogram { func FloatHistogramProtoToFloatHistogram(hp prompb.Histogram) *histogram.FloatHistogram {
if !hp.IsFloatHistogram() {
panic("don't call FloatHistogramProtoToFloatHistogram on an integer histogram")
}
return &histogram.FloatHistogram{ return &histogram.FloatHistogram{
CounterResetHint: histogram.CounterResetHint(hp.ResetHint), CounterResetHint: histogram.CounterResetHint(hp.ResetHint),
Schema: hp.Schema, Schema: hp.Schema,
@ -660,9 +664,11 @@ func FloatHistogramProtoToFloatHistogram(hp prompb.Histogram) *histogram.FloatHi
} }
// HistogramProtoToFloatHistogram extracts and converts a (normal integer) histogram from the provided proto message // HistogramProtoToFloatHistogram extracts and converts a (normal integer) histogram from the provided proto message
// to a float histogram. The caller has to make sure that the proto message represents an integer histogram and not a // to a float histogram.
// float histogram.
func HistogramProtoToFloatHistogram(hp prompb.Histogram) *histogram.FloatHistogram { func HistogramProtoToFloatHistogram(hp prompb.Histogram) *histogram.FloatHistogram {
if hp.IsFloatHistogram() {
panic("don't call HistogramProtoToFloatHistogram on a float histogram")
}
return &histogram.FloatHistogram{ return &histogram.FloatHistogram{
CounterResetHint: histogram.CounterResetHint(hp.ResetHint), CounterResetHint: histogram.CounterResetHint(hp.ResetHint),
Schema: hp.Schema, Schema: hp.Schema,

View file

@ -528,7 +528,7 @@ func TestNilHistogramProto(*testing.T) {
// This function will panic if it impromperly handles nil // This function will panic if it impromperly handles nil
// values, causing the test to fail. // values, causing the test to fail.
HistogramProtoToHistogram(prompb.Histogram{}) HistogramProtoToHistogram(prompb.Histogram{})
FloatHistogramProtoToFloatHistogram(prompb.Histogram{}) HistogramProtoToFloatHistogram(prompb.Histogram{})
} }
func exampleHistogram() histogram.Histogram { func exampleHistogram() histogram.Histogram {

View file

@ -802,7 +802,7 @@ func (c *TestWriteClient) Store(_ context.Context, req []byte) error {
for _, histogram := range ts.Histograms { for _, histogram := range ts.Histograms {
count++ count++
if histogram.GetCountFloat() > 0 || histogram.GetZeroCountFloat() > 0 { if histogram.IsFloatHistogram() {
c.receivedFloatHistograms[seriesName] = append(c.receivedFloatHistograms[seriesName], histogram) c.receivedFloatHistograms[seriesName] = append(c.receivedFloatHistograms[seriesName], histogram)
} else { } else {
c.receivedHistograms[seriesName] = append(c.receivedHistograms[seriesName], histogram) c.receivedHistograms[seriesName] = append(c.receivedHistograms[seriesName], histogram)

View file

@ -125,7 +125,7 @@ func (h *writeHandler) write(ctx context.Context, req *prompb.WriteRequest) (err
} }
for _, hp := range ts.Histograms { for _, hp := range ts.Histograms {
if hp.GetCountFloat() > 0 || hp.GetZeroCountFloat() > 0 { // It is a float histogram. if hp.IsFloatHistogram() {
fhs := FloatHistogramProtoToFloatHistogram(hp) fhs := FloatHistogramProtoToFloatHistogram(hp)
_, err = app.AppendHistogram(0, labels, hp.Timestamp, nil, fhs) _, err = app.AppendHistogram(0, labels, hp.Timestamp, nil, fhs)
} else { } else {

View file

@ -68,7 +68,7 @@ func TestRemoteWriteHandler(t *testing.T) {
} }
for _, hp := range ts.Histograms { for _, hp := range ts.Histograms {
if hp.GetCountFloat() > 0 || hp.GetZeroCountFloat() > 0 { // It is a float histogram. if hp.IsFloatHistogram() {
fh := FloatHistogramProtoToFloatHistogram(hp) fh := FloatHistogramProtoToFloatHistogram(hp)
require.Equal(t, mockHistogram{labels, hp.Timestamp, nil, fh}, appendable.histograms[k]) require.Equal(t, mockHistogram{labels, hp.Timestamp, nil, fh}, appendable.histograms[k])
} else { } else {