fix(backend): fix history rewrite rule handling

This commit is contained in:
Łukasz Mierzwa
2021-08-18 13:51:34 +01:00
committed by Łukasz Mierzwa
parent d936385de4
commit 973ef191ba
3 changed files with 68 additions and 0 deletions

View File

@@ -1,5 +1,12 @@
# Changelog
## [unreleased]
### Fixed
- Alert history rewrite rule wouldn't work unless they had a `/` suffix in
`source` field, this is now fixed and rewrite rules works as documented.
## v0.90
### Fixed

View File

@@ -235,6 +235,8 @@ func hashQuery(uri string, labels map[string]string) string {
}
func rewriteSource(rules []config.HistoryRewrite, uri string) string {
// trim trailing / to ensure all URIs are without a /
uri = strings.TrimSuffix(uri, "/")
for _, rule := range rules {
if !rule.SourceRegex.MatchString(uri) {
continue

View File

@@ -405,6 +405,65 @@ func TestAlertHistory(t *testing.T) {
},
},
},
{
mocks: []mock{
{
method: "GET",
uri: regexp.MustCompile("^http://localhost:9100/api/v1/labels"),
responder: httpmock.NewJsonResponderOrPanic(200, prometheusAPIV1Labels{
Status: "success",
Data: []string{"alertname", "instance", "job"},
}),
},
{
method: "POST",
uri: regexp.MustCompile("^http://localhost:9100/api/v1/query_range"),
responder: httpmock.NewJsonResponderOrPanic(200, prometheusAPIV1QueryRange{
Status: "success",
Data: generateV1Matrix(
[]seriesValues{
{
metric: model.Metric{
"alertname": "Fake Alert",
},
values: generateIntSlice(0, 1, 24),
},
}, time.Hour),
}),
},
},
config: cfg{
enabled: true,
timeout: time.Second * 5,
workers: 5,
rewrite: []config.HistoryRewrite{
{
SourceRegex: regex.MustCompileAnchored("http://(.+):1111"),
URI: "http://$1:9100",
},
{
SourceRegex: regex.MustCompileAnchored("foo"),
URI: "",
},
{
SourceRegex: regex.MustCompileAnchored("http://(.+):909[0-9]"),
URI: "http://$1:9100",
},
},
},
queries: []historyQuery{
{
payload: generateHistoryPayload(AlertHistoryPayload{
Sources: []string{"http://localhost:9090/", "http://localhost:9091/", "http://localhost:1111/"},
Labels: map[string]string{"alertname": "Fake Alert", "cluster": "prod"},
}),
code: 200,
response: AlertHistoryResponse{
Samples: generateHistorySamples(generateIntSlice(0, 3, 24), time.Hour),
},
},
},
},
}
defer func() {