Commit c2850f6
perf(metrics): prevent multi-second engine stalls from scrape hooks (#189)
* metrics: gate heavy scrape hooks behind RETH_DISABLE_HEAVY_METRICS
The two custom hooks registered by `metrics_hooks` walk MDBX page
metadata + the freelist DB and iterate every static-file jar header on
every Prometheus scrape. On large databases this is expensive enough to
stall the metrics endpoint and starve the runtime.
Skip registering both hooks when `RETH_DISABLE_HEAVY_METRICS` is set in
the environment; the rest of the registry (process, jemalloc, io, chain
spec, version) is unaffected and the endpoint still responds normally.
The env var is documented on the function so the escape hatch is
discoverable without grepping the source.
* metrics: run scrape handler and push-gateway render on spawn_blocking
The Prometheus metrics handler is fundamentally synchronous: it
invokes every registered hook and then runs the prometheus exporter's
`render()`, all on the tokio worker that accepted the HTTP request
(or on the runtime worker driving the push-gateway loop).
The default hooks are cheap (procfs, jemalloc atomic reads), but the
two `report_metrics` hooks (DB stat walk, static-file jar enumeration)
can take seconds on large archives. Even with those gated out (see
preceding patch), `render()` itself is O(total time-series) and will
grow over time. A multi-millisecond synchronous block on a runtime
worker is not ideal and can become a real engine latency source if
hook cost ever regresses.
Move the synchronous work off the runtime worker:
- Endpoint handler now offloads `handle_request` (which calls the hook
+ render or the pprof dump) to `spawn_blocking`. On join error,
return a 500 instead of letting the connection task panic.
- Push-gateway loop offloads the hook + render to `spawn_blocking`;
on join error, log a warning and skip this tick rather than killing
the loop. The HTTP put itself was already async so it stays inline.
No behavioral change to what the endpoint or push-gateway returns;
only the thread on which the rendering happens.1 parent 3baee78 commit c2850f6
2 files changed
Lines changed: 116 additions & 100 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1584 | 1584 | | |
1585 | 1585 | | |
1586 | 1586 | | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
| 1590 | + | |
| 1591 | + | |
1587 | 1592 | | |
1588 | | - | |
1589 | | - | |
1590 | | - | |
1591 | | - | |
1592 | | - | |
1593 | | - | |
1594 | | - | |
1595 | | - | |
1596 | | - | |
1597 | | - | |
1598 | | - | |
1599 | | - | |
1600 | | - | |
1601 | | - | |
1602 | | - | |
1603 | | - | |
1604 | | - | |
1605 | | - | |
1606 | | - | |
1607 | | - | |
| 1593 | + | |
| 1594 | + | |
| 1595 | + | |
| 1596 | + | |
| 1597 | + | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
| 1609 | + | |
| 1610 | + | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
1608 | 1617 | | |
1609 | 1618 | | |
1610 | 1619 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
144 | 148 | | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
159 | 185 | | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
| 186 | + | |
168 | 187 | | |
169 | 188 | | |
170 | 189 | | |
| |||
181 | 200 | | |
182 | 201 | | |
183 | 202 | | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
203 | 238 | | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | 239 | | |
208 | 240 | | |
209 | 241 | | |
210 | 242 | | |
211 | | - | |
| 243 | + | |
212 | 244 | | |
213 | 245 | | |
214 | 246 | | |
| |||
314 | 346 | | |
315 | 347 | | |
316 | 348 | | |
317 | | - | |
| 349 | + | |
318 | 350 | | |
319 | 351 | | |
320 | 352 | | |
321 | 353 | | |
322 | 354 | | |
323 | 355 | | |
324 | 356 | | |
325 | | - | |
326 | 357 | | |
327 | 358 | | |
328 | 359 | | |
| |||
412 | 443 | | |
413 | 444 | | |
414 | 445 | | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | 446 | | |
440 | 447 | | |
441 | 448 | | |
| |||
0 commit comments