app/vmselect/promql: add now() function, which returns the current timestamp as a floating-point value in seconds

This commit is contained in:
Aliaksandr Valialkin 2021-11-17 16:25:02 +02:00
parent b3c6334fbb
commit b900560b83
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
8 changed files with 31 additions and 4 deletions

View file

@ -1017,6 +1017,17 @@ func TestExecSuccess(t *testing.T) {
resultExpected := []netstorage.Result{r} resultExpected := []netstorage.Result{r}
f(q, resultExpected) 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.Run("pi()", func(t *testing.T) {
t.Parallel() t.Parallel()
q := `pi()` q := `pi()`
@ -7412,6 +7423,7 @@ func TestExecError(t *testing.T) {
f(`rand_normal(123, 456)`) f(`rand_normal(123, 456)`)
f(`rand_exponential(122, 456)`) f(`rand_exponential(122, 456)`)
f(`pi(123)`) f(`pi(123)`)
f(`now(123)`)
f(`label_copy()`) f(`label_copy()`)
f(`label_move()`) f(`label_move()`)
f(`median_over_time()`) f(`median_over_time()`)

View file

@ -74,6 +74,7 @@ var transformFuncs = map[string]transformFunc{
"log10": newTransformFuncOneArg(transformLog10), "log10": newTransformFuncOneArg(transformLog10),
"minute": newTransformFuncDateTime(transformMinute), "minute": newTransformFuncDateTime(transformMinute),
"month": newTransformFuncDateTime(transformMonth), "month": newTransformFuncDateTime(transformMonth),
"now": transformNow,
"pi": transformPi, "pi": transformPi,
"prometheus_buckets": transformPrometheusBuckets, "prometheus_buckets": transformPrometheusBuckets,
"rad": newTransformFuncOneArg(transformRad), "rad": newTransformFuncOneArg(transformRad),
@ -2045,6 +2046,14 @@ func transformPi(tfa *transformFuncArg) ([]*timeseries, error) {
return evalNumber(tfa.ec, math.Pi), nil 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 { func bitmapAnd(a, b uint64) uint64 {
return a & b return a & b
} }

View file

@ -6,6 +6,7 @@ sort: 15
## tip ## 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: 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). * 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).

View file

@ -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. `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
`pi()` returns [Pi number](https://en.wikipedia.org/wiki/Pi). This function is supported by PromQL. `pi()` returns [Pi number](https://en.wikipedia.org/wiki/Pi). This function is supported by PromQL.

2
go.mod
View file

@ -8,7 +8,7 @@ require (
// like https://github.com/valyala/fasthttp/commit/996610f021ff45fdc98c2ce7884d5fa4e7f9199b // like https://github.com/valyala/fasthttp/commit/996610f021ff45fdc98c2ce7884d5fa4e7f9199b
github.com/VictoriaMetrics/fasthttp v1.1.0 github.com/VictoriaMetrics/fasthttp v1.1.0
github.com/VictoriaMetrics/metrics v1.18.1 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/VividCortex/ewma v1.2.0 // indirect
github.com/aws/aws-sdk-go v1.42.6 github.com/aws/aws-sdk-go v1.42.6
github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect

4
go.sum
View file

@ -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.12.2/go.mod h1:Z1tSfPfngDn12bTfZSCqArT3OPY3u88J12hSoOhuiRE=
github.com/VictoriaMetrics/metrics v1.18.1 h1:OZ0+kTTto8oPfHnVAnTOoyl0XlRhRkoQrD2n2cOuRw0= 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/metrics v1.18.1/go.mod h1:ArjwVz7WpgpegX/JpB0zpNF2h2232kErkEnzH1sxMmA=
github.com/VictoriaMetrics/metricsql v0.30.1 h1:WFRNNocJsU6h98f7fkthHkMH1xwmcVSP2eSAap9FpTc= github.com/VictoriaMetrics/metricsql v0.31.0 h1:7cpjby64WVcRNBiMieEytuvAcU/jOOz+39RLigENz4E=
github.com/VictoriaMetrics/metricsql v0.30.1/go.mod h1:ylO7YITho/Iw6P71oEaGyHbO94bGoGtzWfLGqFhMIg8= 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.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4= github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=

View file

@ -61,6 +61,7 @@ var transformFuncs = map[string]bool{
"log10": true, "log10": true,
"minute": true, "minute": true,
"month": true, "month": true,
"now": true,
"pi": true, "pi": true,
"prometheus_buckets": true, "prometheus_buckets": true,
"rad": true, "rad": true,

2
vendor/modules.txt vendored
View file

@ -21,7 +21,7 @@ github.com/VictoriaMetrics/fasthttp/stackless
# github.com/VictoriaMetrics/metrics v1.18.1 # github.com/VictoriaMetrics/metrics v1.18.1
## explicit ## explicit
github.com/VictoriaMetrics/metrics github.com/VictoriaMetrics/metrics
# github.com/VictoriaMetrics/metricsql v0.30.1 # github.com/VictoriaMetrics/metricsql v0.31.0
## explicit ## explicit
github.com/VictoriaMetrics/metricsql github.com/VictoriaMetrics/metricsql
github.com/VictoriaMetrics/metricsql/binaryop github.com/VictoriaMetrics/metricsql/binaryop