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;
};
using CounterPtr = std::shared_ptr<Counter>;
class CounterFamily : public MetricFamily, public std::enable_shared_from_this<CounterFamily> {
public:
static inline const char* OpaqueName = "CounterMetricFamilyVal";
@ -72,19 +74,20 @@ public:
* Returns the metrics handle for given labels, creating a new instance
* 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
*/
std::shared_ptr<Counter> GetOrAdd(std::initializer_list<LabelView> labels,
prometheus::CollectCallbackPtr callback = nullptr);
CounterPtr GetOrAdd(std::initializer_list<LabelView> labels, prometheus::CollectCallbackPtr callback = nullptr);
zeek_int_t MetricType() const noexcept override { return BifEnum::Telemetry::MetricType::COUNTER; }
private:
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

View file

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

View file

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

View file

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

View file

@ -69,14 +69,13 @@ public:
* @param helptext Short explanation of the metric.
* @param unit Unit of measurement.
*/
std::shared_ptr<telemetry::CounterFamily> CounterFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels,
CounterFamilyPtr CounterFamily(std::string_view prefix, std::string_view name, Span<const std::string_view> labels,
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 = "");
CounterFamilyPtr CounterFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, std::string_view helptext,
std::string_view unit = "");
/**
* Accesses a counter instance. Creates the hosting metric family as well
@ -89,15 +88,13 @@ public:
* @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 = "",
CounterPtr CounterInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels,
std::string_view helptext, 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 = "",
CounterPtr CounterInstance(std::string_view prefix, std::string_view name, std::initializer_list<LabelView> labels,
std::string_view helptext, std::string_view unit = "",
prometheus::CollectCallbackPtr callback = nullptr);
/**
@ -108,14 +105,13 @@ public:
* @param helptext Short explanation of the metric.
* @param unit Unit of measurement.
*/
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 = "");
GaugeFamilyPtr GaugeFamily(std::string_view prefix, std::string_view name, Span<const std::string_view> labels,
std::string_view helptext, 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 = "");
GaugeFamilyPtr GaugeFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, std::string_view helptext,
std::string_view unit = "");
/**
* 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
* 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 = "",
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 = "", prometheus::CollectCallbackPtr callback = nullptr);
GaugePtr GaugeInstance(std::string_view prefix, std::string_view name, std::initializer_list<LabelView> labels,
std::string_view helptext, 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`.
@ -166,16 +162,14 @@ public:
* different bucket settings. Users may also override
* @p bounds via run-time configuration.
*/
std::shared_ptr<telemetry::HistogramFamily> HistogramFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels,
ConstSpan<double> bounds, std::string_view helptext,
std::string_view unit = "");
HistogramFamilyPtr HistogramFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels, ConstSpan<double> bounds,
std::string_view helptext, std::string_view unit = "");
/// @copydoc HistogramFamily
std::shared_ptr<telemetry::HistogramFamily> HistogramFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels,
ConstSpan<double> bounds, std::string_view helptext,
std::string_view unit = "");
HistogramFamilyPtr HistogramFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, ConstSpan<double> bounds,
std::string_view helptext, std::string_view unit = "");
/**
* Returns a histogram. Creates the family lazily if necessary.
@ -194,15 +188,13 @@ public:
* different bucket settings. Users may also override
* @p bounds via run-time configuration.
*/
std::shared_ptr<Histogram> HistogramInstance(std::string_view prefix, std::string_view name,
Span<const LabelView> labels, ConstSpan<double> bounds,
std::string_view helptext, std::string_view unit = "");
HistogramPtr HistogramInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels,
ConstSpan<double> bounds, std::string_view helptext, std::string_view unit = "");
/// @copdoc HistogramInstance
std::shared_ptr<Histogram> HistogramInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> labels,
std::initializer_list<double> bounds, std::string_view helptext,
std::string_view unit = "");
HistogramPtr HistogramInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> labels, std::initializer_list<double> bounds,
std::string_view helptext, std::string_view unit = "");
/**
* @return A JSON description of the cluster configuration for reporting
@ -245,10 +237,10 @@ private:
detail::process_stats current_process_stats;
double process_stats_last_updated = 0.0;
std::shared_ptr<Gauge> rss_gauge;
std::shared_ptr<Gauge> vms_gauge;
std::shared_ptr<Gauge> cpu_gauge;
std::shared_ptr<Gauge> fds_gauge;
GaugePtr rss_gauge;
GaugePtr vms_gauge;
GaugePtr cpu_gauge;
GaugePtr fds_gauge;
std::string endpoint_name;
std::vector<std::string> export_prefixes;