diff --git a/CHANGELOG.md b/CHANGELOG.md index 32a2de68d..dcfcadf73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cmd/karma/alert_history.go b/cmd/karma/alert_history.go index 8bf12904e..6b45d16a4 100644 --- a/cmd/karma/alert_history.go +++ b/cmd/karma/alert_history.go @@ -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 diff --git a/cmd/karma/alert_history_test.go b/cmd/karma/alert_history_test.go index 842d4bb80..41685ca0c 100644 --- a/cmd/karma/alert_history_test.go +++ b/cmd/karma/alert_history_test.go @@ -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() {