u
This commit is contained in:
@@ -83,11 +83,12 @@ static void handle_request(tls_session* sess) {
|
||||
size_t resp_size = resp_body.size();
|
||||
|
||||
const bool is_init = (req.path == "/v1/init");
|
||||
const bool log_request = !is_init
|
||||
|| router.should_log_init(url, resp.status_code, static_cast<int>(elapsed), resp_body);
|
||||
|
||||
if (!log_request) {
|
||||
return;
|
||||
if (is_init) {
|
||||
if (!router.should_log_init(url)) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
router.flush_suppressed_init_logs();
|
||||
}
|
||||
|
||||
// Console log with response size and truncated body
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#include "router.hpp"
|
||||
#include "log.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <algorithm>
|
||||
#include <exception>
|
||||
|
||||
void Router::add_route(const std::string& method, const std::string& path, Handler handler) {
|
||||
@@ -52,32 +50,20 @@ void Router::dispatch(tls_session* sess, PairingStore& pairing, const json& conf
|
||||
});
|
||||
}
|
||||
|
||||
// Init suppression: log only if non-200, slow, or first/different request
|
||||
// within 30s window. Port of server.js shouldLogInit().
|
||||
bool Router::should_log_init(const std::string& url, int status, int duration_ms,
|
||||
const std::string& response) {
|
||||
// Report suppressed count hourly
|
||||
auto now = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
|
||||
if (suppressed_count_ > 0 && (now - suppressed_report_ts_) > 3'600'000) {
|
||||
logc::info("Suppressed %d init log(s) in the last hour", suppressed_count_);
|
||||
suppressed_count_ = 0;
|
||||
suppressed_report_ts_ = now;
|
||||
}
|
||||
|
||||
if (status != 200) return true;
|
||||
if (duration_ms > 400) return true;
|
||||
|
||||
int64_t time_since_last = now - last_init_timestamp_;
|
||||
if (time_since_last < 30'000 && url == last_init_url_ && response == last_init_result_) {
|
||||
last_init_timestamp_ = now;
|
||||
suppressed_count_++;
|
||||
// Suppress consecutive identical /v1/init URLs; report count on non-init logs.
|
||||
bool Router::should_log_init(const std::string& url) {
|
||||
if (last_logged_init_url_ == url) {
|
||||
suppressed_init_count_++;
|
||||
return false;
|
||||
}
|
||||
|
||||
last_init_url_ = url;
|
||||
last_init_result_ = response;
|
||||
last_init_timestamp_ = now;
|
||||
last_logged_init_url_ = url;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Router::flush_suppressed_init_logs() {
|
||||
if (suppressed_init_count_ > 0) {
|
||||
logc::info("Suppressed %d duplicate init log(s)", suppressed_init_count_);
|
||||
suppressed_init_count_ = 0;
|
||||
}
|
||||
last_logged_init_url_.clear();
|
||||
}
|
||||
|
||||
@@ -19,17 +19,16 @@ public:
|
||||
// Dispatch a request. Called from the TLS on_request callback.
|
||||
void dispatch(tls_session* sess, PairingStore& pairing, const json& config);
|
||||
|
||||
// Suppress init logs for repeated identical requests within 30s
|
||||
bool should_log_init(const std::string& url, int status, int duration_ms,
|
||||
const std::string& response);
|
||||
// Suppress consecutive identical init URLs; return false if suppressed.
|
||||
bool should_log_init(const std::string& url);
|
||||
|
||||
// Print suppressed init count (if any) before a non-init log line.
|
||||
void flush_suppressed_init_logs();
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, Handler> routes_;
|
||||
|
||||
// Init suppression state
|
||||
std::string last_init_url_;
|
||||
std::string last_init_result_;
|
||||
int64_t last_init_timestamp_ = 0;
|
||||
int suppressed_count_ = 0;
|
||||
int64_t suppressed_report_ts_ = 0;
|
||||
std::string last_logged_init_url_;
|
||||
int suppressed_init_count_ = 0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user