telemetry: Run callbacks at collect time

Calling collect_metrics() from a script would not invoke metric
callbacks, resulting in most of the process metrics to be zero
when a Zeek process isn't scraped via Prometheus.

Fixes #4309
This commit is contained in:
Arne Welzel 2025-03-26 11:42:52 +01:00
parent 33d7e5a7bf
commit c3c6ee5a2b
4 changed files with 58 additions and 6 deletions

View file

@ -0,0 +1,29 @@
# @TEST-DOC: Calling collect_metrics() invokes callbacks for process (and other) metrics.
# Not compilable to C++ due to globals being initialized to a record that
# has an opaque type as a field.
# @TEST-REQUIRES: test "${ZEEK_USE_CPP}" != "1"
#
# @TEST-EXEC: zeek -r $TRACES/http/get.trace -b %INPUT >out
# @TEST-EXEC: btest-diff out
@load base/frameworks/telemetry
function print_metrics(ms: vector of Telemetry::Metric) {
for (_, m in ms)
print m$opts$name, m$label_values, m$value > 0.0 ? ">0.0, good" : "0.0, bad";
}
event zeek_init()
{
print "zeek_init";
local ms = Telemetry::collect_metrics("process");
print_metrics(ms);
}
event zeek_done()
{
print "zeek_done";
local ms = Telemetry::collect_metrics("process");
print_metrics(ms);
}