Rework everything to access the prometheus-cpp objects more directly

This commit is contained in:
Tim Wojtulewicz 2024-03-26 11:19:25 -07:00
parent ec3e68928f
commit 84aa308527
17 changed files with 469 additions and 606 deletions

View file

@ -15,6 +15,7 @@
#include "zeek/telemetry/Gauge.h"
#include "zeek/telemetry/Histogram.h"
#include "zeek/telemetry/ProcessStats.h"
#include "zeek/telemetry/Utils.h"
#include "prometheus/exposer.h"
#include "prometheus/registry.h"
@ -26,8 +27,6 @@ using RecordValPtr = IntrusivePtr<RecordVal>;
namespace zeek::telemetry {
class OtelReader;
/**
* Manages a collection of metric families.
*/
@ -69,33 +68,31 @@ public:
* @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.
*/
template<class ValueType = int64_t>
auto 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) {
auto fam = LookupFamily(prefix, name);
auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, is_sum);
auto& prom_fam =
prometheus::BuildCounter().Name(full_name).Help(std::string{helptext}).Register(*prometheus_registry);
if constexpr ( std::is_same<ValueType, int64_t>::value ) {
if ( fam )
return std::static_pointer_cast<IntCounterFamily>(fam);
if ( auto it = families.find(prom_fam.GetName()); it != families.end() )
return std::static_pointer_cast<IntCounterFamily>(it->second);
auto int_fam =
std::make_shared<IntCounterFamily>(prefix, name, labels, helptext, prometheus_registry, unit, is_sum);
families.insert_or_assign(int_fam->FullName(), int_fam);
return int_fam;
auto fam = std::make_shared<IntCounterFamily>(&prom_fam, labels);
families.insert({prom_fam.GetName(), fam});
return fam;
}
else {
static_assert(std::is_same<ValueType, double>::value, "metrics only support int64_t and double values");
if ( auto it = families.find(prom_fam.GetName()); it != families.end() )
return std::static_pointer_cast<DblCounterFamily>(it->second);
if ( fam )
return std::static_pointer_cast<DblCounterFamily>(fam);
auto dbl_fam =
std::make_shared<DblCounterFamily>(prefix, name, labels, helptext, prometheus_registry, unit, is_sum);
families.insert_or_assign(dbl_fam->FullName(), dbl_fam);
return dbl_fam;
auto fam = std::make_shared<DblCounterFamily>(&prom_fam, labels);
families.insert({prom_fam.GetName(), fam});
return fam;
}
}
@ -149,32 +146,31 @@ public:
* @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.
*/
template<class ValueType = int64_t>
auto 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) {
auto fam = LookupFamily(prefix, name);
auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, is_sum);
auto& prom_fam =
prometheus::BuildGauge().Name(full_name).Help(std::string{helptext}).Register(*prometheus_registry);
if constexpr ( std::is_same<ValueType, int64_t>::value ) {
if ( fam )
return std::static_pointer_cast<IntGaugeFamily>(fam);
if ( auto it = families.find(prom_fam.GetName()); it != families.end() )
return std::static_pointer_cast<IntGaugeFamily>(it->second);
auto int_fam =
std::make_shared<IntGaugeFamily>(prefix, name, labels, helptext, prometheus_registry, unit, is_sum);
families.insert_or_assign(int_fam->FullName(), int_fam);
return int_fam;
auto fam = std::make_shared<IntGaugeFamily>(&prom_fam, labels);
families.insert({prom_fam.GetName(), fam});
return fam;
}
else {
static_assert(std::is_same<ValueType, double>::value, "metrics only support int64_t and double values");
if ( fam )
return std::static_pointer_cast<DblGaugeFamily>(fam);
if ( auto it = families.find(prom_fam.GetName()); it != families.end() )
return std::static_pointer_cast<DblGaugeFamily>(it->second);
auto dbl_fam =
std::make_shared<DblGaugeFamily>(prefix, name, labels, helptext, prometheus_registry, unit, is_sum);
families.insert_or_assign(dbl_fam->FullName(), dbl_fam);
return dbl_fam;
auto fam = std::make_shared<DblGaugeFamily>(&prom_fam, labels);
families.insert({prom_fam.GetName(), fam});
return fam;
}
}
@ -239,52 +235,48 @@ public:
* reserved.
* @param name The human-readable name of the metric, e.g., `requests`.
* @param labels Names for all label dimensions of the metric.
* @param default_upper_bounds Upper bounds for the metric buckets.
* @param bounds Upper bounds for the metric buckets.
* @param helptext Short explanation of the metric.
* @param unit Unit of measurement. Please use base units such as `bytes` or
* `seconds` (prefer lowercase). The pseudo-unit `1` identifies
* dimensionless counts.
* @param is_sum Setting this to `true` indicates that this metric adds
* something up to a total, where only the total value is of
* interest. For example, the total number of HTTP requests.
* @note The first call wins when calling this function multiple times with
* different bucket settings. Users may also override
* @p default_upper_bounds via run-time configuration.
* @p bounds via run-time configuration.
*/
template<class ValueType = int64_t>
auto HistogramFamily(std::string_view prefix, std::string_view name, Span<const std::string_view> labels,
ConstSpan<ValueType> default_upper_bounds, std::string_view helptext,
std::string_view unit = "") {
auto fam = LookupFamily(prefix, name);
ConstSpan<ValueType> bounds, std::string_view helptext, std::string_view unit = "") {
auto full_name = detail::BuildFullPrometheusName(prefix, name, unit);
auto& prom_fam =
prometheus::BuildHistogram().Name(full_name).Help(std::string{helptext}).Register(*prometheus_registry);
if constexpr ( std::is_same<ValueType, int64_t>::value ) {
if ( fam )
return std::static_pointer_cast<IntHistogramFamily>(fam);
if ( auto it = families.find(prom_fam.GetName()); it != families.end() )
return std::static_pointer_cast<IntHistogramFamily>(it->second);
auto int_fam = std::make_shared<IntHistogramFamily>(prefix, name, labels, default_upper_bounds, helptext,
prometheus_registry, unit);
families.insert_or_assign(int_fam->FullName(), int_fam);
return int_fam;
auto fam = std::make_shared<IntHistogramFamily>(&prom_fam, bounds, labels);
families.insert({prom_fam.GetName(), fam});
return fam;
}
else {
static_assert(std::is_same<ValueType, double>::value, "metrics only support int64_t and double values");
if ( fam )
return std::static_pointer_cast<DblHistogramFamily>(fam);
if ( auto it = families.find(prom_fam.GetName()); it != families.end() )
return std::static_pointer_cast<DblHistogramFamily>(it->second);
auto dbl_fam = std::make_shared<DblHistogramFamily>(prefix, name, labels, default_upper_bounds, helptext,
prometheus_registry, unit);
families.insert_or_assign(dbl_fam->FullName(), dbl_fam);
return dbl_fam;
auto fam = std::make_shared<DblHistogramFamily>(&prom_fam, bounds, labels);
families.insert({prom_fam.GetName(), fam});
return fam;
}
}
/// @copydoc HistogramFamily
template<class ValueType = int64_t>
auto HistogramFamily(std::string_view prefix, std::string_view name, std::initializer_list<std::string_view> labels,
ConstSpan<ValueType> default_upper_bounds, std::string_view helptext,
std::string_view unit = "") {
ConstSpan<ValueType> bounds, std::string_view helptext, std::string_view unit = "") {
auto lbl_span = Span{labels.begin(), labels.size()};
return HistogramFamily<ValueType>(prefix, name, lbl_span, default_upper_bounds, helptext, unit);
return HistogramFamily<ValueType>(prefix, name, lbl_span, bounds, helptext, unit);
}
/**
@ -295,25 +287,21 @@ public:
* reserved.
* @param name The human-readable name of the metric, e.g., `requests`.
* @param labels Names for all label dimensions of the metric.
* @param default_upper_bounds Upper bounds for the metric buckets.
* @param bounds Upper bounds for the metric buckets.
* @param helptext Short explanation of the metric.
* @param unit Unit of measurement. Please use base units such as `bytes` or
* `seconds` (prefer lowercase). The pseudo-unit `1` identifies
* dimensionless counts.
* @param is_sum Setting this to `true` indicates that this metric adds
* something up to a total, where only the total value is of
* interest. For example, the total number of HTTP requests.
* @note The first call wins when calling this function multiple times with
* different bucket settings. Users may also override
* @p default_upper_bounds via run-time configuration.
* @p bounds via run-time configuration.
*/
template<class ValueType = int64_t>
std::shared_ptr<Histogram<ValueType>> HistogramInstance(std::string_view prefix, std::string_view name,
Span<const LabelView> labels,
ConstSpan<ValueType> default_upper_bounds,
Span<const LabelView> labels, ConstSpan<ValueType> bounds,
std::string_view helptext, std::string_view unit = "") {
return WithLabelNames(labels, [&, this](auto labelNames) {
auto family = HistogramFamily<ValueType>(prefix, name, labelNames, default_upper_bounds, helptext, unit);
auto family = HistogramFamily<ValueType>(prefix, name, labelNames, bounds, helptext, unit);
return family->GetOrAdd(labels);
});
}
@ -322,17 +310,11 @@ public:
template<class ValueType = int64_t>
std::shared_ptr<Histogram<ValueType>> HistogramInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> labels,
std::initializer_list<ValueType> default_upper_bounds,
std::initializer_list<ValueType> bounds,
std::string_view helptext, std::string_view unit = "") {
auto lbls = Span{labels.begin(), labels.size()};
auto bounds = Span{default_upper_bounds.begin(), default_upper_bounds.size()};
return HistogramInstance<ValueType>(prefix, name, lbls, bounds, helptext, unit);
}
std::shared_ptr<MetricFamily> GetFamilyByFullName(const std::string& full_name) const {
if ( auto it = families.find(full_name); it != families.end() )
return it->second;
return nullptr;
auto bounds_span = Span{bounds.begin(), bounds.size()};
return HistogramInstance<ValueType>(prefix, name, lbls, bounds_span, helptext, unit);
}
/**
@ -368,10 +350,10 @@ protected:
}
private:
std::shared_ptr<MetricFamily> LookupFamily(std::string_view prefix, std::string_view name) const;
RecordValPtr GetMetricOptsRecord(const prometheus::MetricFamily& metric_family);
std::shared_ptr<OtelReader> otel_reader;
std::map<std::string, std::shared_ptr<MetricFamily>> families;
std::map<std::string, RecordValPtr> opts_records;
detail::process_stats current_process_stats;
double process_stats_last_updated = 0.0;