mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Add count_scalar() function.
Change-Id: I63f09dd0479d0a6b016f5f857dd39dcbda56c7f9
This commit is contained in:
parent
18d9d00100
commit
7e9ecaac3a
|
@ -265,7 +265,18 @@ func scalarImpl(timestamp clientmodel.Timestamp, view *viewAdapter, args []Node)
|
||||||
return clientmodel.SampleValue(v[0].Value)
|
return clientmodel.SampleValue(v[0].Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === count_scalar(vector VectorNode) model.SampleValue ===
|
||||||
|
func countScalarImpl(timestamp clientmodel.Timestamp, view *viewAdapter, args []Node) interface{} {
|
||||||
|
return clientmodel.SampleValue(len(args[0].(VectorNode).Eval(timestamp, view)))
|
||||||
|
}
|
||||||
|
|
||||||
var functions = map[string]*Function{
|
var functions = map[string]*Function{
|
||||||
|
"count_scalar": {
|
||||||
|
name: "count_scalar",
|
||||||
|
argTypes: []ExprType{VECTOR},
|
||||||
|
returnType: SCALAR,
|
||||||
|
callFn: countScalarImpl,
|
||||||
|
},
|
||||||
"delta": {
|
"delta": {
|
||||||
name: "delta",
|
name: "delta",
|
||||||
argTypes: []ExprType{MATRIX, SCALAR},
|
argTypes: []ExprType{MATRIX, SCALAR},
|
||||||
|
|
|
@ -159,7 +159,7 @@ func EvalToString(node Node, timestamp clientmodel.Timestamp, format OutputForma
|
||||||
evalTimer.Stop()
|
evalTimer.Stop()
|
||||||
switch format {
|
switch format {
|
||||||
case TEXT:
|
case TEXT:
|
||||||
return fmt.Sprintf("scalar: %v", scalar)
|
return fmt.Sprintf("scalar: %v @[%v]", scalar, timestamp)
|
||||||
case JSON:
|
case JSON:
|
||||||
return TypedValueToJSON(scalar, "scalar")
|
return TypedValueToJSON(scalar, "scalar")
|
||||||
}
|
}
|
||||||
|
|
|
@ -382,6 +382,18 @@ func TestExpressions(t *testing.T) {
|
||||||
output: []string{`testcounter_reset_end => -90 @[%v]`},
|
output: []string{`testcounter_reset_end => -90 @[%v]`},
|
||||||
fullRanges: 1,
|
fullRanges: 1,
|
||||||
intervalRanges: 0,
|
intervalRanges: 0,
|
||||||
|
}, {
|
||||||
|
// count_scalar for a non-empty vector should return scalar element count.
|
||||||
|
expr: `count_scalar(http_requests)`,
|
||||||
|
output: []string{`scalar: 8 @[%v]`},
|
||||||
|
fullRanges: 0,
|
||||||
|
intervalRanges: 8,
|
||||||
|
}, {
|
||||||
|
// count_scalar for an empty vector should return scalar 0.
|
||||||
|
expr: `count_scalar(nonexistent)`,
|
||||||
|
output: []string{`scalar: 0 @[%v]`},
|
||||||
|
fullRanges: 0,
|
||||||
|
intervalRanges: 0,
|
||||||
}, {
|
}, {
|
||||||
// Empty expressions shouldn"t parse.
|
// Empty expressions shouldn"t parse.
|
||||||
expr: ``,
|
expr: ``,
|
||||||
|
|
Loading…
Reference in a new issue