From e6e1f3a42aace8815f79cc88668c982d9b73e9b5 Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Tue, 8 Apr 2025 18:09:00 +0000 Subject: [PATCH] feat(WebApi_prometheus.cpp): dynamically adjust the initial response buffer size the current implementation allocates a 40kB buffer right from the start. In most cases this is way too big. This patch dynamically learns the right required size for the local setup and preallocates just as much space as needed. Signed-off-by: Peter Lieven --- src/WebApi_prometheus.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/WebApi_prometheus.cpp b/src/WebApi_prometheus.cpp index 9f9c3eba..198ecaec 100644 --- a/src/WebApi_prometheus.cpp +++ b/src/WebApi_prometheus.cpp @@ -20,12 +20,14 @@ void WebApiPrometheusClass::init(AsyncWebServer& server, Scheduler& scheduler) void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* request) { + static size_t initialResponseBufferSize = 1024; + if (!WebApi.checkCredentialsReadonly(request)) { return; } try { - auto stream = request->beginResponseStream("text/plain; charset=utf-8", 40960); + auto stream = request->beginResponseStream("text/plain; charset=utf-8", initialResponseBufferSize); stream->print("# HELP opendtu_build Build info\n"); stream->print("# TYPE opendtu_build gauge\n"); @@ -109,6 +111,10 @@ void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* reques } } stream->addHeader("Cache-Control", "no-cache"); + if (stream->available() > initialResponseBufferSize) { + initialResponseBufferSize = stream->available(); + MessageOutput.printf("Increased /api/prometheus/metrics initialResponseBufferSize to %" PRIu32 " bytes\r\n", initialResponseBufferSize); + } request->send(stream); } catch (std::bad_alloc& bad_alloc) {