Perfetto tracing for /ok. Header parsing not complete

This commit is contained in:
2025-08-20 12:19:20 -04:00
parent 972d87b12e
commit cf692e71ef
6 changed files with 53 additions and 24 deletions

View File

@@ -1,5 +1,6 @@
#include "http_handler.hpp"
#include "arena_allocator.hpp"
#include "perfetto_categories.hpp"
#include <cstring>
#include <string>
#include <strings.h>
@@ -213,6 +214,8 @@ void HttpHandler::handleGetMetrics(Connection &conn,
void HttpHandler::handleGetOk(Connection &conn,
const HttpConnectionState &state) {
TRACE_EVENT("http", "GET /ok", perfetto::Flow::Global(state.request_id));
sendResponse(conn, 200, "text/plain", "OK", state.connection_close);
}
@@ -254,6 +257,8 @@ void HttpHandler::sendResponse(Connection &conn, int status_code,
break;
}
auto *state = static_cast<HttpConnectionState *>(conn.user_data);
response += "\r\n";
response += "Content-Type: ";
response += content_type;
@@ -261,6 +266,9 @@ void HttpHandler::sendResponse(Connection &conn, int status_code,
response += "Content-Length: ";
response += std::to_string(body.size());
response += "\r\n";
response += "X-Response-ID: ";
response += std::to_string(state->request_id);
response += "\r\n";
if (close_connection) {
response += "Connection: close\r\n";
@@ -322,6 +330,20 @@ int HttpHandler::onHeaderValue(llhttp_t *parser, const char *at,
}
}
// Check for X-Request-Id header
if (state->current_header_field.size() == 12 &&
strncasecmp(state->current_header_field.data(), "x-request-id", 12) ==
0) {
uint64_t id = 0;
for (int i = 0; i < int(length); ++i) {
auto c = at[i];
if (c >= '0' && c <= '9') {
id = id * 10 + (c - '0');
}
}
state->request_id = id;
}
return 0;
}
@@ -348,4 +370,4 @@ int HttpHandler::onMessageComplete(llhttp_t *parser) {
auto *state = static_cast<HttpConnectionState *>(parser->data);
state->message_complete = true;
return 0;
}
}

View File

@@ -42,6 +42,7 @@ struct HttpConnectionState {
bool connection_close = false; // Client requested connection close
HttpRoute route = HttpRoute::NotFound;
std::string_view current_header_field; // Current header being parsed
uint64_t request_id = 0; // X-Request-Id header value
explicit HttpConnectionState(ArenaAllocator &arena);
};
@@ -95,4 +96,4 @@ private:
static void sendErrorResponse(Connection &conn, int status_code,
std::string_view message,
bool close_connection = false);
};
};

View File

@@ -2,11 +2,14 @@
#include "connection.hpp"
#include "connection_handler.hpp"
#include "http_handler.hpp"
#include "perfetto_categories.hpp"
#include "server.hpp"
#include <atomic>
#include <csignal>
#include <iostream>
PERFETTO_TRACK_EVENT_STATIC_STORAGE();
// TODO this should be scoped to a particular Server, and it's definition should
// be in server.cpp or connection.cpp
std::atomic<int> activeConnections{0};
@@ -41,6 +44,13 @@ void print_help(const char *program_name) {
}
int main(int argc, char *argv[]) {
#if ENABLE_PERFETTO
perfetto::TracingInitArgs args;
args.backends |= perfetto::kSystemBackend;
perfetto::Tracing::Initialize(args);
perfetto::TrackEvent::Register();
#endif
std::string config_file = "config.toml";
// Parse command line arguments