Merge remote-tracking branch 'origin/topic/awelzel/telemetry-script-land-v0'

* origin/topic/awelzel/telemetry-script-land-v0:
  Introduce telemetry framework
  Bump broker submodule to master.
This commit is contained in:
Robin Sommer 2022-08-05 16:50:18 +02:00
commit 686e740bbe
No known key found for this signature in database
GPG key ID: 6BEDA4DA6B8B23E3
36 changed files with 1972 additions and 38 deletions

View file

@ -5,6 +5,8 @@
#include <cstdint>
#include <initializer_list>
#include <string_view>
#include <unordered_map>
#include <variant>
#include <vector>
#include "zeek/IntrusivePtr.h"
@ -20,6 +22,12 @@ namespace broker
class endpoint;
}
namespace zeek
{
class RecordVal;
using RecordValPtr = IntrusivePtr<RecordVal>;
}
namespace zeek::Broker
{
class Manager;
@ -50,6 +58,128 @@ public:
*/
virtual void InitPostScript();
/**
* Supported metric types.
*/
enum class MetricType
{
Counter,
Gauge,
Histogram
};
/**
* Captures information about counter and gauge metrics.
*/
struct CollectedValueMetric
{
/**
* Constructor.
* @param metric_type The type of this metric.
* @param family Broker layer family handle for this metric.
* @param label_values The string values for each of the metric's labels.
* @param value The metric's current value.
*/
CollectedValueMetric(MetricType metric_type,
const broker::telemetry::metric_family_hdl* family,
std::vector<std::string_view> label_values,
std::variant<double, int64_t> value)
: metric_type(metric_type), family(family), label_values(std::move(label_values)),
value(value)
{
}
/**
* @return A script layer Telemetry::Metric record for this metric.
*/
zeek::RecordValPtr AsMetricRecord() const;
enum MetricType metric_type;
const broker::telemetry::metric_family_hdl* family;
std::vector<std::string_view> label_values;
std::variant<double, int64_t> value;
};
/**
* Captures information about histogram metrics.
*/
struct CollectedHistogramMetric
{
/**
* Helper struct representing a single bucket of a histogram.
* @tparam T The data type used by the histogram (double or int64_t).
*/
template <class T> struct Bucket
{
Bucket(T count, T upper_bound) : count(count), upper_bound(upper_bound) { }
T count;
T upper_bound;
};
/**
* Helper struct representing a histogram as sum and buckets.
* @tparam T The data type used by the histogram (double or int64_t).
*/
template <class T> struct HistogramData
{
T sum;
std::vector<Bucket<T>> buckets;
};
using DblHistogramData = HistogramData<double>;
using IntHistogramData = HistogramData<int64_t>;
/**
* Constructor.
* @param family Broker layer family handle for this metric.
* @param label_values The string values for each of the metric's labels.
* @param histogram The histogram's data (sum and individual buckets).
*/
CollectedHistogramMetric(const broker::telemetry::metric_family_hdl* family,
std::vector<std::string_view> label_values,
std::variant<DblHistogramData, IntHistogramData> histogram)
: family(family), label_values(std::move(label_values)), histogram(std::move(histogram))
{
}
const broker::telemetry::metric_family_hdl* family;
std::vector<std::string_view> label_values;
std::variant<DblHistogramData, IntHistogramData> histogram;
/**
* @return A script layer Telemetry::HistogramMetric record for this histogram.
*/
zeek::RecordValPtr AsHistogramMetricRecord() const;
};
/**
* @return A script layer Telemetry::MetricOpts record for the given metric family.
* @param metric_typ The type of metric.
* @param family Broker layer family handle for the family.
* @tparam T The underlying data type (double or int64_t)
*/
template <typename T>
zeek::RecordValPtr GetMetricOptsRecord(MetricType metric_type,
const broker::telemetry::metric_family_hdl* family);
/**
* @return All counter and gauge metrics and their values matching prefix and name.
* @param prefix The prefix pattern to use for filtering. Supports globbing.
* @param name The name pattern to use for filtering. Supports globbing.
*/
std::vector<CollectedValueMetric> CollectMetrics(std::string_view prefix,
std::string_view name);
/**
* @return All histogram metrics and their data matching prefix and name.
* @param prefix The prefix pattern to use for filtering. Supports globbing.
* @param name The name pattern to use for filtering. Supports globbing.
*/
std::vector<CollectedHistogramMetric> CollectHistogramMetrics(std::string_view prefix,
std::string_view name);
/**
* @return A counter metric family. Creates the family lazily if necessary.
* @param prefix The prefix (namespace) this family belongs to.
@ -67,14 +197,14 @@ public:
{
if constexpr ( std::is_same<ValueType, int64_t>::value )
{
auto fam = int_counter_fam(ptr(), prefix, name, labels, helptext, unit, is_sum);
auto fam = int_counter_fam(Ptr(), prefix, name, labels, helptext, unit, is_sum);
return IntCounterFamily{fam};
}
else
{
static_assert(std::is_same<ValueType, double>::value,
"metrics only support int64_t and double values");
auto fam = dbl_counter_fam(ptr(), prefix, name, labels, helptext, unit, is_sum);
auto fam = dbl_counter_fam(Ptr(), prefix, name, labels, helptext, unit, is_sum);
return DblCounterFamily{fam};
}
}
@ -163,14 +293,14 @@ public:
{
if constexpr ( std::is_same<ValueType, int64_t>::value )
{
auto fam = int_gauge_fam(ptr(), prefix, name, labels, helptext, unit, is_sum);
auto fam = int_gauge_fam(Ptr(), prefix, name, labels, helptext, unit, is_sum);
return IntGaugeFamily{fam};
}
else
{
static_assert(std::is_same<ValueType, double>::value,
"metrics only support int64_t and double values");
auto fam = dbl_gauge_fam(ptr(), prefix, name, labels, helptext, unit, is_sum);
auto fam = dbl_gauge_fam(Ptr(), prefix, name, labels, helptext, unit, is_sum);
return DblGaugeFamily{fam};
}
}
@ -281,7 +411,7 @@ public:
{
if constexpr ( std::is_same<ValueType, int64_t>::value )
{
auto fam = int_histogram_fam(ptr(), prefix, name, labels, default_upper_bounds,
auto fam = int_histogram_fam(Ptr(), prefix, name, labels, default_upper_bounds,
helptext, unit, is_sum);
return IntHistogramFamily{fam};
}
@ -289,7 +419,7 @@ public:
{
static_assert(std::is_same<ValueType, double>::value,
"metrics only support int64_t and double values");
auto fam = dbl_histogram_fam(ptr(), prefix, name, labels, default_upper_bounds,
auto fam = dbl_histogram_fam(Ptr(), prefix, name, labels, default_upper_bounds,
helptext, unit, is_sum);
return DblHistogramFamily{fam};
}
@ -396,6 +526,7 @@ protected:
std::string_view buf[10];
for ( size_t index = 0; index < xs.size(); ++index )
buf[index] = xs[index].first;
return continuation(Span{buf, xs.size()});
}
else
@ -403,17 +534,23 @@ protected:
std::vector<std::string_view> buf;
for ( auto x : xs )
buf.emplace_back(x.first, x.second);
return continuation(Span{buf});
}
}
broker::telemetry::metric_registry_impl* ptr() { return pimpl.get(); }
broker::telemetry::metric_registry_impl* Ptr() { return pimpl.get(); }
// Connects all the dots after the Broker Manager constructed the endpoint
// for this Zeek instance. Called from Broker::Manager::InitPostScript().
void InitPostBrokerSetup(broker::endpoint&);
IntrusivePtr<broker::telemetry::metric_registry_impl> pimpl;
private:
// Caching of metric_family_hdl instances to their Zeek record representation.
std::unordered_map<const broker::telemetry::metric_family_hdl*, zeek::RecordValPtr>
metric_opts_cache;
};
} // namespace zeek::telemetry