From 755779adb77ecd5650b3d7813cde97e824ad8c0a Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 13 Jan 2022 23:30:38 +0200 Subject: [PATCH] app/vmalert: add `parseDuration` function in the same way as Prometheus does See https://github.com/prometheus/prometheus/pull/8817 --- app/vmalert/notifier/template_func.go | 10 ++++++++++ docs/CHANGELOG.md | 1 + 2 files changed, 11 insertions(+) diff --git a/app/vmalert/notifier/template_func.go b/app/vmalert/notifier/template_func.go index 2732895b7..4a42c07e7 100644 --- a/app/vmalert/notifier/template_func.go +++ b/app/vmalert/notifier/template_func.go @@ -27,6 +27,7 @@ import ( textTpl "text/template" "github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource" + "github.com/VictoriaMetrics/metricsql" ) // metric is private copy of datasource.Metric, @@ -102,6 +103,15 @@ func InitTemplateFunc(externalURL *url.URL) { return host }, + // parseDuration parses a duration string such as "1h" into the number of seconds it represents + "parseDuration": func(d string) (float64, error) { + ms, err := metricsql.DurationValue(d, 0) + if err != nil { + return 0, err + } + return float64(ms) / 1000, nil + }, + /* Numbers */ // humanize converts given number to a human readable format diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index da6ab4837..e1269af46 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -24,6 +24,7 @@ scrape_configs: * 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). +* FEATURE: [vmalert](https://docs.victoriametrics.com/vmalert.html): add `parseDuration` template function in the same way as [Prometheus does](https://github.com/prometheus/prometheus/pull/8817). * 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).