From 05c9af511057b221e50454033ee798df13fbc987 Mon Sep 17 00:00:00 2001 From: Santiago Date: Thu, 22 Dec 2022 17:31:40 -0300 Subject: [PATCH] Extract custom template functions (#60695) extract custom template functions and export the FuncMap --- pkg/services/ngalert/state/template.go | 40 +--------------- .../ngalert/state/template_functions.go | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 pkg/services/ngalert/state/template_functions.go diff --git a/pkg/services/ngalert/state/template.go b/pkg/services/ngalert/state/template.go index b5f8458711e..70474b86db8 100644 --- a/pkg/services/ngalert/state/template.go +++ b/pkg/services/ngalert/state/template.go @@ -2,16 +2,12 @@ package state import ( "context" - "encoding/json" - "fmt" "math" "net/url" "strconv" "strings" "time" - text_template "text/template" - "github.com/prometheus/common/model" "github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/promql" @@ -59,15 +55,7 @@ func expandTemplate(ctx context.Context, name, text string, labels map[string]st []string{"missingkey=invalid"}, ) - expander.Funcs(text_template.FuncMap{ - "graphLink": graphLink, - "tableLink": tableLink, - - // This function is a no-op for now. - "strvalue": func(value templateCaptureValue) string { - return "" - }, - }) + expander.Funcs(FuncMap) result, resultErr = expander.Expand() // Replace missing key value to one that does not look like an HTML tag. This can cause problems downstream in some notifiers. // For example, Telegram in HTML mode rejects requests with unsupported tags. @@ -96,29 +84,3 @@ type query struct { Datasource string `json:"datasource"` Expr string `json:"expr"` } - -func graphLink(rawQuery string) string { - var q query - if err := json.Unmarshal([]byte(rawQuery), &q); err != nil { - return "" - } - - escapedExpression := url.QueryEscape(q.Expr) - escapedDatasource := url.QueryEscape(q.Datasource) - - return fmt.Sprintf( - `/explore?left={"datasource":%[1]q,"queries":[{"datasource":%[1]q,"expr":%q,"instant":false,"range":true,"refId":"A"}],"range":{"from":"now-1h","to":"now"}}`, escapedDatasource, escapedExpression) -} - -func tableLink(rawQuery string) string { - var q query - if err := json.Unmarshal([]byte(rawQuery), &q); err != nil { - return "" - } - - escapedExpression := url.QueryEscape(q.Expr) - escapedDatasource := url.QueryEscape(q.Datasource) - - return fmt.Sprintf( - `/explore?left={"datasource":%[1]q,"queries":[{"datasource":%[1]q,"expr":%q,"instant":true,"range":false,"refId":"A"}],"range":{"from":"now-1h","to":"now"}}`, escapedDatasource, escapedExpression) -} diff --git a/pkg/services/ngalert/state/template_functions.go b/pkg/services/ngalert/state/template_functions.go new file mode 100644 index 00000000000..3f7c14b5e3c --- /dev/null +++ b/pkg/services/ngalert/state/template_functions.go @@ -0,0 +1,46 @@ +package state + +import ( + "encoding/json" + "fmt" + "net/url" + text_template "text/template" +) + +// FuncMap is a map of custom functions we use for templates. +var FuncMap = text_template.FuncMap{ + "graphLink": graphLink, + "tableLink": tableLink, + "strvalue": strValue, +} + +func graphLink(rawQuery string) string { + var q query + if err := json.Unmarshal([]byte(rawQuery), &q); err != nil { + return "" + } + + escapedExpression := url.QueryEscape(q.Expr) + escapedDatasource := url.QueryEscape(q.Datasource) + + return fmt.Sprintf( + `/explore?left={"datasource":%[1]q,"queries":[{"datasource":%[1]q,"expr":%q,"instant":false,"range":true,"refId":"A"}],"range":{"from":"now-1h","to":"now"}}`, escapedDatasource, escapedExpression) +} + +func tableLink(rawQuery string) string { + var q query + if err := json.Unmarshal([]byte(rawQuery), &q); err != nil { + return "" + } + + escapedExpression := url.QueryEscape(q.Expr) + escapedDatasource := url.QueryEscape(q.Datasource) + + return fmt.Sprintf( + `/explore?left={"datasource":%[1]q,"queries":[{"datasource":%[1]q,"expr":%q,"instant":true,"range":false,"refId":"A"}],"range":{"from":"now-1h","to":"now"}}`, escapedDatasource, escapedExpression) +} + +// This function is a no-op for now. +func strValue(value templateCaptureValue) string { + return "" +}