feat(monitor): delta reducer (#24063)

This commit is contained in:
Zexi Li
2026-01-12 17:27:32 +08:00
committed by GitHub
parent 35649ce641
commit f1f66fbed1
6 changed files with 39 additions and 3 deletions
+2 -1
View File
@@ -27,6 +27,7 @@ const (
REDUCER_LAST ReducerType = "last"
REDUCER_MEDIAN ReducerType = "median"
REDUCER_DIFF ReducerType = "diff"
REDUCER_DELTA ReducerType = "delta"
REDUCER_PERCENT_DIFF ReducerType = "percent_diff"
REDUCER_COUNT_NON_NULL ReducerType = "count_non_null"
REDUCER_PERCENTILE ReducerType = "percentile"
@@ -37,7 +38,7 @@ var ValidateReducerTypes = sets.NewString()
func init() {
for _, rt := range []ReducerType{REDUCER_AVG, REDUCER_SUM, REDUCER_MIN,
REDUCER_MAX, REDUCER_COUNT, REDUCER_LAST, REDUCER_MEDIAN, REDUCER_DIFF,
REDUCER_PERCENT_DIFF, REDUCER_COUNT_NON_NULL, REDUCER_PERCENTILE} {
REDUCER_DELTA, REDUCER_PERCENT_DIFF, REDUCER_COUNT_NON_NULL, REDUCER_PERCENTILE} {
ValidateReducerTypes.Insert(string(rt))
}
}
+1
View File
@@ -62,6 +62,7 @@ var (
"last": "Latest value",
"median": "median",
"diff": "The difference between the latest value and the oldest value. The judgment basis value must be legal",
"delta": "The signed difference between the latest value and the oldest value (can be positive or negative)",
"percent_diff": "The difference between the new value and the old value,based on the percentage of the old value",
}
)
+2 -2
View File
@@ -63,11 +63,11 @@ func (e *thresholdEvaluator) Eval(reducedValue *float64) bool {
val := *reducedValue
switch e.Type {
case "gt":
return val >= e.Threshold
return val > e.Threshold
case "eq":
return val == e.Threshold
case "lt":
return val <= e.Threshold
return val < e.Threshold
}
return false
@@ -148,6 +148,8 @@ func (s *mathReducer) Reduce(series *monitor.TimeSeries) (*float64, []string) {
}
case "diff":
allNull, value = calculateDiff(series, allNull, value, diff)
case "delta":
allNull, value = calculateDelta(series, allNull, value)
case "percent_diff":
allNull, value = calculateDiff(series, allNull, value, percentDiff)
case "count_non_null":
@@ -137,6 +137,8 @@ func (s *queryReducer) Reduce(series *monitor.TimeSeries) (*float64, []string) {
}
case monitor.REDUCER_DIFF:
allNull, value = calculateDiff(series, allNull, value, diff)
case monitor.REDUCER_DELTA:
allNull, value = calculateDelta(series, allNull, value)
case monitor.REDUCER_PERCENT_DIFF:
allNull, value = calculateDiff(series, allNull, value, percentDiff)
case monitor.REDUCER_COUNT_NON_NULL:
@@ -226,6 +228,33 @@ var percentDiff = func(newest, oldest float64) float64 {
return (newest - oldest) / oldest * 100
}
func calculateDelta(series *monitor.TimeSeries, allNull bool, value float64) (bool, float64) {
var (
points = series.Points
first float64
i int
)
// get the newest point
for i = len(points) - 1; i >= 0; i-- {
if points[i].IsValid() {
allNull = false
first = points[i].Value()
break
}
}
if i >= 1 {
// get the oldest point
for i := 0; i < len(points); i++ {
if points[i].IsValid() {
allNull = false
value = first - points[i].Value()
break
}
}
}
return allNull, value
}
func NewAlertReducer(cond *monitor.Condition) (Reducer, error) {
if len(cond.Operators) == 0 {
return newSimpleReducer(cond), nil
@@ -42,5 +42,8 @@ var system = SMeasurement{
{
"load15_pcore", "Loadavg load15 per cpu core", monitor.METRIC_UNIT_NULL,
},
{
"uptime", "System uptime", monitor.METRIC_UNIT_NULL,
},
},
}