Integrate render into /metrics handler

This commit is contained in:
2025-08-30 18:28:44 -04:00
parent ff7642195b
commit 4f72840e51
2 changed files with 60 additions and 9 deletions

View File

@@ -1,10 +1,19 @@
#include "http_handler.hpp"
#include "arena_allocator.hpp"
#include "perfetto_categories.hpp"
#include <cstring>
#include <string>
#include <strings.h>
#include "arena_allocator.hpp"
#include "format.hpp"
#include "metric.hpp"
#include "perfetto_categories.hpp"
auto requests_counter_family = metric::create_counter(
"weaseldb_http_requests_total", "Total http requests");
thread_local auto metrics_counter =
requests_counter_family.create({{"path", "/metrics"}});
// HttpConnectionState implementation
HttpConnectionState::HttpConnectionState(ArenaAllocator &arena)
: current_header_field_buf(ArenaStlAllocator<char>(&arena)),
@@ -227,10 +236,44 @@ void HttpHandler::handleDeleteRetention(Connection &conn,
void HttpHandler::handleGetMetrics(Connection &conn,
const HttpConnectionState &state) {
// TODO: Implement metrics collection and formatting
sendResponse(conn, 200, "text/plain",
"# WeaselDB metrics\nweaseldb_requests_total 0\n",
state.connection_close);
metrics_counter.inc();
ArenaAllocator &arena = conn.get_arena();
auto metrics_span = metric::render(arena);
// Calculate total size for the response body
size_t total_size = 0;
for (const auto &sv : metrics_span) {
total_size += sv.size();
}
auto *http_state = static_cast<HttpConnectionState *>(conn.user_data);
// Build HTTP response headers using arena
std::string_view headers;
if (state.connection_close) {
headers = static_format(
arena, "HTTP/1.1 200 OK\r\n",
"Content-Type: text/plain; version=0.0.4\r\n",
"Content-Length: ", static_cast<uint64_t>(total_size), "\r\n",
"X-Response-ID: ", static_cast<int64_t>(http_state->request_id), "\r\n",
"Connection: close\r\n", "\r\n");
conn.close_after_send();
} else {
headers = static_format(
arena, "HTTP/1.1 200 OK\r\n",
"Content-Type: text/plain; version=0.0.4\r\n",
"Content-Length: ", static_cast<uint64_t>(total_size), "\r\n",
"X-Response-ID: ", static_cast<int64_t>(http_state->request_id), "\r\n",
"Connection: keep-alive\r\n", "\r\n");
}
// Send headers
conn.append_message(headers, false);
// Send body in chunks
for (const auto &sv : metrics_span) {
conn.append_message(sv, false);
}
}
void HttpHandler::handleGetOk(Connection &conn,