2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2014-05-28 10:44:54 -07:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-05-28 12:22:08 -07:00
|
|
|
package template
|
2014-05-28 10:44:54 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2022-06-08 01:44:08 -07:00
|
|
|
"errors"
|
2014-05-28 10:44:54 -07:00
|
|
|
"fmt"
|
2020-10-22 02:00:08 -07:00
|
|
|
html_template "html/template"
|
2014-06-05 06:07:54 -07:00
|
|
|
"math"
|
2021-12-16 01:27:31 -08:00
|
|
|
"net"
|
2017-05-13 06:47:04 -07:00
|
|
|
"net/url"
|
2014-06-05 06:07:54 -07:00
|
|
|
"sort"
|
2021-04-13 15:30:15 -07:00
|
|
|
"strconv"
|
2014-06-05 10:44:19 -07:00
|
|
|
"strings"
|
2014-06-10 07:30:06 -07:00
|
|
|
text_template "text/template"
|
2020-10-22 02:00:08 -07:00
|
|
|
"time"
|
2014-05-28 10:44:54 -07:00
|
|
|
|
2022-02-12 15:58:27 -08:00
|
|
|
"github.com/grafana/regexp"
|
2019-03-25 16:01:12 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2024-06-10 11:40:11 -07:00
|
|
|
common_templates "github.com/prometheus/common/helpers/templates"
|
|
|
|
|
2015-03-30 10:43:19 -07:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
2015-05-29 04:30:30 -07:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
2014-05-28 10:44:54 -07:00
|
|
|
)
|
|
|
|
|
2018-11-06 06:39:06 -08:00
|
|
|
var (
|
|
|
|
templateTextExpansionFailures = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_template_text_expansion_failures_total",
|
|
|
|
Help: "The total number of template text expansion failures.",
|
|
|
|
})
|
|
|
|
templateTextExpansionTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_template_text_expansions_total",
|
|
|
|
Help: "The total number of template text expansions.",
|
|
|
|
})
|
2022-07-14 14:45:32 -07:00
|
|
|
|
|
|
|
errNaNOrInf = errors.New("value is NaN or Inf")
|
2018-11-06 06:39:06 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(templateTextExpansionFailures)
|
|
|
|
prometheus.MustRegister(templateTextExpansionTotal)
|
|
|
|
}
|
|
|
|
|
2014-05-28 10:44:54 -07:00
|
|
|
// A version of vector that's easier to use from templates.
|
|
|
|
type sample struct {
|
|
|
|
Labels map[string]string
|
2024-03-26 14:17:19 -07:00
|
|
|
Value interface{}
|
2014-05-28 10:44:54 -07:00
|
|
|
}
|
|
|
|
type queryResult []*sample
|
|
|
|
|
2014-06-05 06:07:54 -07:00
|
|
|
type queryResultByLabelSorter struct {
|
|
|
|
results queryResult
|
|
|
|
by string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q queryResultByLabelSorter) Len() int {
|
|
|
|
return len(q.results)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q queryResultByLabelSorter) Less(i, j int) bool {
|
|
|
|
return q.results[i].Labels[q.by] < q.results[j].Labels[q.by]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q queryResultByLabelSorter) Swap(i, j int) {
|
|
|
|
q.results[i], q.results[j] = q.results[j], q.results[i]
|
|
|
|
}
|
|
|
|
|
2017-11-23 04:04:54 -08:00
|
|
|
// QueryFunc executes a PromQL query at the given time.
|
|
|
|
type QueryFunc func(context.Context, string, time.Time) (promql.Vector, error)
|
|
|
|
|
|
|
|
func query(ctx context.Context, q string, ts time.Time, queryFn QueryFunc) (queryResult, error) {
|
|
|
|
vector, err := queryFn(ctx, q, ts)
|
2014-06-10 07:30:06 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-28 10:44:54 -07:00
|
|
|
|
2015-03-30 10:43:19 -07:00
|
|
|
// promql.Vector is hard to work with in templates, so convert to
|
2014-06-10 07:30:06 -07:00
|
|
|
// base data types.
|
2016-12-24 15:37:46 -08:00
|
|
|
// TODO(fabxc): probably not true anymore after type rework.
|
2021-10-22 01:06:44 -07:00
|
|
|
result := make(queryResult, len(vector))
|
2014-06-10 07:30:06 -07:00
|
|
|
for n, v := range vector {
|
|
|
|
s := sample{
|
promql: Separate `Point` into `FPoint` and `HPoint`
In other words: Instead of having a “polymorphous” `Point` that can
either contain a float value or a histogram value, use an `FPoint` for
floats and an `HPoint` for histograms.
This seemingly small change has a _lot_ of repercussions throughout
the codebase.
The idea here is to avoid the increase in size of `Point` arrays that
happened after native histograms had been added.
The higher-level data structures (`Sample`, `Series`, etc.) are still
“polymorphous”. The same idea could be applied to them, but at each
step the trade-offs needed to be evaluated.
The idea with this change is to do the minimum necessary to get back
to pre-histogram performance for functions that do not touch
histograms. Here are comparisons for the `changes` function. The test
data doesn't include histograms yet. Ideally, there would be no change
in the benchmark result at all.
First runtime v2.39 compared to directly prior to this commit:
```
name old time/op new time/op delta
RangeQuery/expr=changes(a_one[1d]),steps=1-16 391µs ± 2% 542µs ± 1% +38.58% (p=0.000 n=9+8)
RangeQuery/expr=changes(a_one[1d]),steps=10-16 452µs ± 2% 617µs ± 2% +36.48% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_one[1d]),steps=100-16 1.12ms ± 1% 1.36ms ± 2% +21.58% (p=0.000 n=8+10)
RangeQuery/expr=changes(a_one[1d]),steps=1000-16 7.83ms ± 1% 8.94ms ± 1% +14.21% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_ten[1d]),steps=1-16 2.98ms ± 0% 3.30ms ± 1% +10.67% (p=0.000 n=9+10)
RangeQuery/expr=changes(a_ten[1d]),steps=10-16 3.66ms ± 1% 4.10ms ± 1% +11.82% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_ten[1d]),steps=100-16 10.5ms ± 0% 11.8ms ± 1% +12.50% (p=0.000 n=8+10)
RangeQuery/expr=changes(a_ten[1d]),steps=1000-16 77.6ms ± 1% 87.4ms ± 1% +12.63% (p=0.000 n=9+9)
RangeQuery/expr=changes(a_hundred[1d]),steps=1-16 30.4ms ± 2% 32.8ms ± 1% +8.01% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=10-16 37.1ms ± 2% 40.6ms ± 2% +9.64% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=100-16 105ms ± 1% 117ms ± 1% +11.69% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=1000-16 783ms ± 3% 876ms ± 1% +11.83% (p=0.000 n=9+10)
```
And then runtime v2.39 compared to after this commit:
```
name old time/op new time/op delta
RangeQuery/expr=changes(a_one[1d]),steps=1-16 391µs ± 2% 547µs ± 1% +39.84% (p=0.000 n=9+8)
RangeQuery/expr=changes(a_one[1d]),steps=10-16 452µs ± 2% 616µs ± 2% +36.15% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_one[1d]),steps=100-16 1.12ms ± 1% 1.26ms ± 1% +12.20% (p=0.000 n=8+10)
RangeQuery/expr=changes(a_one[1d]),steps=1000-16 7.83ms ± 1% 7.95ms ± 1% +1.59% (p=0.000 n=10+8)
RangeQuery/expr=changes(a_ten[1d]),steps=1-16 2.98ms ± 0% 3.38ms ± 2% +13.49% (p=0.000 n=9+10)
RangeQuery/expr=changes(a_ten[1d]),steps=10-16 3.66ms ± 1% 4.02ms ± 1% +9.80% (p=0.000 n=10+9)
RangeQuery/expr=changes(a_ten[1d]),steps=100-16 10.5ms ± 0% 10.8ms ± 1% +3.08% (p=0.000 n=8+10)
RangeQuery/expr=changes(a_ten[1d]),steps=1000-16 77.6ms ± 1% 78.1ms ± 1% +0.58% (p=0.035 n=9+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=1-16 30.4ms ± 2% 33.5ms ± 4% +10.18% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=10-16 37.1ms ± 2% 40.0ms ± 1% +7.98% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=100-16 105ms ± 1% 107ms ± 1% +1.92% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=1000-16 783ms ± 3% 775ms ± 1% -1.02% (p=0.019 n=9+9)
```
In summary, the runtime doesn't really improve with this change for
queries with just a few steps. For queries with many steps, this
commit essentially reinstates the old performance. This is good
because the many-step queries are the one that matter most (longest
absolute runtime).
In terms of allocations, though, this commit doesn't make a dent at
all (numbers not shown). The reason is that most of the allocations
happen in the sampleRingIterator (in the storage package), which has
to be addressed in a separate commit.
Signed-off-by: beorn7 <beorn@grafana.com>
2022-10-28 07:58:40 -07:00
|
|
|
Value: v.F,
|
2016-12-24 15:37:46 -08:00
|
|
|
Labels: v.Metric.Map(),
|
2014-06-10 07:30:06 -07:00
|
|
|
}
|
2024-03-26 14:17:19 -07:00
|
|
|
if v.H != nil {
|
|
|
|
s.Value = v.H
|
|
|
|
}
|
2014-06-10 07:30:06 -07:00
|
|
|
result[n] = &s
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2014-05-28 10:44:54 -07:00
|
|
|
|
2021-04-13 15:30:15 -07:00
|
|
|
func convertToFloat(i interface{}) (float64, error) {
|
|
|
|
switch v := i.(type) {
|
|
|
|
case float64:
|
|
|
|
return v, nil
|
|
|
|
case string:
|
|
|
|
return strconv.ParseFloat(v, 64)
|
2021-11-05 10:34:58 -07:00
|
|
|
case int:
|
|
|
|
return float64(v), nil
|
|
|
|
case uint:
|
|
|
|
return float64(v), nil
|
2022-03-29 07:13:54 -07:00
|
|
|
case int64:
|
|
|
|
return float64(v), nil
|
|
|
|
case uint64:
|
|
|
|
return float64(v), nil
|
2021-04-13 15:30:15 -07:00
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("can't convert %T to float", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
// Expander executes templates in text or HTML mode with a common set of Prometheus template functions.
|
|
|
|
type Expander struct {
|
2014-06-10 07:30:06 -07:00
|
|
|
text string
|
|
|
|
name string
|
|
|
|
data interface{}
|
|
|
|
funcMap text_template.FuncMap
|
2021-09-13 04:49:08 -07:00
|
|
|
options []string
|
2014-06-10 07:30:06 -07:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NewTemplateExpander returns a template expander ready to use.
|
2017-11-23 04:04:54 -08:00
|
|
|
func NewTemplateExpander(
|
|
|
|
ctx context.Context,
|
|
|
|
text string,
|
|
|
|
name string,
|
|
|
|
data interface{},
|
|
|
|
timestamp model.Time,
|
|
|
|
queryFunc QueryFunc,
|
|
|
|
externalURL *url.URL,
|
2021-09-13 04:49:08 -07:00
|
|
|
options []string,
|
2017-11-23 04:04:54 -08:00
|
|
|
) *Expander {
|
2021-09-13 04:49:08 -07:00
|
|
|
if options == nil {
|
|
|
|
options = []string{"missingkey=zero"}
|
|
|
|
}
|
2015-08-24 06:07:27 -07:00
|
|
|
return &Expander{
|
2014-06-10 07:30:06 -07:00
|
|
|
text: text,
|
|
|
|
name: name,
|
|
|
|
data: data,
|
|
|
|
funcMap: text_template.FuncMap{
|
|
|
|
"query": func(q string) (queryResult, error) {
|
2017-11-23 04:04:54 -08:00
|
|
|
return query(ctx, q, timestamp.Time(), queryFunc)
|
2014-06-10 07:30:06 -07:00
|
|
|
},
|
|
|
|
"first": func(v queryResult) (*sample, error) {
|
|
|
|
if len(v) > 0 {
|
|
|
|
return v[0], nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("first() called on vector with no elements")
|
|
|
|
},
|
|
|
|
"label": func(label string, s *sample) string {
|
|
|
|
return s.Labels[label]
|
|
|
|
},
|
2024-03-26 14:17:19 -07:00
|
|
|
"value": func(s *sample) interface{} {
|
2014-06-10 07:30:06 -07:00
|
|
|
return s.Value
|
|
|
|
},
|
|
|
|
"strvalue": func(s *sample) string {
|
|
|
|
return s.Labels["__value__"]
|
|
|
|
},
|
|
|
|
"args": func(args ...interface{}) map[string]interface{} {
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
for i, a := range args {
|
|
|
|
result[fmt.Sprintf("arg%d", i)] = a
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
},
|
|
|
|
"reReplaceAll": func(pattern, repl, text string) string {
|
|
|
|
re := regexp.MustCompile(pattern)
|
|
|
|
return re.ReplaceAllString(text, repl)
|
|
|
|
},
|
|
|
|
"safeHtml": func(text string) html_template.HTML {
|
|
|
|
return html_template.HTML(text)
|
|
|
|
},
|
2014-07-25 05:23:47 -07:00
|
|
|
"match": regexp.MatchString,
|
2023-10-31 04:35:13 -07:00
|
|
|
"title": strings.Title, //nolint:staticcheck
|
2016-08-15 04:00:22 -07:00
|
|
|
"toUpper": strings.ToUpper,
|
|
|
|
"toLower": strings.ToLower,
|
2015-05-28 12:33:48 -07:00
|
|
|
"graphLink": strutil.GraphLinkForExpression,
|
|
|
|
"tableLink": strutil.TableLinkForExpression,
|
2014-06-10 07:30:06 -07:00
|
|
|
"sortByLabel": func(label string, v queryResult) queryResult {
|
|
|
|
sorter := queryResultByLabelSorter{v[:], label}
|
|
|
|
sort.Stable(sorter)
|
|
|
|
return v
|
|
|
|
},
|
2021-12-16 01:27:31 -08:00
|
|
|
"stripPort": func(hostPort string) string {
|
|
|
|
host, _, err := net.SplitHostPort(hostPort)
|
|
|
|
if err != nil {
|
|
|
|
return hostPort
|
|
|
|
}
|
|
|
|
return host
|
|
|
|
},
|
2022-04-27 02:30:05 -07:00
|
|
|
"stripDomain": func(hostPort string) string {
|
|
|
|
host, port, err := net.SplitHostPort(hostPort)
|
|
|
|
if err != nil {
|
|
|
|
host = hostPort
|
|
|
|
}
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip != nil {
|
|
|
|
return hostPort
|
|
|
|
}
|
|
|
|
host = strings.Split(host, ".")[0]
|
|
|
|
if port != "" {
|
|
|
|
return net.JoinHostPort(host, port)
|
|
|
|
}
|
|
|
|
return host
|
|
|
|
},
|
2021-04-13 15:30:15 -07:00
|
|
|
"humanize": func(i interface{}) (string, error) {
|
|
|
|
v, err := convertToFloat(i)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-03-28 11:51:41 -07:00
|
|
|
if v == 0 || math.IsNaN(v) || math.IsInf(v, 0) {
|
2021-04-13 15:30:15 -07:00
|
|
|
return fmt.Sprintf("%.4g", v), nil
|
2014-06-10 07:30:06 -07:00
|
|
|
}
|
|
|
|
if math.Abs(v) >= 1 {
|
|
|
|
prefix := ""
|
|
|
|
for _, p := range []string{"k", "M", "G", "T", "P", "E", "Z", "Y"} {
|
|
|
|
if math.Abs(v) < 1000 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
prefix = p
|
|
|
|
v /= 1000
|
2014-06-05 06:07:54 -07:00
|
|
|
}
|
2021-04-13 15:30:15 -07:00
|
|
|
return fmt.Sprintf("%.4g%s", v, prefix), nil
|
2014-12-10 07:16:49 -08:00
|
|
|
}
|
|
|
|
prefix := ""
|
|
|
|
for _, p := range []string{"m", "u", "n", "p", "f", "a", "z", "y"} {
|
|
|
|
if math.Abs(v) >= 1 {
|
|
|
|
break
|
2014-06-10 07:30:06 -07:00
|
|
|
}
|
2014-12-10 07:16:49 -08:00
|
|
|
prefix = p
|
|
|
|
v *= 1000
|
2014-06-10 07:30:06 -07:00
|
|
|
}
|
2021-04-13 15:30:15 -07:00
|
|
|
return fmt.Sprintf("%.4g%s", v, prefix), nil
|
2014-06-10 07:30:06 -07:00
|
|
|
},
|
2021-04-13 15:30:15 -07:00
|
|
|
"humanize1024": func(i interface{}) (string, error) {
|
|
|
|
v, err := convertToFloat(i)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-03-28 11:51:41 -07:00
|
|
|
if math.Abs(v) <= 1 || math.IsNaN(v) || math.IsInf(v, 0) {
|
2021-04-13 15:30:15 -07:00
|
|
|
return fmt.Sprintf("%.4g", v), nil
|
2014-06-05 06:07:54 -07:00
|
|
|
}
|
|
|
|
prefix := ""
|
2014-06-10 07:30:06 -07:00
|
|
|
for _, p := range []string{"ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"} {
|
|
|
|
if math.Abs(v) < 1024 {
|
2014-06-05 06:07:54 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
prefix = p
|
2014-06-10 07:30:06 -07:00
|
|
|
v /= 1024
|
2014-06-05 06:07:54 -07:00
|
|
|
}
|
2021-04-13 15:30:15 -07:00
|
|
|
return fmt.Sprintf("%.4g%s", v, prefix), nil
|
2014-06-11 03:32:19 -07:00
|
|
|
},
|
2024-06-10 11:40:11 -07:00
|
|
|
"humanizeDuration": common_templates.HumanizeDuration,
|
2021-04-13 15:30:15 -07:00
|
|
|
"humanizePercentage": func(i interface{}) (string, error) {
|
|
|
|
v, err := convertToFloat(i)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.4g%%", v*100), nil
|
2019-06-15 00:59:57 -07:00
|
|
|
},
|
2021-04-13 15:30:15 -07:00
|
|
|
"humanizeTimestamp": func(i interface{}) (string, error) {
|
|
|
|
v, err := convertToFloat(i)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-07-14 14:45:32 -07:00
|
|
|
|
|
|
|
tm, err := floatToTime(v)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, errNaNOrInf):
|
2021-04-13 15:30:15 -07:00
|
|
|
return fmt.Sprintf("%.4g", v), nil
|
2022-07-14 14:45:32 -07:00
|
|
|
case err != nil:
|
|
|
|
return "", err
|
2015-06-23 08:46:57 -07:00
|
|
|
}
|
2022-07-14 14:45:32 -07:00
|
|
|
|
|
|
|
return fmt.Sprint(tm), nil
|
|
|
|
},
|
|
|
|
"toTime": func(i interface{}) (*time.Time, error) {
|
|
|
|
v, err := convertToFloat(i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-05 10:34:58 -07:00
|
|
|
}
|
2022-07-14 14:45:32 -07:00
|
|
|
|
|
|
|
return floatToTime(v)
|
2015-06-23 08:46:57 -07:00
|
|
|
},
|
2015-03-24 14:04:38 -07:00
|
|
|
"pathPrefix": func() string {
|
2017-05-13 06:47:04 -07:00
|
|
|
return externalURL.Path
|
|
|
|
},
|
|
|
|
"externalURL": func() string {
|
|
|
|
return externalURL.String()
|
2015-03-24 14:04:38 -07:00
|
|
|
},
|
2021-07-14 21:15:14 -07:00
|
|
|
"parseDuration": func(d string) (float64, error) {
|
2021-05-12 03:06:21 -07:00
|
|
|
v, err := model.ParseDuration(d)
|
|
|
|
if err != nil {
|
2021-07-14 21:15:14 -07:00
|
|
|
return 0, err
|
2021-05-12 03:06:21 -07:00
|
|
|
}
|
2021-07-14 21:15:14 -07:00
|
|
|
return float64(time.Duration(v)) / float64(time.Second), nil
|
2021-05-12 03:06:21 -07:00
|
|
|
},
|
2014-06-05 06:07:54 -07:00
|
|
|
},
|
2021-09-13 04:49:08 -07:00
|
|
|
options: options,
|
2014-05-28 10:44:54 -07:00
|
|
|
}
|
2014-06-10 07:30:06 -07:00
|
|
|
}
|
|
|
|
|
2018-09-13 05:55:58 -07:00
|
|
|
// AlertTemplateData returns the interface to be used in expanding the template.
|
2024-03-12 12:14:31 -07:00
|
|
|
func AlertTemplateData(labels, externalLabels map[string]string, externalURL string, smpl promql.Sample) interface{} {
|
|
|
|
res := struct {
|
2018-12-17 11:16:28 -08:00
|
|
|
Labels map[string]string
|
|
|
|
ExternalLabels map[string]string
|
2021-05-30 20:35:26 -07:00
|
|
|
ExternalURL string
|
2024-03-12 12:14:31 -07:00
|
|
|
Value interface{}
|
2018-09-13 05:55:58 -07:00
|
|
|
}{
|
2018-12-17 11:16:28 -08:00
|
|
|
Labels: labels,
|
|
|
|
ExternalLabels: externalLabels,
|
2021-05-30 20:35:26 -07:00
|
|
|
ExternalURL: externalURL,
|
2024-03-12 12:14:31 -07:00
|
|
|
Value: smpl.F,
|
2018-09-13 05:55:58 -07:00
|
|
|
}
|
2024-03-12 12:14:31 -07:00
|
|
|
|
|
|
|
if smpl.H != nil {
|
|
|
|
res.Value = smpl.H
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
2018-09-13 05:55:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
// Funcs adds the functions in fm to the Expander's function map.
|
2015-06-23 08:46:57 -07:00
|
|
|
// Existing functions will be overwritten in case of conflict.
|
2015-08-24 06:07:27 -07:00
|
|
|
func (te Expander) Funcs(fm text_template.FuncMap) {
|
2015-06-23 08:46:57 -07:00
|
|
|
for k, v := range fm {
|
|
|
|
te.funcMap[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
// Expand expands a template in text (non-HTML) mode.
|
|
|
|
func (te Expander) Expand() (result string, resultErr error) {
|
2014-06-10 07:30:06 -07:00
|
|
|
// It'd better to have no alert description than to kill the whole process
|
|
|
|
// if there's a bug in the template.
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
var ok bool
|
|
|
|
resultErr, ok = r.(error)
|
|
|
|
if !ok {
|
2022-06-08 01:44:08 -07:00
|
|
|
resultErr = fmt.Errorf("panic expanding template %v: %v", te.name, r)
|
2014-06-10 07:30:06 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-06 06:39:06 -08:00
|
|
|
if resultErr != nil {
|
|
|
|
templateTextExpansionFailures.Inc()
|
|
|
|
}
|
2014-06-10 07:30:06 -07:00
|
|
|
}()
|
2014-05-28 10:44:54 -07:00
|
|
|
|
2018-11-06 06:39:06 -08:00
|
|
|
templateTextExpansionTotal.Inc()
|
|
|
|
|
2021-09-13 04:49:08 -07:00
|
|
|
tmpl := text_template.New(te.name).Funcs(te.funcMap)
|
|
|
|
tmpl.Option(te.options...)
|
|
|
|
tmpl, err := tmpl.Parse(te.text)
|
2014-05-28 10:44:54 -07:00
|
|
|
if err != nil {
|
2022-06-08 01:44:08 -07:00
|
|
|
return "", fmt.Errorf("error parsing template %v: %w", te.name, err)
|
2014-05-28 10:44:54 -07:00
|
|
|
}
|
2015-09-12 16:06:40 -07:00
|
|
|
var buffer bytes.Buffer
|
2014-06-10 07:30:06 -07:00
|
|
|
err = tmpl.Execute(&buffer, te.data)
|
2014-05-28 10:44:54 -07:00
|
|
|
if err != nil {
|
2022-06-08 01:44:08 -07:00
|
|
|
return "", fmt.Errorf("error executing template %v: %w", te.name, err)
|
2014-05-28 10:44:54 -07:00
|
|
|
}
|
|
|
|
return buffer.String(), nil
|
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
// ExpandHTML expands a template with HTML escaping, with templates read from the given files.
|
|
|
|
func (te Expander) ExpandHTML(templateFiles []string) (result string, resultErr error) {
|
2014-06-10 07:30:06 -07:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
var ok bool
|
|
|
|
resultErr, ok = r.(error)
|
|
|
|
if !ok {
|
2022-06-08 01:44:08 -07:00
|
|
|
resultErr = fmt.Errorf("panic expanding template %s: %v", te.name, r)
|
2014-06-10 07:30:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2023-04-09 00:08:40 -07:00
|
|
|
//nolint:unconvert // Before Go 1.19 conversion from text_template to html_template is mandatory
|
2014-07-25 09:32:17 -07:00
|
|
|
tmpl := html_template.New(te.name).Funcs(html_template.FuncMap(te.funcMap))
|
2021-09-13 04:49:08 -07:00
|
|
|
tmpl.Option(te.options...)
|
2014-07-29 09:28:48 -07:00
|
|
|
tmpl.Funcs(html_template.FuncMap{
|
|
|
|
"tmpl": func(name string, data interface{}) (html_template.HTML, error) {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
err := tmpl.ExecuteTemplate(&buffer, name, data)
|
|
|
|
return html_template.HTML(buffer.String()), err
|
|
|
|
},
|
|
|
|
})
|
|
|
|
tmpl, err := tmpl.Parse(te.text)
|
2014-05-28 10:44:54 -07:00
|
|
|
if err != nil {
|
2022-06-08 01:44:08 -07:00
|
|
|
return "", fmt.Errorf("error parsing template %v: %w", te.name, err)
|
2014-05-28 10:44:54 -07:00
|
|
|
}
|
2014-06-10 07:30:06 -07:00
|
|
|
if len(templateFiles) > 0 {
|
|
|
|
_, err = tmpl.ParseFiles(templateFiles...)
|
|
|
|
if err != nil {
|
2022-06-08 01:44:08 -07:00
|
|
|
return "", fmt.Errorf("error parsing template files for %v: %w", te.name, err)
|
2014-05-28 10:44:54 -07:00
|
|
|
}
|
|
|
|
}
|
2015-09-12 16:06:40 -07:00
|
|
|
var buffer bytes.Buffer
|
2014-06-10 07:30:06 -07:00
|
|
|
err = tmpl.Execute(&buffer, te.data)
|
|
|
|
if err != nil {
|
2022-06-08 01:44:08 -07:00
|
|
|
return "", fmt.Errorf("error executing template %v: %w", te.name, err)
|
2014-06-10 07:30:06 -07:00
|
|
|
}
|
|
|
|
return buffer.String(), nil
|
2014-05-28 10:44:54 -07:00
|
|
|
}
|
2018-09-13 05:55:58 -07:00
|
|
|
|
|
|
|
// ParseTest parses the templates and returns the error if any.
|
|
|
|
func (te Expander) ParseTest() error {
|
|
|
|
_, err := text_template.New(te.name).Funcs(te.funcMap).Option("missingkey=zero").Parse(te.text)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-14 14:45:32 -07:00
|
|
|
|
|
|
|
func floatToTime(v float64) (*time.Time, error) {
|
|
|
|
if math.IsNaN(v) || math.IsInf(v, 0) {
|
|
|
|
return nil, errNaNOrInf
|
|
|
|
}
|
|
|
|
timestamp := v * 1e9
|
|
|
|
if timestamp > math.MaxInt64 || timestamp < math.MinInt64 {
|
|
|
|
return nil, fmt.Errorf("%v cannot be represented as a nanoseconds timestamp since it overflows int64", v)
|
|
|
|
}
|
|
|
|
t := model.TimeFromUnixNano(int64(timestamp)).Time().UTC()
|
|
|
|
return &t, nil
|
|
|
|
}
|