diff --git a/src/Timer.cc b/src/Timer.cc index ef2763071a..191d9c9e6e 100644 --- a/src/Timer.cc +++ b/src/Timer.cc @@ -10,6 +10,7 @@ #include "zeek/broker/Manager.h" #include "zeek/iosource/Manager.h" #include "zeek/iosource/PktSrc.h" +#include "zeek/telemetry/Manager.h" #include "zeek/util.h" namespace zeek::detail { @@ -97,6 +98,34 @@ void TimerMgr::InitPostScript() { iosource_mgr->Register(this, true); dispatch_all_expired = zeek::detail::max_timer_expires == 0; + + cumulative_num_metric = telemetry_mgr->CounterInstance("zeek", "timers", {}, "Cumulative number of timers", "", + []() -> prometheus::ClientMetric { + prometheus::ClientMetric metric; + metric.counter.value = + static_cast(timer_mgr->CumulativeNum()); + return metric; + }); + + lag_time_metric = telemetry_mgr->GaugeInstance("zeek", "timers_lag_time", {}, + "Lag between current network time and last expired timer", "seconds", + []() -> prometheus::ClientMetric { + prometheus::ClientMetric metric; + metric.gauge.value = + run_state::network_time - timer_mgr->last_timestamp; + return metric; + }); + + std::shared_ptr family = + telemetry_mgr->GaugeFamily("zeek", "timers_pending", {"type"}, "Number of timers for a certain type"); + for ( int i = 0; i < NUM_TIMER_TYPES; i++ ) { + current_timer_metrics[i] = family->GetOrAdd({{"type", timer_type_to_string(static_cast(i))}}, + [i]() -> prometheus::ClientMetric { + prometheus::ClientMetric metric; + metric.gauge.value = TimerMgr::CurrentTimers()[i]; + return metric; + }); + } } void TimerMgr::Add(Timer* timer) { diff --git a/src/Timer.h b/src/Timer.h index ff535f64e1..45ba50314e 100644 --- a/src/Timer.h +++ b/src/Timer.h @@ -10,7 +10,14 @@ namespace zeek { class ODesc; -} + +namespace telemetry { +class Gauge; +class Counter; +using GaugePtr = std::shared_ptr; +using CounterPtr = std::shared_ptr; +} // namespace telemetry +} // namespace zeek namespace zeek::detail { @@ -153,10 +160,12 @@ private: // for the max_timer_expires=0 case. bool dispatch_all_expired = false; - size_t peak_size = 0; - size_t cumulative_num = 0; - static unsigned int current_timers[NUM_TIMER_TYPES]; + + telemetry::CounterPtr cumulative_num_metric; + telemetry::GaugePtr lag_time_metric; + telemetry::GaugePtr current_timer_metrics[NUM_TIMER_TYPES]; + std::unique_ptr q; };