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 <nierro92@gmail.com>
Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
This commit is contained in:
Melissa Kilby 2025-05-21 15:12:50 +00:00 committed by poiana
parent 22d40e2a65
commit 309ccf65d3
2 changed files with 31 additions and 37 deletions

View File

@ -233,7 +233,6 @@ void stats_writer::worker() noexcept {
bool use_file = !m_config->m_metrics_output_file.empty(); bool use_file = !m_config->m_metrics_output_file.empty();
auto tick = stats_writer::get_ticker(); auto tick = stats_writer::get_ticker();
auto last_tick = tick; auto last_tick = tick;
auto first_tick = tick;
while(true) { while(true) {
// blocks until a message becomes availables // blocks until a message becomes availables
@ -244,9 +243,8 @@ void stats_writer::worker() noexcept {
return; return;
} }
// this helps waiting for the first tick
tick = stats_writer::get_ticker(); tick = stats_writer::get_ticker();
if(m_first_run || first_tick != tick) {
if(last_tick != tick) { if(last_tick != tick) {
m_total_samples++; m_total_samples++;
} }
@ -274,8 +272,6 @@ void stats_writer::worker() noexcept {
"stats_writer (worker): " + std::string(e.what()) + "\n"); "stats_writer (worker): " + std::string(e.what()) + "\n");
} }
} }
m_first_run = false;
}
} }
stats_writer::collector::collector(const std::shared_ptr<stats_writer>& writer): m_writer(writer) {} stats_writer::collector::collector(const std::shared_ptr<stats_writer>& writer): m_writer(writer) {}
@ -640,7 +636,7 @@ void stats_writer::collector::collect(const std::shared_ptr<sinsp>& inspector,
#endif #endif
/* Collect stats / metrics once per ticker period. */ /* Collect stats / metrics once per ticker period. */
auto tick = stats_writer::get_ticker(); auto tick = stats_writer::get_ticker();
if(m_first_run || tick != m_last_tick) { if(tick != m_last_tick) {
m_last_tick = tick; m_last_tick = tick;
auto now = std::chrono::duration_cast<std::chrono::nanoseconds>( auto now = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()) std::chrono::system_clock::now().time_since_epoch())
@ -661,10 +657,8 @@ void stats_writer::collector::collect(const std::shared_ptr<sinsp>& inspector,
num_evts, num_evts,
now, now,
stats_snapshot_time_delta_sec); stats_snapshot_time_delta_sec);
if(!m_first_run) {
get_metrics_output_fields_additional(output_fields, stats_snapshot_time_delta_sec); get_metrics_output_fields_additional(output_fields, stats_snapshot_time_delta_sec);
}
m_first_run = false;
/* Send message in the queue */ /* Send message in the queue */
stats_writer::msg msg; stats_writer::msg msg;

View File

@ -82,8 +82,9 @@ public:
double stats_snapshot_time_delta_sec); double stats_snapshot_time_delta_sec);
std::shared_ptr<stats_writer> m_writer; std::shared_ptr<stats_writer> m_writer;
bool m_first_run = true; // Init m_last_tick w/ invalid value to enable metrics logging immediately after
stats_writer::ticker_t m_last_tick = 0; // startup/reload
stats_writer::ticker_t m_last_tick = -1;
uint64_t m_last_now = 0; uint64_t m_last_now = 0;
uint64_t m_last_n_evts = 0; uint64_t m_last_n_evts = 0;
uint64_t m_last_n_drops = 0; uint64_t m_last_n_drops = 0;
@ -145,7 +146,6 @@ private:
inline void push(const stats_writer::msg& m); inline void push(const stats_writer::msg& m);
bool m_initialized = false; bool m_initialized = false;
bool m_first_run = true;
uint64_t m_total_samples = 0; uint64_t m_total_samples = 0;
std::thread m_worker; std::thread m_worker;
std::ofstream m_file_output; std::ofstream m_file_output;