From d7897e0d70756e5a0d19d04c85f0fdd6e6ef3754 Mon Sep 17 00:00:00 2001 From: luckyxiaoqiang Date: Tue, 28 Nov 2023 18:54:00 +0800 Subject: [PATCH] app/vmselect/promql: add day_of_year() function (#5368) Co-authored-by: dingxiaoqiang Co-authored-by: Roman Khavronenko --- app/vmselect/promql/exec_test.go | 11 +++++++++++ app/vmselect/promql/transform.go | 5 +++++ docs/CHANGELOG.md | 3 +++ docs/MetricsQL.md | 9 +++++++++ go.mod | 2 +- go.sum | 4 ++-- .../github.com/VictoriaMetrics/metricsql/transform.go | 1 + vendor/modules.txt | 2 +- 8 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/vmselect/promql/exec_test.go b/app/vmselect/promql/exec_test.go index 049789082..20abdebcb 100644 --- a/app/vmselect/promql/exec_test.go +++ b/app/vmselect/promql/exec_test.go @@ -853,6 +853,17 @@ func TestExecSuccess(t *testing.T) { resultExpected := []netstorage.Result{r} f(q, resultExpected) }) + t.Run("day_of_year()", func(t *testing.T) { + t.Parallel() + q := `day_of_year(time()*1e4)` + r := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{116, 139, 163, 186, 209, 232}, + Timestamps: timestampsExpected, + } + resultExpected := []netstorage.Result{r} + f(q, resultExpected) + }) t.Run("days_in_month()", func(t *testing.T) { t.Parallel() q := `days_in_month(time()*2e4)` diff --git a/app/vmselect/promql/transform.go b/app/vmselect/promql/transform.go index 0088e3044..a52055dbb 100644 --- a/app/vmselect/promql/transform.go +++ b/app/vmselect/promql/transform.go @@ -42,6 +42,7 @@ var transformFuncs = map[string]transformFunc{ "cosh": newTransformFuncOneArg(transformCosh), "day_of_month": newTransformFuncDateTime(transformDayOfMonth), "day_of_week": newTransformFuncDateTime(transformDayOfWeek), + "day_of_year": newTransformFuncDateTime(transformDayOfYear), "days_in_month": newTransformFuncDateTime(transformDaysInMonth), "deg": newTransformFuncOneArg(transformDeg), "drop_common_labels": transformDropCommonLabels, @@ -353,6 +354,10 @@ func newTransformFuncDateTime(f func(t time.Time) int) transformFunc { } } +func transformDayOfYear(t time.Time) int { + return t.YearDay() +} + func transformDayOfMonth(t time.Time) int { return t.Day() } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 8a48a8481..3253fbe21 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -31,10 +31,13 @@ The sandbox cluster installation is running under the constant load generated by * FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add `-remoteWrite.disableOnDiskQueue` command-line flag, which can be used for disabling data queueing to disk when the remote storage cannot keep up with the data ingestion rate. See [these docs](https://docs.victoriametrics.com/vmagent.html#disabling-on-disk-persistence) and [this feature request](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2110). * FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add support for reading and writing samples via [Google PubSub](https://cloud.google.com/pubsub). See [these docs](https://docs.victoriametrics.com/vmagent.html#google-pubsub-integration). * FEATURE: reduce the default value for `-import.maxLineLen` command-line flag from 100MB to 10MB in order to prevent excessive memory usage during data import via [/api/v1/import](https://docs.victoriametrics.com/#how-to-import-data-in-json-line-format). +* FEATURE: [MetricsQL](https://docs.victoriametrics.com/MetricsQL.html): add [day_of_year()](https://docs.victoriametrics.com/MetricsQL.html#day_of_year) function, which returns the day of the year for each of the given unix timestamps. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5345) for details. * BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): prevent from `FATAL: cannot flush metainfo` panic when [`-remoteWrite.multitenantURL`](https://docs.victoriametrics.com/vmagent.html#multitenancy) command-line flag is set. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5357). + * BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): properly decode zstd-encoded data blocks received via [VictoriaMetrics remote_write protocol](https://docs.victoriametrics.com/vmagent.html#victoriametrics-remote-write-protocol). See [this issue comment](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5301#issuecomment-1815871992). + ## [v1.95.1](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.95.1) Released at 2023-11-16 diff --git a/docs/MetricsQL.md b/docs/MetricsQL.md index 92d713cf0..fa876b9d8 100644 --- a/docs/MetricsQL.md +++ b/docs/MetricsQL.md @@ -1049,6 +1049,15 @@ Metric names are stripped from the resulting series. Add [keep_metric_names](#ke This function is supported by PromQL. +#### day_of_year + +`day_of_year(q)` is a [transform function](#transform-functions), which returns the day of year 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...365]` for non-leap years, and `[1 to 366]` in leap years. + +Metric names are stripped from the resulting series. Add [keep_metric_names](#keep_metric_names) modifier in order to keep metric names. + +This function is supported by PromQL. + #### days_in_month `days_in_month(q)` is a [transform function](#transform-functions), which returns the number of days in the month identified diff --git a/go.mod b/go.mod index d2e7837d4..c915c977c 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( // like https://github.com/valyala/fasthttp/commit/996610f021ff45fdc98c2ce7884d5fa4e7f9199b github.com/VictoriaMetrics/fasthttp v1.2.0 github.com/VictoriaMetrics/metrics v1.24.0 - github.com/VictoriaMetrics/metricsql v0.69.0 + github.com/VictoriaMetrics/metricsql v0.70.0 github.com/aws/aws-sdk-go-v2 v1.23.0 github.com/aws/aws-sdk-go-v2/config v1.25.1 github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.13.8 diff --git a/go.sum b/go.sum index 7617c98c4..369a5c93d 100644 --- a/go.sum +++ b/go.sum @@ -64,8 +64,8 @@ github.com/VictoriaMetrics/fasthttp v1.2.0 h1:nd9Wng4DlNtaI27WlYh5mGXCJOmee/2c2b github.com/VictoriaMetrics/fasthttp v1.2.0/go.mod h1:zv5YSmasAoSyv8sBVexfArzFDIGGTN4TfCKAtAw7IfE= github.com/VictoriaMetrics/metrics v1.24.0 h1:ILavebReOjYctAGY5QU2F9X0MYvkcrG3aEn2RKa1Zkw= github.com/VictoriaMetrics/metrics v1.24.0/go.mod h1:eFT25kvsTidQFHb6U0oa0rTrDRdz4xTYjpL8+UPohys= -github.com/VictoriaMetrics/metricsql v0.69.0 h1:6np68zGOnMiGEJR/rCvywS1gbLGXVrmQC3BKydsbWHw= -github.com/VictoriaMetrics/metricsql v0.69.0/go.mod h1:k4UaP/+CjuZslIjd+kCigNG9TQmUqh5v0TP/nMEy90I= +github.com/VictoriaMetrics/metricsql v0.70.0 h1:G0k/m1yAF6pmk0dM3VT9/XI5PZ8dL7EbcLhREf4bgeI= +github.com/VictoriaMetrics/metricsql v0.70.0/go.mod h1:k4UaP/+CjuZslIjd+kCigNG9TQmUqh5v0TP/nMEy90I= github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= diff --git a/vendor/github.com/VictoriaMetrics/metricsql/transform.go b/vendor/github.com/VictoriaMetrics/metricsql/transform.go index a7d9f6698..e8d89aa8a 100644 --- a/vendor/github.com/VictoriaMetrics/metricsql/transform.go +++ b/vendor/github.com/VictoriaMetrics/metricsql/transform.go @@ -24,6 +24,7 @@ var transformFuncs = map[string]bool{ "clamp_min": true, "cos": true, "cosh": true, + "day_of_year": true, "day_of_month": true, "day_of_week": true, "days_in_month": true, diff --git a/vendor/modules.txt b/vendor/modules.txt index c5af4dd9f..aa4ec922e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -100,7 +100,7 @@ github.com/VictoriaMetrics/fasthttp/stackless # github.com/VictoriaMetrics/metrics v1.24.0 ## explicit; go 1.20 github.com/VictoriaMetrics/metrics -# github.com/VictoriaMetrics/metricsql v0.69.0 +# github.com/VictoriaMetrics/metricsql v0.70.0 ## explicit; go 1.13 github.com/VictoriaMetrics/metricsql github.com/VictoriaMetrics/metricsql/binaryop