From e9b977859b0355557711a0776291ec90899e9e24 Mon Sep 17 00:00:00 2001 From: Roman Khavronenko Date: Fri, 8 Jul 2022 10:26:13 +0200 Subject: [PATCH] vmalert: deprecate alert's status link (#2840) * vmalert: deprecate alert's status link Deprecate alert's status link `/api/v1///status` in favour of `api/v1/alerts?group_id=&alert_id="`. The change was needed for simplifying logic in vmselect for proxying vmalert's requests. The old alert's status link will be still supported for a few versions but will be removed in the future. https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2825 Signed-off-by: hagen1778 * vmalert: fix review comments Signed-off-by: hagen1778 --- app/vmalert/README.md | 4 +- app/vmalert/main.go | 7 +- app/vmalert/main_test.go | 3 +- app/vmalert/tpl/footer.qtpl | 10 +- app/vmalert/tpl/footer.qtpl.go | 54 ++-- app/vmalert/tpl/header.qtpl | 9 +- app/vmalert/tpl/header.qtpl.go | 127 +++++---- app/vmalert/utils/links.go | 12 + app/vmalert/web.go | 78 ++++-- app/vmalert/web.qtpl | 8 +- app/vmalert/web.qtpl.go | 498 +++++++++++++++++---------------- app/vmalert/web_test.go | 59 +++- app/vmalert/web_types.go | 13 + docs/CHANGELOG.md | 6 + docs/vmalert.md | 4 +- 15 files changed, 511 insertions(+), 381 deletions(-) create mode 100644 app/vmalert/utils/links.go diff --git a/app/vmalert/README.md b/app/vmalert/README.md index 3b65798e7..56722c72d 100644 --- a/app/vmalert/README.md +++ b/app/vmalert/README.md @@ -481,7 +481,7 @@ or time series modification via [relabeling](https://docs.victoriametrics.com/vm * `http://` - UI; * `http:///api/v1/rules` - list of all loaded groups and rules; * `http:///api/v1/alerts` - list of all active alerts; -* `http:///api/v1///status"` - get alert status by ID. +* `http:///vmalert/api/v1/alert?group_id=&alert_id="` - get alert status by ID. Used as alert source in AlertManager. * `http:///metrics` - application metrics. * `http:///-/reload` - hot configuration reload. @@ -681,7 +681,7 @@ The shortlist of configuration flags is the following: How often to evaluate the rules (default 1m0s) -external.alert.source string External Alert Source allows to override the Source link for alerts sent to AlertManager for cases where you want to build a custom link to Grafana, Prometheus or any other service. - eg. 'explore?orgId=1&left=[\"now-1h\",\"now\",\"VictoriaMetrics\",{\"expr\": \"{{$expr|quotesEscape|crlfEscape|queryEscape}}\"},{\"mode\":\"Metrics\"},{\"ui\":[true,true,true,\"none\"]}]'.If empty '/api/v1/:groupID/alertID/status' is used + eg. 'explore?orgId=1&left=[\"now-1h\",\"now\",\"VictoriaMetrics\",{\"expr\": \"{{$expr|quotesEscape|crlfEscape|queryEscape}}\"},{\"mode\":\"Metrics\"},{\"ui\":[true,true,true,\"none\"]}]'.If empty '/vmalert/api/v1/alert?group_id=&alert_id=' is used -external.label array Optional label in the form 'Name=value' to add to all generated recording rules and alerts. Pass multiple -label flags in order to add multiple label sets. Supports an array of values separated by comma or specified via multiple flags. diff --git a/app/vmalert/main.go b/app/vmalert/main.go index eccf4ef3e..feef1691c 100644 --- a/app/vmalert/main.go +++ b/app/vmalert/main.go @@ -59,7 +59,7 @@ absolute path to all .tpl files in root.`) externalURL = flag.String("external.url", "", "External URL is used as alert's source for sent alerts to the notifier") externalAlertSource = flag.String("external.alert.source", "", `External Alert Source allows to override the Source link for alerts sent to AlertManager for cases where you want to build a custom link to Grafana, Prometheus or any other service. -eg. 'explore?orgId=1&left=[\"now-1h\",\"now\",\"VictoriaMetrics\",{\"expr\": \"{{$expr|quotesEscape|crlfEscape|queryEscape}}\"},{\"mode\":\"Metrics\"},{\"ui\":[true,true,true,\"none\"]}]'.If empty '/api/v1/:groupID/alertID/status' is used`) +eg. 'explore?orgId=1&left=[\"now-1h\",\"now\",\"VictoriaMetrics\",{\"expr\": \"{{$expr|quotesEscape|crlfEscape|queryEscape}}\"},{\"mode\":\"Metrics\"},{\"ui\":[true,true,true,\"none\"]}]'.If empty '/vmalert/api/v1/alert?group_id=&alert_id=' is used`) externalLabels = flagutil.NewArray("external.label", "Optional label in the form 'Name=value' to add to all generated recording rules and alerts. "+ "Pass multiple -label flags in order to add multiple label sets.") @@ -236,8 +236,9 @@ func getExternalURL(externalURL, httpListenAddr string, isSecure bool) (*url.URL func getAlertURLGenerator(externalURL *url.URL, externalAlertSource string, validateTemplate bool) (notifier.AlertURLGenerator, error) { if externalAlertSource == "" { - return func(alert notifier.Alert) string { - return fmt.Sprintf("%s/api/v1/%s/%s/status", externalURL, strconv.FormatUint(alert.GroupID, 10), strconv.FormatUint(alert.ID, 10)) + return func(a notifier.Alert) string { + gID, aID := strconv.FormatUint(a.GroupID, 10), strconv.FormatUint(a.ID, 10) + return fmt.Sprintf("%s/vmalert/api/v1/alert?%s=%s&%s=%s", externalURL, paramGroupID, gID, paramAlertID, aID) }, nil } if validateTemplate { diff --git a/app/vmalert/main_test.go b/app/vmalert/main_test.go index 4999723ea..30595d2f0 100644 --- a/app/vmalert/main_test.go +++ b/app/vmalert/main_test.go @@ -41,7 +41,8 @@ func TestGetAlertURLGenerator(t *testing.T) { if err != nil { t.Errorf("unexpected error %s", err) } - if exp := "https://victoriametrics.com/path/api/v1/42/2/status"; exp != fn(testAlert) { + exp := fmt.Sprintf("https://victoriametrics.com/path/vmalert/api/v1/alert?%s=42&%s=2", paramGroupID, paramAlertID) + if exp != fn(testAlert) { t.Errorf("unexpected url want %s, got %s", exp, fn(testAlert)) } _, err = getAlertURLGenerator(nil, "foo?{{invalid}}", true) diff --git a/app/vmalert/tpl/footer.qtpl b/app/vmalert/tpl/footer.qtpl index eef27785d..df1e3a4ba 100644 --- a/app/vmalert/tpl/footer.qtpl +++ b/app/vmalert/tpl/footer.qtpl @@ -1,16 +1,12 @@ {% import ( "net/http" - "strings" + + "github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/utils" ) %} {% func Footer(r *http.Request) %} -{%code - prefix := "/vmalert/" - if strings.HasPrefix(r.URL.Path, prefix) { - prefix = "" - } -%} + {%code prefix := utils.Prefix(r.URL.Path) %} diff --git a/app/vmalert/tpl/footer.qtpl.go b/app/vmalert/tpl/footer.qtpl.go index 65eea9fa4..3f8b09801 100644 --- a/app/vmalert/tpl/footer.qtpl.go +++ b/app/vmalert/tpl/footer.qtpl.go @@ -7,45 +7,43 @@ package tpl //line app/vmalert/tpl/footer.qtpl:1 import ( "net/http" - "strings" + + "github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/utils" ) -//line app/vmalert/tpl/footer.qtpl:7 +//line app/vmalert/tpl/footer.qtpl:8 import ( qtio422016 "io" qt422016 "github.com/valyala/quicktemplate" ) -//line app/vmalert/tpl/footer.qtpl:7 +//line app/vmalert/tpl/footer.qtpl:8 var ( _ = qtio422016.Copy _ = qt422016.AcquireByteBuffer ) -//line app/vmalert/tpl/footer.qtpl:7 +//line app/vmalert/tpl/footer.qtpl:8 func StreamFooter(qw422016 *qt422016.Writer, r *http.Request) { -//line app/vmalert/tpl/footer.qtpl:7 +//line app/vmalert/tpl/footer.qtpl:8 qw422016.N().S(` -`) + `) //line app/vmalert/tpl/footer.qtpl:9 - prefix := "/vmalert/" - if strings.HasPrefix(r.URL.Path, prefix) { - prefix = "" - } + prefix := utils.Prefix(r.URL.Path) -//line app/vmalert/tpl/footer.qtpl:13 +//line app/vmalert/tpl/footer.qtpl:9 qw422016.N().S(`