diff --git a/app/vmalert/templates/template.go b/app/vmalert/templates/template.go index 3907ba97e2..6b81f0dc56 100644 --- a/app/vmalert/templates/template.go +++ b/app/vmalert/templates/template.go @@ -373,10 +373,23 @@ func templateFuncs() textTpl.FuncMap { if math.IsNaN(v) || math.IsInf(v, 0) { return fmt.Sprintf("%.4g", v), nil } - t := TimeFromUnixNano(int64(v * 1e9)).Time().UTC() + t := timeFromUnixTimestamp(v).Time().UTC() return fmt.Sprint(t), nil }, + // toTime converts given timestamp to a time.Time. + "toTime": func(i interface{}) (time.Time, error) { + v, err := toFloat64(i) + if err != nil { + return time.Time{}, err + } + if math.IsNaN(v) || math.IsInf(v, 0) { + return time.Time{}, fmt.Errorf("cannot convert %v to time.Time", v) + } + t := timeFromUnixTimestamp(v).Time().UTC() + return t, nil + }, + /* URLs */ // externalURL returns value of `external.url` flag @@ -492,10 +505,9 @@ func templateFuncs() textTpl.FuncMap { // (1970-01-01 00:00 UTC) excluding leap seconds. type Time int64 -// TimeFromUnixNano returns the Time equivalent to the Unix Time -// t provided in nanoseconds. -func TimeFromUnixNano(t int64) Time { - return Time(t / nanosPerTick) +// timeFromUnixTimestamp returns the Time equivalent to t in unix timestamp. +func timeFromUnixTimestamp(t float64) Time { + return Time(t * 1e3) } // The number of nanoseconds per minimum tick. diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index f6e197561a..8f7ab34976 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -19,6 +19,7 @@ The following tip changes can be tested by building VictoriaMetrics components f * FEATURE: [VictoriaMetrics cluster](https://docs.victoriametrics.com/Cluster-VictoriaMetrics.html): improve performance for heavy queries on systems with many CPU cores. * FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add support for MX record types in [dns_sd_configs](https://docs.victoriametrics.com/sd_configs.html#dns_sd_configs) in the same way as Prometheus 2.38 [does](https://github.com/prometheus/prometheus/pull/10099). +* FEATURE: [vmalert](https://docs.victoriametrics.com/vmalert.html): add `toTime()` template function in the same way as Prometheus 2.38 [does](https://github.com/prometheus/prometheus/pull/10993). See [these docs](https://prometheus.io/docs/prometheus/latest/configuration/template_reference/#numbers). * BUGFIX: prevent from excess CPU usage when the storage enters [read-only mode](https://docs.victoriametrics.com/Cluster-VictoriaMetrics.html#readonly-mode).