41 lines
include/metrics_aggregator.h
Declares MetricsAggregator, EndpointStats, and the record/flush contract.
// MetricsAggregator: accumulates per-endpoint counters for periodic flush.#pragma once#include <cstdint>#include <functional>#include <string>#include <unordered_map>// Per-endpoint counters accumulated within one flush window.struct EndpointStats { uint8_t hitCount; // request hits since last flush uint32_t errorCount; // error responses since last flush};
// Callback type invoked by flush() for each endpoint with non-zero counters.using ReportFn = std::function<void(const std::string&, uint32_t, uint32_t)>;// Aggregates request hits and errors per endpoint.// Not thread-safe; callers must serialise access.class MetricsAggregator {public: // Parameters: // reporter - callback invoked once per endpoint during flush // Returns: new MetricsAggregator with empty counters explicit MetricsAggregator(ReportFn reporter); // Records one request event for the given endpoint. // Parameters: // endpoint - request path to credit // isError - true if the response was an error (4xx/5xx) // Returns: void; counters for endpoint are incremented void record(const std::string& endpoint, bool isError); // Flushes accumulated counters to the reporter and resets them. // Parameters: none // Returns: void; all per-endpoint counters are zero after this call void flush();private: std::unordered_map<std::string, EndpointStats> counters_; // per-endpoint accumulators ReportFn reporter_; // callback used to ship counters to the backend};