#include "Stats.hpp" #include #include namespace molo::example::fw { namespace { // Upper bounds in microseconds; the last bucket is open-ended. constexpr std::array kBounds = {500, 1000, 5000, 10000, 50000, 100000, 500000}; std::string quote(const std::string& s) { return "\"" + json_escape(s) + "\""; } } // namespace Stats::Stats() { ring_.reserve(stats_caps::kLatencyRing); started_ = now(); } void Stats::set_clock(long (*now_sec)()) { std::lock_guard lock(mu_); clock_ = now_sec; } long Stats::now() const { if (clock_) return clock_(); return (long)std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) .count(); } Stats::Bucket& Stats::bucket_for(long slot) { Bucket& b = buckets_[(std::size_t)(slot % (long)stats_caps::kBuckets)]; if (b.slot != slot) { b = Bucket{}; b.slot = slot; } return b; } void Stats::record_request(const RequestSample& s) { std::lock_guard lock(mu_); const bool failed = s.status >= 400; ++requests_; if (failed) ++errors_; bytes_in_ += (long)s.bytes_in; bytes_out_ += (long)s.bytes_out; micros_ += s.micros; ++by_method_[method_name(s.method)]; ++by_status_[s.status]; std::string key = s.route.empty() ? "(unmatched)" : s.route; if (!by_route_.count(key) && by_route_.size() >= stats_caps::kMaxRoutes) key = "(other)"; RouteStat& r = by_route_[key]; ++r.count; if (failed) ++r.errors; r.micros += s.micros; r.max_micros = std::max(r.max_micros, s.micros); r.bytes_out += (long)s.bytes_out; std::size_t h = 0; while (h < kBounds.size() && s.micros >= kBounds[h]) ++h; ++histogram_[h]; if (ring_.size() < stats_caps::kLatencyRing) { ring_.push_back(s.micros); } else { ring_[ring_next_] = s.micros; ring_next_ = (ring_next_ + 1) % stats_caps::kLatencyRing; } Bucket& b = bucket_for(now() / stats_caps::kBucketSec); ++b.requests; if (failed) ++b.errors; b.bytes_in += (long)s.bytes_in; b.bytes_out += (long)s.bytes_out; b.micros += s.micros; } void Stats::connection_opened() { std::lock_guard lock(mu_); ++conn_total_; ++conn_active_; conn_peak_ = std::max(conn_peak_, conn_active_); ++bucket_for(now() / stats_caps::kBucketSec).connections; } void Stats::connection_closed(ConnEnd how, std::size_t in, std::size_t out) { std::lock_guard lock(mu_); if (conn_active_ > 0) --conn_active_; if (how == ConnEnd::Timeout) ++conn_timeout_; if (how == ConnEnd::Malformed) ++conn_malformed_; conn_bytes_in_ += (long)in; conn_bytes_out_ += (long)out; } void Stats::connection_rejected() { std::lock_guard lock(mu_); ++conn_rejected_; } void Stats::set_connection_cap(long cap) { std::lock_guard lock(mu_); conn_cap_ = cap; } long Stats::total_requests() const { std::lock_guard lock(mu_); return requests_; } long Stats::total_connections() const { std::lock_guard lock(mu_); return conn_total_; } long Stats::active_connections() const { std::lock_guard lock(mu_); return conn_active_; } std::string Stats::to_json() const { std::lock_guard lock(mu_); const long t = now(); auto num = [](long v) { return std::to_string(v); }; auto dbl = [](double v) { char buf[48]; std::snprintf(buf, sizeof(buf), "%.3f", v); return std::string(buf); }; std::string out = "{"; out += "\"generated_at\":" + num(t) + ",\"uptime_sec\":" + num(t - started_); out += ",\"totals\":{\"requests\":" + num(requests_) + ",\"errors\":" + num(errors_) + ",\"bytes_in\":" + num(bytes_in_) + ",\"bytes_out\":" + num(bytes_out_) + ",\"avg_ms\":" + dbl(requests_ ? micros_ / 1000.0 / (double)requests_ : 0.0) + "}"; out += ",\"by_method\":{"; bool first = true; for (const auto& [k, v] : by_method_) { out += (first ? "" : ",") + quote(k) + ":" + num(v); first = false; } out += "},\"by_status\":{"; first = true; for (const auto& [k, v] : by_status_) { out += (first ? "" : ",") + quote(std::to_string(k)) + ":" + num(v); first = false; } out += "},\"by_route\":["; std::vector> routes(by_route_.begin(), by_route_.end()); std::sort(routes.begin(), routes.end(), [](const auto& a, const auto& b) { return a.second.count > b.second.count; }); first = true; for (const auto& [k, r] : routes) { out += std::string(first ? "" : ",") + "{\"route\":" + quote(k) + ",\"count\":" + num(r.count) + ",\"errors\":" + num(r.errors) + ",\"avg_ms\":" + dbl(r.micros / 1000.0 / (double)r.count) + ",\"max_ms\":" + dbl(r.max_micros / 1000.0) + ",\"bytes_out\":" + num(r.bytes_out) + "}"; first = false; } out += "]"; static const char* kLabels[8] = {"<0.5ms", "<1ms", "<5ms", "<10ms", "<50ms", "<100ms", "<500ms", ">=500ms"}; out += ",\"latency\":{\"histogram\":["; for (std::size_t i = 0; i < histogram_.size(); ++i) out += std::string(i ? "," : "") + "{\"bucket\":" + quote(kLabels[i]) + ",\"count\":" + num(histogram_[i]) + "}"; std::vector sorted = ring_; std::sort(sorted.begin(), sorted.end()); auto pct = [&](double p) { if (sorted.empty()) return 0.0; std::size_t idx = (std::size_t)(p * (double)(sorted.size() - 1)); return sorted[idx] / 1000.0; }; out += "],\"p50_ms\":" + dbl(pct(0.50)) + ",\"p95_ms\":" + dbl(pct(0.95)) + ",\"p99_ms\":" + dbl(pct(0.99)) + ",\"samples\":" + num((long)sorted.size()) + "}"; out += ",\"connections\":{\"total\":" + num(conn_total_) + ",\"active\":" + num(conn_active_) + ",\"peak\":" + num(conn_peak_) + ",\"timeouts\":" + num(conn_timeout_) + ",\"malformed\":" + num(conn_malformed_) + ",\"rejected\":" + num(conn_rejected_) + ",\"cap\":" + num(conn_cap_) + ",\"bytes_in\":" + num(conn_bytes_in_) + ",\"bytes_out\":" + num(conn_bytes_out_) + "}"; // Oldest first, one entry per bucket slot including empty ones. const long cur = t / stats_caps::kBucketSec; const long n = (long)stats_caps::kBuckets; out += ",\"series\":{\"bucket_sec\":" + num(stats_caps::kBucketSec) + ",\"points\":["; for (long i = n - 1; i >= 0; --i) { const long slot = cur - i; const Bucket& b = buckets_[(std::size_t)(slot % n)]; const bool live = b.slot == slot; const long reqs = live ? b.requests : 0; out += std::string(i == n - 1 ? "" : ",") + "{\"t\":" + num(slot * stats_caps::kBucketSec) + ",\"requests\":" + num(reqs) + ",\"errors\":" + num(live ? b.errors : 0) + ",\"bytes_in\":" + num(live ? b.bytes_in : 0) + ",\"bytes_out\":" + num(live ? b.bytes_out : 0) + ",\"avg_ms\":" + dbl(reqs ? b.micros / 1000.0 / (double)reqs : 0.0) + ",\"connections\":" + num(live ? b.connections : 0) + "}"; } out += "]}}"; return out; } } // namespace molo::example::fw