From 309ccf65d398f90523c0eebf90a37e3de08a10bb Mon Sep 17 00:00:00 2001 From: Melissa Kilby Date: Wed, 21 May 2025 15:12:50 +0000 Subject: [PATCH] cleanup(metrics): simplify logic around immediate metrics logging after start/reload * For consistency don't make first run metrics log special * Remove firt tick variable altogether to enable metrics logging immediately after startup/reload Co-authored-by: Federico Di Pierro Signed-off-by: Melissa Kilby --- userspace/falco/stats_writer.cpp | 62 +++++++++++++++----------------- userspace/falco/stats_writer.h | 6 ++-- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/userspace/falco/stats_writer.cpp b/userspace/falco/stats_writer.cpp index ec777ae9..ac601b25 100644 --- a/userspace/falco/stats_writer.cpp +++ b/userspace/falco/stats_writer.cpp @@ -233,7 +233,6 @@ void stats_writer::worker() noexcept { bool use_file = !m_config->m_metrics_output_file.empty(); auto tick = stats_writer::get_ticker(); auto last_tick = tick; - auto first_tick = tick; while(true) { // blocks until a message becomes availables @@ -244,37 +243,34 @@ void stats_writer::worker() noexcept { return; } - // this helps waiting for the first tick tick = stats_writer::get_ticker(); - if(m_first_run || first_tick != tick) { - if(last_tick != tick) { - m_total_samples++; - } - last_tick = tick; - try { - if(use_outputs) { - std::string rule = "Falco internal: metrics snapshot"; - std::string msg = "Falco metrics snapshot"; - m_outputs->handle_msg(m.ts, - falco_common::PRIORITY_INFORMATIONAL, - msg, - rule, - m.output_fields); - } - - if(use_file) { - nlohmann::json jmsg; - jmsg["sample"] = m_total_samples; - jmsg["output_fields"] = m.output_fields; - m_file_output << jmsg.dump() << std::endl; - } - } catch(const std::exception& e) { - falco_logger::log(falco_logger::level::ERR, - "stats_writer (worker): " + std::string(e.what()) + "\n"); - } + if(last_tick != tick) { + m_total_samples++; + } + last_tick = tick; + + try { + if(use_outputs) { + std::string rule = "Falco internal: metrics snapshot"; + std::string msg = "Falco metrics snapshot"; + m_outputs->handle_msg(m.ts, + falco_common::PRIORITY_INFORMATIONAL, + msg, + rule, + m.output_fields); + } + + if(use_file) { + nlohmann::json jmsg; + jmsg["sample"] = m_total_samples; + jmsg["output_fields"] = m.output_fields; + m_file_output << jmsg.dump() << std::endl; + } + } catch(const std::exception& e) { + falco_logger::log(falco_logger::level::ERR, + "stats_writer (worker): " + std::string(e.what()) + "\n"); } - m_first_run = false; } } @@ -640,7 +636,7 @@ void stats_writer::collector::collect(const std::shared_ptr& inspector, #endif /* Collect stats / metrics once per ticker period. */ auto tick = stats_writer::get_ticker(); - if(m_first_run || tick != m_last_tick) { + if(tick != m_last_tick) { m_last_tick = tick; auto now = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) @@ -661,10 +657,8 @@ void stats_writer::collector::collect(const std::shared_ptr& inspector, num_evts, now, stats_snapshot_time_delta_sec); - if(!m_first_run) { - get_metrics_output_fields_additional(output_fields, stats_snapshot_time_delta_sec); - } - m_first_run = false; + + get_metrics_output_fields_additional(output_fields, stats_snapshot_time_delta_sec); /* Send message in the queue */ stats_writer::msg msg; diff --git a/userspace/falco/stats_writer.h b/userspace/falco/stats_writer.h index 53c7f45b..54a43e62 100644 --- a/userspace/falco/stats_writer.h +++ b/userspace/falco/stats_writer.h @@ -82,8 +82,9 @@ public: double stats_snapshot_time_delta_sec); std::shared_ptr m_writer; - bool m_first_run = true; - stats_writer::ticker_t m_last_tick = 0; + // Init m_last_tick w/ invalid value to enable metrics logging immediately after + // startup/reload + stats_writer::ticker_t m_last_tick = -1; uint64_t m_last_now = 0; uint64_t m_last_n_evts = 0; uint64_t m_last_n_drops = 0; @@ -145,7 +146,6 @@ private: inline void push(const stats_writer::msg& m); bool m_initialized = false; - bool m_first_run = true; uint64_t m_total_samples = 0; std::thread m_worker; std::ofstream m_file_output;