From 80f966b80c1c4a0e385a46bbfef56454fbf9af32 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 13 Jan 2022 22:53:40 +0200 Subject: [PATCH] app/vmalert: add `stripPort` template function in the same way as Prometheus does See https://github.com/prometheus/prometheus/pull/10002 --- app/vmalert/notifier/template_func.go | 11 +++++++++++ docs/CHANGELOG.md | 1 + 2 files changed, 12 insertions(+) diff --git a/app/vmalert/notifier/template_func.go b/app/vmalert/notifier/template_func.go index 9704f17c2..2732895b7 100644 --- a/app/vmalert/notifier/template_func.go +++ b/app/vmalert/notifier/template_func.go @@ -17,6 +17,7 @@ import ( "errors" "fmt" "math" + "net" "net/url" "regexp" "strings" @@ -61,6 +62,7 @@ var tmplFunc textTpl.FuncMap // InitTemplateFunc initiates template helper functions func InitTemplateFunc(externalURL *url.URL) { + // See https://prometheus.io/docs/prometheus/latest/configuration/template_reference/ tmplFunc = textTpl.FuncMap{ /* Strings */ @@ -91,6 +93,15 @@ func InitTemplateFunc(externalURL *url.URL) { // alias for https://golang.org/pkg/strings/#ToLower "toLower": strings.ToLower, + // stripPort splits string into host and port, then returns only host. + "stripPort": func(hostPort string) string { + host, _, err := net.SplitHostPort(hostPort) + if err != nil { + return hostPort + } + return host + }, + /* Numbers */ // humanize converts given number to a human readable format diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 605b2b4b8..4010882a1 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -22,6 +22,7 @@ scrape_configs: * FEATURE: [vmrestore](https://docs.victoriametrics.com/vmrestore.html): store `restore-in-progress` file in `-dst` directory while `vmrestore` is running. This file is automatically deleted when `vmrestore` is successfully finished. This helps detecting incompletely restored data on VictoriaMetrics start. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1958). * FEATURE: [vmctl](https://docs.victoriametrics.com/vmctl.html): print the last sample timestamp when the data migration is interrupted either by user or by error. This helps continuing the data migration from the interruption moment. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1236). * FEATURE: [vmalert](https://docs.victoriametrics.com/vmalert.html): expose `vmalert_remotewrite_total` metric at `/metrics` page. This makes possible calculating SLOs for error rate during writing recording rules and alert state to `-remoteWrite.url` with the query `vmalert_remotewrite_errors_total / vmalert_remotewrite_total`. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/2040). Thanks to @afoninsky . +* FEATURE: [vmalert](https://docs.victoriametrics.com/vmalert.html): add `stripPort` template function in the same way as [Prometheus does](https://github.com/prometheus/prometheus/pull/10002). * BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): make sure that `vmagent` replicas scrape the same targets at different time offsets when [replication is enabled in vmagent clustering mode](https://docs.victoriametrics.com/vmagent.html#scraping-big-number-of-targets). This guarantees that the [deduplication](https://docs.victoriametrics.com/#deduplication) consistently leaves samples from the same `vmagent` replica. * BUGFIX: return the proper response stub from `/api/v1/query_exemplars` handler, which is needed for Grafana v8+. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1999).