Skip to content

promql: avoid unnecessary Metric.Get() calls in functions.go#17676

Merged
bboreham merged 3 commits intoprometheus:mainfrom
vpranckaitis:avoid_unnecesary_extraction_of_metric_name
Jan 8, 2026
Merged

promql: avoid unnecessary Metric.Get() calls in functions.go#17676
bboreham merged 3 commits intoprometheus:mainfrom
vpranckaitis:avoid_unnecesary_extraction_of_metric_name

Conversation

@vpranckaitis
Copy link
Copy Markdown
Contributor

Moved some Metric.Get() calls in PromQL functions to avoid unnecessary label extraction. With stringlabels that is a non-trivial amount of work, as shown by CPU profiles. In many cases, this work was done to extract metric name, and was only used if annotations were emitted.

In the same go I also replaced labels.MetricName with model.MetricNameLabel, since the former was deprecated.

image

Which issue(s) does the PR fix:

Does this PR introduce a user-facing change?

[PERF] PromQL: Avoid unnecessary label extraction in PromQL functions 

@bboreham
Copy link
Copy Markdown
Member

Please show benchmark results, as requested in the PR template.

Copy link
Copy Markdown
Member

@bboreham bboreham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this; I think it's a good idea.

I would rather defer evaluation as far as possible, e.g. into histogramRate, and add a small helper function to shorten the code where used. Then don't create a new variable metricName anywhere it would only be used once.

@vpranckaitis
Copy link
Copy Markdown
Contributor Author

Please show benchmark results, as requested in the PR template.

Did not write a separate benchmark, but used the exisitng BenchmarkRangeQuery() to show the improvement. To cut out the noise, I removed all the test cases except these two:

cases := []benchCase{
    // Simple rate.
    {
	    expr: "rate(a_X[1m])",
    },
    {
	    expr:  "rate(a_X[1m])",
	    steps: 10000,
    },
}

The benchmark results show 1–6% reduction in runtime, and practically no change in allocations.

goos: darwin
goarch: arm64
pkg: github.com/prometheus/prometheus/promql
cpu: Apple M1 Pro
                                                   │   old.txt   │              new.txt               │
                                                   │   sec/op    │   sec/op     vs base               │
RangeQuery/expr=rate(a_one[1m]),steps=1-10           9.016µ ± 1%   8.819µ ± 1%  -2.19% (p=0.000 n=10)
RangeQuery/expr=rate(a_one[1m]),steps=1000-10        84.93µ ± 0%   79.18µ ± 1%  -6.77% (p=0.000 n=10)
RangeQuery/expr=rate(a_hundred[1m]),steps=1-10       216.7µ ± 0%   214.4µ ± 1%  -1.06% (p=0.002 n=10)
RangeQuery/expr=rate(a_hundred[1m]),steps=1000-10    8.172m ± 1%   7.763m ± 2%  -5.01% (p=0.000 n=10)
RangeQuery/expr=rate(a_one[1m]),steps=10000-10       824.9µ ± 0%   779.1µ ± 1%  -5.55% (p=0.000 n=10)
RangeQuery/expr=rate(a_hundred[1m]),steps=10000-10   85.89m ± 1%   80.89m ± 1%  -5.82% (p=0.000 n=10)
geomean                                              676.7µ        646.8µ       -4.42%

                                                   │   old.txt    │               new.txt               │
                                                   │     B/op     │     B/op      vs base               │
RangeQuery/expr=rate(a_one[1m]),steps=1-10           7.883Ki ± 0%   7.883Ki ± 0%       ~ (p=0.752 n=10)
RangeQuery/expr=rate(a_one[1m]),steps=1000-10        11.58Ki ± 0%   11.58Ki ± 0%       ~ (p=0.631 n=10)
RangeQuery/expr=rate(a_hundred[1m]),steps=1-10       68.97Ki ± 0%   68.97Ki ± 0%       ~ (p=0.670 n=10)
RangeQuery/expr=rate(a_hundred[1m]),steps=1000-10    309.4Ki ± 0%   309.5Ki ± 0%       ~ (p=0.781 n=10)
RangeQuery/expr=rate(a_one[1m]),steps=10000-10       52.84Ki ± 1%   52.83Ki ± 1%       ~ (p=0.469 n=10)
RangeQuery/expr=rate(a_hundred[1m]),steps=10000-10   2.585Mi ± 0%   2.581Mi ± 1%  -0.14% (p=0.009 n=10)
geomean                                              80.52Ki        80.50Ki       -0.03%

                                                   │   old.txt   │               new.txt                │
                                                   │  allocs/op  │  allocs/op   vs base                 │
RangeQuery/expr=rate(a_one[1m]),steps=1-10            136.0 ± 0%    136.0 ± 0%       ~ (p=1.000 n=10) ¹
RangeQuery/expr=rate(a_one[1m]),steps=1000-10         165.0 ± 0%    165.0 ± 0%       ~ (p=1.000 n=10) ¹
RangeQuery/expr=rate(a_hundred[1m]),steps=1-10       1.136k ± 0%   1.136k ± 0%       ~ (p=1.000 n=10) ¹
RangeQuery/expr=rate(a_hundred[1m]),steps=1000-10    3.577k ± 0%   3.577k ± 0%       ~ (p=0.265 n=10)
RangeQuery/expr=rate(a_one[1m]),steps=10000-10        457.0 ± 0%    457.0 ± 0%       ~ (p=1.000 n=10) ¹
RangeQuery/expr=rate(a_hundred[1m]),steps=10000-10   27.51k ± 0%   27.51k ± 0%       ~ (p=0.524 n=10)
geomean                                              1.023k        1.023k       -0.00%
¹ all samples are equal

Signed-off-by: Vilius Pranckaitis <[email protected]>
@vpranckaitis
Copy link
Copy Markdown
Contributor Author

Thanks for this; I think it's a good idea.

I would rather defer evaluation as far as possible, e.g. into histogramRate, and add a small helper function to shorten the code where used. Then don't create a new variable metricName anywhere it would only be used once.

@bboreham I've refactored the code to use getMetricName() helper function. I considered to name it getName() so that in majority of places where it's used it would look like getName(samples.Metric). The latter is shorter, but still should convey that the metric name is being retrieved. However, I decided to go with a more conservative option first.

Also, pushed the metric name extraction further down into histogramRate() function.

Copy link
Copy Markdown
Contributor

@linasm linasm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one nit.

Copy link
Copy Markdown
Member

@bboreham bboreham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM.

@bboreham bboreham merged commit 6a81e44 into prometheus:main Jan 8, 2026
28 checks passed
renovate bot added a commit to sdwilsh/ansible-playbooks that referenced this pull request Mar 12, 2026
##### [\`v3.10.0\`](https://github.com/prometheus/prometheus/releases/tag/v3.10.0)

Prometheus now offers a distroless Docker image variant alongside the default
busybox image. The distroless variant provides enhanced security with a minimal
base image, uses UID/GID 65532 (nonroot) instead of nobody, and removes the
VOLUME declaration. Both variants are available with `-busybox` and `-distroless`
tag suffixes (e.g., `prom/prometheus:latest-busybox`, `prom/prometheus:latest-distroless`).
The busybox image remains the default with no suffix for backwards compatibility
(e.g., `prom/prometheus:latest` points to the busybox variant).

For users migrating existing **named** volumes from the busybox image to the distroless variant, the ownership can be adjusted with:

```
docker run --rm -v prometheus-data:/prometheus alpine chown -R 65532:65532 /prometheus
```

Then, the container can be started with the old volume with:

```
docker run -v prometheus-data:/prometheus prom/prometheus:latest-distroless
```

User migrating from bind mounts might need to ajust permissions too, depending on their setup.

- \[CHANGE] Alerting: Add `alertmanager` dimension to following metrics: `prometheus_notifications_dropped_total`, `prometheus_notifications_queue_capacity`, `prometheus_notifications_queue_length`. [#16355](prometheus/prometheus#16355)
- \[CHANGE] UI: Hide expanded alert annotations by default, enabling more information density on the `/alerts` page. [#17611](prometheus/prometheus#17611)
- \[FEATURE] AWS SD: Add MSK Role. [#17600](prometheus/prometheus#17600)
- \[FEATURE] PromQL: Add `fill()` / `fill_left()` / `fill_right()` binop modifiers for specifying default values for missing series. [#17644](prometheus/prometheus#17644)
- \[FEATURE] Web: Add OpenAPI 3.2 specification for the HTTP API at `/api/v1/openapi.yaml`. [#17825](prometheus/prometheus#17825)
- \[FEATURE] Dockerfile: Add distroless image variant using UID/GID 65532 and no VOLUME declaration. Busybox image remains default. [#17876](prometheus/prometheus#17876)
- \[FEATURE] Web: Add on-demand wall time profiling under `<URL>/debug/pprof/fgprof`. [#18027](prometheus/prometheus#18027)
- \[ENHANCEMENT] PromQL: Add more detail to histogram quantile monotonicity info annotations. [#15578](prometheus/prometheus#15578)
- \[ENHANCEMENT] Alerting: Independent alertmanager sendloops. [#16355](prometheus/prometheus#16355)
- \[ENHANCEMENT] TSDB: Experimental support for early compaction of stale series in the memory with configurable threshold `stale_series_compaction_threshold` in the config file. [#16929](prometheus/prometheus#16929)
- \[ENHANCEMENT] Service Discovery: Service discoveries are now removable from the Prometheus binary through the Go build tag `remove_all_sd` and individual service discoveries can be re-added with the build tags `enable_<sd name>_sd`. Users can build a custom Prometheus with only the necessary SDs for a smaller binary size. [#17736](prometheus/prometheus#17736)
- \[ENHANCEMENT] Promtool: Support promql syntax features `promql-duration-expr` and `promql-extended-range-selectors`. [#17926](prometheus/prometheus#17926)
- \[PERF] PromQL: Avoid unnecessary label extraction in PromQL functions. [#17676](prometheus/prometheus#17676)
- \[PERF] PromQL: Improve performance of regex matchers like `.*-.*-.*`. [#17707](prometheus/prometheus#17707)
- \[PERF] OTLP: Add label caching for OTLP-to-Prometheus conversion to reduce allocations and improve latency. [#17860](prometheus/prometheus#17860)
- \[PERF] API: Compute `/api/v1/targets/relabel_steps` in a single pass instead of re-running relabeling for each prefix. [#17969](prometheus/prometheus#17969)
- \[PERF] tsdb: Optimize LabelValues intersection performance for matchers. [#18069](prometheus/prometheus#18069)
- \[BUGFIX] PromQL: Prevent query strings containing only UTF-8 continuation bytes from crashing Prometheus. [#17735](prometheus/prometheus#17735)
- \[BUGFIX] Web: Fix missing `X-Prometheus-Stopping` header for `/-/ready` endpoint in `NotReady` state. [#17795](prometheus/prometheus#17795)
- \[BUGFIX] PromQL: Fix PromQL `info()` function returning empty results when filtering by a label that exists on both the input metric and `target_info`. [#17817](prometheus/prometheus#17817)
- \[BUGFIX] TSDB: Fix a bug during exemplar buffer grow/shrink that could cause exemplars to be incorrectly discarded. [#17863](prometheus/prometheus#17863)
- \[BUGFIX] UI: Fix broken graph display after page reload, due to broken Y axis min encoding/decoding. [#17869](prometheus/prometheus#17869)
- \[BUGFIX] TSDB: Fix memory leaks in buffer pools by clearing reference fields (Labels, Histogram pointers, metadata strings) before returning buffers to pools. [#17879](prometheus/prometheus#17879)
- \[BUGFIX] PromQL: info function: fix series without identifying labels not being returned. [#17898](prometheus/prometheus#17898)
- \[BUGFIX] OTLP: Filter `__name__` from OTLP attributes to prevent duplicate labels. [#17917](prometheus/prometheus#17917)
- \[BUGFIX] TSDB: Fix division by zero when computing stale series ratio with empty head. [#17952](prometheus/prometheus#17952)
- \[BUGFIX] OTLP: Fix potential silent data loss for sum metrics. [#17954](prometheus/prometheus#17954)
- \[BUGFIX] PromQL: Fix smoothed interpolation across counter resets. [#17988](prometheus/prometheus#17988)
- \[BUGFIX] PromQL: Fix panic with `@` modifier on empty ranges. [#18020](prometheus/prometheus#18020)
- \[BUGFIX] PromQL: Fix `avg_over_time` for a single native histogram. [#18058](prometheus/prometheus#18058)
renovate bot added a commit to sdwilsh/ansible-playbooks that referenced this pull request Mar 13, 2026
##### [\`v3.10.0\`](https://github.com/prometheus/prometheus/releases/tag/v3.10.0)

Prometheus now offers a distroless Docker image variant alongside the default
busybox image. The distroless variant provides enhanced security with a minimal
base image, uses UID/GID 65532 (nonroot) instead of nobody, and removes the
VOLUME declaration. Both variants are available with `-busybox` and `-distroless`
tag suffixes (e.g., `prom/prometheus:latest-busybox`, `prom/prometheus:latest-distroless`).
The busybox image remains the default with no suffix for backwards compatibility
(e.g., `prom/prometheus:latest` points to the busybox variant).

For users migrating existing **named** volumes from the busybox image to the distroless variant, the ownership can be adjusted with:

```
docker run --rm -v prometheus-data:/prometheus alpine chown -R 65532:65532 /prometheus
```

Then, the container can be started with the old volume with:

```
docker run -v prometheus-data:/prometheus prom/prometheus:latest-distroless
```

User migrating from bind mounts might need to ajust permissions too, depending on their setup.

- \[CHANGE] Alerting: Add `alertmanager` dimension to following metrics: `prometheus_notifications_dropped_total`, `prometheus_notifications_queue_capacity`, `prometheus_notifications_queue_length`. [#16355](prometheus/prometheus#16355)
- \[CHANGE] UI: Hide expanded alert annotations by default, enabling more information density on the `/alerts` page. [#17611](prometheus/prometheus#17611)
- \[FEATURE] AWS SD: Add MSK Role. [#17600](prometheus/prometheus#17600)
- \[FEATURE] PromQL: Add `fill()` / `fill_left()` / `fill_right()` binop modifiers for specifying default values for missing series. [#17644](prometheus/prometheus#17644)
- \[FEATURE] Web: Add OpenAPI 3.2 specification for the HTTP API at `/api/v1/openapi.yaml`. [#17825](prometheus/prometheus#17825)
- \[FEATURE] Dockerfile: Add distroless image variant using UID/GID 65532 and no VOLUME declaration. Busybox image remains default. [#17876](prometheus/prometheus#17876)
- \[FEATURE] Web: Add on-demand wall time profiling under `<URL>/debug/pprof/fgprof`. [#18027](prometheus/prometheus#18027)
- \[ENHANCEMENT] PromQL: Add more detail to histogram quantile monotonicity info annotations. [#15578](prometheus/prometheus#15578)
- \[ENHANCEMENT] Alerting: Independent alertmanager sendloops. [#16355](prometheus/prometheus#16355)
- \[ENHANCEMENT] TSDB: Experimental support for early compaction of stale series in the memory with configurable threshold `stale_series_compaction_threshold` in the config file. [#16929](prometheus/prometheus#16929)
- \[ENHANCEMENT] Service Discovery: Service discoveries are now removable from the Prometheus binary through the Go build tag `remove_all_sd` and individual service discoveries can be re-added with the build tags `enable_<sd name>_sd`. Users can build a custom Prometheus with only the necessary SDs for a smaller binary size. [#17736](prometheus/prometheus#17736)
- \[ENHANCEMENT] Promtool: Support promql syntax features `promql-duration-expr` and `promql-extended-range-selectors`. [#17926](prometheus/prometheus#17926)
- \[PERF] PromQL: Avoid unnecessary label extraction in PromQL functions. [#17676](prometheus/prometheus#17676)
- \[PERF] PromQL: Improve performance of regex matchers like `.*-.*-.*`. [#17707](prometheus/prometheus#17707)
- \[PERF] OTLP: Add label caching for OTLP-to-Prometheus conversion to reduce allocations and improve latency. [#17860](prometheus/prometheus#17860)
- \[PERF] API: Compute `/api/v1/targets/relabel_steps` in a single pass instead of re-running relabeling for each prefix. [#17969](prometheus/prometheus#17969)
- \[PERF] tsdb: Optimize LabelValues intersection performance for matchers. [#18069](prometheus/prometheus#18069)
- \[BUGFIX] PromQL: Prevent query strings containing only UTF-8 continuation bytes from crashing Prometheus. [#17735](prometheus/prometheus#17735)
- \[BUGFIX] Web: Fix missing `X-Prometheus-Stopping` header for `/-/ready` endpoint in `NotReady` state. [#17795](prometheus/prometheus#17795)
- \[BUGFIX] PromQL: Fix PromQL `info()` function returning empty results when filtering by a label that exists on both the input metric and `target_info`. [#17817](prometheus/prometheus#17817)
- \[BUGFIX] TSDB: Fix a bug during exemplar buffer grow/shrink that could cause exemplars to be incorrectly discarded. [#17863](prometheus/prometheus#17863)
- \[BUGFIX] UI: Fix broken graph display after page reload, due to broken Y axis min encoding/decoding. [#17869](prometheus/prometheus#17869)
- \[BUGFIX] TSDB: Fix memory leaks in buffer pools by clearing reference fields (Labels, Histogram pointers, metadata strings) before returning buffers to pools. [#17879](prometheus/prometheus#17879)
- \[BUGFIX] PromQL: info function: fix series without identifying labels not being returned. [#17898](prometheus/prometheus#17898)
- \[BUGFIX] OTLP: Filter `__name__` from OTLP attributes to prevent duplicate labels. [#17917](prometheus/prometheus#17917)
- \[BUGFIX] TSDB: Fix division by zero when computing stale series ratio with empty head. [#17952](prometheus/prometheus#17952)
- \[BUGFIX] OTLP: Fix potential silent data loss for sum metrics. [#17954](prometheus/prometheus#17954)
- \[BUGFIX] PromQL: Fix smoothed interpolation across counter resets. [#17988](prometheus/prometheus#17988)
- \[BUGFIX] PromQL: Fix panic with `@` modifier on empty ranges. [#18020](prometheus/prometheus#18020)
- \[BUGFIX] PromQL: Fix `avg_over_time` for a single native histogram. [#18058](prometheus/prometheus#18058)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants