Commit c9cfa60
Optimize metric name validation to fix 2-3x performance regression (#1662)
Regex validation in `isValidLegacyLabelName()`,
`isValidLegacyMetricName()`, and `validateUnitName()` was being called
on every metric name during text format export, causing significant
overhead.
## Changes
- Replace regex pattern matching with character-by-character validation
in `isValidLegacyLabelName()`, `isValidLegacyMetricName()`, and
`validateUnitName()`
- Deprecate unused `METRIC_NAME_PATTERN`, `LEGACY_LABEL_NAME_PATTERN`,
and `UNIT_NAME_PATTERN` fields (kept for API compatibility)
- Update JavaDoc to reflect validation approach
## Implementation
Before:
```java
public static boolean isValidLegacyLabelName(String name) {
return LEGACY_LABEL_NAME_PATTERN.matcher(name).matches();
}
```
After:
```java
public static boolean isValidLegacyLabelName(String name) {
if (name.isEmpty()) return false;
char first = name.charAt(0);
if (!((first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z') || first == '_')) {
return false;
}
for (int i = 1; i < name.length(); i++) {
char c = name.charAt(i);
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_')) {
return false;
}
}
return true;
}
```
Benchmark results show recovery to near-baseline performance (532k ops/s
vs 534k ops/s on main).
> [!WARNING]
>
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
>
<__filter_complete__></__filter_complete__></details><issue_title>Performance
regression in text-format export on 1.4.0+</issue_title>
><issue_description>It seems like the adding of support for UTF-8
characters has considerably increased processing time in the
`io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter.writeNameAndLabels`
method due to it now calling
`io.prometheus.metrics.model.snapshots.PrometheusNaming.isValidLegacyMetricName`.
>
> I ran a local test with 1000 metrics exported. Average time per export
on version 1.3.10 was 0.9-1.1 ms, while on version 1.4.1 I got an
average of 2.7-2.9 ms.
>
> I'm not sure which avenues make sense to avoid this regression and I'm
not sure how relevant it is. I just found this while evaluating a move
from the old `simpleclient` to the new `client-java` and that drew my
attention, as the text format export is now slower than in
`simpleclient`.
>
> I have attached the flamegraph I captured on both versions.
>
><a
href="https://github.com/user-attachments/files/23315564/flamegraph-v141.html">flamegraph-v141.html</a>
> <a
href="https://github.com/user-attachments/files/23315565/flamegraph-v1310.html">flamegraph-v1310.html</a></issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
><comments>
><comment_new><author>@zeitlinger</author>
> Thanks for the report
>
> I can confirm that the existing benchmark shows the same - we just
have not integrated that into our release process (yet).
>
> ```
> ## Report details
> - **Date generated:** 2025-11-04 08:16:07
>
> ## Hardware Information:
> - **Hardware Model:** Micro-Star International Co., Ltd. MS-7D76
> - **Memory:** 96.0 GiB
> - **Processor:** AMD Ryzen™ 9 7900 × 24
>
> ## Software Information:
> - **Firmware Version:** A.N1
> - **OS Name:** Ubuntu 24.04.3 LTS
> - **OS Build:** (null)
> - **OS Type:** 64-bit
> - **GNOME Version:** 46
> - **Windowing System:** X11
> - **Kernel Version:** Linux 6.14.0-114034-tuxedo
>
> tooling
>
> temurin-25.0.1+8.0.LTS
>
> main
>
> Benchmark Mode Cnt Score Error Units
> TextFormatUtilBenchmark.openMetricsWriteToByteArray thrpt 25
489698.973 ± 16399.308 ops/s
> TextFormatUtilBenchmark.openMetricsWriteToNull thrpt 25 507779.365 ±
2619.768 ops/s
> TextFormatUtilBenchmark.prometheusWriteToByteArray thrpt 25 534028.708
± 5998.689 ops/s
> TextFormatUtilBenchmark.prometheusWriteToNull thrpt 25 522323.579 ±
18123.729 ops/s
>
> 1.3.10
> Benchmark Mode Cnt Score Error Units
> TextFormatUtilBenchmark.openMetricsWriteToByteArray thrpt 25
934183.222 ± 10919.023 ops/s
> TextFormatUtilBenchmark.openMetricsWriteToNull thrpt 25 936023.986 ±
10402.193 ops/s
> TextFormatUtilBenchmark.prometheusWriteToByteArray thrpt 25 958813.578
± 17958.230 ops/s
> TextFormatUtilBenchmark.prometheusWriteToNull thrpt 25 965133.616 ±
10907.457 ops/s
> ```
> </comment_new>
></comments>
>
- Fixes #1660
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Performance regression in text-format export on
1.4.0+</issue_title>
> <issue_description>It seems like the adding of support for UTF-8
characters has considerably increased processing time in the
`io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter.writeNameAndLabels`
method due to it now calling
`io.prometheus.metrics.model.snapshots.PrometheusNaming.isValidLegacyMetricName`.
>
> I ran a local test with 1000 metrics exported. Average time per export
on version 1.3.10 was 0.9-1.1 ms, while on version 1.4.1 I got an
average of 2.7-2.9 ms.
>
> I'm not sure which avenues make sense to avoid this regression and I'm
not sure how relevant it is. I just found this while evaluating a move
from the old `simpleclient` to the new `client-java` and that drew my
attention, as the text format export is now slower than in
`simpleclient`.
>
> I have attached the flamegraph I captured on both versions.
>
>
[flamegraph-v141.html](https://github.com/user-attachments/files/23315564/flamegraph-v141.html)
>
[flamegraph-v1310.html](https://github.com/user-attachments/files/23315565/flamegraph-v1310.html)</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> <comment_new><author>@zeitlinger</author><body>
> Thanks for the report
>
> I can confirm that the existing benchmark shows the same - we just
have not integrated that into our release process (yet).
>
> ```
> ## Report details
> - **Date generated:** 2025-11-04 08:16:07
>
> ## Hardware Information:
> - **Hardware Model:** Micro-Star International Co., Ltd. MS-7D76
> - **Memory:** 96.0 GiB
> - **Processor:** AMD Ryzen™ 9 7900 × 24
>
> ## Software Information:
> - **Firmware Version:** A.N1
> - **OS Name:** Ubuntu 24.04.3 LTS
> - **OS Build:** (null)
> - **OS Type:** 64-bit
> - **GNOME Version:** 46
> - **Windowing System:** X11
> - **Kernel Version:** Linux 6.14.0-114034-tuxedo
>
> tooling
>
> temurin-25.0.1+8.0.LTS
>
> main
>
> Benchmark Mode Cnt Score Error Units
> TextFormatUtilBenchmark.openMetricsWriteToByteArray thrpt 25
489698.973 ± 16399.308 ops/s
> TextFormatUtilBenchmark.openMetricsWriteToNull thrpt 25 507779.365 ±
2619.768 ops/s
> TextFormatUtilBenchmark.prometheusWriteToByteArray thrpt 25 534028.708
± 5998.689 ops/s
> TextFormatUtilBenchmark.prometheusWriteToNull thrpt 25 522323.579 ±
18123.729 ops/s
>
> 1.3.10
> Benchmark Mode Cnt Score Error Units
> TextFormatUtilBenchmark.openMetricsWriteToByteArray thrpt 25
934183.222 ± 10919.023 ops/s
> TextFormatUtilBenchmark.openMetricsWriteToNull thrpt 25 936023.986 ±
10402.193 ops/s
> TextFormatUtilBenchmark.prometheusWriteToByteArray thrpt 25 958813.578
± 17958.230 ops/s
> TextFormatUtilBenchmark.prometheusWriteToNull thrpt 25 965133.616 ±
10907.457 ops/s
> ```
> </body></comment_new>
> </comments>
>
</details>
- Fixes #1660
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 We'd love your input! Share your thoughts on Copilot coding agent in
our [2 minute survey](https://gh.io/copilot-coding-agent-survey).
---------
Signed-off-by: Gregor Zeitlinger <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: zeitlinger <[email protected]>
Co-authored-by: Gregor Zeitlinger <[email protected]>1 parent dedbf91 commit c9cfa60
File tree
1 file changed
+57
-16
lines changed- prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots
1 file changed
+57
-16
lines changedprometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/PrometheusNaming.java
Lines changed: 57 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| |||
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | 20 | | |
31 | 21 | | |
32 | 22 | | |
| |||
51 | 41 | | |
52 | 42 | | |
53 | 43 | | |
54 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
55 | 47 | | |
56 | 48 | | |
57 | 49 | | |
| |||
90 | 82 | | |
91 | 83 | | |
92 | 84 | | |
93 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
94 | 108 | | |
95 | 109 | | |
96 | 110 | | |
| |||
106 | 120 | | |
107 | 121 | | |
108 | 122 | | |
109 | | - | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
110 | 142 | | |
111 | 143 | | |
112 | 144 | | |
| |||
129 | 161 | | |
130 | 162 | | |
131 | 163 | | |
132 | | - | |
133 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
134 | 175 | | |
135 | 176 | | |
136 | 177 | | |
| |||
246 | 287 | | |
247 | 288 | | |
248 | 289 | | |
249 | | - | |
| 290 | + | |
250 | 291 | | |
251 | 292 | | |
252 | 293 | | |
| |||
0 commit comments