Extract custom template functions (#60695)

extract custom template functions and export the FuncMap
This commit is contained in:
Santiago 2022-12-22 17:31:40 -03:00 committed by GitHub
parent 4e2bd63572
commit 05c9af5110
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 39 deletions

View File

@ -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)
}

View File

@ -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 ""
}