diff --git a/app/vmselect/prometheus/prometheus.go b/app/vmselect/prometheus/prometheus.go index 1d86ee250..b0a251d49 100644 --- a/app/vmselect/prometheus/prometheus.go +++ b/app/vmselect/prometheus/prometheus.go @@ -1089,6 +1089,7 @@ func QueryHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r QuotedRemoteAddr: httpserver.GetQuotedRemoteAddr(r), Deadline: deadline, LookbackDelta: lookbackDelta, + RoundDigits: getRoundDigits(r), EnforcedTagFilters: etf, DenyPartialResponse: searchutils.GetDenyPartialResponse(r), @@ -1196,6 +1197,7 @@ func queryRangeHandler(startTime time.Time, at *auth.Token, w http.ResponseWrite Deadline: deadline, MayCache: mayCache, LookbackDelta: lookbackDelta, + RoundDigits: getRoundDigits(r), EnforcedTagFilters: etf, DenyPartialResponse: searchutils.GetDenyPartialResponse(r), @@ -1374,6 +1376,18 @@ func getMatchesFromRequest(r *http.Request) []string { return matches } +func getRoundDigits(r *http.Request) int { + s := r.FormValue("round_digits") + if len(s) == 0 { + return 100 + } + n, err := strconv.Atoi(s) + if err != nil { + return 100 + } + return n +} + func getLatencyOffsetMilliseconds() int64 { d := latencyOffset.Milliseconds() if d <= 1000 { diff --git a/app/vmselect/promql/eval.go b/app/vmselect/promql/eval.go index fc45596bb..276213344 100644 --- a/app/vmselect/promql/eval.go +++ b/app/vmselect/promql/eval.go @@ -100,9 +100,13 @@ type EvalConfig struct { // LookbackDelta is analog to `-query.lookback-delta` from Prometheus. LookbackDelta int64 + // How many decimal digits after the point to leave in response. + RoundDigits int + // EnforcedTagFilters used for apply additional label filters to query. EnforcedTagFilters []storage.TagFilter + // Whether to deny partial response. DenyPartialResponse bool // IsPartialResponse is set during query execution and can be used by Exec caller after query execution. @@ -122,6 +126,7 @@ func newEvalConfig(src *EvalConfig) *EvalConfig { ec.Deadline = src.Deadline ec.MayCache = src.MayCache ec.LookbackDelta = src.LookbackDelta + ec.RoundDigits = src.RoundDigits ec.EnforcedTagFilters = src.EnforcedTagFilters ec.DenyPartialResponse = src.DenyPartialResponse ec.IsPartialResponse = src.IsPartialResponse diff --git a/app/vmselect/promql/exec.go b/app/vmselect/promql/exec.go index 7a7a04da0..b30465085 100644 --- a/app/vmselect/promql/exec.go +++ b/app/vmselect/promql/exec.go @@ -12,6 +12,7 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/netstorage" "github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/querystats" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/decimal" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/metrics" "github.com/VictoriaMetrics/metricsql" @@ -73,6 +74,14 @@ func Exec(ec *EvalConfig, q string, isFirstPointOnly bool) ([]netstorage.Result, if err != nil { return nil, err } + if n := ec.RoundDigits; n < 100 { + for i := range result { + values := result[i].Values + for j, v := range values { + values[j] = decimal.RoundToDecimalDigits(v, n) + } + } + } return result, err } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 4442b209b..3b0439047 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -12,6 +12,7 @@ * FATURE: vmagent: accept `scrape_offset` option at `scrape_config`. This option may be useful when scrapes must start at the specified offset of every scrape interval. See [these docs](https://victoriametrics.github.io/vmagent.html#troubleshooting) for details. * FEATURE: vmagent: support `proxy_tls_config`, `proxy_basic_auth`, `proxy_bearer_token` and `proxy_bearer_token_file` options at `scrape_config` section for configuring proxies specified via `proxy_url`. See [these docs](https://victoriametrics.github.io/vmagent.html#scraping-targets-via-a-proxy). * FEATURE: vmauth: allow using regexp paths in `url_map`. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1112) for details. +* FEATURE: accept `round_digits` query arg at `/api/v1/query` and `/api/v1/query_range` handlers. This option can be set at Prometheus datasource in Grafana for limiting the number of digits after the decimal point in response values. * BUGFIX: vmagent: prevent from high CPU usage bug during failing scrapes with small `scrape_timeout` (less than a few seconds). * BUGFIX: vmagent: reduce memory usage when Kubernetes service discovery is used in big number of distinct scrape config jobs by sharing Kubernetes object cache. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1113 diff --git a/docs/Single-server-VictoriaMetrics.md b/docs/Single-server-VictoriaMetrics.md index 3ace5755d..ef1c954be 100644 --- a/docs/Single-server-VictoriaMetrics.md +++ b/docs/Single-server-VictoriaMetrics.md @@ -562,14 +562,17 @@ in front of VictoriaMetrics. [Contact us](mailto:sales@victoriametrics.com) if y VictoriaMetrics accepts relative times in `time`, `start` and `end` query args additionally to unix timestamps and [RFC3339](https://www.ietf.org/rfc/rfc3339.txt). For example, the following query would return data for the last 30 minutes: `/api/v1/query_range?start=-30m&query=...`. +VictoriaMetrics accepts `round_digits` query arg for `/api/v1/query` and `/api/v1/query_range` handlers. It can be used for rounding response values to the given number of digits after the decimal point. For example, `/api/v1/query?query=avg_over_time(temperature[1h])&round_digits=2` would round response values to up to two digits after the decimal point. + By default, VictoriaMetrics returns time series for the last 5 minutes from `/api/v1/series`, while the Prometheus API defaults to all time. Use `start` and `end` to select a different time range. VictoriaMetrics accepts additional args for `/api/v1/labels` and `/api/v1/label/.../values` handlers. -See [this feature request](https://github.com/prometheus/prometheus/issues/6178) for details: * Any number [time series selectors](https://prometheus.io/docs/prometheus/latest/querying/basics/#time-series-selectors) via `match[]` query arg. * Optional `start` and `end` query args for limiting the time range for the selected labels or label values. +See [this feature request](https://github.com/prometheus/prometheus/issues/6178) for details. + Additionally VictoriaMetrics provides the following handlers: * `/api/v1/series/count` - returns the total number of time series in the database. Some notes: