feat(web): count HTTP clients by the account they were signed in as

Each address's requests are counted per signed-in account (the dashboard's
session or API key, as the auth middleware found it), so several people
behind one address stay apart, with the account's user name; a WebSocket
upgrade counts under its account.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-29 22:00:55 -05:00
parent 7c37fca9fb
commit 569ac6fb31
4 changed files with 58 additions and 13 deletions

View File

@@ -264,6 +264,8 @@ namespace {
bool fromServer{};
std::string address;
uint64_t requestBytes{};
uint32_t accountId{};
std::string user;
};
std::unordered_map<unsigned long, DeferredTiming> g_DeferredTiming;
@@ -284,11 +286,12 @@ namespace {
std::filesystem::remove(reply.file, ec);
}
// `accountId` and `user`: who the request was signed in as (0 and "" for none), for the Network page's web clients
void CountRequest(const std::string& route, uint16_t status, TrafficClock::time_point started, uint64_t bytes, bool fromServer,
const std::string& address, uint64_t requestBytes) {
const std::string& address, uint64_t requestBytes, uint32_t accountId = 0, const std::string& user = {}) {
const auto micros = std::chrono::duration_cast<std::chrono::microseconds>(TrafficClock::now() - started).count();
TrafficStats::Local().Http(TrafficStats::Now(), route, status, static_cast<uint64_t>(std::max<int64_t>(micros, 0)), bytes, fromServer);
TrafficStats::Local().HttpClient(address, fromServer, requestBytes, bytes);
TrafficStats::Local().HttpClient(address, fromServer, requestBytes, bytes, accountId, user);
}
}
@@ -303,6 +306,9 @@ void HandleHTTPMessage(mg_connection* connection, const mg_http_message* http_ms
const bool fromServer = http_msg && mg_http_get_header(const_cast<mg_http_message*>(http_msg), TrafficStats::SERVER_HEADER) != nullptr;
const auto clientAddress = GetClientIP(connection);
const uint64_t requestBytes = http_msg ? http_msg->message.len : 0;
// Who the request was signed in as, once the middleware has looked (the dashboard's session or API key)
uint32_t signedInAccount = 0;
std::string signedInUser;
if (!http_msg) {
reply.status = eHTTPStatusCode::BAD_REQUEST;
@@ -389,7 +395,7 @@ void HandleHTTPMessage(mg_connection* connection, const mg_http_message* http_ms
}
}
CountRequest("GET /ws", level ? 101 : 401, started, 0, fromServer, clientAddress, requestBytes);
CountRequest("GET /ws", level ? 101 : 401, started, 0, fromServer, clientAddress, requestBytes, level ? level->accountId : 0);
if (level) {
mg_ws_upgrade(connection, const_cast<mg_http_message*>(http_msg), NULL);
g_AuthenticatedWSConnections[connection] = { level->level, level->accountId, connectToken, apiToken,
@@ -497,6 +503,11 @@ void HandleHTTPMessage(mg_connection* connection, const mg_http_message* http_ms
}
}
if (context.isAuthenticated) {
signedInAccount = context.accountId;
signedInUser = context.authenticatedUser;
}
// Call handler only if all middleware passed. A failing handler (e.g. a database error) answers 500
// instead of taking the whole server down.
if (chainPassed) {
@@ -523,14 +534,14 @@ void HandleHTTPMessage(mg_connection* connection, const mg_http_message* http_ms
g_Deferred.SetReplyOptions(connection->id, reply.headers, cc && mg_strcasecmp(*cc, mg_str("close")) == 0);
// Requests the answers never came for (the client left) are forgotten now and then
if (g_DeferredTiming.size() > 10000) g_DeferredTiming.clear();
g_DeferredTiming[connection->id] = { std::move(trafficRoute), started, fromServer, clientAddress, requestBytes };
g_DeferredTiming[connection->id] = { std::move(trafficRoute), started, fromServer, clientAddress, requestBytes, signedInAccount, std::move(signedInUser) };
return;
}
// The handler deferred and then failed: its late answer is dropped
if (g_Deferred.IsPending(connection->id)) g_Deferred.Close(connection->id);
SendReply(connection, reply, http_msg);
CountRequest(trafficRoute, static_cast<uint16_t>(reply.status), started, ReplyBytes(reply), fromServer, clientAddress, requestBytes);
CountRequest(trafficRoute, static_cast<uint16_t>(reply.status), started, ReplyBytes(reply), fromServer, clientAddress, requestBytes, signedInAccount, signedInUser);
RemoveSentFile(reply);
}
@@ -812,7 +823,7 @@ void Web::SendDeferredReplies() {
SendReply(connection, finished.reply, nullptr);
if (const auto timing = g_DeferredTiming.find(finished.connection); timing != g_DeferredTiming.end()) {
CountRequest(timing->second.route, static_cast<uint16_t>(finished.reply.status), timing->second.started, ReplyBytes(finished.reply), timing->second.fromServer,
timing->second.address, timing->second.requestBytes);
timing->second.address, timing->second.requestBytes, timing->second.accountId, timing->second.user);
g_DeferredTiming.erase(timing);
}
RemoveSentFile(finished.reply);