mirror of
https://github.com/prometheus/prometheus.git
synced 2025-08-07 06:37:17 +02:00
Merge b4995dee38
into 25aee26a57
This commit is contained in:
commit
5bf41fad55
@ -294,6 +294,9 @@ func (h *FloatHistogram) Mul(factor float64) *FloatHistogram {
|
|||||||
for i := range h.NegativeBuckets {
|
for i := range h.NegativeBuckets {
|
||||||
h.NegativeBuckets[i] *= factor
|
h.NegativeBuckets[i] *= factor
|
||||||
}
|
}
|
||||||
|
if factor < 0 {
|
||||||
|
h.CounterResetHint = GaugeType
|
||||||
|
}
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,6 +320,9 @@ func (h *FloatHistogram) Div(scalar float64) *FloatHistogram {
|
|||||||
for i := range h.NegativeBuckets {
|
for i := range h.NegativeBuckets {
|
||||||
h.NegativeBuckets[i] /= scalar
|
h.NegativeBuckets[i] /= scalar
|
||||||
}
|
}
|
||||||
|
if scalar < 0 {
|
||||||
|
h.CounterResetHint = GaugeType
|
||||||
|
}
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -410,53 +416,7 @@ func (h *FloatHistogram) Add(other *FloatHistogram) (*FloatHistogram, error) {
|
|||||||
|
|
||||||
// Sub works like Add but subtracts the other histogram.
|
// Sub works like Add but subtracts the other histogram.
|
||||||
func (h *FloatHistogram) Sub(other *FloatHistogram) (*FloatHistogram, error) {
|
func (h *FloatHistogram) Sub(other *FloatHistogram) (*FloatHistogram, error) {
|
||||||
if h.UsesCustomBuckets() != other.UsesCustomBuckets() {
|
return h.Add(other.Copy().Mul(-1))
|
||||||
return nil, ErrHistogramsIncompatibleSchema
|
|
||||||
}
|
|
||||||
if h.UsesCustomBuckets() && !FloatBucketsMatch(h.CustomValues, other.CustomValues) {
|
|
||||||
return nil, ErrHistogramsIncompatibleBounds
|
|
||||||
}
|
|
||||||
|
|
||||||
if !h.UsesCustomBuckets() {
|
|
||||||
otherZeroCount := h.reconcileZeroBuckets(other)
|
|
||||||
h.ZeroCount -= otherZeroCount
|
|
||||||
}
|
|
||||||
h.Count -= other.Count
|
|
||||||
h.Sum -= other.Sum
|
|
||||||
|
|
||||||
var (
|
|
||||||
hPositiveSpans = h.PositiveSpans
|
|
||||||
hPositiveBuckets = h.PositiveBuckets
|
|
||||||
otherPositiveSpans = other.PositiveSpans
|
|
||||||
otherPositiveBuckets = other.PositiveBuckets
|
|
||||||
)
|
|
||||||
|
|
||||||
if h.UsesCustomBuckets() {
|
|
||||||
h.PositiveSpans, h.PositiveBuckets = addBuckets(h.Schema, h.ZeroThreshold, true, hPositiveSpans, hPositiveBuckets, otherPositiveSpans, otherPositiveBuckets)
|
|
||||||
return h, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
hNegativeSpans = h.NegativeSpans
|
|
||||||
hNegativeBuckets = h.NegativeBuckets
|
|
||||||
otherNegativeSpans = other.NegativeSpans
|
|
||||||
otherNegativeBuckets = other.NegativeBuckets
|
|
||||||
)
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case other.Schema < h.Schema:
|
|
||||||
hPositiveSpans, hPositiveBuckets = reduceResolution(hPositiveSpans, hPositiveBuckets, h.Schema, other.Schema, false, true)
|
|
||||||
hNegativeSpans, hNegativeBuckets = reduceResolution(hNegativeSpans, hNegativeBuckets, h.Schema, other.Schema, false, true)
|
|
||||||
h.Schema = other.Schema
|
|
||||||
case other.Schema > h.Schema:
|
|
||||||
otherPositiveSpans, otherPositiveBuckets = reduceResolution(otherPositiveSpans, otherPositiveBuckets, other.Schema, h.Schema, false, false)
|
|
||||||
otherNegativeSpans, otherNegativeBuckets = reduceResolution(otherNegativeSpans, otherNegativeBuckets, other.Schema, h.Schema, false, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
h.PositiveSpans, h.PositiveBuckets = addBuckets(h.Schema, h.ZeroThreshold, true, hPositiveSpans, hPositiveBuckets, otherPositiveSpans, otherPositiveBuckets)
|
|
||||||
h.NegativeSpans, h.NegativeBuckets = addBuckets(h.Schema, h.ZeroThreshold, true, hNegativeSpans, hNegativeBuckets, otherNegativeSpans, otherNegativeBuckets)
|
|
||||||
|
|
||||||
return h, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equals returns true if the given float histogram matches exactly.
|
// Equals returns true if the given float histogram matches exactly.
|
||||||
|
@ -153,6 +153,7 @@ func TestFloatHistogramMul(t *testing.T) {
|
|||||||
PositiveBuckets: []float64{-1, 0, -3, -4, -7},
|
PositiveBuckets: []float64{-1, 0, -3, -4, -7},
|
||||||
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
NegativeBuckets: []float64{-3, -1, -5, -6},
|
NegativeBuckets: []float64{-3, -1, -5, -6},
|
||||||
|
CounterResetHint: GaugeType,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -177,6 +178,7 @@ func TestFloatHistogramMul(t *testing.T) {
|
|||||||
PositiveBuckets: []float64{-2, 0, -6, -8, -14},
|
PositiveBuckets: []float64{-2, 0, -6, -8, -14},
|
||||||
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
NegativeBuckets: []float64{-6, -2, -10, -12},
|
NegativeBuckets: []float64{-6, -2, -10, -12},
|
||||||
|
CounterResetHint: GaugeType,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -475,6 +477,7 @@ func TestFloatHistogramDiv(t *testing.T) {
|
|||||||
PositiveBuckets: []float64{-1, -3.3, -4.2, -0.1},
|
PositiveBuckets: []float64{-1, -3.3, -4.2, -0.1},
|
||||||
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
NegativeBuckets: []float64{-3.1, -3, -1.234e5, -1000},
|
NegativeBuckets: []float64{-3.1, -3, -1.234e5, -1000},
|
||||||
|
CounterResetHint: GaugeType,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -499,6 +502,7 @@ func TestFloatHistogramDiv(t *testing.T) {
|
|||||||
PositiveBuckets: []float64{-0.5, 0, -1.5, -2, -3.5},
|
PositiveBuckets: []float64{-0.5, 0, -1.5, -2, -3.5},
|
||||||
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
NegativeBuckets: []float64{-1.5, -0.5, -2.5, -3},
|
NegativeBuckets: []float64{-1.5, -0.5, -2.5, -3},
|
||||||
|
CounterResetHint: GaugeType,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -2387,6 +2391,7 @@ func TestFloatHistogramSub(t *testing.T) {
|
|||||||
PositiveBuckets: []float64{1, 0, 1, 1, 1},
|
PositiveBuckets: []float64{1, 0, 1, 1, 1},
|
||||||
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
NegativeBuckets: []float64{2, 0, 1, 2},
|
NegativeBuckets: []float64{2, 0, 1, 2},
|
||||||
|
CounterResetHint: GaugeType,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
},
|
},
|
||||||
@ -2423,6 +2428,7 @@ func TestFloatHistogramSub(t *testing.T) {
|
|||||||
PositiveBuckets: []float64{1, 5, 4, 2, 2, 2, 0, 5},
|
PositiveBuckets: []float64{1, 5, 4, 2, 2, 2, 0, 5},
|
||||||
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
||||||
NegativeBuckets: []float64{1, 9, 1, 4, 9, 1},
|
NegativeBuckets: []float64{1, 9, 1, 4, 9, 1},
|
||||||
|
CounterResetHint: GaugeType,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
},
|
},
|
||||||
@ -2451,6 +2457,7 @@ func TestFloatHistogramSub(t *testing.T) {
|
|||||||
PositiveSpans: []Span{{0, 2}, {1, 3}},
|
PositiveSpans: []Span{{0, 2}, {1, 3}},
|
||||||
PositiveBuckets: []float64{1, 0, 1, 1, 1},
|
PositiveBuckets: []float64{1, 0, 1, 1, 1},
|
||||||
CustomValues: []float64{1, 2, 3, 4},
|
CustomValues: []float64{1, 2, 3, 4},
|
||||||
|
CounterResetHint: GaugeType,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
},
|
},
|
||||||
|
@ -4049,3 +4049,85 @@ func TestSubQueryHistogramsCopy(t *testing.T) {
|
|||||||
queryable.Close()
|
queryable.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHistogram_CounterResetHint(t *testing.T) {
|
||||||
|
baseT := timestamp.Time(0)
|
||||||
|
load := `
|
||||||
|
load 2m
|
||||||
|
test_histogram {{count:0}}+{{count:1}}x4
|
||||||
|
`
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
query string
|
||||||
|
want histogram.CounterResetHint
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "adding histograms",
|
||||||
|
query: `test_histogram + test_histogram`,
|
||||||
|
want: histogram.NotCounterReset,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "subtraction",
|
||||||
|
query: `test_histogram - test_histogram`,
|
||||||
|
want: histogram.GaugeType,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "negated addition",
|
||||||
|
query: `test_histogram + (-test_histogram)`,
|
||||||
|
want: histogram.GaugeType,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "negated subtraction",
|
||||||
|
query: `test_histogram - (-test_histogram)`,
|
||||||
|
want: histogram.GaugeType,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "unary negation",
|
||||||
|
query: `-test_histogram`,
|
||||||
|
want: histogram.GaugeType,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "repeated unary negation",
|
||||||
|
query: `-(-test_histogram)`,
|
||||||
|
want: histogram.GaugeType,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiplication",
|
||||||
|
query: `2 * test_histogram`,
|
||||||
|
want: histogram.NotCounterReset,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "negative multiplication",
|
||||||
|
query: `-1 * test_histogram`,
|
||||||
|
want: histogram.GaugeType,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "division",
|
||||||
|
query: `test_histogram / 2`,
|
||||||
|
want: histogram.NotCounterReset,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "negative division",
|
||||||
|
query: `test_histogram / -1`,
|
||||||
|
want: histogram.GaugeType,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
queryable := promqltest.LoadedStorage(t, load)
|
||||||
|
defer queryable.Close()
|
||||||
|
engine := promqltest.NewTestEngine(t, false, 0, promqltest.DefaultMaxSamplesPerQuery)
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
q, err := engine.NewInstantQuery(ctx, queryable, nil, tc.query, baseT.Add(2*time.Minute))
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer q.Close()
|
||||||
|
res := q.Exec(ctx)
|
||||||
|
require.NoError(t, res.Err)
|
||||||
|
v, err := res.Vector()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, v, 1)
|
||||||
|
require.NotNil(t, v[0].H)
|
||||||
|
require.Equal(t, tc.want, v[0].H.CounterResetHint)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -5310,6 +5310,7 @@ func TestParseHistogramSeries(t *testing.T) {
|
|||||||
Offset: 0,
|
Offset: 0,
|
||||||
Length: 3,
|
Length: 3,
|
||||||
}},
|
}},
|
||||||
|
CounterResetHint: histogram.GaugeType,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Schema: 1,
|
Schema: 1,
|
||||||
@ -5318,6 +5319,7 @@ func TestParseHistogramSeries(t *testing.T) {
|
|||||||
Offset: 0,
|
Offset: 0,
|
||||||
Length: 3,
|
Length: 3,
|
||||||
}},
|
}},
|
||||||
|
CounterResetHint: histogram.GaugeType,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user