update(metrics): introduce immediate initial metrics msg (output_rule or output_file) upon start/restart/hot_reload

Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
This commit is contained in:
Melissa Kilby 2025-05-18 16:31:03 +00:00 committed by Federico Di Pierro
parent c5c51eb14d
commit 0123af47e7
2 changed files with 9 additions and 3 deletions

View File

@ -246,7 +246,7 @@ void stats_writer::worker() noexcept {
// this helps waiting for the first tick // this helps waiting for the first tick
tick = stats_writer::get_ticker(); tick = stats_writer::get_ticker();
if(first_tick != tick) { if(m_first_run || first_tick != tick) {
if(last_tick != tick) { if(last_tick != tick) {
m_total_samples++; m_total_samples++;
} }
@ -274,6 +274,7 @@ 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;
} }
} }
@ -638,7 +639,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(tick != m_last_tick) { if(m_first_run || 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())
@ -659,7 +660,10 @@ 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);
get_metrics_output_fields_additional(output_fields, 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;
/* Send message in the queue */ /* Send message in the queue */
stats_writer::msg msg; stats_writer::msg msg;

View File

@ -82,6 +82,7 @@ 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;
stats_writer::ticker_t m_last_tick = 0; stats_writer::ticker_t m_last_tick = 0;
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;
@ -144,6 +145,7 @@ 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;