2019-05-22 21:16:55 +00:00
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
2020-04-20 16:41:59 +00:00
|
|
|
"flag"
|
2019-05-22 21:16:55 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/decimal"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2020-01-03 21:50:47 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2020-04-28 12:28:22 +00:00
|
|
|
"github.com/VictoriaMetrics/metricsql"
|
2019-05-22 21:16:55 +00:00
|
|
|
"github.com/valyala/histogram"
|
|
|
|
)
|
|
|
|
|
2020-04-20 16:41:59 +00:00
|
|
|
var minStalenessInterval = flag.Duration("search.minStalenessInterval", 0, "The mimimum interval for staleness calculations. "+
|
|
|
|
"This flag could be useful for removing gaps on graphs generated from time series with irregular intervals between samples. "+
|
|
|
|
"See also '-search.maxStalenessInterval'")
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
var rollupFuncs = map[string]newRollupFunc{
|
|
|
|
// Standard rollup funcs from PromQL.
|
|
|
|
// See funcs accepting range-vector on https://prometheus.io/docs/prometheus/latest/querying/functions/ .
|
|
|
|
"changes": newRollupFuncOneArg(rollupChanges),
|
|
|
|
"delta": newRollupFuncOneArg(rollupDelta),
|
2019-06-21 19:54:32 +00:00
|
|
|
"deriv": newRollupFuncOneArg(rollupDerivSlow),
|
2019-06-21 20:03:19 +00:00
|
|
|
"deriv_fast": newRollupFuncOneArg(rollupDerivFast),
|
2019-05-22 21:16:55 +00:00
|
|
|
"holt_winters": newRollupHoltWinters,
|
|
|
|
"idelta": newRollupFuncOneArg(rollupIdelta),
|
2019-11-29 23:24:49 +00:00
|
|
|
"increase": newRollupFuncOneArg(rollupIncrease), // + rollupFuncsRemoveCounterResets
|
2020-01-08 12:41:35 +00:00
|
|
|
"irate": newRollupFuncOneArg(rollupIderiv), // + rollupFuncsRemoveCounterResets
|
2019-05-22 21:16:55 +00:00
|
|
|
"predict_linear": newRollupPredictLinear,
|
2020-01-08 12:41:35 +00:00
|
|
|
"rate": newRollupFuncOneArg(rollupDerivFast), // + rollupFuncsRemoveCounterResets
|
2019-05-22 21:16:55 +00:00
|
|
|
"resets": newRollupFuncOneArg(rollupResets),
|
|
|
|
"avg_over_time": newRollupFuncOneArg(rollupAvg),
|
|
|
|
"min_over_time": newRollupFuncOneArg(rollupMin),
|
|
|
|
"max_over_time": newRollupFuncOneArg(rollupMax),
|
|
|
|
"sum_over_time": newRollupFuncOneArg(rollupSum),
|
|
|
|
"count_over_time": newRollupFuncOneArg(rollupCount),
|
|
|
|
"quantile_over_time": newRollupQuantile,
|
|
|
|
"stddev_over_time": newRollupFuncOneArg(rollupStddev),
|
|
|
|
"stdvar_over_time": newRollupFuncOneArg(rollupStdvar),
|
2020-01-03 22:46:39 +00:00
|
|
|
"absent_over_time": newRollupFuncOneArg(rollupAbsent),
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
// Additional rollup funcs.
|
2020-01-21 17:05:10 +00:00
|
|
|
"default_rollup": newRollupFuncOneArg(rollupDefault), // default rollup func
|
|
|
|
"range_over_time": newRollupFuncOneArg(rollupRange),
|
2020-01-11 12:40:32 +00:00
|
|
|
"sum2_over_time": newRollupFuncOneArg(rollupSum2),
|
|
|
|
"geomean_over_time": newRollupFuncOneArg(rollupGeomean),
|
|
|
|
"first_over_time": newRollupFuncOneArg(rollupFirst),
|
|
|
|
"last_over_time": newRollupFuncOneArg(rollupLast),
|
|
|
|
"distinct_over_time": newRollupFuncOneArg(rollupDistinct),
|
|
|
|
"increases_over_time": newRollupFuncOneArg(rollupIncreases),
|
|
|
|
"decreases_over_time": newRollupFuncOneArg(rollupDecreases),
|
|
|
|
"integrate": newRollupFuncOneArg(rollupIntegrate),
|
|
|
|
"ideriv": newRollupFuncOneArg(rollupIderiv),
|
|
|
|
"lifetime": newRollupFuncOneArg(rollupLifetime),
|
|
|
|
"lag": newRollupFuncOneArg(rollupLag),
|
|
|
|
"scrape_interval": newRollupFuncOneArg(rollupScrapeInterval),
|
|
|
|
"tmin_over_time": newRollupFuncOneArg(rollupTmin),
|
|
|
|
"tmax_over_time": newRollupFuncOneArg(rollupTmax),
|
|
|
|
"share_le_over_time": newRollupShareLE,
|
|
|
|
"share_gt_over_time": newRollupShareGT,
|
|
|
|
"histogram_over_time": newRollupFuncOneArg(rollupHistogram),
|
|
|
|
"rollup": newRollupFuncOneArg(rollupFake),
|
|
|
|
"rollup_rate": newRollupFuncOneArg(rollupFake), // + rollupFuncsRemoveCounterResets
|
|
|
|
"rollup_deriv": newRollupFuncOneArg(rollupFake),
|
|
|
|
"rollup_delta": newRollupFuncOneArg(rollupFake),
|
|
|
|
"rollup_increase": newRollupFuncOneArg(rollupFake), // + rollupFuncsRemoveCounterResets
|
|
|
|
"rollup_candlestick": newRollupFuncOneArg(rollupFake),
|
|
|
|
"aggr_over_time": newRollupFuncTwoArgs(rollupFake),
|
|
|
|
"hoeffding_bound_upper": newRollupHoeffdingBoundUpper,
|
|
|
|
"hoeffding_bound_lower": newRollupHoeffdingBoundLower,
|
2020-05-21 09:05:08 +00:00
|
|
|
"ascent_over_time": newRollupFuncOneArg(rollupAscentOverTime),
|
|
|
|
"descent_over_time": newRollupFuncOneArg(rollupDescentOverTime),
|
2020-04-15 09:00:39 +00:00
|
|
|
|
|
|
|
// `timestamp` function must return timestamp for the last datapoint on the current window
|
|
|
|
// in order to properly handle offset and timestamps unaligned to the current step.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/415 for details.
|
|
|
|
"timestamp": newRollupFuncOneArg(rollupTimestamp),
|
2020-07-17 15:28:19 +00:00
|
|
|
|
|
|
|
// See https://en.wikipedia.org/wiki/Mode_(statistics)
|
|
|
|
"mode_over_time": newRollupFuncOneArg(rollupModeOverTime),
|
2020-07-23 22:13:05 +00:00
|
|
|
|
|
|
|
"rate_over_sum": newRollupFuncOneArg(rollupRateOverSum),
|
2020-01-10 19:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// rollupAggrFuncs are functions that can be passed to `aggr_over_time()`
|
|
|
|
var rollupAggrFuncs = map[string]rollupFunc{
|
|
|
|
// Standard rollup funcs from PromQL.
|
|
|
|
"changes": rollupChanges,
|
|
|
|
"delta": rollupDelta,
|
|
|
|
"deriv": rollupDerivSlow,
|
|
|
|
"deriv_fast": rollupDerivFast,
|
|
|
|
"idelta": rollupIdelta,
|
|
|
|
"increase": rollupIncrease, // + rollupFuncsRemoveCounterResets
|
|
|
|
"irate": rollupIderiv, // + rollupFuncsRemoveCounterResets
|
|
|
|
"rate": rollupDerivFast, // + rollupFuncsRemoveCounterResets
|
|
|
|
"resets": rollupResets,
|
|
|
|
"avg_over_time": rollupAvg,
|
|
|
|
"min_over_time": rollupMin,
|
|
|
|
"max_over_time": rollupMax,
|
|
|
|
"sum_over_time": rollupSum,
|
|
|
|
"count_over_time": rollupCount,
|
|
|
|
"stddev_over_time": rollupStddev,
|
|
|
|
"stdvar_over_time": rollupStdvar,
|
|
|
|
"absent_over_time": rollupAbsent,
|
|
|
|
|
|
|
|
// Additional rollup funcs.
|
2020-01-21 17:05:10 +00:00
|
|
|
"range_over_time": rollupRange,
|
2020-01-10 19:16:14 +00:00
|
|
|
"sum2_over_time": rollupSum2,
|
|
|
|
"geomean_over_time": rollupGeomean,
|
|
|
|
"first_over_time": rollupFirst,
|
|
|
|
"last_over_time": rollupLast,
|
|
|
|
"distinct_over_time": rollupDistinct,
|
|
|
|
"increases_over_time": rollupIncreases,
|
|
|
|
"decreases_over_time": rollupDecreases,
|
|
|
|
"integrate": rollupIntegrate,
|
|
|
|
"ideriv": rollupIderiv,
|
|
|
|
"lifetime": rollupLifetime,
|
|
|
|
"lag": rollupLag,
|
|
|
|
"scrape_interval": rollupScrapeInterval,
|
|
|
|
"tmin_over_time": rollupTmin,
|
|
|
|
"tmax_over_time": rollupTmax,
|
2020-05-21 09:05:08 +00:00
|
|
|
"ascent_over_time": rollupAscentOverTime,
|
|
|
|
"descent_over_time": rollupDescentOverTime,
|
|
|
|
"timestamp": rollupTimestamp,
|
2020-07-17 15:28:19 +00:00
|
|
|
"mode_over_time": rollupModeOverTime,
|
2020-07-23 22:13:05 +00:00
|
|
|
"rate_over_sum": rollupRateOverSum,
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 10:47:29 +00:00
|
|
|
var rollupFuncsCannotAdjustWindow = map[string]bool{
|
|
|
|
"changes": true,
|
|
|
|
"delta": true,
|
|
|
|
"holt_winters": true,
|
|
|
|
"idelta": true,
|
|
|
|
"increase": true,
|
|
|
|
"predict_linear": true,
|
|
|
|
"resets": true,
|
2020-07-23 19:24:28 +00:00
|
|
|
"avg_over_time": true,
|
2020-01-11 10:47:29 +00:00
|
|
|
"sum_over_time": true,
|
|
|
|
"count_over_time": true,
|
|
|
|
"quantile_over_time": true,
|
|
|
|
"stddev_over_time": true,
|
|
|
|
"stdvar_over_time": true,
|
|
|
|
"absent_over_time": true,
|
|
|
|
"sum2_over_time": true,
|
|
|
|
"geomean_over_time": true,
|
|
|
|
"distinct_over_time": true,
|
|
|
|
"increases_over_time": true,
|
|
|
|
"decreases_over_time": true,
|
|
|
|
"integrate": true,
|
2020-05-21 09:05:08 +00:00
|
|
|
"ascent_over_time": true,
|
|
|
|
"descent_over_time": true,
|
2019-06-23 16:06:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
var rollupFuncsRemoveCounterResets = map[string]bool{
|
|
|
|
"increase": true,
|
|
|
|
"irate": true,
|
|
|
|
"rate": true,
|
|
|
|
"rollup_rate": true,
|
|
|
|
"rollup_increase": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
var rollupFuncsKeepMetricGroup = map[string]bool{
|
2020-01-11 12:40:32 +00:00
|
|
|
"default_rollup": true,
|
|
|
|
"avg_over_time": true,
|
|
|
|
"min_over_time": true,
|
|
|
|
"max_over_time": true,
|
|
|
|
"quantile_over_time": true,
|
|
|
|
"rollup": true,
|
|
|
|
"geomean_over_time": true,
|
|
|
|
"hoeffding_bound_lower": true,
|
|
|
|
"hoeffding_bound_upper": true,
|
2020-04-04 11:44:40 +00:00
|
|
|
"first_over_time": true,
|
|
|
|
"last_over_time": true,
|
2020-07-17 15:28:19 +00:00
|
|
|
"mode_over_time": true,
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 19:16:14 +00:00
|
|
|
func getRollupAggrFuncNames(expr metricsql.Expr) ([]string, error) {
|
2020-01-10 19:56:59 +00:00
|
|
|
afe, ok := expr.(*metricsql.AggrFuncExpr)
|
|
|
|
if ok {
|
|
|
|
// This is for incremental aggregate function case:
|
|
|
|
//
|
|
|
|
// sum(aggr_over_time(...))
|
|
|
|
//
|
|
|
|
// See aggr_incremental.go for details.
|
|
|
|
expr = afe.Args[0]
|
|
|
|
}
|
2020-01-10 19:16:14 +00:00
|
|
|
fe, ok := expr.(*metricsql.FuncExpr)
|
|
|
|
if !ok {
|
|
|
|
logger.Panicf("BUG: unexpected expression; want metricsql.FuncExpr; got %T; value: %s", expr, expr.AppendString(nil))
|
|
|
|
}
|
|
|
|
if fe.Name != "aggr_over_time" {
|
|
|
|
logger.Panicf("BUG: unexpected function name: %q; want `aggr_over_time`", fe.Name)
|
|
|
|
}
|
|
|
|
if len(fe.Args) != 2 {
|
|
|
|
return nil, fmt.Errorf("unexpected number of args to aggr_over_time(); got %d; want %d", len(fe.Args), 2)
|
|
|
|
}
|
|
|
|
arg := fe.Args[0]
|
|
|
|
var aggrFuncNames []string
|
|
|
|
if se, ok := arg.(*metricsql.StringExpr); ok {
|
|
|
|
aggrFuncNames = append(aggrFuncNames, se.S)
|
|
|
|
} else {
|
|
|
|
fe, ok := arg.(*metricsql.FuncExpr)
|
|
|
|
if !ok || fe.Name != "" {
|
|
|
|
return nil, fmt.Errorf("%s cannot be passed to aggr_over_time(); expecting quoted aggregate function name or a list of quoted aggregate function names",
|
|
|
|
arg.AppendString(nil))
|
|
|
|
}
|
|
|
|
for _, e := range fe.Args {
|
|
|
|
se, ok := e.(*metricsql.StringExpr)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%s cannot be passed here; expecting quoted aggregate function name", e.AppendString(nil))
|
|
|
|
}
|
|
|
|
aggrFuncNames = append(aggrFuncNames, se.S)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(aggrFuncNames) == 0 {
|
|
|
|
return nil, fmt.Errorf("aggr_over_time() must contain at least a single aggregate function name")
|
|
|
|
}
|
|
|
|
for _, s := range aggrFuncNames {
|
|
|
|
if rollupAggrFuncs[s] == nil {
|
|
|
|
return nil, fmt.Errorf("%q cannot be used in `aggr_over_time` function; expecting quoted aggregate function name", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aggrFuncNames, nil
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func getRollupArgIdx(funcName string) int {
|
|
|
|
funcName = strings.ToLower(funcName)
|
|
|
|
if rollupFuncs[funcName] == nil {
|
|
|
|
logger.Panicf("BUG: getRollupArgIdx is called for non-rollup func %q", funcName)
|
|
|
|
}
|
2020-01-10 19:16:14 +00:00
|
|
|
switch funcName {
|
2020-01-11 12:40:32 +00:00
|
|
|
case "quantile_over_time", "aggr_over_time",
|
|
|
|
"hoeffding_bound_lower", "hoeffding_bound_upper":
|
2019-05-22 21:16:55 +00:00
|
|
|
return 1
|
2020-01-10 19:16:14 +00:00
|
|
|
default:
|
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2020-01-10 19:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getRollupConfigs(name string, rf rollupFunc, expr metricsql.Expr, start, end, step, window int64, lookbackDelta int64, sharedTimestamps []int64) (
|
|
|
|
func(values []float64, timestamps []int64), []*rollupConfig, error) {
|
|
|
|
preFunc := func(values []float64, timestamps []int64) {}
|
|
|
|
if rollupFuncsRemoveCounterResets[name] {
|
|
|
|
preFunc = func(values []float64, timestamps []int64) {
|
|
|
|
removeCounterResets(values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newRollupConfig := func(rf rollupFunc, tagValue string) *rollupConfig {
|
|
|
|
return &rollupConfig{
|
|
|
|
TagValue: tagValue,
|
|
|
|
Func: rf,
|
|
|
|
Start: start,
|
|
|
|
End: end,
|
|
|
|
Step: step,
|
|
|
|
Window: window,
|
2020-01-11 10:47:29 +00:00
|
|
|
MayAdjustWindow: !rollupFuncsCannotAdjustWindow[name],
|
2020-01-10 19:16:14 +00:00
|
|
|
LookbackDelta: lookbackDelta,
|
|
|
|
Timestamps: sharedTimestamps,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appendRollupConfigs := func(dst []*rollupConfig) []*rollupConfig {
|
|
|
|
dst = append(dst, newRollupConfig(rollupMin, "min"))
|
|
|
|
dst = append(dst, newRollupConfig(rollupMax, "max"))
|
|
|
|
dst = append(dst, newRollupConfig(rollupAvg, "avg"))
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
var rcs []*rollupConfig
|
|
|
|
switch name {
|
|
|
|
case "rollup":
|
|
|
|
rcs = appendRollupConfigs(rcs)
|
|
|
|
case "rollup_rate", "rollup_deriv":
|
|
|
|
preFuncPrev := preFunc
|
|
|
|
preFunc = func(values []float64, timestamps []int64) {
|
|
|
|
preFuncPrev(values, timestamps)
|
|
|
|
derivValues(values, timestamps)
|
|
|
|
}
|
|
|
|
rcs = appendRollupConfigs(rcs)
|
|
|
|
case "rollup_increase", "rollup_delta":
|
|
|
|
preFuncPrev := preFunc
|
|
|
|
preFunc = func(values []float64, timestamps []int64) {
|
|
|
|
preFuncPrev(values, timestamps)
|
|
|
|
deltaValues(values)
|
|
|
|
}
|
|
|
|
rcs = appendRollupConfigs(rcs)
|
|
|
|
case "rollup_candlestick":
|
2020-02-04 20:42:10 +00:00
|
|
|
rcs = append(rcs, newRollupConfig(rollupOpen, "open"))
|
|
|
|
rcs = append(rcs, newRollupConfig(rollupClose, "close"))
|
|
|
|
rcs = append(rcs, newRollupConfig(rollupLow, "low"))
|
|
|
|
rcs = append(rcs, newRollupConfig(rollupHigh, "high"))
|
2020-01-10 19:16:14 +00:00
|
|
|
case "aggr_over_time":
|
|
|
|
aggrFuncNames, err := getRollupAggrFuncNames(expr)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, nil, fmt.Errorf("invalid args to %s: %w", expr.AppendString(nil), err)
|
2020-01-10 19:16:14 +00:00
|
|
|
}
|
|
|
|
for _, aggrFuncName := range aggrFuncNames {
|
|
|
|
if rollupFuncsRemoveCounterResets[aggrFuncName] {
|
|
|
|
// There is no need to save the previous preFunc, since it is either empty or the same.
|
|
|
|
preFunc = func(values []float64, timestamps []int64) {
|
|
|
|
removeCounterResets(values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rf := rollupAggrFuncs[aggrFuncName]
|
|
|
|
rcs = append(rcs, newRollupConfig(rf, aggrFuncName))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
rcs = append(rcs, newRollupConfig(rf, ""))
|
|
|
|
}
|
|
|
|
return preFunc, rcs, nil
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getRollupFunc(funcName string) newRollupFunc {
|
|
|
|
funcName = strings.ToLower(funcName)
|
|
|
|
return rollupFuncs[funcName]
|
|
|
|
}
|
|
|
|
|
|
|
|
type rollupFuncArg struct {
|
|
|
|
prevValue float64
|
|
|
|
prevTimestamp int64
|
|
|
|
values []float64
|
|
|
|
timestamps []int64
|
|
|
|
|
2020-01-08 12:41:35 +00:00
|
|
|
currTimestamp int64
|
|
|
|
idx int
|
|
|
|
step int64
|
2019-12-24 13:54:04 +00:00
|
|
|
|
|
|
|
// Real previous value even if it is located too far from the current window.
|
|
|
|
// It matches prevValue if prevValue is not nan.
|
2019-11-29 23:24:49 +00:00
|
|
|
realPrevValue float64
|
2020-01-03 21:50:47 +00:00
|
|
|
|
|
|
|
tsm *timeseriesMap
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rfa *rollupFuncArg) reset() {
|
|
|
|
rfa.prevValue = 0
|
|
|
|
rfa.prevTimestamp = 0
|
|
|
|
rfa.values = nil
|
|
|
|
rfa.timestamps = nil
|
2019-11-01 10:21:12 +00:00
|
|
|
rfa.currTimestamp = 0
|
2019-05-22 21:16:55 +00:00
|
|
|
rfa.idx = 0
|
|
|
|
rfa.step = 0
|
2019-11-29 23:24:49 +00:00
|
|
|
rfa.realPrevValue = nan
|
2020-01-03 21:50:47 +00:00
|
|
|
rfa.tsm = nil
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// rollupFunc must return rollup value for the given rfa.
|
|
|
|
//
|
|
|
|
// prevValue may be nan, values and timestamps may be empty.
|
|
|
|
type rollupFunc func(rfa *rollupFuncArg) float64
|
|
|
|
|
|
|
|
type rollupConfig struct {
|
|
|
|
// This tag value must be added to "rollup" tag if non-empty.
|
|
|
|
TagValue string
|
|
|
|
|
|
|
|
Func rollupFunc
|
|
|
|
Start int64
|
|
|
|
End int64
|
|
|
|
Step int64
|
|
|
|
Window int64
|
|
|
|
|
2019-06-23 16:06:16 +00:00
|
|
|
// Whether window may be adjusted to 2 x interval between data points.
|
|
|
|
// This is needed for functions which have dt in the denominator
|
|
|
|
// such as rate, deriv, etc.
|
|
|
|
// Without the adjustement their value would jump in unexpected directions
|
|
|
|
// when using window smaller than 2 x scrape_interval.
|
|
|
|
MayAdjustWindow bool
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
Timestamps []int64
|
2019-10-15 16:12:27 +00:00
|
|
|
|
|
|
|
// LoookbackDelta is the analog to `-query.lookback-delta` from Prometheus world.
|
|
|
|
LookbackDelta int64
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
nan = math.NaN()
|
|
|
|
inf = math.Inf(1)
|
|
|
|
)
|
|
|
|
|
|
|
|
// The maximum interval without previous rows.
|
|
|
|
const maxSilenceInterval = 5 * 60 * 1000
|
|
|
|
|
2020-01-03 21:50:47 +00:00
|
|
|
type timeseriesMap struct {
|
|
|
|
origin *timeseries
|
|
|
|
labelName string
|
|
|
|
h metrics.Histogram
|
|
|
|
m map[string]*timeseries
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTimeseriesMap(funcName string, sharedTimestamps []int64, mnSrc *storage.MetricName) *timeseriesMap {
|
|
|
|
if funcName != "histogram_over_time" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
values := make([]float64, len(sharedTimestamps))
|
|
|
|
for i := range values {
|
|
|
|
values[i] = nan
|
|
|
|
}
|
|
|
|
var origin timeseries
|
|
|
|
origin.MetricName.CopyFrom(mnSrc)
|
|
|
|
origin.MetricName.ResetMetricGroup()
|
|
|
|
origin.Timestamps = sharedTimestamps
|
|
|
|
origin.Values = values
|
|
|
|
return ×eriesMap{
|
|
|
|
origin: &origin,
|
|
|
|
labelName: "vmrange",
|
|
|
|
m: make(map[string]*timeseries),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsm *timeseriesMap) AppendTimeseriesTo(dst []*timeseries) []*timeseries {
|
|
|
|
for _, ts := range tsm.m {
|
|
|
|
dst = append(dst, ts)
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsm *timeseriesMap) GetOrCreateTimeseries(labelValue string) *timeseries {
|
|
|
|
ts := tsm.m[labelValue]
|
|
|
|
if ts != nil {
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
ts = ×eries{}
|
|
|
|
ts.CopyFromShallowTimestamps(tsm.origin)
|
|
|
|
ts.MetricName.RemoveTag(tsm.labelName)
|
|
|
|
ts.MetricName.AddTag(tsm.labelName, labelValue)
|
|
|
|
tsm.m[labelValue] = ts
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// Do calculates rollups for the given timestamps and values, appends
|
|
|
|
// them to dstValues and returns results.
|
|
|
|
//
|
|
|
|
// rc.Timestamps are used as timestamps for dstValues.
|
|
|
|
//
|
2020-02-05 17:20:54 +00:00
|
|
|
// timestamps must cover time range [rc.Start - rc.Window - maxSilenceInterval ... rc.End].
|
2019-05-22 21:16:55 +00:00
|
|
|
//
|
2020-01-03 21:50:47 +00:00
|
|
|
// Do cannot be called from concurrent goroutines.
|
2019-05-22 21:16:55 +00:00
|
|
|
func (rc *rollupConfig) Do(dstValues []float64, values []float64, timestamps []int64) []float64 {
|
2020-01-03 21:50:47 +00:00
|
|
|
return rc.doInternal(dstValues, nil, values, timestamps)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DoTimeseriesMap calculates rollups for the given timestamps and values and puts them to tsm.
|
|
|
|
func (rc *rollupConfig) DoTimeseriesMap(tsm *timeseriesMap, values []float64, timestamps []int64) {
|
|
|
|
ts := getTimeseries()
|
|
|
|
ts.Values = rc.doInternal(ts.Values[:0], tsm, values, timestamps)
|
|
|
|
putTimeseries(ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *rollupConfig) doInternal(dstValues []float64, tsm *timeseriesMap, values []float64, timestamps []int64) []float64 {
|
2019-05-22 21:16:55 +00:00
|
|
|
// Sanity checks.
|
|
|
|
if rc.Step <= 0 {
|
|
|
|
logger.Panicf("BUG: Step must be bigger than 0; got %d", rc.Step)
|
|
|
|
}
|
|
|
|
if rc.Start > rc.End {
|
|
|
|
logger.Panicf("BUG: Start cannot exceed End; got %d vs %d", rc.Start, rc.End)
|
|
|
|
}
|
|
|
|
if rc.Window < 0 {
|
|
|
|
logger.Panicf("BUG: Window must be non-negative; got %d", rc.Window)
|
|
|
|
}
|
|
|
|
if err := ValidateMaxPointsPerTimeseries(rc.Start, rc.End, rc.Step); err != nil {
|
|
|
|
logger.Panicf("BUG: %s; this must be validated before the call to rollupConfig.Do", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extend dstValues in order to remove mallocs below.
|
|
|
|
dstValues = decimal.ExtendFloat64sCapacity(dstValues, len(rc.Timestamps))
|
|
|
|
|
2019-12-24 13:54:04 +00:00
|
|
|
scrapeInterval := getScrapeInterval(timestamps)
|
|
|
|
maxPrevInterval := getMaxPrevInterval(scrapeInterval)
|
2019-10-15 16:12:27 +00:00
|
|
|
if rc.LookbackDelta > 0 && maxPrevInterval > rc.LookbackDelta {
|
|
|
|
maxPrevInterval = rc.LookbackDelta
|
|
|
|
}
|
2020-04-20 16:41:59 +00:00
|
|
|
if *minStalenessInterval > 0 {
|
|
|
|
if msi := minStalenessInterval.Milliseconds(); msi > 0 && maxPrevInterval < msi {
|
|
|
|
maxPrevInterval = msi
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
window := rc.Window
|
|
|
|
if window <= 0 {
|
|
|
|
window = rc.Step
|
|
|
|
}
|
2019-06-23 16:06:16 +00:00
|
|
|
if rc.MayAdjustWindow && window < maxPrevInterval {
|
2019-05-22 21:16:55 +00:00
|
|
|
window = maxPrevInterval
|
|
|
|
}
|
|
|
|
rfa := getRollupFuncArg()
|
|
|
|
rfa.idx = 0
|
|
|
|
rfa.step = rc.Step
|
2019-11-29 23:24:49 +00:00
|
|
|
rfa.realPrevValue = nan
|
2020-01-03 21:50:47 +00:00
|
|
|
rfa.tsm = tsm
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
i := 0
|
|
|
|
j := 0
|
2019-08-19 10:39:12 +00:00
|
|
|
ni := 0
|
|
|
|
nj := 0
|
2019-06-21 18:50:44 +00:00
|
|
|
for _, tEnd := range rc.Timestamps {
|
2019-05-22 21:16:55 +00:00
|
|
|
tStart := tEnd - window
|
2019-08-19 10:39:12 +00:00
|
|
|
ni = seekFirstTimestampIdxAfter(timestamps[i:], tStart, ni)
|
|
|
|
i += ni
|
2019-05-22 21:16:55 +00:00
|
|
|
if j < i {
|
|
|
|
j = i
|
|
|
|
}
|
2019-08-19 10:39:12 +00:00
|
|
|
nj = seekFirstTimestampIdxAfter(timestamps[j:], tEnd, nj)
|
|
|
|
j += nj
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
rfa.prevValue = nan
|
|
|
|
rfa.prevTimestamp = tStart - maxPrevInterval
|
2019-09-04 13:22:06 +00:00
|
|
|
if i < len(timestamps) && i > 0 && timestamps[i-1] > rfa.prevTimestamp {
|
2019-05-22 21:16:55 +00:00
|
|
|
rfa.prevValue = values[i-1]
|
|
|
|
rfa.prevTimestamp = timestamps[i-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
rfa.values = values[i:j]
|
|
|
|
rfa.timestamps = timestamps[i:j]
|
2019-11-01 10:21:12 +00:00
|
|
|
rfa.currTimestamp = tEnd
|
2019-11-29 23:24:49 +00:00
|
|
|
if i > 0 {
|
|
|
|
rfa.realPrevValue = values[i-1]
|
2020-07-29 08:36:51 +00:00
|
|
|
} else {
|
|
|
|
rfa.realPrevValue = nan
|
2019-11-29 23:24:49 +00:00
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
value := rc.Func(rfa)
|
|
|
|
rfa.idx++
|
|
|
|
dstValues = append(dstValues, value)
|
|
|
|
}
|
|
|
|
putRollupFuncArg(rfa)
|
|
|
|
|
|
|
|
return dstValues
|
|
|
|
}
|
|
|
|
|
2019-08-19 10:01:37 +00:00
|
|
|
func seekFirstTimestampIdxAfter(timestamps []int64, seekTimestamp int64, nHint int) int {
|
|
|
|
if len(timestamps) == 0 || timestamps[0] > seekTimestamp {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
startIdx := nHint - 2
|
|
|
|
if startIdx < 0 {
|
|
|
|
startIdx = 0
|
|
|
|
}
|
|
|
|
if startIdx >= len(timestamps) {
|
|
|
|
startIdx = len(timestamps) - 1
|
|
|
|
}
|
|
|
|
endIdx := nHint + 2
|
|
|
|
if endIdx > len(timestamps) {
|
|
|
|
endIdx = len(timestamps)
|
|
|
|
}
|
|
|
|
if startIdx > 0 && timestamps[startIdx] <= seekTimestamp {
|
|
|
|
timestamps = timestamps[startIdx:]
|
|
|
|
endIdx -= startIdx
|
|
|
|
} else {
|
|
|
|
startIdx = 0
|
|
|
|
}
|
|
|
|
if endIdx < len(timestamps) && timestamps[endIdx] > seekTimestamp {
|
|
|
|
timestamps = timestamps[:endIdx]
|
|
|
|
}
|
|
|
|
if len(timestamps) < 16 {
|
|
|
|
// Fast path: the number of timestamps to search is small, so scan them all.
|
|
|
|
for i, timestamp := range timestamps {
|
|
|
|
if timestamp > seekTimestamp {
|
|
|
|
return startIdx + i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return startIdx + len(timestamps)
|
|
|
|
}
|
|
|
|
// Slow path: too big len(timestamps), so use binary search.
|
2019-11-25 12:01:36 +00:00
|
|
|
i := binarySearchInt64(timestamps, seekTimestamp+1)
|
|
|
|
return startIdx + int(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func binarySearchInt64(a []int64, v int64) uint {
|
|
|
|
// Copy-pasted sort.Search from https://golang.org/src/sort/search.go?s=2246:2286#L49
|
|
|
|
i, j := uint(0), uint(len(a))
|
|
|
|
for i < j {
|
|
|
|
h := (i + j) >> 1
|
|
|
|
if h < uint(len(a)) && a[h] < v {
|
|
|
|
i = h + 1
|
|
|
|
} else {
|
|
|
|
j = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
2019-08-19 10:01:37 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 13:54:04 +00:00
|
|
|
func getScrapeInterval(timestamps []int64) int64 {
|
2019-05-22 21:16:55 +00:00
|
|
|
if len(timestamps) < 2 {
|
|
|
|
return int64(maxSilenceInterval)
|
|
|
|
}
|
2019-12-02 11:41:48 +00:00
|
|
|
|
|
|
|
// Estimate scrape interval as 0.6 quantile for the first 100 intervals.
|
|
|
|
h := histogram.GetFast()
|
|
|
|
tsPrev := timestamps[0]
|
|
|
|
timestamps = timestamps[1:]
|
|
|
|
if len(timestamps) > 100 {
|
|
|
|
timestamps = timestamps[:100]
|
|
|
|
}
|
|
|
|
for _, ts := range timestamps {
|
|
|
|
h.Update(float64(ts - tsPrev))
|
|
|
|
tsPrev = ts
|
|
|
|
}
|
2019-12-24 13:54:04 +00:00
|
|
|
scrapeInterval := int64(h.Quantile(0.6))
|
2019-12-02 11:41:48 +00:00
|
|
|
histogram.PutFast(h)
|
2019-12-24 13:54:04 +00:00
|
|
|
if scrapeInterval <= 0 {
|
2019-08-04 16:32:18 +00:00
|
|
|
return int64(maxSilenceInterval)
|
|
|
|
}
|
2019-12-24 13:54:04 +00:00
|
|
|
return scrapeInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMaxPrevInterval(scrapeInterval int64) int64 {
|
|
|
|
// Increase scrapeInterval more for smaller scrape intervals in order to hide possible gaps
|
2019-08-04 16:32:18 +00:00
|
|
|
// when high jitter is present.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/139 .
|
2019-12-24 13:54:04 +00:00
|
|
|
if scrapeInterval <= 2*1000 {
|
|
|
|
return scrapeInterval + 4*scrapeInterval
|
2019-08-04 16:32:18 +00:00
|
|
|
}
|
2019-12-24 13:54:04 +00:00
|
|
|
if scrapeInterval <= 4*1000 {
|
|
|
|
return scrapeInterval + 2*scrapeInterval
|
2019-08-04 16:32:18 +00:00
|
|
|
}
|
2019-12-24 13:54:04 +00:00
|
|
|
if scrapeInterval <= 8*1000 {
|
|
|
|
return scrapeInterval + scrapeInterval
|
2019-08-04 16:32:18 +00:00
|
|
|
}
|
2019-12-24 13:54:04 +00:00
|
|
|
if scrapeInterval <= 16*1000 {
|
|
|
|
return scrapeInterval + scrapeInterval/2
|
2019-08-04 16:32:18 +00:00
|
|
|
}
|
2019-12-24 13:54:04 +00:00
|
|
|
if scrapeInterval <= 32*1000 {
|
|
|
|
return scrapeInterval + scrapeInterval/4
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-12-24 13:54:04 +00:00
|
|
|
return scrapeInterval + scrapeInterval/8
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeCounterResets(values []float64) {
|
|
|
|
// There is no need in handling NaNs here, since they are impossible
|
|
|
|
// on values from vmstorage.
|
|
|
|
if len(values) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var correction float64
|
|
|
|
prevValue := values[0]
|
|
|
|
for i, v := range values {
|
|
|
|
d := v - prevValue
|
|
|
|
if d < 0 {
|
|
|
|
if (-d * 8) < prevValue {
|
|
|
|
// This is likely jitter from `Prometheus HA pairs`.
|
|
|
|
// Just substitute v with prevValue.
|
|
|
|
v = prevValue
|
|
|
|
} else {
|
|
|
|
correction += prevValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prevValue = v
|
|
|
|
values[i] = v + correction
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deltaValues(values []float64) {
|
|
|
|
// There is no need in handling NaNs here, since they are impossible
|
|
|
|
// on values from vmstorage.
|
|
|
|
if len(values) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-07-03 09:27:07 +00:00
|
|
|
prevDelta := float64(0)
|
2019-05-22 21:16:55 +00:00
|
|
|
prevValue := values[0]
|
|
|
|
for i, v := range values[1:] {
|
2019-07-03 09:27:07 +00:00
|
|
|
prevDelta = v - prevValue
|
|
|
|
values[i] = prevDelta
|
2019-05-22 21:16:55 +00:00
|
|
|
prevValue = v
|
|
|
|
}
|
2019-07-03 09:27:07 +00:00
|
|
|
values[len(values)-1] = prevDelta
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func derivValues(values []float64, timestamps []int64) {
|
|
|
|
// There is no need in handling NaNs here, since they are impossible
|
|
|
|
// on values from vmstorage.
|
|
|
|
if len(values) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-07-03 09:27:07 +00:00
|
|
|
prevDeriv := float64(0)
|
2019-05-22 21:16:55 +00:00
|
|
|
prevValue := values[0]
|
|
|
|
prevTs := timestamps[0]
|
|
|
|
for i, v := range values[1:] {
|
|
|
|
ts := timestamps[i+1]
|
2019-07-03 09:27:07 +00:00
|
|
|
if ts == prevTs {
|
|
|
|
// Use the previous value for duplicate timestamps.
|
|
|
|
values[i] = prevDeriv
|
|
|
|
continue
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
dt := float64(ts-prevTs) * 1e-3
|
2019-07-03 09:27:07 +00:00
|
|
|
prevDeriv = (v - prevValue) / dt
|
|
|
|
values[i] = prevDeriv
|
2019-05-22 21:16:55 +00:00
|
|
|
prevValue = v
|
|
|
|
prevTs = ts
|
|
|
|
}
|
2019-07-03 09:27:07 +00:00
|
|
|
values[len(values)-1] = prevDeriv
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type newRollupFunc func(args []interface{}) (rollupFunc, error)
|
|
|
|
|
|
|
|
func newRollupFuncOneArg(rf rollupFunc) newRollupFunc {
|
|
|
|
return func(args []interface{}) (rollupFunc, error) {
|
|
|
|
if err := expectRollupArgsNum(args, 1); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rf, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 19:16:14 +00:00
|
|
|
func newRollupFuncTwoArgs(rf rollupFunc) newRollupFunc {
|
|
|
|
return func(args []interface{}) (rollupFunc, error) {
|
|
|
|
if err := expectRollupArgsNum(args, 2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rf, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func newRollupHoltWinters(args []interface{}) (rollupFunc, error) {
|
|
|
|
if err := expectRollupArgsNum(args, 3); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sfs, err := getScalar(args[1], 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tfs, err := getScalar(args[2], 2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rf := func(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
return rfa.prevValue
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
sf := sfs[rfa.idx]
|
|
|
|
if sf <= 0 || sf >= 1 {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
tf := tfs[rfa.idx]
|
|
|
|
if tf <= 0 || tf >= 1 {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
|
|
|
|
// See https://en.wikipedia.org/wiki/Exponential_smoothing#Double_exponential_smoothing .
|
|
|
|
// TODO: determine whether this shit really works.
|
|
|
|
s0 := rfa.prevValue
|
|
|
|
if math.IsNaN(s0) {
|
|
|
|
s0 = values[0]
|
|
|
|
values = values[1:]
|
|
|
|
if len(values) == 0 {
|
|
|
|
return s0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b0 := values[0] - s0
|
|
|
|
for _, v := range values {
|
|
|
|
s1 := sf*v + (1-sf)*(s0+b0)
|
|
|
|
b1 := tf*(s1-s0) + (1-tf)*b0
|
|
|
|
s0 = s1
|
|
|
|
b0 = b1
|
|
|
|
}
|
|
|
|
return s0
|
|
|
|
}
|
|
|
|
return rf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRollupPredictLinear(args []interface{}) (rollupFunc, error) {
|
|
|
|
if err := expectRollupArgsNum(args, 2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
secs, err := getScalar(args[1], 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rf := func(rfa *rollupFuncArg) float64 {
|
2019-06-21 19:54:32 +00:00
|
|
|
v, k := linearRegression(rfa)
|
|
|
|
if math.IsNaN(v) {
|
2019-05-22 21:16:55 +00:00
|
|
|
return nan
|
|
|
|
}
|
|
|
|
sec := secs[rfa.idx]
|
|
|
|
return v + k*sec
|
|
|
|
}
|
|
|
|
return rf, nil
|
|
|
|
}
|
|
|
|
|
2019-06-21 19:54:32 +00:00
|
|
|
func linearRegression(rfa *rollupFuncArg) (float64, float64) {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-06-21 19:54:32 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
timestamps := rfa.timestamps
|
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
return rfa.prevValue, 0
|
2019-06-21 19:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// See https://en.wikipedia.org/wiki/Simple_linear_regression#Numerical_example
|
|
|
|
tFirst := rfa.prevTimestamp
|
|
|
|
vSum := rfa.prevValue
|
2019-06-23 16:06:16 +00:00
|
|
|
tSum := float64(0)
|
|
|
|
tvSum := float64(0)
|
|
|
|
ttSum := float64(0)
|
2019-06-21 19:54:32 +00:00
|
|
|
n := 1.0
|
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
tFirst = timestamps[0]
|
|
|
|
vSum = 0
|
|
|
|
n = 0
|
|
|
|
}
|
|
|
|
for i, v := range values {
|
|
|
|
dt := float64(timestamps[i]-tFirst) * 1e-3
|
|
|
|
vSum += v
|
|
|
|
tSum += dt
|
|
|
|
tvSum += dt * v
|
|
|
|
ttSum += dt * dt
|
|
|
|
}
|
|
|
|
n += float64(len(values))
|
|
|
|
if n == 1 {
|
|
|
|
return vSum, 0
|
|
|
|
}
|
|
|
|
k := (n*tvSum - tSum*vSum) / (n*ttSum - tSum*tSum)
|
|
|
|
v := (vSum - k*tSum) / n
|
2019-06-24 19:27:17 +00:00
|
|
|
// Adjust v to the last timestamp on the given time range.
|
|
|
|
v += k * (float64(timestamps[len(timestamps)-1]-tFirst) * 1e-3)
|
2019-06-21 19:54:32 +00:00
|
|
|
return v, k
|
|
|
|
}
|
|
|
|
|
2020-01-02 22:41:09 +00:00
|
|
|
func newRollupShareLE(args []interface{}) (rollupFunc, error) {
|
|
|
|
return newRollupShareFilter(args, countFilterLE)
|
|
|
|
}
|
|
|
|
|
|
|
|
func countFilterLE(values []float64, le float64) int {
|
|
|
|
n := 0
|
|
|
|
for _, v := range values {
|
|
|
|
if v <= le {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRollupShareGT(args []interface{}) (rollupFunc, error) {
|
|
|
|
return newRollupShareFilter(args, countFilterGT)
|
|
|
|
}
|
|
|
|
|
|
|
|
func countFilterGT(values []float64, gt float64) int {
|
|
|
|
n := 0
|
|
|
|
for _, v := range values {
|
|
|
|
if v > gt {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRollupShareFilter(args []interface{}, countFilter func(values []float64, limit float64) int) (rollupFunc, error) {
|
|
|
|
if err := expectRollupArgsNum(args, 2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
limits, err := getScalar(args[1], 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rf := func(rfa *rollupFuncArg) float64 {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
limit := limits[rfa.idx]
|
|
|
|
n := countFilter(values, limit)
|
|
|
|
return float64(n) / float64(len(values))
|
|
|
|
}
|
|
|
|
return rf, nil
|
|
|
|
}
|
|
|
|
|
2020-01-11 12:40:32 +00:00
|
|
|
func newRollupHoeffdingBoundLower(args []interface{}) (rollupFunc, error) {
|
|
|
|
if err := expectRollupArgsNum(args, 2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
phis, err := getScalar(args[0], 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rf := func(rfa *rollupFuncArg) float64 {
|
|
|
|
bound, avg := rollupHoeffdingBoundInternal(rfa, phis)
|
|
|
|
return avg - bound
|
|
|
|
}
|
|
|
|
return rf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRollupHoeffdingBoundUpper(args []interface{}) (rollupFunc, error) {
|
|
|
|
if err := expectRollupArgsNum(args, 2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
phis, err := getScalar(args[0], 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rf := func(rfa *rollupFuncArg) float64 {
|
|
|
|
bound, avg := rollupHoeffdingBoundInternal(rfa, phis)
|
|
|
|
return avg + bound
|
|
|
|
}
|
|
|
|
return rf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupHoeffdingBoundInternal(rfa *rollupFuncArg, phis []float64) (float64, float64) {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
|
|
|
return nan, nan
|
|
|
|
}
|
|
|
|
if len(values) == 1 {
|
|
|
|
return 0, values[0]
|
|
|
|
}
|
|
|
|
vMax := rollupMax(rfa)
|
|
|
|
vMin := rollupMin(rfa)
|
|
|
|
vAvg := rollupAvg(rfa)
|
|
|
|
vRange := vMax - vMin
|
|
|
|
if vRange <= 0 {
|
|
|
|
return 0, vAvg
|
|
|
|
}
|
|
|
|
phi := phis[rfa.idx]
|
|
|
|
if phi >= 1 {
|
|
|
|
return inf, vAvg
|
|
|
|
}
|
|
|
|
if phi <= 0 {
|
|
|
|
return 0, vAvg
|
|
|
|
}
|
|
|
|
// See https://en.wikipedia.org/wiki/Hoeffding%27s_inequality
|
|
|
|
// and https://www.youtube.com/watch?v=6UwcqiNsZ8U&feature=youtu.be&t=1237
|
|
|
|
bound := vRange * math.Sqrt(math.Log(1/(1-phi))/(2*float64(len(values))))
|
|
|
|
return bound, vAvg
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func newRollupQuantile(args []interface{}) (rollupFunc, error) {
|
|
|
|
if err := expectRollupArgsNum(args, 2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
phis, err := getScalar(args[0], 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rf := func(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
return rfa.prevValue
|
|
|
|
}
|
|
|
|
if len(values) == 1 {
|
|
|
|
// Fast path - only a single value.
|
|
|
|
return values[0]
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
hf := histogram.GetFast()
|
|
|
|
for _, v := range values {
|
|
|
|
hf.Update(v)
|
|
|
|
}
|
|
|
|
phi := phis[rfa.idx]
|
|
|
|
qv := hf.Quantile(phi)
|
|
|
|
histogram.PutFast(hf)
|
|
|
|
return qv
|
|
|
|
}
|
|
|
|
return rf, nil
|
|
|
|
}
|
|
|
|
|
2020-01-03 21:50:47 +00:00
|
|
|
func rollupHistogram(rfa *rollupFuncArg) float64 {
|
|
|
|
values := rfa.values
|
|
|
|
tsm := rfa.tsm
|
|
|
|
tsm.h.Reset()
|
|
|
|
for _, v := range values {
|
|
|
|
tsm.h.Update(v)
|
|
|
|
}
|
|
|
|
idx := rfa.idx
|
|
|
|
tsm.h.VisitNonZeroBuckets(func(vmrange string, count uint64) {
|
|
|
|
ts := tsm.GetOrCreateTimeseries(vmrange)
|
|
|
|
ts.Values[idx] = float64(count)
|
|
|
|
})
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func rollupAvg(rfa *rollupFuncArg) float64 {
|
|
|
|
// Do not use `Rapid calculation methods` at https://en.wikipedia.org/wiki/Standard_deviation,
|
|
|
|
// since it is slower and has no significant benefits in precision.
|
|
|
|
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
2020-01-11 10:47:29 +00:00
|
|
|
// Do not take into account rfa.prevValue, since it may lead
|
|
|
|
// to inconsistent results comparing to Prometheus on broken time series
|
|
|
|
// with irregular data points.
|
|
|
|
return nan
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
var sum float64
|
|
|
|
for _, v := range values {
|
|
|
|
sum += v
|
|
|
|
}
|
|
|
|
return sum / float64(len(values))
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupMin(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
2020-01-10 22:24:31 +00:00
|
|
|
if len(values) == 0 {
|
|
|
|
// Do not take into account rfa.prevValue, since it may lead
|
|
|
|
// to inconsistent results comparing to Prometheus on broken time series
|
|
|
|
// with irregular data points.
|
|
|
|
return nan
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2020-01-10 22:24:31 +00:00
|
|
|
minValue := values[0]
|
2019-05-22 21:16:55 +00:00
|
|
|
for _, v := range values {
|
|
|
|
if v < minValue {
|
|
|
|
minValue = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return minValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupMax(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
2020-01-10 22:24:31 +00:00
|
|
|
if len(values) == 0 {
|
|
|
|
// Do not take into account rfa.prevValue, since it may lead
|
|
|
|
// to inconsistent results comparing to Prometheus on broken time series
|
|
|
|
// with irregular data points.
|
|
|
|
return nan
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2020-01-10 22:24:31 +00:00
|
|
|
maxValue := values[0]
|
2019-05-22 21:16:55 +00:00
|
|
|
for _, v := range values {
|
|
|
|
if v > maxValue {
|
|
|
|
maxValue = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxValue
|
|
|
|
}
|
|
|
|
|
2020-01-10 17:38:46 +00:00
|
|
|
func rollupTmin(rfa *rollupFuncArg) float64 {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
timestamps := rfa.timestamps
|
2020-01-10 22:24:31 +00:00
|
|
|
if len(values) == 0 {
|
|
|
|
return nan
|
2020-01-10 17:38:46 +00:00
|
|
|
}
|
2020-01-10 22:24:31 +00:00
|
|
|
minValue := values[0]
|
|
|
|
minTimestamp := timestamps[0]
|
2020-01-10 17:38:46 +00:00
|
|
|
for i, v := range values {
|
|
|
|
if v < minValue {
|
|
|
|
minValue = v
|
|
|
|
minTimestamp = timestamps[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return float64(minTimestamp) * 1e-3
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupTmax(rfa *rollupFuncArg) float64 {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
timestamps := rfa.timestamps
|
2020-01-10 22:24:31 +00:00
|
|
|
if len(values) == 0 {
|
|
|
|
return nan
|
2020-01-10 17:38:46 +00:00
|
|
|
}
|
2020-01-10 22:24:31 +00:00
|
|
|
maxValue := values[0]
|
|
|
|
maxTimestamp := timestamps[0]
|
2020-01-10 17:38:46 +00:00
|
|
|
for i, v := range values {
|
|
|
|
if v > maxValue {
|
|
|
|
maxValue = v
|
|
|
|
maxTimestamp = timestamps[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return float64(maxTimestamp) * 1e-3
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func rollupSum(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
2019-10-08 09:28:06 +00:00
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
var sum float64
|
|
|
|
for _, v := range values {
|
|
|
|
sum += v
|
|
|
|
}
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
2020-07-23 22:13:05 +00:00
|
|
|
func rollupRateOverSum(rfa *rollupFuncArg) float64 {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
timestamps := rfa.timestamps
|
|
|
|
prevTimestamp := rfa.prevTimestamp
|
|
|
|
if math.IsNaN(rfa.prevValue) {
|
2020-07-29 08:36:51 +00:00
|
|
|
if len(timestamps) == 0 {
|
2020-07-23 22:13:05 +00:00
|
|
|
return nan
|
|
|
|
}
|
2020-07-23 23:14:48 +00:00
|
|
|
prevTimestamp = timestamps[0]
|
2020-07-23 22:13:05 +00:00
|
|
|
values = values[1:]
|
|
|
|
timestamps = timestamps[1:]
|
|
|
|
}
|
2020-07-29 08:36:51 +00:00
|
|
|
if len(timestamps) == 0 {
|
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
// Assume that the value didn't change since rfa.prevValue.
|
|
|
|
return 0
|
2020-07-23 22:13:05 +00:00
|
|
|
}
|
|
|
|
sum := float64(0)
|
|
|
|
for _, v := range values {
|
|
|
|
sum += v
|
|
|
|
}
|
|
|
|
dt := timestamps[len(timestamps)-1] - prevTimestamp
|
|
|
|
return sum / (float64(dt) / 1e3)
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:05:10 +00:00
|
|
|
func rollupRange(rfa *rollupFuncArg) float64 {
|
|
|
|
max := rollupMax(rfa)
|
|
|
|
min := rollupMin(rfa)
|
|
|
|
return max - min
|
|
|
|
}
|
|
|
|
|
2019-06-24 13:17:28 +00:00
|
|
|
func rollupSum2(rfa *rollupFuncArg) float64 {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
|
|
|
return rfa.prevValue * rfa.prevValue
|
|
|
|
}
|
|
|
|
var sum2 float64
|
|
|
|
for _, v := range values {
|
|
|
|
sum2 += v * v
|
|
|
|
}
|
|
|
|
return sum2
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupGeomean(rfa *rollupFuncArg) float64 {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
|
|
|
return rfa.prevValue
|
|
|
|
}
|
|
|
|
p := 1.0
|
|
|
|
for _, v := range values {
|
|
|
|
p *= v
|
|
|
|
}
|
|
|
|
return math.Pow(p, 1/float64(len(values)))
|
|
|
|
}
|
|
|
|
|
2020-01-03 22:46:39 +00:00
|
|
|
func rollupAbsent(rfa *rollupFuncArg) float64 {
|
|
|
|
if len(rfa.values) == 0 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func rollupCount(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
return float64(len(values))
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupStddev(rfa *rollupFuncArg) float64 {
|
|
|
|
stdvar := rollupStdvar(rfa)
|
|
|
|
return math.Sqrt(stdvar)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupStdvar(rfa *rollupFuncArg) float64 {
|
|
|
|
// See `Rapid calculation methods` at https://en.wikipedia.org/wiki/Standard_deviation
|
|
|
|
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if len(values) == 1 {
|
|
|
|
// Fast path.
|
|
|
|
return values[0]
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
var avg float64
|
|
|
|
var count float64
|
|
|
|
var q float64
|
|
|
|
for _, v := range values {
|
|
|
|
count++
|
|
|
|
avgNew := avg + (v-avg)/count
|
|
|
|
q += (v - avg) * (v - avgNew)
|
|
|
|
avg = avgNew
|
|
|
|
}
|
|
|
|
return q / count
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupDelta(rfa *rollupFuncArg) float64 {
|
2019-11-29 23:24:49 +00:00
|
|
|
return rollupDeltaInternal(rfa, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupIncrease(rfa *rollupFuncArg) float64 {
|
|
|
|
return rollupDeltaInternal(rfa, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupDeltaInternal(rfa *rollupFuncArg, canUseRealPrevValue bool) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
prevValue := rfa.prevValue
|
|
|
|
if math.IsNaN(prevValue) {
|
|
|
|
if len(values) == 0 {
|
|
|
|
return nan
|
|
|
|
}
|
2020-01-28 14:29:48 +00:00
|
|
|
// Assume that the previous non-existing value was 0
|
2020-07-21 14:22:36 +00:00
|
|
|
// only if the first value doesn't exceed too much the delta with the next value.
|
|
|
|
//
|
2020-01-28 14:29:48 +00:00
|
|
|
// This should prevent from improper increase() results for os-level counters
|
|
|
|
// such as cpu time or bytes sent over the network interface.
|
|
|
|
// These counters may start long ago before the first value appears in the db.
|
2020-07-21 14:22:36 +00:00
|
|
|
//
|
|
|
|
// This also should prevent from improper increase() results when a part of label values are changed
|
|
|
|
// without counter reset.
|
|
|
|
d := float64(10)
|
|
|
|
if len(values) > 1 {
|
|
|
|
d = values[1] - values[0]
|
|
|
|
}
|
|
|
|
if math.Abs(values[0]) < 10*(math.Abs(d)+1) {
|
2020-01-28 14:29:48 +00:00
|
|
|
prevValue = 0
|
|
|
|
if canUseRealPrevValue && !math.IsNaN(rfa.realPrevValue) {
|
|
|
|
prevValue = rfa.realPrevValue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prevValue = values[0]
|
2020-07-29 08:36:51 +00:00
|
|
|
values = values[1:]
|
2020-01-08 12:41:35 +00:00
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
if len(values) == 0 {
|
2019-08-15 20:29:59 +00:00
|
|
|
// Assume that the value didn't change on the given interval.
|
2019-06-23 16:06:16 +00:00
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
return values[len(values)-1] - prevValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupIdelta(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
return nan
|
|
|
|
}
|
2019-08-15 20:29:59 +00:00
|
|
|
// Assume that the value didn't change on the given interval.
|
2019-06-23 16:06:16 +00:00
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
lastValue := values[len(values)-1]
|
|
|
|
values = values[:len(values)-1]
|
|
|
|
if len(values) == 0 {
|
|
|
|
prevValue := rfa.prevValue
|
|
|
|
if math.IsNaN(prevValue) {
|
2019-08-15 20:29:59 +00:00
|
|
|
// Assume that the previous non-existing value was 0.
|
|
|
|
return lastValue
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
return lastValue - prevValue
|
|
|
|
}
|
|
|
|
return lastValue - values[len(values)-1]
|
|
|
|
}
|
|
|
|
|
2019-06-21 19:54:32 +00:00
|
|
|
func rollupDerivSlow(rfa *rollupFuncArg) float64 {
|
|
|
|
// Use linear regression like Prometheus does.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/73
|
|
|
|
_, k := linearRegression(rfa)
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupDerivFast(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
timestamps := rfa.timestamps
|
|
|
|
prevValue := rfa.prevValue
|
|
|
|
prevTimestamp := rfa.prevTimestamp
|
|
|
|
if math.IsNaN(prevValue) {
|
2019-12-24 13:54:04 +00:00
|
|
|
if len(values) == 0 {
|
2019-05-22 21:16:55 +00:00
|
|
|
return nan
|
|
|
|
}
|
2020-01-08 12:41:35 +00:00
|
|
|
if len(values) == 1 {
|
|
|
|
// It is impossible to determine the duration during which the value changed
|
|
|
|
// from 0 to the current value.
|
|
|
|
// The following attempts didn't work well:
|
|
|
|
// - using scrape interval as the duration. It fails on Prometheus restarts when it
|
|
|
|
// skips scraping for the counter. This results in too high rate() value for the first point
|
|
|
|
// after Prometheus restarts.
|
|
|
|
// - using window or step as the duration. It results in too small rate() values for the first
|
|
|
|
// points of time series.
|
|
|
|
//
|
|
|
|
// So just return nan
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
prevValue = values[0]
|
|
|
|
prevTimestamp = timestamps[0]
|
2020-01-03 17:00:20 +00:00
|
|
|
} else if len(values) == 0 {
|
2019-08-15 20:29:59 +00:00
|
|
|
// Assume that the value didn't change on the given interval.
|
2019-06-23 16:06:16 +00:00
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
vEnd := values[len(values)-1]
|
|
|
|
tEnd := timestamps[len(timestamps)-1]
|
|
|
|
dv := vEnd - prevValue
|
|
|
|
dt := float64(tEnd-prevTimestamp) * 1e-3
|
|
|
|
return dv / dt
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupIderiv(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
timestamps := rfa.timestamps
|
2019-08-15 20:29:59 +00:00
|
|
|
if len(values) < 2 {
|
2020-01-03 17:00:20 +00:00
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
return nan
|
|
|
|
}
|
2020-01-08 12:41:35 +00:00
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
// It is impossible to determine the duration during which the value changed
|
|
|
|
// from 0 to the current value.
|
|
|
|
// The following attempts didn't work well:
|
|
|
|
// - using scrape interval as the duration. It fails on Prometheus restarts when it
|
|
|
|
// skips scraping for the counter. This results in too high rate() value for the first point
|
|
|
|
// after Prometheus restarts.
|
|
|
|
// - using window or step as the duration. It results in too small rate() values for the first
|
|
|
|
// points of time series.
|
|
|
|
//
|
|
|
|
// So just return nan
|
|
|
|
return nan
|
2020-01-03 17:00:20 +00:00
|
|
|
}
|
2020-01-08 12:41:35 +00:00
|
|
|
return (values[0] - rfa.prevValue) / (float64(timestamps[0]-rfa.prevTimestamp) * 1e-3)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
vEnd := values[len(values)-1]
|
|
|
|
tEnd := timestamps[len(timestamps)-1]
|
|
|
|
values = values[:len(values)-1]
|
|
|
|
timestamps = timestamps[:len(timestamps)-1]
|
2019-07-03 09:27:07 +00:00
|
|
|
// Skip data points with duplicate timestamps.
|
|
|
|
for len(timestamps) > 0 && timestamps[len(timestamps)-1] >= tEnd {
|
|
|
|
timestamps = timestamps[:len(timestamps)-1]
|
|
|
|
}
|
|
|
|
var tStart int64
|
|
|
|
var vStart float64
|
|
|
|
if len(timestamps) == 0 {
|
|
|
|
if math.IsNaN(rfa.prevValue) {
|
2019-06-23 16:06:16 +00:00
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-07-03 09:27:07 +00:00
|
|
|
tStart = rfa.prevTimestamp
|
|
|
|
vStart = rfa.prevValue
|
2019-05-22 21:16:55 +00:00
|
|
|
} else {
|
2019-07-03 09:27:07 +00:00
|
|
|
tStart = timestamps[len(timestamps)-1]
|
|
|
|
vStart = values[len(timestamps)-1]
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-07-03 09:27:07 +00:00
|
|
|
dv := vEnd - vStart
|
|
|
|
dt := tEnd - tStart
|
2019-08-16 08:55:10 +00:00
|
|
|
return dv / (float64(dt) * 1e-3)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 08:57:56 +00:00
|
|
|
func rollupLifetime(rfa *rollupFuncArg) float64 {
|
|
|
|
// Calculate the duration between the first and the last data points.
|
|
|
|
timestamps := rfa.timestamps
|
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
if len(timestamps) < 2 {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return float64(timestamps[len(timestamps)-1]-timestamps[0]) * 1e-3
|
|
|
|
}
|
|
|
|
if len(timestamps) == 0 {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return float64(timestamps[len(timestamps)-1]-rfa.prevTimestamp) * 1e-3
|
2019-11-01 10:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func rollupLag(rfa *rollupFuncArg) float64 {
|
|
|
|
// Calculate the duration between the current timestamp and the last data point.
|
|
|
|
timestamps := rfa.timestamps
|
|
|
|
if len(timestamps) == 0 {
|
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return float64(rfa.currTimestamp-rfa.prevTimestamp) * 1e-3
|
|
|
|
}
|
|
|
|
return float64(rfa.currTimestamp-timestamps[len(timestamps)-1]) * 1e-3
|
2019-08-16 08:57:56 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 18:07:59 +00:00
|
|
|
func rollupScrapeInterval(rfa *rollupFuncArg) float64 {
|
|
|
|
// Calculate the average interval between data points.
|
|
|
|
timestamps := rfa.timestamps
|
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
if len(timestamps) < 2 {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return float64(timestamps[len(timestamps)-1]-timestamps[0]) * 1e-3 / float64(len(timestamps)-1)
|
|
|
|
}
|
|
|
|
if len(timestamps) == 0 {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return (float64(timestamps[len(timestamps)-1]-rfa.prevTimestamp) * 1e-3) / float64(len(timestamps))
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func rollupChanges(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
prevValue := rfa.prevValue
|
2019-06-23 16:06:16 +00:00
|
|
|
n := 0
|
2019-05-22 21:16:55 +00:00
|
|
|
if math.IsNaN(prevValue) {
|
2019-06-23 16:06:16 +00:00
|
|
|
if len(values) == 0 {
|
|
|
|
return nan
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
prevValue = values[0]
|
2019-06-23 16:06:16 +00:00
|
|
|
values = values[1:]
|
|
|
|
n++
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
for _, v := range values {
|
|
|
|
if v != prevValue {
|
|
|
|
n++
|
|
|
|
prevValue = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return float64(n)
|
|
|
|
}
|
|
|
|
|
2019-09-25 17:36:51 +00:00
|
|
|
func rollupIncreases(rfa *rollupFuncArg) float64 {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
prevValue := rfa.prevValue
|
|
|
|
if math.IsNaN(prevValue) {
|
|
|
|
prevValue = values[0]
|
|
|
|
values = values[1:]
|
|
|
|
}
|
|
|
|
if len(values) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
n := 0
|
|
|
|
for _, v := range values {
|
|
|
|
if v > prevValue {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
prevValue = v
|
|
|
|
}
|
|
|
|
return float64(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// `decreases_over_time` logic is the same as `resets` logic.
|
|
|
|
var rollupDecreases = rollupResets
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func rollupResets(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
prevValue := rfa.prevValue
|
|
|
|
if math.IsNaN(prevValue) {
|
|
|
|
prevValue = values[0]
|
|
|
|
values = values[1:]
|
|
|
|
}
|
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
n := 0
|
|
|
|
for _, v := range values {
|
|
|
|
if v < prevValue {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
prevValue = v
|
|
|
|
}
|
|
|
|
return float64(n)
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:42:10 +00:00
|
|
|
// getCandlestickValues returns a subset of rfa.values suitable for rollup_candlestick
|
|
|
|
//
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/309 for details.
|
|
|
|
func getCandlestickValues(rfa *rollupFuncArg) []float64 {
|
|
|
|
currTimestamp := rfa.currTimestamp
|
|
|
|
timestamps := rfa.timestamps
|
|
|
|
for len(timestamps) > 0 && timestamps[len(timestamps)-1] >= currTimestamp {
|
|
|
|
timestamps = timestamps[:len(timestamps)-1]
|
|
|
|
}
|
|
|
|
if len(timestamps) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return rfa.values[:len(timestamps)]
|
|
|
|
}
|
|
|
|
|
2020-02-24 11:24:18 +00:00
|
|
|
func getFirstValueForCandlestick(rfa *rollupFuncArg) float64 {
|
|
|
|
if rfa.prevTimestamp+rfa.step >= rfa.currTimestamp {
|
2020-02-23 16:03:52 +00:00
|
|
|
return rfa.prevValue
|
|
|
|
}
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:42:10 +00:00
|
|
|
func rollupOpen(rfa *rollupFuncArg) float64 {
|
2020-02-24 11:24:18 +00:00
|
|
|
v := getFirstValueForCandlestick(rfa)
|
|
|
|
if !math.IsNaN(v) {
|
|
|
|
return v
|
|
|
|
}
|
2020-02-04 20:42:10 +00:00
|
|
|
values := getCandlestickValues(rfa)
|
|
|
|
if len(values) == 0 {
|
2020-02-24 11:24:18 +00:00
|
|
|
return nan
|
2020-02-04 20:42:10 +00:00
|
|
|
}
|
|
|
|
return values[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupClose(rfa *rollupFuncArg) float64 {
|
|
|
|
values := getCandlestickValues(rfa)
|
|
|
|
if len(values) == 0 {
|
2020-02-24 11:24:18 +00:00
|
|
|
return getFirstValueForCandlestick(rfa)
|
2020-02-04 20:42:10 +00:00
|
|
|
}
|
|
|
|
return values[len(values)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupHigh(rfa *rollupFuncArg) float64 {
|
|
|
|
values := getCandlestickValues(rfa)
|
2020-02-24 11:24:18 +00:00
|
|
|
max := getFirstValueForCandlestick(rfa)
|
|
|
|
if math.IsNaN(max) {
|
|
|
|
if len(values) == 0 {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
max = values[0]
|
|
|
|
values = values[1:]
|
2020-02-04 20:42:10 +00:00
|
|
|
}
|
2020-02-24 11:24:18 +00:00
|
|
|
for _, v := range values {
|
2020-02-04 20:42:10 +00:00
|
|
|
if v > max {
|
|
|
|
max = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupLow(rfa *rollupFuncArg) float64 {
|
|
|
|
values := getCandlestickValues(rfa)
|
2020-02-24 11:24:18 +00:00
|
|
|
min := getFirstValueForCandlestick(rfa)
|
|
|
|
if math.IsNaN(min) {
|
|
|
|
if len(values) == 0 {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
min = values[0]
|
|
|
|
values = values[1:]
|
2020-02-04 20:42:10 +00:00
|
|
|
}
|
2020-02-24 11:24:18 +00:00
|
|
|
for _, v := range values {
|
2020-02-04 20:42:10 +00:00
|
|
|
if v < min {
|
|
|
|
min = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
|
2020-04-15 09:00:39 +00:00
|
|
|
func rollupTimestamp(rfa *rollupFuncArg) float64 {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
|
|
|
timestamps := rfa.timestamps
|
|
|
|
if len(timestamps) == 0 {
|
|
|
|
// Do not take into account rfa.prevTimestamp, since it may lead
|
|
|
|
// to inconsistent results comparing to Prometheus on broken time series
|
|
|
|
// with irregular data points.
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return float64(timestamps[len(timestamps)-1]) / 1e3
|
|
|
|
}
|
|
|
|
|
2020-07-17 15:28:19 +00:00
|
|
|
func rollupModeOverTime(rfa *rollupFuncArg) float64 {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
2020-07-20 12:18:20 +00:00
|
|
|
return modeNoNaNs(rfa.prevValue, rfa.values)
|
2020-07-17 15:28:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 09:05:08 +00:00
|
|
|
func rollupAscentOverTime(rfa *rollupFuncArg) float64 {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
prevValue := rfa.prevValue
|
|
|
|
if math.IsNaN(prevValue) {
|
|
|
|
if len(values) == 0 {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
prevValue = values[0]
|
|
|
|
values = values[1:]
|
|
|
|
}
|
|
|
|
s := float64(0)
|
|
|
|
for _, v := range values {
|
|
|
|
d := v - prevValue
|
|
|
|
if d > 0 {
|
|
|
|
s += d
|
|
|
|
}
|
|
|
|
prevValue = v
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupDescentOverTime(rfa *rollupFuncArg) float64 {
|
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
prevValue := rfa.prevValue
|
|
|
|
if math.IsNaN(prevValue) {
|
|
|
|
if len(values) == 0 {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
prevValue = values[0]
|
|
|
|
values = values[1:]
|
|
|
|
}
|
|
|
|
s := float64(0)
|
|
|
|
for _, v := range values {
|
|
|
|
d := prevValue - v
|
|
|
|
if d > 0 {
|
|
|
|
s += d
|
|
|
|
}
|
|
|
|
prevValue = v
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func rollupFirst(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
2020-01-10 22:24:31 +00:00
|
|
|
// Do not take into account rfa.prevValue, since it may lead
|
|
|
|
// to inconsistent results comparing to Prometheus on broken time series
|
|
|
|
// with irregular data points.
|
2019-05-22 21:16:55 +00:00
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return values[0]
|
|
|
|
}
|
|
|
|
|
2019-06-21 18:50:44 +00:00
|
|
|
var rollupDefault = rollupLast
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
func rollupLast(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
2020-01-10 22:24:31 +00:00
|
|
|
// Do not take into account rfa.prevValue, since it may lead
|
|
|
|
// to inconsistent results comparing to Prometheus on broken time series
|
|
|
|
// with irregular data points.
|
|
|
|
return nan
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
return values[len(values)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupDistinct(rfa *rollupFuncArg) float64 {
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
m := make(map[float64]struct{})
|
|
|
|
for _, v := range values {
|
|
|
|
m[v] = struct{}{}
|
|
|
|
}
|
|
|
|
return float64(len(m))
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupIntegrate(rfa *rollupFuncArg) float64 {
|
|
|
|
prevTimestamp := rfa.prevTimestamp
|
|
|
|
|
2019-06-21 20:22:35 +00:00
|
|
|
// There is no need in handling NaNs here, since they must be cleaned up
|
2019-05-22 21:16:55 +00:00
|
|
|
// before calling rollup funcs.
|
|
|
|
values := rfa.values
|
|
|
|
timestamps := rfa.timestamps
|
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
if math.IsNaN(rfa.prevValue) {
|
|
|
|
return nan
|
|
|
|
}
|
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
prevValue := rfa.prevValue
|
|
|
|
if math.IsNaN(prevValue) {
|
|
|
|
prevValue = values[0]
|
|
|
|
prevTimestamp = timestamps[0]
|
|
|
|
values = values[1:]
|
|
|
|
timestamps = timestamps[1:]
|
|
|
|
}
|
|
|
|
if len(values) == 0 {
|
2019-06-23 16:06:16 +00:00
|
|
|
return 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sum float64
|
|
|
|
for i, v := range values {
|
|
|
|
timestamp := timestamps[i]
|
|
|
|
dt := float64(timestamp-prevTimestamp) * 1e-3
|
|
|
|
sum += 0.5 * (v + prevValue) * dt
|
2019-11-13 19:10:08 +00:00
|
|
|
prevTimestamp = timestamp
|
|
|
|
prevValue = v
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
|
|
|
func rollupFake(rfa *rollupFuncArg) float64 {
|
|
|
|
logger.Panicf("BUG: rollupFake shouldn't be called")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func getScalar(arg interface{}, argNum int) ([]float64, error) {
|
|
|
|
ts, ok := arg.([]*timeseries)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf(`unexpected type for arg #%d; got %T; want %T`, argNum+1, arg, ts)
|
|
|
|
}
|
|
|
|
if len(ts) != 1 {
|
|
|
|
return nil, fmt.Errorf(`arg #%d must contain a single timeseries; got %d timeseries`, argNum+1, len(ts))
|
|
|
|
}
|
|
|
|
return ts[0].Values, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getString(tss []*timeseries, argNum int) (string, error) {
|
|
|
|
if len(tss) != 1 {
|
|
|
|
return "", fmt.Errorf(`arg #%d must contain a single timeseries; got %d timeseries`, argNum+1, len(tss))
|
|
|
|
}
|
|
|
|
ts := tss[0]
|
|
|
|
for _, v := range ts.Values {
|
|
|
|
if !math.IsNaN(v) {
|
|
|
|
return "", fmt.Errorf(`arg #%d contains non-string timeseries`, argNum+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(ts.MetricName.MetricGroup), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectRollupArgsNum(args []interface{}, expectedNum int) error {
|
|
|
|
if len(args) == expectedNum {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf(`unexpected number of args; got %d; want %d`, len(args), expectedNum)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRollupFuncArg() *rollupFuncArg {
|
|
|
|
v := rfaPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &rollupFuncArg{}
|
|
|
|
}
|
|
|
|
return v.(*rollupFuncArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putRollupFuncArg(rfa *rollupFuncArg) {
|
|
|
|
rfa.reset()
|
|
|
|
rfaPool.Put(rfa)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rfaPool sync.Pool
|