mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
refactor duplication of irate and idelta functions implementations
This commit is contained in:
parent
dbf83666bb
commit
f02df4138c
|
@ -145,8 +145,17 @@ func funcIncrease(ev *evaluator, args Expressions) model.Value {
|
|||
|
||||
// === irate(node model.ValMatrix) Vector ===
|
||||
func funcIrate(ev *evaluator, args Expressions) model.Value {
|
||||
return instantValue(ev, args[0], true)
|
||||
}
|
||||
|
||||
// === idelta(node model.ValMatric) Vector ===
|
||||
func funcIdelta(ev *evaluator, args Expressions) model.Value {
|
||||
return instantValue(ev, args[0], false)
|
||||
}
|
||||
|
||||
func instantValue(ev *evaluator, arg Expr, isRate bool) model.Value {
|
||||
resultVector := vector{}
|
||||
for _, samples := range ev.evalMatrix(args[0]) {
|
||||
for _, samples := range ev.evalMatrix(arg) {
|
||||
// No sense in trying to compute a rate without at least two points. Drop
|
||||
// this vector element.
|
||||
if len(samples.Values) < 2 {
|
||||
|
@ -157,7 +166,7 @@ func funcIrate(ev *evaluator, args Expressions) model.Value {
|
|||
previousSample := samples.Values[len(samples.Values)-2]
|
||||
|
||||
var resultValue model.SampleValue
|
||||
if lastSample.Value < previousSample.Value {
|
||||
if isRate && lastSample.Value < previousSample.Value {
|
||||
// Counter reset.
|
||||
resultValue = lastSample.Value
|
||||
} else {
|
||||
|
@ -169,38 +178,10 @@ func funcIrate(ev *evaluator, args Expressions) model.Value {
|
|||
// Avoid dividing by 0.
|
||||
continue
|
||||
}
|
||||
// Convert to per-second.
|
||||
resultValue /= model.SampleValue(sampledInterval.Seconds())
|
||||
|
||||
resultSample := &sample{
|
||||
Metric: samples.Metric,
|
||||
Value: resultValue,
|
||||
Timestamp: ev.Timestamp,
|
||||
}
|
||||
resultSample.Metric.Del(model.MetricNameLabel)
|
||||
resultVector = append(resultVector, resultSample)
|
||||
}
|
||||
return resultVector
|
||||
}
|
||||
|
||||
// === idelta(node model.ValMatric) Vector ===
|
||||
func funcIdelta(ev *evaluator, args Expressions) model.Value {
|
||||
resultVector := vector{}
|
||||
for _, samples := range ev.evalMatrix(args[0]) {
|
||||
// No sense in trying to compute a rate without at least two points. Drop
|
||||
// this vector element.
|
||||
if len(samples.Values) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
lastSample := samples.Values[len(samples.Values)-1]
|
||||
previousSample := samples.Values[len(samples.Values)-2]
|
||||
resultValue := lastSample.Value - previousSample.Value
|
||||
|
||||
sampledInterval := lastSample.Timestamp.Sub(previousSample.Timestamp)
|
||||
if sampledInterval == 0 {
|
||||
// Avoid dividing by 0.
|
||||
continue
|
||||
if isRate {
|
||||
// Convert to per-second.
|
||||
resultValue /= model.SampleValue(sampledInterval.Seconds())
|
||||
}
|
||||
|
||||
resultSample := &sample{
|
||||
|
|
Loading…
Reference in a new issue