graphLink and tableLink template functions (#41369)
* graphLink and tableLink functions, docs updated * Code review changes * extract query struct outside of graphLink and tableLink functions * Fix docs * Update docs/sources/alerting/unified-alerting/alerting-rules/alert-annotation-label.md Co-authored-by: Jean-Philippe Quéméner <JohnnyQQQQ@users.noreply.github.com> * Update docs/sources/alerting/unified-alerting/alerting-rules/alert-annotation-label.md Co-authored-by: Jean-Philippe Quéméner <JohnnyQQQQ@users.noreply.github.com> * Update docs/sources/alerting/unified-alerting/alerting-rules/alert-annotation-label.md Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Update docs/sources/alerting/unified-alerting/alerting-rules/alert-annotation-label.md Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Fix linting errors Co-authored-by: Jean-Philippe Quéméner <JohnnyQQQQ@users.noreply.github.com> Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
This commit is contained in:
co-authored by
Jean-Philippe Quéméner
achatterjee-grafana
parent
0d4533ae74
commit
a45e4ff73f
@@ -2,6 +2,8 @@ package state
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -56,16 +58,13 @@ func expandTemplate(name, text string, labels map[string]string, alertInstance e
|
||||
)
|
||||
|
||||
expander.Funcs(text_template.FuncMap{
|
||||
// These three functions are no-ops for now.
|
||||
"graphLink": graphLink,
|
||||
"tableLink": tableLink,
|
||||
|
||||
// This function is a no-op for now.
|
||||
"strvalue": func(value templateCaptureValue) string {
|
||||
return ""
|
||||
},
|
||||
"graphLink": func() string {
|
||||
return ""
|
||||
},
|
||||
"tableLink": func() string {
|
||||
return ""
|
||||
},
|
||||
})
|
||||
|
||||
return expander.Expand()
|
||||
@@ -87,3 +86,34 @@ func newTemplateCaptureValues(values map[string]eval.NumberValueCapture) map[str
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
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=["now-1h","now",%[1]q,{"datasource":%[1]q,"expr":%q,"instant":false,"range":true}]`, 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=["now-1h","now",%[1]q,{"datasource":%[1]q,"expr":%q,"instant":true,"range":false}]`, escapedDatasource, escapedExpression)
|
||||
}
|
||||
|
||||
@@ -355,11 +355,27 @@ func TestExpandTemplate(t *testing.T) {
|
||||
text: "{{ query \"metric{instance='a'}\" | first | label \"instance\" }}",
|
||||
expected: "",
|
||||
}, {
|
||||
name: "check that graphLink returns an empty string",
|
||||
name: "graphLink",
|
||||
text: `{{ graphLink "{\"expr\": \"up\", \"datasource\": \"gdev-prometheus\"}" }}`,
|
||||
expected: `/explore?left=["now-1h","now","gdev-prometheus",{"datasource":"gdev-prometheus","expr":"up","instant":false,"range":true}]`,
|
||||
}, {
|
||||
name: "graphLink should escape both the expression and the datasource",
|
||||
text: `{{ graphLink "{\"expr\": \"process_open_fds > 0\", \"datasource\": \"gdev prometheus\"}" }}`,
|
||||
expected: `/explore?left=["now-1h","now","gdev+prometheus",{"datasource":"gdev+prometheus","expr":"process_open_fds+%3E+0","instant":false,"range":true}]`,
|
||||
}, {
|
||||
name: "check that graphLink returns an empty string when the query is not formatted correctly",
|
||||
text: "{{ graphLink \"up\" }}",
|
||||
expected: "",
|
||||
}, {
|
||||
name: "check that tableLink returns an empty string",
|
||||
name: "tableLink",
|
||||
text: `{{ tableLink "{\"expr\": \"up\", \"datasource\": \"gdev-prometheus\"}" }}`,
|
||||
expected: `/explore?left=["now-1h","now","gdev-prometheus",{"datasource":"gdev-prometheus","expr":"up","instant":true,"range":false}]`,
|
||||
}, {
|
||||
name: "tableLink should escape both the expression and the datasource",
|
||||
text: `{{ tableLink "{\"expr\": \"process_open_fds > 0\", \"datasource\": \"gdev prometheus\"}" }}`,
|
||||
expected: `/explore?left=["now-1h","now","gdev+prometheus",{"datasource":"gdev+prometheus","expr":"process_open_fds+%3E+0","instant":true,"range":false}]`,
|
||||
}, {
|
||||
name: "check that tableLink returns an empty string when the query is not formatted correctly",
|
||||
text: "{{ tableLink \"up\" }}",
|
||||
expected: "",
|
||||
}, {
|
||||
|
||||
Reference in New Issue
Block a user