diff --git a/docs/configuration/template_reference.md b/docs/configuration/template_reference.md index 06942891b..47df9d1e0 100644 --- a/docs/configuration/template_reference.md +++ b/docs/configuration/template_reference.md @@ -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. diff --git a/template/template.go b/template/template.go index f04f5ea53..43772805c 100644 --- a/template/template.go +++ b/template/template.go @@ -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 { diff --git a/template/template_test.go b/template/template_test.go index e7bdcc3b8..57de1d0f5 100644 --- a/template/template_test.go +++ b/template/template_test.go @@ -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\" }}",