diff --git a/scripts/base/frameworks/telemetry/main.zeek b/scripts/base/frameworks/telemetry/main.zeek index 6f335c8d4a..59e480a125 100644 --- a/scripts/base/frameworks/telemetry/main.zeek +++ b/scripts/base/frameworks/telemetry/main.zeek @@ -297,8 +297,7 @@ function register_counter_family(opts: MetricOpts): CounterFamily opts$name, opts$labels, opts$help_text, - opts$unit, - opts?$is_total ? opts$is_total : T + opts$unit ); return CounterFamily($__family=f, $__labels=opts$labels); } @@ -357,8 +356,7 @@ function register_gauge_family(opts: MetricOpts): GaugeFamily opts$name, opts$labels, opts$help_text, - opts$unit, - opts?$is_total ? opts$is_total : F + opts$unit ); return GaugeFamily($__family=f, $__labels=opts$labels); } diff --git a/src/EventHandler.cc b/src/EventHandler.cc index 9e5066189a..a5701aa5fa 100644 --- a/src/EventHandler.cc +++ b/src/EventHandler.cc @@ -47,7 +47,7 @@ void EventHandler::Call(Args* vl, bool no_remote, double ts) { if ( ! call_count ) { static auto eh_invocations_family = telemetry_mgr->CounterFamily("zeek", "event-handler-invocations", {"name"}, - "Number of times the given event handler was called", "", true); + "Number of times the given event handler was called"); call_count = eh_invocations_family->GetOrAdd({{"name", name}}); } diff --git a/src/logging/Manager.cc b/src/logging/Manager.cc index b3b3f2213e..5a46cbcd33 100644 --- a/src/logging/Manager.cc +++ b/src/logging/Manager.cc @@ -418,14 +418,12 @@ void Manager::Stream::DispatchDelayExpiredTimer(double t, bool is_expire) { Manager::Manager() : plugin::ComponentManager("Log", "Writer"), total_log_stream_writes_family(telemetry_mgr->CounterFamily("zeek", "log-stream-writes", {"module", "stream"}, - "Total number of log writes for the given stream.", - "", true)), + "Total number of log writes for the given stream.")), total_log_writer_writes_family( telemetry_mgr ->CounterFamily("zeek", "log-writer-writes", {"writer", "module", "stream", "filter-name", "path"}, "Total number of log writes passed to a concrete log writer not vetoed by stream or " - "filter policies.", - "", true)) { + "filter policies.")) { rotations_pending = 0; } diff --git a/src/session/Manager.cc b/src/session/Manager.cc index cd10740fba..8f2fbc6881 100644 --- a/src/session/Manager.cc +++ b/src/session/Manager.cc @@ -48,7 +48,7 @@ public: auto active_family = telemetry_mgr->GaugeFamily("zeek", "active-sessions", {"protocol"}, "Active Zeek Sessions"); auto total_family = - telemetry_mgr->CounterFamily("zeek", "total-sessions", {"protocol"}, "Total number of sessions", "", true); + telemetry_mgr->CounterFamily("zeek", "total-sessions", {"protocol"}, "Total number of sessions"); auto [it, inserted] = entries.insert({protocol, Protocol{active_family, total_family, protocol}}); diff --git a/src/telemetry/Manager.cc b/src/telemetry/Manager.cc index c85177ef43..45eef81c39 100644 --- a/src/telemetry/Manager.cc +++ b/src/telemetry/Manager.cc @@ -81,7 +81,7 @@ void Manager::InitPostScript() { return &this->current_process_stats; }; - rss_gauge = GaugeInstance("process", "resident_memory", {}, "Resident memory size", "bytes", false, + rss_gauge = GaugeInstance("process", "resident_memory", {}, "Resident memory size", "bytes", []() -> prometheus::ClientMetric { auto* s = get_stats(); prometheus::ClientMetric metric; @@ -89,7 +89,7 @@ void Manager::InitPostScript() { return metric; }); - vms_gauge = GaugeInstance("process", "virtual_memory", {}, "Virtual memory size", "bytes", false, + vms_gauge = GaugeInstance("process", "virtual_memory", {}, "Virtual memory size", "bytes", []() -> prometheus::ClientMetric { auto* s = get_stats(); prometheus::ClientMetric metric; @@ -97,7 +97,7 @@ void Manager::InitPostScript() { return metric; }); - cpu_gauge = GaugeInstance("process", "cpu", {}, "Total user and system CPU time spent", "seconds", false, + cpu_gauge = GaugeInstance("process", "cpu", {}, "Total user and system CPU time spent", "seconds", []() -> prometheus::ClientMetric { auto* s = get_stats(); prometheus::ClientMetric metric; @@ -105,7 +105,7 @@ void Manager::InitPostScript() { return metric; }); - fds_gauge = GaugeInstance("process", "open_fds", {}, "Number of open file descriptors", "", false, + fds_gauge = GaugeInstance("process", "open_fds", {}, "Number of open file descriptors", "", []() -> prometheus::ClientMetric { auto* s = get_stats(); prometheus::ClientMetric metric; @@ -445,9 +445,8 @@ std::string Manager::GetClusterJson() const { std::shared_ptr Manager::CounterFamily(std::string_view prefix, std::string_view name, Span labels, - std::string_view helptext, std::string_view unit, - bool is_sum) { - auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, is_sum); + std::string_view helptext, std::string_view unit) { + auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, true); auto& prom_fam = prometheus::BuildCounter().Name(full_name).Help(std::string{helptext}).Register(*prometheus_registry); @@ -462,35 +461,31 @@ std::shared_ptr Manager::CounterFamily(std::string_vie std::shared_ptr Manager::CounterFamily(std::string_view prefix, std::string_view name, std::initializer_list labels, - std::string_view helptext, std::string_view unit, - bool is_sum) { + std::string_view helptext, std::string_view unit) { auto lbl_span = Span{labels.begin(), labels.size()}; - return CounterFamily(prefix, name, lbl_span, helptext, unit, is_sum); + return CounterFamily(prefix, name, lbl_span, helptext, unit); } std::shared_ptr Manager::CounterInstance(std::string_view prefix, std::string_view name, Span labels, std::string_view helptext, - std::string_view unit, bool is_sum, - prometheus::CollectCallbackPtr callback) { + std::string_view unit, prometheus::CollectCallbackPtr callback) { return WithLabelNames(labels, [&, this](auto labelNames) { - auto family = CounterFamily(prefix, name, labelNames, helptext, unit, is_sum); + auto family = CounterFamily(prefix, name, labelNames, helptext, unit); return family->GetOrAdd(labels, callback); }); } std::shared_ptr Manager::CounterInstance(std::string_view prefix, std::string_view name, std::initializer_list labels, std::string_view helptext, - std::string_view unit, bool is_sum, - prometheus::CollectCallbackPtr callback) { + std::string_view unit, prometheus::CollectCallbackPtr callback) { auto lbl_span = Span{labels.begin(), labels.size()}; - return CounterInstance(prefix, name, lbl_span, helptext, unit, is_sum, std::move(callback)); + return CounterInstance(prefix, name, lbl_span, helptext, unit, std::move(callback)); } std::shared_ptr Manager::GaugeFamily(std::string_view prefix, std::string_view name, Span labels, - std::string_view helptext, std::string_view unit, - bool is_sum) { - auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, is_sum); + std::string_view helptext, std::string_view unit) { + auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, false); auto& prom_fam = prometheus::BuildGauge().Name(full_name).Help(std::string{helptext}).Register(*prometheus_registry); @@ -505,28 +500,25 @@ std::shared_ptr Manager::GaugeFamily(std::string_view pr std::shared_ptr Manager::GaugeFamily(std::string_view prefix, std::string_view name, std::initializer_list labels, - std::string_view helptext, std::string_view unit, - bool is_sum) { + std::string_view helptext, std::string_view unit) { auto lbl_span = Span{labels.begin(), labels.size()}; - return GaugeFamily(prefix, name, lbl_span, helptext, unit, is_sum); + return GaugeFamily(prefix, name, lbl_span, helptext, unit); } std::shared_ptr Manager::GaugeInstance(std::string_view prefix, std::string_view name, Span labels, std::string_view helptext, - std::string_view unit, bool is_sum, - prometheus::CollectCallbackPtr callback) { + std::string_view unit, prometheus::CollectCallbackPtr callback) { return WithLabelNames(labels, [&, this](auto labelNames) { - auto family = GaugeFamily(prefix, name, labelNames, helptext, unit, is_sum); + auto family = GaugeFamily(prefix, name, labelNames, helptext, unit); return family->GetOrAdd(labels, callback); }); } std::shared_ptr Manager::GaugeInstance(std::string_view prefix, std::string_view name, std::initializer_list labels, std::string_view helptext, - std::string_view unit, bool is_sum, - prometheus::CollectCallbackPtr callback) { + std::string_view unit, prometheus::CollectCallbackPtr callback) { auto lbl_span = Span{labels.begin(), labels.size()}; - return GaugeInstance(prefix, name, lbl_span, helptext, unit, is_sum, callback); + return GaugeInstance(prefix, name, lbl_span, helptext, unit, callback); } std::shared_ptr Manager::HistogramFamily(std::string_view prefix, std::string_view name, @@ -595,7 +587,7 @@ SCENARIO("telemetry managers provide access to counter families") { GIVEN("a telemetry manager") { Manager mgr; WHEN("retrieving an IntCounter family") { - auto family = mgr.CounterFamily("zeek", "requests", {"method"}, "test", "", true); + auto family = mgr.CounterFamily("zeek", "requests", {"method"}, "test"); THEN("GetOrAdd returns the same metric for the same labels") { auto first = family->GetOrAdd({{"method", "get"}}); auto second = family->GetOrAdd({{"method", "get"}}); @@ -608,7 +600,7 @@ SCENARIO("telemetry managers provide access to counter families") { } } WHEN("retrieving a DblCounter family") { - auto family = mgr.CounterFamily("zeek", "runtime", {"query"}, "test", "seconds", true); + auto family = mgr.CounterFamily("zeek", "runtime", {"query"}, "test", "seconds"); THEN("GetOrAdd returns the same metric for the same labels") { auto first = family->GetOrAdd({{"query", "foo"}}); auto second = family->GetOrAdd({{"query", "foo"}}); @@ -627,7 +619,7 @@ SCENARIO("telemetry managers provide access to gauge families") { GIVEN("a telemetry manager") { Manager mgr; WHEN("retrieving an IntGauge family") { - auto family = mgr.GaugeFamily("zeek", "open-connections", {"protocol"}, "test", ""); + auto family = mgr.GaugeFamily("zeek", "open-connections", {"protocol"}, "test"); THEN("GetOrAdd returns the same metric for the same labels") { auto first = family->GetOrAdd({{"protocol", "tcp"}}); auto second = family->GetOrAdd({{"protocol", "tcp"}}); diff --git a/src/telemetry/Manager.h b/src/telemetry/Manager.h index b43f0a8c01..66cb07e04f 100644 --- a/src/telemetry/Manager.h +++ b/src/telemetry/Manager.h @@ -68,18 +68,15 @@ public: * @param labels Names for all label dimensions of the metric. * @param helptext Short explanation of the metric. * @param unit Unit of measurement. - * @param is_sum Indicates whether this metric accumulates something, where only the total value is of interest. */ std::shared_ptr CounterFamily(std::string_view prefix, std::string_view name, Span labels, - std::string_view helptext, std::string_view unit = "", - bool is_sum = false); + std::string_view helptext, std::string_view unit = ""); /// @copydoc CounterFamily std::shared_ptr CounterFamily(std::string_view prefix, std::string_view name, std::initializer_list labels, - std::string_view helptext, std::string_view unit = "", - bool is_sum = false); + std::string_view helptext, std::string_view unit = ""); /** * Accesses a counter instance. Creates the hosting metric family as well @@ -89,19 +86,18 @@ public: * @param labels Values for all label dimensions of the metric. * @param helptext Short explanation of the metric. * @param unit Unit of measurement. - * @param is_sum Indicates whether this metric accumulates something, where only the total value is of interest. * @param callback Passing a callback method will enable asynchronous mode. The callback method will be called by * the metrics subsystem whenever data is requested. */ std::shared_ptr CounterInstance(std::string_view prefix, std::string_view name, Span labels, std::string_view helptext, - std::string_view unit = "", bool is_sum = false, + std::string_view unit = "", prometheus::CollectCallbackPtr callback = nullptr); /// @copydoc counterInstance std::shared_ptr CounterInstance(std::string_view prefix, std::string_view name, std::initializer_list labels, std::string_view helptext, - std::string_view unit = "", bool is_sum = false, + std::string_view unit = "", prometheus::CollectCallbackPtr callback = nullptr); /** @@ -111,17 +107,15 @@ public: * @param labels Names for all label dimensions of the metric. * @param helptext Short explanation of the metric. * @param unit Unit of measurement. - * @param is_sum Indicates whether this metric accumulates something, where only the total value is of interest. */ std::shared_ptr GaugeFamily(std::string_view prefix, std::string_view name, Span labels, std::string_view helptext, - std::string_view unit = "", bool is_sum = false); + std::string_view unit = ""); /// @copydoc GaugeFamily std::shared_ptr GaugeFamily(std::string_view prefix, std::string_view name, std::initializer_list labels, - std::string_view helptext, std::string_view unit = "", - bool is_sum = false); + std::string_view helptext, std::string_view unit = ""); /** * Accesses a gauge instance. Creates the hosting metric family as well @@ -131,19 +125,17 @@ public: * @param labels Values for all label dimensions of the metric. * @param helptext Short explanation of the metric. * @param unit Unit of measurement. - * @param is_sum Indicates whether this metric accumulates something, where only the total value is of interest. * @param callback Passing a callback method will enable asynchronous mode. The callback method will be called by * the metrics subsystem whenever data is requested. */ std::shared_ptr GaugeInstance(std::string_view prefix, std::string_view name, Span labels, - std::string_view helptext, std::string_view unit = "", bool is_sum = false, + std::string_view helptext, std::string_view unit = "", prometheus::CollectCallbackPtr callback = nullptr); /// @copydoc GaugeInstance std::shared_ptr GaugeInstance(std::string_view prefix, std::string_view name, std::initializer_list labels, std::string_view helptext, - std::string_view unit = "", bool is_sum = false, - prometheus::CollectCallbackPtr callback = nullptr); + std::string_view unit = "", prometheus::CollectCallbackPtr callback = nullptr); // Forces the compiler to use the type `Span` instead of trying to // match parameters to a `span`. diff --git a/src/telemetry/Utils.cc b/src/telemetry/Utils.cc index 5cae319853..3332861bb0 100644 --- a/src/telemetry/Utils.cc +++ b/src/telemetry/Utils.cc @@ -21,6 +21,12 @@ std::string BuildFullPrometheusName(std::string_view prefix, std::string_view na c = '_'; }); + // Suffixes of full metric names of _total are reserved by Prometheus. Disallow their use here. + if ( util::ends_with(fn, "_total") ) + reporter->FatalError("Metric names cannot end with '_total'"); + else if ( unit == "total" || util::ends_with(unit, "_total") ) + reporter->FatalError("Metric units cannot end with '_total'"); + // We were previously using "1" to mean "no unit value" for whatever reason, so we have to handle that now // to mean the same thing. if ( ! unit.empty() && unit != "1" ) diff --git a/src/telemetry/telemetry.bif b/src/telemetry/telemetry.bif index 610509f1d8..6042bae826 100644 --- a/src/telemetry/telemetry.bif +++ b/src/telemetry/telemetry.bif @@ -111,12 +111,11 @@ function Telemetry::__counter_family%(prefix: string, name: string, labels: string_vec, helptext: string &default = "Zeek Script Metric", - unit: string &default = "", - is_sum: bool &default = F%): opaque of counter_metric_family + unit: string &default = ""%): opaque of counter_metric_family %{ auto lbl_vec = sv_vec(labels->AsVectorVal()); auto hdl = telemetry_mgr->CounterFamily(sv(prefix), sv(name), lbl_vec, - sv(helptext), sv(unit), is_sum); + sv(helptext), sv(unit)); return zeek::make_intrusive(hdl); %} @@ -171,12 +170,11 @@ function Telemetry::__gauge_family%(prefix: string, name: string, labels: string_vec, helptext: string &default = "Zeek Script Metric", - unit: string &default = "", - is_sum: bool &default = F%): opaque of gauge_metric_family + unit: string &default = ""%): opaque of gauge_metric_family %{ auto lbl_vec = sv_vec(labels->AsVectorVal()); auto hdl = telemetry_mgr->GaugeFamily(sv(prefix), sv(name), lbl_vec, - sv(helptext), sv(unit), is_sum); + sv(helptext), sv(unit)); return zeek::make_intrusive(hdl); %}