From aa8c13b4e45489c58efd46987998bd21dfb874e7 Mon Sep 17 00:00:00 2001 From: Melissa Kilby Date: Tue, 30 May 2023 17:24:36 +0000 Subject: [PATCH] cleanup(userspace): adjust stats n_drops_perc Signed-off-by: Melissa Kilby --- userspace/falco/stats_writer.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/userspace/falco/stats_writer.cpp b/userspace/falco/stats_writer.cpp index d348580a..00a64c43 100644 --- a/userspace/falco/stats_writer.cpp +++ b/userspace/falco/stats_writer.cpp @@ -319,6 +319,8 @@ void stats_writer::collector::get_metrics_output_fields_additional( /* Cache n_evts and n_drops to derive n_drops_perc. */ uint64_t n_evts = 0; uint64_t n_drops = 0; + uint64_t n_evts_delta = 0; + uint64_t n_drops_delta = 0; for(uint32_t stat = 0; stat < nstats; stat++) { // todo: as we expand scap_stats_v2 prefix may be pushed to scap or we may need to expand @@ -334,10 +336,11 @@ void stats_writer::collector::get_metrics_output_fields_additional( n_evts = stats_v2[stat].value.u64; output_fields[metric_name] = n_evts; output_fields["scap.n_evts_prev"] = m_last_n_evts; - if (m_last_n_evts != 0 && stats_snapshot_time_delta_sec > 0) + n_evts_delta = n_evts - m_last_n_evts; + if (n_evts_delta != 0 && stats_snapshot_time_delta_sec > 0) { /* n_evts is total number of kernel side events. */ - output_fields["scap.evts_rate_sec"] = (double)((n_evts - m_last_n_evts) / stats_snapshot_time_delta_sec); + output_fields["scap.evts_rate_sec"] = (double)(n_evts_delta / stats_snapshot_time_delta_sec); } else { @@ -351,23 +354,16 @@ void stats_writer::collector::get_metrics_output_fields_additional( n_drops = stats_v2[stat].value.u64; output_fields[metric_name] = n_drops; output_fields["scap.n_drops_prev"] = m_last_n_drops; - if (m_last_n_drops != 0 && stats_snapshot_time_delta_sec > 0) + n_drops_delta = n_drops - m_last_n_drops; + if (n_drops_delta != 0 && stats_snapshot_time_delta_sec > 0) { /* n_drops is total number of kernel side event drops. */ - output_fields["scap.evts_drop_rate_sec"] = (double)((n_drops - m_last_n_drops) / stats_snapshot_time_delta_sec); + output_fields["scap.evts_drop_rate_sec"] = (double)(n_drops_delta / stats_snapshot_time_delta_sec); } else { output_fields["scap.evts_drop_rate_sec"] = (double)(0); } - if((n_evts - m_last_n_evts) > 0) - { - output_fields["scap.n_drops_perc"] = (double)((100.0 * (n_drops - m_last_n_drops)) / (n_evts - m_last_n_evts)); - } - else - { - output_fields["scap.n_drops_perc"] = (double)(0); - } m_last_n_drops = n_drops; } if (stats_v2[stat].value.u64 == 0 && !m_writer->m_config->m_metrics_include_empty_values) @@ -380,6 +376,16 @@ void stats_writer::collector::get_metrics_output_fields_additional( break; } } + /* n_drops_perc needs to be calculated outside the loop given no field ordering guarantees. + * Always send n_drops_perc, even if zero. */ + if(n_evts_delta > 0) + { + output_fields["scap.n_drops_perc"] = (double)((100.0 * n_drops_delta) / n_evts_delta); + } + else + { + output_fields["scap.n_drops_perc"] = (double)(0); + } } #endif }