Add type aliases for instrument and family shared_ptrs

This commit is contained in:
Tim Wojtulewicz 2024-05-31 13:10:19 -07:00
parent bbc14cfff0
commit 52e6314f0e
5 changed files with 93 additions and 95 deletions

View file

@ -61,6 +61,8 @@ private:
bool has_callback = false; bool has_callback = false;
}; };
using CounterPtr = std::shared_ptr<Counter>;
class CounterFamily : public MetricFamily, public std::enable_shared_from_this<CounterFamily> { class CounterFamily : public MetricFamily, public std::enable_shared_from_this<CounterFamily> {
public: public:
static inline const char* OpaqueName = "CounterMetricFamilyVal"; static inline const char* OpaqueName = "CounterMetricFamilyVal";
@ -72,19 +74,20 @@ public:
* Returns the metrics handle for given labels, creating a new instance * Returns the metrics handle for given labels, creating a new instance
* lazily if necessary. * lazily if necessary.
*/ */
std::shared_ptr<Counter> GetOrAdd(Span<const LabelView> labels, prometheus::CollectCallbackPtr callback = nullptr); CounterPtr GetOrAdd(Span<const LabelView> labels, prometheus::CollectCallbackPtr callback = nullptr);
/** /**
* @copydoc GetOrAdd * @copydoc GetOrAdd
*/ */
std::shared_ptr<Counter> GetOrAdd(std::initializer_list<LabelView> labels, CounterPtr GetOrAdd(std::initializer_list<LabelView> labels, prometheus::CollectCallbackPtr callback = nullptr);
prometheus::CollectCallbackPtr callback = nullptr);
zeek_int_t MetricType() const noexcept override { return BifEnum::Telemetry::MetricType::COUNTER; } zeek_int_t MetricType() const noexcept override { return BifEnum::Telemetry::MetricType::COUNTER; }
private: private:
prometheus::Family<prometheus::Counter>* family; prometheus::Family<prometheus::Counter>* family;
std::vector<std::shared_ptr<Counter>> counters; std::vector<CounterPtr> counters;
}; };
using CounterFamilyPtr = std::shared_ptr<CounterFamily>;
} // namespace zeek::telemetry } // namespace zeek::telemetry

View file

@ -79,6 +79,8 @@ private:
bool has_callback = false; bool has_callback = false;
}; };
using GaugePtr = std::shared_ptr<Gauge>;
class GaugeFamily : public MetricFamily, public std::enable_shared_from_this<GaugeFamily> { class GaugeFamily : public MetricFamily, public std::enable_shared_from_this<GaugeFamily> {
public: public:
static inline const char* OpaqueName = "GaugeMetricFamilyVal"; static inline const char* OpaqueName = "GaugeMetricFamilyVal";
@ -87,13 +89,12 @@ public:
* Returns the metrics handle for given labels, creating a new instance * Returns the metrics handle for given labels, creating a new instance
* lazily if necessary. * lazily if necessary.
*/ */
std::shared_ptr<Gauge> GetOrAdd(Span<const LabelView> labels, prometheus::CollectCallbackPtr callback = nullptr); GaugePtr GetOrAdd(Span<const LabelView> labels, prometheus::CollectCallbackPtr callback = nullptr);
/** /**
* @copydoc GetOrAdd * @copydoc GetOrAdd
*/ */
std::shared_ptr<Gauge> GetOrAdd(std::initializer_list<LabelView> labels, GaugePtr GetOrAdd(std::initializer_list<LabelView> labels, prometheus::CollectCallbackPtr callback = nullptr);
prometheus::CollectCallbackPtr callback = nullptr);
zeek_int_t MetricType() const noexcept override { return BifEnum::Telemetry::MetricType::GAUGE; } zeek_int_t MetricType() const noexcept override { return BifEnum::Telemetry::MetricType::GAUGE; }
@ -102,7 +103,9 @@ public:
private: private:
prometheus::Family<prometheus::Gauge>* family; prometheus::Family<prometheus::Gauge>* family;
std::vector<std::shared_ptr<Gauge>> gauges; std::vector<GaugePtr> gauges;
}; };
using GaugeFamilyPtr = std::shared_ptr<GaugeFamily>;
} // namespace zeek::telemetry } // namespace zeek::telemetry

View file

@ -44,6 +44,8 @@ private:
prometheus::Labels labels; prometheus::Labels labels;
}; };
using HistogramPtr = std::shared_ptr<Histogram>;
class HistogramFamily : public MetricFamily, public std::enable_shared_from_this<HistogramFamily> { class HistogramFamily : public MetricFamily, public std::enable_shared_from_this<HistogramFamily> {
public: public:
static inline const char* OpaqueName = "HistogramMetricFamilyVal"; static inline const char* OpaqueName = "HistogramMetricFamilyVal";
@ -55,19 +57,21 @@ public:
* Returns the metrics handle for given labels, creating a new instance * Returns the metrics handle for given labels, creating a new instance
* lazily if necessary. * lazily if necessary.
*/ */
std::shared_ptr<Histogram> GetOrAdd(Span<const LabelView> labels); HistogramPtr GetOrAdd(Span<const LabelView> labels);
/** /**
* @copydoc GetOrAdd * @copydoc GetOrAdd
*/ */
std::shared_ptr<Histogram> GetOrAdd(std::initializer_list<LabelView> labels); HistogramPtr GetOrAdd(std::initializer_list<LabelView> labels);
zeek_int_t MetricType() const noexcept override { return BifEnum::Telemetry::MetricType::HISTOGRAM; } zeek_int_t MetricType() const noexcept override { return BifEnum::Telemetry::MetricType::HISTOGRAM; }
private: private:
prometheus::Family<prometheus::Histogram>* family; prometheus::Family<prometheus::Histogram>* family;
prometheus::Histogram::BucketBoundaries boundaries; prometheus::Histogram::BucketBoundaries boundaries;
std::vector<std::shared_ptr<Histogram>> histograms; std::vector<HistogramPtr> histograms;
}; };
using HistogramFamilyPtr = std::shared_ptr<HistogramFamily>;
} // namespace zeek::telemetry } // namespace zeek::telemetry

View file

@ -443,9 +443,9 @@ std::string Manager::GetClusterJson() const {
return buffer.GetString(); return buffer.GetString();
} }
std::shared_ptr<telemetry::CounterFamily> Manager::CounterFamily(std::string_view prefix, std::string_view name, CounterFamilyPtr Manager::CounterFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels, Span<const std::string_view> labels, std::string_view helptext,
std::string_view helptext, std::string_view unit) { std::string_view unit) {
auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, true); auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, true);
auto& prom_fam = auto& prom_fam =
@ -459,32 +459,32 @@ std::shared_ptr<telemetry::CounterFamily> Manager::CounterFamily(std::string_vie
return fam; return fam;
} }
std::shared_ptr<telemetry::CounterFamily> Manager::CounterFamily(std::string_view prefix, std::string_view name, CounterFamilyPtr Manager::CounterFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, std::initializer_list<std::string_view> labels, std::string_view helptext,
std::string_view helptext, std::string_view unit) { std::string_view unit) {
auto lbl_span = Span{labels.begin(), labels.size()}; auto lbl_span = Span{labels.begin(), labels.size()};
return CounterFamily(prefix, name, lbl_span, helptext, unit); return CounterFamily(prefix, name, lbl_span, helptext, unit);
} }
std::shared_ptr<Counter> Manager::CounterInstance(std::string_view prefix, std::string_view name, CounterPtr Manager::CounterInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels,
Span<const LabelView> labels, std::string_view helptext, std::string_view helptext, std::string_view unit,
std::string_view unit, prometheus::CollectCallbackPtr callback) { prometheus::CollectCallbackPtr callback) {
return WithLabelNames(labels, [&, this](auto labelNames) { return WithLabelNames(labels, [&, this](auto labelNames) {
auto family = CounterFamily(prefix, name, labelNames, helptext, unit); auto family = CounterFamily(prefix, name, labelNames, helptext, unit);
return family->GetOrAdd(labels, callback); return family->GetOrAdd(labels, callback);
}); });
} }
std::shared_ptr<Counter> Manager::CounterInstance(std::string_view prefix, std::string_view name, CounterPtr Manager::CounterInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> labels, std::string_view helptext, std::initializer_list<LabelView> labels, std::string_view helptext,
std::string_view unit, prometheus::CollectCallbackPtr callback) { std::string_view unit, prometheus::CollectCallbackPtr callback) {
auto lbl_span = Span{labels.begin(), labels.size()}; auto lbl_span = Span{labels.begin(), labels.size()};
return CounterInstance(prefix, name, lbl_span, helptext, unit, 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, std::shared_ptr<GaugeFamily> Manager::GaugeFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels, Span<const std::string_view> labels, std::string_view helptext,
std::string_view helptext, std::string_view unit) { std::string_view unit) {
auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, false); auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, false);
auto& prom_fam = auto& prom_fam =
@ -498,33 +498,32 @@ std::shared_ptr<telemetry::GaugeFamily> Manager::GaugeFamily(std::string_view pr
return fam; return fam;
} }
std::shared_ptr<telemetry::GaugeFamily> Manager::GaugeFamily(std::string_view prefix, std::string_view name, GaugeFamilyPtr Manager::GaugeFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, std::initializer_list<std::string_view> labels, std::string_view helptext,
std::string_view helptext, std::string_view unit) { std::string_view unit) {
auto lbl_span = Span{labels.begin(), labels.size()}; auto lbl_span = Span{labels.begin(), labels.size()};
return GaugeFamily(prefix, name, lbl_span, helptext, unit); return GaugeFamily(prefix, name, lbl_span, helptext, unit);
} }
std::shared_ptr<Gauge> Manager::GaugeInstance(std::string_view prefix, std::string_view name, GaugePtr Manager::GaugeInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels,
Span<const LabelView> labels, std::string_view helptext, std::string_view helptext, std::string_view unit,
std::string_view unit, prometheus::CollectCallbackPtr callback) { prometheus::CollectCallbackPtr callback) {
return WithLabelNames(labels, [&, this](auto labelNames) { return WithLabelNames(labels, [&, this](auto labelNames) {
auto family = GaugeFamily(prefix, name, labelNames, helptext, unit); auto family = GaugeFamily(prefix, name, labelNames, helptext, unit);
return family->GetOrAdd(labels, callback); return family->GetOrAdd(labels, callback);
}); });
} }
std::shared_ptr<Gauge> Manager::GaugeInstance(std::string_view prefix, std::string_view name, GaugePtr Manager::GaugeInstance(std::string_view prefix, std::string_view name, std::initializer_list<LabelView> labels,
std::initializer_list<LabelView> labels, std::string_view helptext, std::string_view helptext, std::string_view unit,
std::string_view unit, prometheus::CollectCallbackPtr callback) { prometheus::CollectCallbackPtr callback) {
auto lbl_span = Span{labels.begin(), labels.size()}; auto lbl_span = Span{labels.begin(), labels.size()};
return GaugeInstance(prefix, name, lbl_span, helptext, unit, 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, HistogramFamilyPtr Manager::HistogramFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels, Span<const std::string_view> labels, ConstSpan<double> bounds,
ConstSpan<double> bounds, std::string_view helptext, std::string_view unit) {
std::string_view helptext, std::string_view unit) {
auto full_name = detail::BuildFullPrometheusName(prefix, name, unit); auto full_name = detail::BuildFullPrometheusName(prefix, name, unit);
auto& prom_fam = auto& prom_fam =
@ -538,27 +537,24 @@ std::shared_ptr<telemetry::HistogramFamily> Manager::HistogramFamily(std::string
return fam; return fam;
} }
std::shared_ptr<telemetry::HistogramFamily> Manager::HistogramFamily(std::string_view prefix, std::string_view name, HistogramFamilyPtr Manager::HistogramFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, std::initializer_list<std::string_view> labels, ConstSpan<double> bounds,
ConstSpan<double> bounds, std::string_view helptext, std::string_view unit) {
std::string_view helptext, std::string_view unit) {
auto lbl_span = Span{labels.begin(), labels.size()}; auto lbl_span = Span{labels.begin(), labels.size()};
return HistogramFamily(prefix, name, lbl_span, bounds, helptext, unit); return HistogramFamily(prefix, name, lbl_span, bounds, helptext, unit);
} }
std::shared_ptr<Histogram> Manager::HistogramInstance(std::string_view prefix, std::string_view name, HistogramPtr Manager::HistogramInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels,
Span<const LabelView> labels, ConstSpan<double> bounds, ConstSpan<double> bounds, std::string_view helptext, std::string_view unit) {
std::string_view helptext, std::string_view unit) {
return WithLabelNames(labels, [&, this](auto labelNames) { return WithLabelNames(labels, [&, this](auto labelNames) {
auto family = HistogramFamily(prefix, name, labelNames, bounds, helptext, unit); auto family = HistogramFamily(prefix, name, labelNames, bounds, helptext, unit);
return family->GetOrAdd(labels); return family->GetOrAdd(labels);
}); });
} }
std::shared_ptr<Histogram> Manager::HistogramInstance(std::string_view prefix, std::string_view name, HistogramPtr Manager::HistogramInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> labels, std::initializer_list<LabelView> labels, std::initializer_list<double> bounds,
std::initializer_list<double> bounds, std::string_view helptext, std::string_view helptext, std::string_view unit) {
std::string_view unit) {
auto lbls = Span{labels.begin(), labels.size()}; auto lbls = Span{labels.begin(), labels.size()};
auto bounds_span = Span{bounds.begin(), bounds.size()}; auto bounds_span = Span{bounds.begin(), bounds.size()};
return HistogramInstance(prefix, name, lbls, bounds_span, helptext, unit); return HistogramInstance(prefix, name, lbls, bounds_span, helptext, unit);

View file

@ -69,14 +69,13 @@ public:
* @param helptext Short explanation of the metric. * @param helptext Short explanation of the metric.
* @param unit Unit of measurement. * @param unit Unit of measurement.
*/ */
std::shared_ptr<telemetry::CounterFamily> CounterFamily(std::string_view prefix, std::string_view name, CounterFamilyPtr CounterFamily(std::string_view prefix, std::string_view name, Span<const std::string_view> labels,
Span<const std::string_view> labels, std::string_view helptext, std::string_view unit = "");
std::string_view helptext, std::string_view unit = "");
/// @copydoc CounterFamily /// @copydoc CounterFamily
std::shared_ptr<telemetry::CounterFamily> CounterFamily(std::string_view prefix, std::string_view name, CounterFamilyPtr CounterFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, std::initializer_list<std::string_view> labels, std::string_view helptext,
std::string_view helptext, std::string_view unit = ""); std::string_view unit = "");
/** /**
* Accesses a counter instance. Creates the hosting metric family as well * Accesses a counter instance. Creates the hosting metric family as well
@ -89,16 +88,14 @@ public:
* @param callback Passing a callback method will enable asynchronous mode. The callback method will be called by * @param callback Passing a callback method will enable asynchronous mode. The callback method will be called by
* the metrics subsystem whenever data is requested. * the metrics subsystem whenever data is requested.
*/ */
std::shared_ptr<Counter> CounterInstance(std::string_view prefix, std::string_view name, CounterPtr CounterInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels,
Span<const LabelView> labels, std::string_view helptext, std::string_view helptext, std::string_view unit = "",
std::string_view unit = "", prometheus::CollectCallbackPtr callback = nullptr);
prometheus::CollectCallbackPtr callback = nullptr);
/// @copydoc counterInstance /// @copydoc counterInstance
std::shared_ptr<Counter> CounterInstance(std::string_view prefix, std::string_view name, CounterPtr CounterInstance(std::string_view prefix, std::string_view name, std::initializer_list<LabelView> labels,
std::initializer_list<LabelView> labels, std::string_view helptext, std::string_view helptext, std::string_view unit = "",
std::string_view unit = "", prometheus::CollectCallbackPtr callback = nullptr);
prometheus::CollectCallbackPtr callback = nullptr);
/** /**
* @return A gauge metric family. Creates the family lazily if necessary. * @return A gauge metric family. Creates the family lazily if necessary.
@ -108,14 +105,13 @@ public:
* @param helptext Short explanation of the metric. * @param helptext Short explanation of the metric.
* @param unit Unit of measurement. * @param unit Unit of measurement.
*/ */
std::shared_ptr<telemetry::GaugeFamily> GaugeFamily(std::string_view prefix, std::string_view name, GaugeFamilyPtr GaugeFamily(std::string_view prefix, std::string_view name, Span<const std::string_view> labels,
Span<const std::string_view> labels, std::string_view helptext, std::string_view helptext, std::string_view unit = "");
std::string_view unit = "");
/// @copydoc GaugeFamily /// @copydoc GaugeFamily
std::shared_ptr<telemetry::GaugeFamily> GaugeFamily(std::string_view prefix, std::string_view name, GaugeFamilyPtr GaugeFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, std::initializer_list<std::string_view> labels, std::string_view helptext,
std::string_view helptext, std::string_view unit = ""); std::string_view unit = "");
/** /**
* Accesses a gauge instance. Creates the hosting metric family as well * Accesses a gauge instance. Creates the hosting metric family as well
@ -128,14 +124,14 @@ public:
* @param callback Passing a callback method will enable asynchronous mode. The callback method will be called by * @param callback Passing a callback method will enable asynchronous mode. The callback method will be called by
* the metrics subsystem whenever data is requested. * the metrics subsystem whenever data is requested.
*/ */
std::shared_ptr<Gauge> GaugeInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels, GaugePtr GaugeInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels,
std::string_view helptext, std::string_view unit = "", std::string_view helptext, std::string_view unit = "",
prometheus::CollectCallbackPtr callback = nullptr); prometheus::CollectCallbackPtr callback = nullptr);
/// @copydoc GaugeInstance /// @copydoc GaugeInstance
std::shared_ptr<Gauge> GaugeInstance(std::string_view prefix, std::string_view name, GaugePtr GaugeInstance(std::string_view prefix, std::string_view name, std::initializer_list<LabelView> labels,
std::initializer_list<LabelView> labels, std::string_view helptext, std::string_view helptext, std::string_view unit = "",
std::string_view unit = "", prometheus::CollectCallbackPtr callback = nullptr); prometheus::CollectCallbackPtr callback = nullptr);
// Forces the compiler to use the type `Span<const T>` instead of trying to // Forces the compiler to use the type `Span<const T>` instead of trying to
// match parameters to a `span`. // match parameters to a `span`.
@ -166,16 +162,14 @@ public:
* different bucket settings. Users may also override * different bucket settings. Users may also override
* @p bounds via run-time configuration. * @p bounds via run-time configuration.
*/ */
std::shared_ptr<telemetry::HistogramFamily> HistogramFamily(std::string_view prefix, std::string_view name, HistogramFamilyPtr HistogramFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels, Span<const std::string_view> labels, ConstSpan<double> bounds,
ConstSpan<double> bounds, std::string_view helptext, std::string_view helptext, std::string_view unit = "");
std::string_view unit = "");
/// @copydoc HistogramFamily /// @copydoc HistogramFamily
std::shared_ptr<telemetry::HistogramFamily> HistogramFamily(std::string_view prefix, std::string_view name, HistogramFamilyPtr HistogramFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, std::initializer_list<std::string_view> labels, ConstSpan<double> bounds,
ConstSpan<double> bounds, std::string_view helptext, std::string_view helptext, std::string_view unit = "");
std::string_view unit = "");
/** /**
* Returns a histogram. Creates the family lazily if necessary. * Returns a histogram. Creates the family lazily if necessary.
@ -194,15 +188,13 @@ public:
* different bucket settings. Users may also override * different bucket settings. Users may also override
* @p bounds via run-time configuration. * @p bounds via run-time configuration.
*/ */
std::shared_ptr<Histogram> HistogramInstance(std::string_view prefix, std::string_view name, HistogramPtr HistogramInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels,
Span<const LabelView> labels, ConstSpan<double> bounds, ConstSpan<double> bounds, std::string_view helptext, std::string_view unit = "");
std::string_view helptext, std::string_view unit = "");
/// @copdoc HistogramInstance /// @copdoc HistogramInstance
std::shared_ptr<Histogram> HistogramInstance(std::string_view prefix, std::string_view name, HistogramPtr HistogramInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> labels, std::initializer_list<LabelView> labels, std::initializer_list<double> bounds,
std::initializer_list<double> bounds, std::string_view helptext, std::string_view helptext, std::string_view unit = "");
std::string_view unit = "");
/** /**
* @return A JSON description of the cluster configuration for reporting * @return A JSON description of the cluster configuration for reporting
@ -245,10 +237,10 @@ private:
detail::process_stats current_process_stats; detail::process_stats current_process_stats;
double process_stats_last_updated = 0.0; double process_stats_last_updated = 0.0;
std::shared_ptr<Gauge> rss_gauge; GaugePtr rss_gauge;
std::shared_ptr<Gauge> vms_gauge; GaugePtr vms_gauge;
std::shared_ptr<Gauge> cpu_gauge; GaugePtr cpu_gauge;
std::shared_ptr<Gauge> fds_gauge; GaugePtr fds_gauge;
std::string endpoint_name; std::string endpoint_name;
std::vector<std::string> export_prefixes; std::vector<std::string> export_prefixes;