From 8e2c307bdb501db0ab0ce2d51bc61b552855ee11 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 23 Mar 2026 12:54:14 +0100 Subject: Unique session/client tracking using HyperLogLog (#884) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Adds probabilistic cardinality estimation for tracking unique HTTP clients and sessions using a HyperLogLog implementation. - Add a `HyperLogLog` template in `zentelemetry` with thread-safe lock-free register updates, merge support, and XXH3 hashing - Feed client IP addresses (via raw bytes) and session IDs (via `Oid` bytes) into their respective HyperLogLog estimators from both the ASIO and http.sys server backends - Emit `distinct_clients` and `distinct_sessions` cardinality estimates in HTTP `CollectStats()` - Add tests covering empty, single, duplicates, accuracy, merge, and clear scenarios ## Why HyperLogLog Tracking exact unique counts would require storing every observed IP or session ID. HyperLogLog provides a memory-bounded probabilistic estimate (~1–2% error) using only a few KB of memory regardless of traffic volume. --- src/zenhttp/servers/httpsys.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/zenhttp/servers/httpsys.cpp') diff --git a/src/zenhttp/servers/httpsys.cpp b/src/zenhttp/servers/httpsys.cpp index 9fe9a2254..2cad97725 100644 --- a/src/zenhttp/servers/httpsys.cpp +++ b/src/zenhttp/servers/httpsys.cpp @@ -2020,6 +2020,23 @@ HttpSysTransaction::InvokeRequestHandler(HttpService& Service, IoBuffer Payload) m_HttpServer.MarkRequest(); + // Track distinct client addresses + { + const SOCKADDR* SockAddr = HttpRequest()->Address.pRemoteAddress; + if (SockAddr->sa_family == AF_INET) + { + const SOCKADDR_IN* V4 = reinterpret_cast(SockAddr); + m_HttpServer.MarkClientAddress(&V4->sin_addr, sizeof(V4->sin_addr)); + } + else if (SockAddr->sa_family == AF_INET6) + { + const SOCKADDR_IN6* V6 = reinterpret_cast(SockAddr); + m_HttpServer.MarkClientAddress(&V6->sin6_addr, sizeof(V6->sin6_addr)); + } + } + + m_HttpServer.MarkSessionId(ThisRequest.SessionId()); + // Default request handling # if ZEN_WITH_OTEL -- cgit v1.2.3