Remove is_sum arguments from counters and gauges

This commit is contained in:
Tim Wojtulewicz 2024-05-24 15:11:15 -07:00
parent 46ff48c29a
commit 93717ca8f8
8 changed files with 47 additions and 63 deletions

View file

@ -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);
}

View file

@ -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}});
}

View file

@ -418,14 +418,12 @@ void Manager::Stream::DispatchDelayExpiredTimer(double t, bool is_expire) {
Manager::Manager()
: plugin::ComponentManager<logging::Component>("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;
}

View file

@ -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}});

View file

@ -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<telemetry::CounterFamily> Manager::CounterFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> 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<telemetry::CounterFamily> Manager::CounterFamily(std::string_vie
std::shared_ptr<telemetry::CounterFamily> Manager::CounterFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> 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<Counter> Manager::CounterInstance(std::string_view prefix, std::string_view name,
Span<const LabelView> 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<Counter> Manager::CounterInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> 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<telemetry::GaugeFamily> Manager::GaugeFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> 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<telemetry::GaugeFamily> Manager::GaugeFamily(std::string_view pr
std::shared_ptr<telemetry::GaugeFamily> Manager::GaugeFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> 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<Gauge> Manager::GaugeInstance(std::string_view prefix, std::string_view name,
Span<const LabelView> 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<Gauge> Manager::GaugeInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> 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<telemetry::HistogramFamily> 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"}});

View file

@ -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<telemetry::CounterFamily> CounterFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> 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<telemetry::CounterFamily> CounterFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> 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<Counter> CounterInstance(std::string_view prefix, std::string_view name,
Span<const LabelView> 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<Counter> CounterInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> 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<telemetry::GaugeFamily> GaugeFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels, std::string_view helptext,
std::string_view unit = "", bool is_sum = false);
std::string_view unit = "");
/// @copydoc GaugeFamily
std::shared_ptr<telemetry::GaugeFamily> GaugeFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> 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<Gauge> GaugeInstance(std::string_view prefix, std::string_view name, Span<const LabelView> 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<Gauge> GaugeInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> 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<const T>` instead of trying to
// match parameters to a `span`.

View file

@ -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" )

View file

@ -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<CounterMetricFamilyVal>(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<GaugeMetricFamilyVal>(hdl);
%}