Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(alertmanager): support loki alerts GeneratorURL in template functions #6256

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: support loki alerts GeneratorURL
  • Loading branch information
fgouteroux committed Oct 9, 2023
commit dc980789f98890b93ed4f6ef0d3f6242b8a756c7
57 changes: 40 additions & 17 deletions pkg/alertmanager/alertmanager_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
tmplhtml "html/template"
"net/url"
"strings"
tmpltext "text/template"

"github.com/prometheus/alertmanager/template"
Expand Down Expand Up @@ -37,29 +38,51 @@ type grafanaExploreParams struct {

// grafanaExploreURL is a template helper function to generate Grafana range query explore URL in the alert template.
func grafanaExploreURL(grafanaURL, datasource, from, to, expr string) (string, error) {
res, err := json.Marshal(&grafanaExploreParams{
Range: grafanaExploreRange{
From: from,
To: to,
},
Queries: []grafanaExploreQuery{
{
Datasource: grafanaDatasource{
Type: "prometheus",
UID: datasource,
grafanaExploreQueryPrefix := "/explore?left="
var res []byte
var err error
var grafanaExplore grafanaExploreParams

if strings.HasPrefix(expr, grafanaExploreQueryPrefix) {
// remove begging to get queries params
expr = strings.ReplaceAll(expr, grafanaExploreQueryPrefix, "")
if err = json.Unmarshal([]byte(expr), &grafanaExplore); err != nil {
return "", err
}
grafanaExplore.Queries[0].Range = true
grafanaExplore.Queries[0].RefID = "A"
grafanaExplore.Range.From = from
grafanaExplore.Range.To = to
} else {
grafanaExplore = grafanaExploreParams{
Range: grafanaExploreRange{
From: from,
To: to,
},
Queries: []grafanaExploreQuery{
{
Datasource: grafanaDatasource{
Type: "prometheus",
UID: datasource,
},
Expr: expr,
Instant: false,
Range: true,
RefID: "A",
},
Expr: expr,
Instant: false,
Range: true,
RefID: "A",
},
},
})
return grafanaURL + "/explore?left=" + url.QueryEscape(string(res)), err
}
}
res, err = json.Marshal(grafanaExplore)
return grafanaURL + grafanaExploreQueryPrefix + url.QueryEscape(string(res)), err
}

// queryFromGeneratorURL returns a PromQL expression parsed out from a GeneratorURL in Prometheus alert
func queryFromGeneratorURL(generatorURL string) (string, error) {
// if generator url source is a grafana product
if strings.HasPrefix(generatorURL, "/explore?left=") {
return generatorURL, nil
}
u, err := url.Parse(generatorURL)
if err != nil {
return "", fmt.Errorf("failed to parse generator URL: %w", err)
Expand Down
16 changes: 13 additions & 3 deletions pkg/alertmanager/alertmanager_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ func Test_withCustomFunctions(t *testing.T) {
GeneratorURL: "http://localhost:9090?foo=bar",
},
},
template: `{{ queryFormGeneratorURL (index .Alerts 0).GeneratorURL }}`,
template: `{{ queryFromGeneratorURL (index .Alerts 0).GeneratorURL }}`,
expectError: true,
},
{
name: "error on URL decoding query in GeneratorURL",
alerts: template.Alerts{
template.Alert{
GeneratorURL: "http://localhost:9090?g0.expr=up{foo=bar}",
GeneratorURL: "http://localhost:9090?expr=up{foo=bar}",
},
},
template: `{{ queryFormGeneratorURL (index .Alerts 0).GeneratorURL }}`,
template: `{{ queryFromGeneratorURL (index .Alerts 0).GeneratorURL }}`,
expectError: true,
},
{
Expand All @@ -77,6 +77,16 @@ func Test_withCustomFunctions(t *testing.T) {
template: `{{ grafanaExploreURL "https://foo.bar" "test_datasoruce" "now-12h" "now" (queryFromGeneratorURL (index .Alerts 0).GeneratorURL) }}`,
result: `https://foo.bar/explore?left=` + url.QueryEscape(`{"range":{"from":"now-12h","to":"now"},"queries":[{"datasource":{"type":"prometheus","uid":"test_datasoruce"},"expr":"up{foo!=\"bar\"}","instant":false,"range":true,"refId":"A"}]}`),
},
{
name: "Generate Grafana Explore URL from GeneratorURL query from loki",
alerts: template.Alerts{
template.Alert{
GeneratorURL: `/explore?left={"queries":[{"datasource":{"type":"loki","uid":"loki"},"expr":"up{foo!=\"bar\"}","queryType":"range"}]}`,
},
},
template: `{{ grafanaExploreURL "http://localhost:9090" "test_datasource" "now-12h" "now" (queryFromGeneratorURL (index .Alerts 0).GeneratorURL) }}`,
result: `http://localhost:9090/explore?left=%7B%22range%22%3A%7B%22from%22%3A%22now-12h%22%2C%22to%22%3A%22now%22%7D%2C%22queries%22%3A%5B%7B%22datasource%22%3A%7B%22type%22%3A%22loki%22%2C%22uid%22%3A%22loki%22%7D%2C%22expr%22%3A%22up%7Bfoo%21%3D%5C%22bar%5C%22%7D%22%2C%22instant%22%3Afalse%2C%22range%22%3Atrue%2C%22refId%22%3A%22A%22%7D%5D%7D`,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
Expand Down