mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
writev2: Add basic support for nhcb.
Signed-off-by: bwplotka <bwplotka@gmail.com>
This commit is contained in:
parent
9198952f7c
commit
10ecd7664e
|
@ -86,7 +86,6 @@ func (h Histogram) IsFloatHistogram() bool {
|
||||||
|
|
||||||
// ToIntHistogram returns integer Prometheus histogram from the remote implementation
|
// ToIntHistogram returns integer Prometheus histogram from the remote implementation
|
||||||
// of integer histogram. If it's a float histogram, the method returns nil.
|
// of integer histogram. If it's a float histogram, the method returns nil.
|
||||||
// TODO(bwplotka): Add support for incoming NHCB.
|
|
||||||
func (h Histogram) ToIntHistogram() *histogram.Histogram {
|
func (h Histogram) ToIntHistogram() *histogram.Histogram {
|
||||||
if h.IsFloatHistogram() {
|
if h.IsFloatHistogram() {
|
||||||
return nil
|
return nil
|
||||||
|
@ -102,13 +101,13 @@ func (h Histogram) ToIntHistogram() *histogram.Histogram {
|
||||||
PositiveBuckets: h.GetPositiveDeltas(),
|
PositiveBuckets: h.GetPositiveDeltas(),
|
||||||
NegativeSpans: spansProtoToSpans(h.GetNegativeSpans()),
|
NegativeSpans: spansProtoToSpans(h.GetNegativeSpans()),
|
||||||
NegativeBuckets: h.GetNegativeDeltas(),
|
NegativeBuckets: h.GetNegativeDeltas(),
|
||||||
|
CustomValues: h.GetCustomValues(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToFloatHistogram returns float Prometheus histogram from the remote implementation
|
// ToFloatHistogram returns float Prometheus histogram from the remote implementation
|
||||||
// of float histogram. If the underlying implementation is an integer histogram, a
|
// of float histogram. If the underlying implementation is an integer histogram, a
|
||||||
// conversion is performed.
|
// conversion is performed.
|
||||||
// TODO(bwplotka): Add support for incoming NHCB.
|
|
||||||
func (h Histogram) ToFloatHistogram() *histogram.FloatHistogram {
|
func (h Histogram) ToFloatHistogram() *histogram.FloatHistogram {
|
||||||
if h.IsFloatHistogram() {
|
if h.IsFloatHistogram() {
|
||||||
return &histogram.FloatHistogram{
|
return &histogram.FloatHistogram{
|
||||||
|
@ -122,6 +121,7 @@ func (h Histogram) ToFloatHistogram() *histogram.FloatHistogram {
|
||||||
PositiveBuckets: h.GetPositiveCounts(),
|
PositiveBuckets: h.GetPositiveCounts(),
|
||||||
NegativeSpans: spansProtoToSpans(h.GetNegativeSpans()),
|
NegativeSpans: spansProtoToSpans(h.GetNegativeSpans()),
|
||||||
NegativeBuckets: h.GetNegativeCounts(),
|
NegativeBuckets: h.GetNegativeCounts(),
|
||||||
|
CustomValues: h.GetCustomValues(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Conversion from integer histogram.
|
// Conversion from integer histogram.
|
||||||
|
@ -136,6 +136,7 @@ func (h Histogram) ToFloatHistogram() *histogram.FloatHistogram {
|
||||||
PositiveBuckets: deltasToCounts(h.GetPositiveDeltas()),
|
PositiveBuckets: deltasToCounts(h.GetPositiveDeltas()),
|
||||||
NegativeSpans: spansProtoToSpans(h.GetNegativeSpans()),
|
NegativeSpans: spansProtoToSpans(h.GetNegativeSpans()),
|
||||||
NegativeBuckets: deltasToCounts(h.GetNegativeDeltas()),
|
NegativeBuckets: deltasToCounts(h.GetNegativeDeltas()),
|
||||||
|
CustomValues: h.GetCustomValues(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,6 +172,7 @@ func FromIntHistogram(timestamp int64, h *histogram.Histogram) Histogram {
|
||||||
PositiveSpans: spansToSpansProto(h.PositiveSpans),
|
PositiveSpans: spansToSpansProto(h.PositiveSpans),
|
||||||
PositiveDeltas: h.PositiveBuckets,
|
PositiveDeltas: h.PositiveBuckets,
|
||||||
ResetHint: Histogram_ResetHint(h.CounterResetHint),
|
ResetHint: Histogram_ResetHint(h.CounterResetHint),
|
||||||
|
CustomValues: h.CustomValues,
|
||||||
Timestamp: timestamp,
|
Timestamp: timestamp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,6 +190,7 @@ func FromFloatHistogram(timestamp int64, fh *histogram.FloatHistogram) Histogram
|
||||||
PositiveSpans: spansToSpansProto(fh.PositiveSpans),
|
PositiveSpans: spansToSpansProto(fh.PositiveSpans),
|
||||||
PositiveCounts: fh.PositiveBuckets,
|
PositiveCounts: fh.PositiveBuckets,
|
||||||
ResetHint: Histogram_ResetHint(fh.CounterResetHint),
|
ResetHint: Histogram_ResetHint(fh.CounterResetHint),
|
||||||
|
CustomValues: fh.CustomValues,
|
||||||
Timestamp: timestamp,
|
Timestamp: timestamp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -144,10 +144,12 @@ func TestToHistogram_Empty(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE(bwplotka): This is technically not a valid histogram, but it represents
|
||||||
|
// important cases to test when copying or converting to/from int/float histograms.
|
||||||
func testIntHistogram() histogram.Histogram {
|
func testIntHistogram() histogram.Histogram {
|
||||||
return histogram.Histogram{
|
return histogram.Histogram{
|
||||||
CounterResetHint: histogram.GaugeType,
|
CounterResetHint: histogram.GaugeType,
|
||||||
Schema: 0,
|
Schema: 1,
|
||||||
Count: 19,
|
Count: 19,
|
||||||
Sum: 2.7,
|
Sum: 2.7,
|
||||||
ZeroThreshold: 1e-128,
|
ZeroThreshold: 1e-128,
|
||||||
|
@ -163,13 +165,16 @@ func testIntHistogram() histogram.Histogram {
|
||||||
{Offset: 0, Length: 1},
|
{Offset: 0, Length: 1},
|
||||||
},
|
},
|
||||||
NegativeBuckets: []int64{1, 2, -2, 1, -1, 0},
|
NegativeBuckets: []int64{1, 2, -2, 1, -1, 0},
|
||||||
|
CustomValues: []float64{21421, 523},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE(bwplotka): This is technically not a valid histogram, but it represents
|
||||||
|
// important cases to test when copying or converting to/from int/float histograms.
|
||||||
func testFloatHistogram() histogram.FloatHistogram {
|
func testFloatHistogram() histogram.FloatHistogram {
|
||||||
return histogram.FloatHistogram{
|
return histogram.FloatHistogram{
|
||||||
CounterResetHint: histogram.GaugeType,
|
CounterResetHint: histogram.GaugeType,
|
||||||
Schema: 0,
|
Schema: 1,
|
||||||
Count: 19,
|
Count: 19,
|
||||||
Sum: 2.7,
|
Sum: 2.7,
|
||||||
ZeroThreshold: 1e-128,
|
ZeroThreshold: 1e-128,
|
||||||
|
@ -185,22 +190,29 @@ func testFloatHistogram() histogram.FloatHistogram {
|
||||||
{Offset: 0, Length: 1},
|
{Offset: 0, Length: 1},
|
||||||
},
|
},
|
||||||
NegativeBuckets: []float64{1, 3, 1, 2, 1, 1},
|
NegativeBuckets: []float64{1, 3, 1, 2, 1, 1},
|
||||||
|
CustomValues: []float64{21421, 523},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFromIntToFloatOrIntHistogram(t *testing.T) {
|
func TestFromIntToFloatOrIntHistogram(t *testing.T) {
|
||||||
testIntHist := testIntHistogram()
|
|
||||||
testFloatHist := testFloatHistogram()
|
|
||||||
|
|
||||||
t.Run("v1", func(t *testing.T) {
|
t.Run("v1", func(t *testing.T) {
|
||||||
h := prompb.FromIntHistogram(123, testIntHist.Copy())
|
// v1 does not support nhcb.
|
||||||
|
testIntHistWithoutNHCB := testIntHistogram()
|
||||||
|
testIntHistWithoutNHCB.CustomValues = nil
|
||||||
|
testFloatHistWithoutNHCB := testFloatHistogram()
|
||||||
|
testFloatHistWithoutNHCB.CustomValues = nil
|
||||||
|
|
||||||
|
h := prompb.FromIntHistogram(123, &testIntHistWithoutNHCB)
|
||||||
require.False(t, h.IsFloatHistogram())
|
require.False(t, h.IsFloatHistogram())
|
||||||
require.Equal(t, int64(123), h.Timestamp)
|
require.Equal(t, int64(123), h.Timestamp)
|
||||||
require.Equal(t, testIntHist, *h.ToIntHistogram())
|
require.Equal(t, testIntHistWithoutNHCB, *h.ToIntHistogram())
|
||||||
require.Equal(t, testFloatHist, *h.ToFloatHistogram())
|
require.Equal(t, testFloatHistWithoutNHCB, *h.ToFloatHistogram())
|
||||||
})
|
})
|
||||||
t.Run("v2", func(t *testing.T) {
|
t.Run("v2", func(t *testing.T) {
|
||||||
h := writev2.FromIntHistogram(123, testIntHist.Copy())
|
testIntHist := testIntHistogram()
|
||||||
|
testFloatHist := testFloatHistogram()
|
||||||
|
|
||||||
|
h := writev2.FromIntHistogram(123, &testIntHist)
|
||||||
require.False(t, h.IsFloatHistogram())
|
require.False(t, h.IsFloatHistogram())
|
||||||
require.Equal(t, int64(123), h.Timestamp)
|
require.Equal(t, int64(123), h.Timestamp)
|
||||||
require.Equal(t, testIntHist, *h.ToIntHistogram())
|
require.Equal(t, testIntHist, *h.ToIntHistogram())
|
||||||
|
@ -209,17 +221,21 @@ func TestFromIntToFloatOrIntHistogram(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFromFloatToFloatHistogram(t *testing.T) {
|
func TestFromFloatToFloatHistogram(t *testing.T) {
|
||||||
testFloatHist := testFloatHistogram()
|
|
||||||
|
|
||||||
t.Run("v1", func(t *testing.T) {
|
t.Run("v1", func(t *testing.T) {
|
||||||
h := prompb.FromFloatHistogram(123, testFloatHist.Copy())
|
// v1 does not support nhcb.
|
||||||
|
testFloatHistWithoutNHCB := testFloatHistogram()
|
||||||
|
testFloatHistWithoutNHCB.CustomValues = nil
|
||||||
|
|
||||||
|
h := prompb.FromFloatHistogram(123, &testFloatHistWithoutNHCB)
|
||||||
require.True(t, h.IsFloatHistogram())
|
require.True(t, h.IsFloatHistogram())
|
||||||
require.Equal(t, int64(123), h.Timestamp)
|
require.Equal(t, int64(123), h.Timestamp)
|
||||||
require.Nil(t, h.ToIntHistogram())
|
require.Nil(t, h.ToIntHistogram())
|
||||||
require.Equal(t, testFloatHist, *h.ToFloatHistogram())
|
require.Equal(t, testFloatHistWithoutNHCB, *h.ToFloatHistogram())
|
||||||
})
|
})
|
||||||
t.Run("v2", func(t *testing.T) {
|
t.Run("v2", func(t *testing.T) {
|
||||||
h := writev2.FromFloatHistogram(123, testFloatHist.Copy())
|
testFloatHist := testFloatHistogram()
|
||||||
|
|
||||||
|
h := writev2.FromFloatHistogram(123, &testFloatHist)
|
||||||
require.True(t, h.IsFloatHistogram())
|
require.True(t, h.IsFloatHistogram())
|
||||||
require.Equal(t, int64(123), h.Timestamp)
|
require.Equal(t, int64(123), h.Timestamp)
|
||||||
require.Nil(t, h.ToIntHistogram())
|
require.Nil(t, h.ToIntHistogram())
|
||||||
|
|
Loading…
Reference in a new issue