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 <pl@dlhnet.de>
This commit is contained in:
Peter Lieven
2025-04-08 18:09:00 +00:00
parent e8a2d09906
commit e6e1f3a42a

View File

@@ -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) {