mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 21:54:10 -08:00
Merge pull request #687 from prometheus/fabxc/tmpl_query
Do implicit conversion in template queries.
This commit is contained in:
commit
870ad3e32e
|
@ -60,9 +60,32 @@ func query(q string, timestamp clientmodel.Timestamp, queryEngine *promql.Engine
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
vector, err := query.Exec().Vector()
|
res := query.Exec()
|
||||||
if err != nil {
|
if res.Err != nil {
|
||||||
return nil, err
|
return nil, res.Err
|
||||||
|
}
|
||||||
|
var vector promql.Vector
|
||||||
|
|
||||||
|
switch v := res.Value.(type) {
|
||||||
|
case promql.Matrix:
|
||||||
|
return nil, errors.New("matrix return values not supported")
|
||||||
|
case promql.Vector:
|
||||||
|
vector = v
|
||||||
|
case *promql.Scalar:
|
||||||
|
vector = promql.Vector{&promql.Sample{
|
||||||
|
Value: v.Value,
|
||||||
|
Timestamp: v.Timestamp,
|
||||||
|
}}
|
||||||
|
case *promql.String:
|
||||||
|
vector = promql.Vector{&promql.Sample{
|
||||||
|
Metric: clientmodel.COWMetric{
|
||||||
|
Metric: clientmodel.Metric{"__value__": clientmodel.LabelValue(v.Value)},
|
||||||
|
Copied: true,
|
||||||
|
},
|
||||||
|
Timestamp: v.Timestamp,
|
||||||
|
}}
|
||||||
|
default:
|
||||||
|
panic("template.query: unhandled result value type")
|
||||||
}
|
}
|
||||||
|
|
||||||
// promql.Vector is hard to work with in templates, so convert to
|
// promql.Vector is hard to work with in templates, so convert to
|
||||||
|
|
|
@ -65,6 +65,15 @@ func TestTemplateExpansion(t *testing.T) {
|
||||||
text: "{{define \"x\"}}{{.arg0}} {{.arg1}}{{end}}{{template \"x\" (args 1 \"2\")}}",
|
text: "{{define \"x\"}}{{.arg0}} {{.arg1}}{{end}}{{template \"x\" (args 1 \"2\")}}",
|
||||||
output: "1 2",
|
output: "1 2",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
text: "{{ query \"1.5\" | first | value }}",
|
||||||
|
output: "1.5",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Get value from scalar query.
|
||||||
|
text: "{{ query \"scalar(count(metric))\" | first | value }}",
|
||||||
|
output: "2",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
// Get value from query.
|
// Get value from query.
|
||||||
text: "{{ query \"metric{instance='a'}\" | first | value }}",
|
text: "{{ query \"metric{instance='a'}\" | first | value }}",
|
||||||
|
|
Loading…
Reference in a new issue