Support native histogram values in template functions

Co-authored-by: Aleks Fazlieva <britishrum@users.noreply.github.com>
Signed-off-by: suntala <arati.rana@grafana.com>
This commit is contained in:
suntala 2024-03-26 22:17:19 +01:00
parent 44f385fd51
commit 9a7c6a5cc4
3 changed files with 26 additions and 4 deletions

View file

@ -18,7 +18,7 @@ The primary data structure for dealing with time series data is the sample, defi
```go
type sample struct {
Labels map[string]string
Value float64
Value interface{}
}
```
@ -44,7 +44,7 @@ If functions are used in a pipeline, the pipeline value is passed as the last ar
| query | query string | []sample | Queries the database, does not support returning range vectors. |
| first | []sample | sample | Equivalent to `index a 0` |
| label | label, sample | string | Equivalent to `index sample.Labels label` |
| value | sample | float64 | Equivalent to `sample.Value` |
| value | sample | interface{} | Equivalent to `sample.Value` |
| sortByLabel | label, []samples | []sample | Sorts the samples by the given label. Is stable. |
`first`, `label` and `value` are intended to make query results easily usable in pipelines.

View file

@ -57,7 +57,7 @@ func init() {
// A version of vector that's easier to use from templates.
type sample struct {
Labels map[string]string
Value float64
Value interface{}
}
type queryResult []*sample
@ -96,6 +96,9 @@ func query(ctx context.Context, q string, ts time.Time, queryFn QueryFunc) (quer
Value: v.F,
Labels: v.Metric.Map(),
}
if v.H != nil {
s.Value = v.H
}
result[n] = &s
}
return result, nil
@ -160,7 +163,7 @@ func NewTemplateExpander(
"label": func(label string, s *sample) string {
return s.Labels[label]
},
"value": func(s *sample) float64 {
"value": func(s *sample) interface{} {
return s.Value
},
"strvalue": func(s *sample) string {

View file

@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql"
)
@ -39,6 +40,12 @@ func TestTemplateExpansion(t *testing.T) {
text: "{{ 1 }}",
output: "1",
},
{
// Native histogram value.
text: "{{ . | value }}",
input: &sample{Value: &histogram.FloatHistogram{Count: 3, Sum: 10}},
output: (&histogram.FloatHistogram{Count: 3, Sum: 10}).String(),
},
{
// Non-ASCII space (not allowed in text/template, see https://github.com/golang/go/blob/master/src/text/template/parse/lex.go#L98)
text: "{{ }}",
@ -84,6 +91,18 @@ func TestTemplateExpansion(t *testing.T) {
},
output: "11",
},
{
// Get value of a native histogram from query.
text: "{{ query \"metric{instance='a'}\" | first | value }}",
queryResult: promql.Vector{
{
Metric: labels.FromStrings(labels.MetricName, "metric", "instance", "a"),
T: 0,
H: &histogram.FloatHistogram{Count: 3, Sum: 10},
},
},
output: (&histogram.FloatHistogram{Count: 3, Sum: 10}).String(),
},
{
// Get label from query.
text: "{{ query \"metric{instance='a'}\" | first | label \"instance\" }}",