diff --git a/app/vmselect/promql/exec_test.go b/app/vmselect/promql/exec_test.go index 0c58c84b1..ace74c39b 100644 --- a/app/vmselect/promql/exec_test.go +++ b/app/vmselect/promql/exec_test.go @@ -1017,6 +1017,17 @@ func TestExecSuccess(t *testing.T) { resultExpected := []netstorage.Result{r} f(q, resultExpected) }) + t.Run("now()", func(t *testing.T) { + t.Parallel() + q := `round(now()/now())` + r := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{1, 1, 1, 1, 1, 1}, + Timestamps: timestampsExpected, + } + resultExpected := []netstorage.Result{r} + f(q, resultExpected) + }) t.Run("pi()", func(t *testing.T) { t.Parallel() q := `pi()` @@ -7412,6 +7423,7 @@ func TestExecError(t *testing.T) { f(`rand_normal(123, 456)`) f(`rand_exponential(122, 456)`) f(`pi(123)`) + f(`now(123)`) f(`label_copy()`) f(`label_move()`) f(`median_over_time()`) diff --git a/app/vmselect/promql/transform.go b/app/vmselect/promql/transform.go index 985154823..963a0b611 100644 --- a/app/vmselect/promql/transform.go +++ b/app/vmselect/promql/transform.go @@ -74,6 +74,7 @@ var transformFuncs = map[string]transformFunc{ "log10": newTransformFuncOneArg(transformLog10), "minute": newTransformFuncDateTime(transformMinute), "month": newTransformFuncDateTime(transformMonth), + "now": transformNow, "pi": transformPi, "prometheus_buckets": transformPrometheusBuckets, "rad": newTransformFuncOneArg(transformRad), @@ -2045,6 +2046,14 @@ func transformPi(tfa *transformFuncArg) ([]*timeseries, error) { return evalNumber(tfa.ec, math.Pi), nil } +func transformNow(tfa *transformFuncArg) ([]*timeseries, error) { + if err := expectTransformArgsNum(tfa.args, 0); err != nil { + return nil, err + } + now := float64(time.Now().UnixNano()) / 1e9 + return evalNumber(tfa.ec, now), nil +} + func bitmapAnd(a, b uint64) uint64 { return a & b } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 181d6c6e7..e6af31900 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,6 +6,7 @@ sort: 15 ## tip +* FEATURE: add `now()` function to MetricsQL. This function returns the current timestamp in seconds. See [these docs](https://docs.victoriametrics.com/MetricsQL.html#now). * FEATURE: vmauth: allow using optional `name` field in configs. This field is then used as `username` label value for `vmauth_user_requests_total` metric. See [this feature request](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1805). * FEATURE: [vmbackup](https://docs.victoriametrics.com/vmbackup.html), [vmrestore](https://docs.victoriametrics.com/vmrestore.html): add `-s3ForcePathStyle` command-line flag, which can be used for making backups to [Aliyun OSS](https://www.aliyun.com/product/oss). See [this pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/1802). diff --git a/docs/MetricsQL.md b/docs/MetricsQL.md index bf9d35c8c..0bde60d12 100644 --- a/docs/MetricsQL.md +++ b/docs/MetricsQL.md @@ -507,6 +507,10 @@ See also [implicit query conversions](#implicit-query-conversions). `month(q)` returns the month for every point of every time series returned by `q`. It is expected that `q` returns unix timestamps. The returned values are in the range `[1...12]`, where `1` means January and `12` means December. Metric names are stripped from the resulting series. This function is supported by PromQL. +#### now + +`now()` returns the current timestamp as a floating-point value in seconds. See also [time](#time). + #### pi `pi()` returns [Pi number](https://en.wikipedia.org/wiki/Pi). This function is supported by PromQL. diff --git a/go.mod b/go.mod index c4c800cc1..b56c582ae 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( // like https://github.com/valyala/fasthttp/commit/996610f021ff45fdc98c2ce7884d5fa4e7f9199b github.com/VictoriaMetrics/fasthttp v1.1.0 github.com/VictoriaMetrics/metrics v1.18.1 - github.com/VictoriaMetrics/metricsql v0.30.1 + github.com/VictoriaMetrics/metricsql v0.31.0 github.com/VividCortex/ewma v1.2.0 // indirect github.com/aws/aws-sdk-go v1.42.6 github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect diff --git a/go.sum b/go.sum index 867328c4f..8d4854a84 100644 --- a/go.sum +++ b/go.sum @@ -108,8 +108,8 @@ github.com/VictoriaMetrics/fasthttp v1.1.0/go.mod h1:/7DMcogqd+aaD3G3Hg5kFgoFwlR github.com/VictoriaMetrics/metrics v1.12.2/go.mod h1:Z1tSfPfngDn12bTfZSCqArT3OPY3u88J12hSoOhuiRE= github.com/VictoriaMetrics/metrics v1.18.1 h1:OZ0+kTTto8oPfHnVAnTOoyl0XlRhRkoQrD2n2cOuRw0= github.com/VictoriaMetrics/metrics v1.18.1/go.mod h1:ArjwVz7WpgpegX/JpB0zpNF2h2232kErkEnzH1sxMmA= -github.com/VictoriaMetrics/metricsql v0.30.1 h1:WFRNNocJsU6h98f7fkthHkMH1xwmcVSP2eSAap9FpTc= -github.com/VictoriaMetrics/metricsql v0.30.1/go.mod h1:ylO7YITho/Iw6P71oEaGyHbO94bGoGtzWfLGqFhMIg8= +github.com/VictoriaMetrics/metricsql v0.31.0 h1:7cpjby64WVcRNBiMieEytuvAcU/jOOz+39RLigENz4E= +github.com/VictoriaMetrics/metricsql v0.31.0/go.mod h1:ylO7YITho/Iw6P71oEaGyHbO94bGoGtzWfLGqFhMIg8= github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA= github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4= diff --git a/vendor/github.com/VictoriaMetrics/metricsql/transform.go b/vendor/github.com/VictoriaMetrics/metricsql/transform.go index a08d50d05..af3b3463e 100644 --- a/vendor/github.com/VictoriaMetrics/metricsql/transform.go +++ b/vendor/github.com/VictoriaMetrics/metricsql/transform.go @@ -61,6 +61,7 @@ var transformFuncs = map[string]bool{ "log10": true, "minute": true, "month": true, + "now": true, "pi": true, "prometheus_buckets": true, "rad": true, diff --git a/vendor/modules.txt b/vendor/modules.txt index b5ca5bdf5..3abf84338 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -21,7 +21,7 @@ github.com/VictoriaMetrics/fasthttp/stackless # github.com/VictoriaMetrics/metrics v1.18.1 ## explicit github.com/VictoriaMetrics/metrics -# github.com/VictoriaMetrics/metricsql v0.30.1 +# github.com/VictoriaMetrics/metricsql v0.31.0 ## explicit github.com/VictoriaMetrics/metricsql github.com/VictoriaMetrics/metricsql/binaryop