diff --git a/src/telemetry/Counter.cc b/src/telemetry/Counter.cc index 1b2e0aefae..e8f8841ce2 100644 --- a/src/telemetry/Counter.cc +++ b/src/telemetry/Counter.cc @@ -6,93 +6,49 @@ #include "caf/telemetry/metric_family.hpp" #include "caf/telemetry/metric_family_impl.hpp" -namespace zeek::telemetry { - -// -- private utilities -------------------------------------------------------- - -namespace { +#include "zeek/telemetry/Detail.h" namespace ct = caf::telemetry; -using NativeMetricFamily = ct::metric_family; +namespace zeek::telemetry { -using NativeIntCounter = ct::int_counter; +// -- bindings to private utility functions ------------------------------------ -using NativeIntCounterFamily = ct::metric_family_impl; - -using NativeDblCounter = ct::dbl_counter; - -using NativeDblCounterFamily = ct::metric_family_impl; - -auto& deref(IntCounter::Impl* ptr) +template <> +struct PimplTrait { - return *reinterpret_cast(ptr); - } + using Native = ct::int_counter; + using Oqaque = IntCounter::Impl; + using NativeFamily = ct::metric_family_impl; + }; -auto& deref(IntCounterFamily*, MetricFamily::Impl* ptr) +template <> +struct PimplTrait : PimplTrait { }; + +template <> +struct PimplTrait { - auto basePtr = reinterpret_cast(ptr); - return *static_cast(basePtr); - } + using Native = typename PimplTrait::NativeFamily; + using Oqaque = IntCounterFamily::Impl; + }; -auto upcast(IntCounterFamily::Impl* ptr) +template <> +struct PimplTrait { - auto native = reinterpret_cast(ptr); - auto basePtr = static_cast(native); - return reinterpret_cast(basePtr); - } + using Native = ct::dbl_counter; + using Oqaque = DblCounter::Impl; + using NativeFamily = ct::metric_family_impl; + }; -auto opaque(NativeIntCounter* ptr) +template <> +struct PimplTrait : PimplTrait { }; + +template <> +struct PimplTrait { - return reinterpret_cast(ptr); - } - -auto& deref(DblCounter::Impl* ptr) - { - return *reinterpret_cast(ptr); - } - -auto& deref(DblCounterFamily*, MetricFamily::Impl* ptr) - { - auto basePtr = reinterpret_cast(ptr); - return *static_cast(basePtr); - } - -auto upcast(DblCounterFamily::Impl* ptr) - { - auto native = reinterpret_cast(ptr); - auto basePtr = static_cast(native); - return reinterpret_cast(basePtr); - } - -auto opaque(NativeDblCounter* ptr) - { - return reinterpret_cast(ptr); - } - -template -auto withNativeLabels(Span xs, F continuation) - { - if ( xs.size() <= 10 ) - { - ct::label_view buf[10]={ - {{},{}}, {{},{}}, {{},{}}, {{},{}}, {{},{}}, - {{},{}}, {{},{}}, {{},{}}, {{},{}}, {{},{}}, - }; - for ( size_t index = 0; index < xs.size(); ++index ) - buf[index] = ct::label_view{xs[index].first, xs[index].second}; - return continuation(Span{buf, xs.size()}); - } - else - { - std::vector buf; - for ( auto x : xs ) - buf.emplace_back(x.first, x.second); - return continuation(Span{buf}); - } - } - -} // namespace + using Native = typename PimplTrait::NativeFamily; + using Oqaque = DblCounterFamily::Impl; + }; // -- IntCounter --------------------------------------------------------------- @@ -122,7 +78,7 @@ IntCounterFamily::IntCounterFamily(Impl* ptr) : MetricFamily(upcast(ptr)) IntCounter IntCounterFamily::GetOrAdd(Span labels) { - return withNativeLabels(labels, [this](auto nativeLabels) + return with_native_labels(labels, [this](auto nativeLabels) { auto hdl = opaque(deref(this, pimpl).get_or_add(nativeLabels)); return IntCounter{hdl}; @@ -152,7 +108,7 @@ DblCounterFamily::DblCounterFamily(Impl* ptr) : MetricFamily(upcast(ptr)) DblCounter DblCounterFamily::GetOrAdd(Span labels) { - return withNativeLabels(labels, [this](auto nativeLabels) + return with_native_labels(labels, [this](auto nativeLabels) { auto hdl = opaque(deref(this, pimpl).get_or_add(nativeLabels)); return DblCounter{hdl}; diff --git a/src/telemetry/Counter.h b/src/telemetry/Counter.h index 6c4d88ce0a..885a0f20f3 100644 --- a/src/telemetry/Counter.h +++ b/src/telemetry/Counter.h @@ -91,6 +91,8 @@ public: class Impl; + using InstanceType = IntCounter; + IntCounterFamily(const IntCounterFamily&) noexcept = default; IntCounterFamily& operator=(const IntCounterFamily&) noexcept = default; @@ -183,6 +185,8 @@ public: class Impl; + using InstanceType = DblCounter; + DblCounterFamily(const DblCounterFamily&) noexcept = default; DblCounterFamily& operator=(const DblCounterFamily&) noexcept = default; diff --git a/src/telemetry/Detail.h b/src/telemetry/Detail.h new file mode 100644 index 0000000000..15a3d867f3 --- /dev/null +++ b/src/telemetry/Detail.h @@ -0,0 +1,90 @@ +// See the file "COPYING" in the main distribution directory for copyright. + +// This header contains private implementation details for telemetry classes +// and should not get included outside of .cc files. + +#pragma once + +#include +#include +#include + +#include "zeek/Span.h" +#include "zeek/telemetry/MetricFamily.h" + +#include "caf/telemetry/label_view.hpp" +#include "caf/telemetry/metric_family.hpp" + +namespace zeek::telemetry { + +/** + * This trait must provide the member types @c Native for referring to the CAF + * type, @c Opaque for referring to the @c Impl type. For instance types such as + * @c IntCounter, the trait must also provide the member type @c NativeFamily. + */ +template +struct PimplTrait; + +template ::Native> +auto& deref(T* ptr) + { + return *reinterpret_cast(ptr); + } + +template +auto& deref(Family*, MetricFamily::Impl* ptr) + { + using InstanceType = typename Family::InstanceType; + using ImplType = typename InstanceType::Impl; + using NativeType = typename PimplTrait::NativeFamily; + return *reinterpret_cast(ptr); + } + +template ::Oqaque> +auto opaque(T* ptr) + { + return reinterpret_cast(ptr); + } + +template +auto opaque(const Family*, MetricFamily::Impl* ptr) + { + using InstanceType = typename Family::InstanceType; + using ImplType = typename InstanceType::Impl; + using OpaqueType = typename PimplTrait::NativeFamily; + return reinterpret_cast(ptr); + } + +template ::Native> +auto upcast(T* ptr) + { + auto native = reinterpret_cast(ptr); + auto base_ptr = static_cast(native); + return reinterpret_cast(base_ptr); + } + +template +auto with_native_labels(Span xs, F continuation) + { + namespace ct = caf::telemetry; + + if ( xs.size() <= 10 ) + { + ct::label_view buf[10]={ + {{},{}}, {{},{}}, {{},{}}, {{},{}}, {{},{}}, + {{},{}}, {{},{}}, {{},{}}, {{},{}}, {{},{}}, + }; + for ( size_t index = 0; index < xs.size(); ++index ) + buf[index] = ct::label_view{xs[index].first, xs[index].second}; + return continuation(Span{buf, xs.size()}); + } + else + { + std::vector buf; + for ( auto x : xs ) + buf.emplace_back(x.first, x.second); + return continuation(Span{buf}); + } + } + +} // namespace zeek::telemetry diff --git a/src/telemetry/Gauge.cc b/src/telemetry/Gauge.cc index 4c12477f32..0cd4a1a91e 100644 --- a/src/telemetry/Gauge.cc +++ b/src/telemetry/Gauge.cc @@ -6,93 +6,49 @@ #include "caf/telemetry/metric_family.hpp" #include "caf/telemetry/metric_family_impl.hpp" -namespace zeek::telemetry { - -// -- private utilities -------------------------------------------------------- - -namespace { +#include "zeek/telemetry/Detail.h" namespace ct = caf::telemetry; -using NativeMetricFamily = ct::metric_family; +namespace zeek::telemetry { -using NativeIntGauge = ct::int_gauge; +// -- bindings to private utility functions ------------------------------------ -using NativeIntGaugeFamily = ct::metric_family_impl; - -using NativeDblGauge = ct::dbl_gauge; - -using NativeDblGaugeFamily = ct::metric_family_impl; - -auto& deref(IntGauge::Impl* ptr) +template <> +struct PimplTrait { - return *reinterpret_cast(ptr); - } + using Native = ct::int_gauge; + using Oqaque = IntGauge::Impl; + using NativeFamily = ct::metric_family_impl; + }; -auto& deref(IntGaugeFamily*, MetricFamily::Impl* ptr) +template <> +struct PimplTrait : PimplTrait { }; + +template <> +struct PimplTrait { - auto basePtr = reinterpret_cast(ptr); - return *static_cast(basePtr); - } + using Native = typename PimplTrait::NativeFamily; + using Oqaque = IntGaugeFamily::Impl; + }; -auto upcast(IntGaugeFamily::Impl* ptr) +template <> +struct PimplTrait { - auto native = reinterpret_cast(ptr); - auto basePtr = static_cast(native); - return reinterpret_cast(basePtr); - } + using Native = ct::dbl_gauge; + using Oqaque = DblGauge::Impl; + using NativeFamily = ct::metric_family_impl; + }; -auto opaque(NativeIntGauge* ptr) +template <> +struct PimplTrait : PimplTrait { }; + +template <> +struct PimplTrait { - return reinterpret_cast(ptr); - } - -auto& deref(DblGauge::Impl* ptr) - { - return *reinterpret_cast(ptr); - } - -auto& deref(DblGaugeFamily*, MetricFamily::Impl* ptr) - { - auto basePtr = reinterpret_cast(ptr); - return *static_cast(basePtr); - } - -auto upcast(DblGaugeFamily::Impl* ptr) - { - auto native = reinterpret_cast(ptr); - auto basePtr = static_cast(native); - return reinterpret_cast(basePtr); - } - -auto opaque(NativeDblGauge* ptr) - { - return reinterpret_cast(ptr); - } - -template -auto withNativeLabels(Span xs, F continuation) - { - if ( xs.size() <= 10 ) - { - ct::label_view buf[10]={ - {{},{}}, {{},{}}, {{},{}}, {{},{}}, {{},{}}, - {{},{}}, {{},{}}, {{},{}}, {{},{}}, {{},{}}, - }; - for ( size_t index = 0; index < xs.size(); ++index ) - buf[index] = ct::label_view{xs[index].first, xs[index].second}; - return continuation(Span{buf, xs.size()}); - } - else - { - std::vector buf; - for ( auto x : xs ) - buf.emplace_back(x.first, x.second); - return continuation(Span{buf}); - } - } - -} // namespace + using Native = typename PimplTrait::NativeFamily; + using Oqaque = DblGaugeFamily::Impl; + }; // -- IntGauge --------------------------------------------------------------- @@ -137,7 +93,7 @@ IntGaugeFamily::IntGaugeFamily(Impl* ptr) : MetricFamily(upcast(ptr)) IntGauge IntGaugeFamily::GetOrAdd(Span labels) { - return withNativeLabels(labels, [this](auto nativeLabels) + return with_native_labels(labels, [this](auto nativeLabels) { auto hdl = opaque(deref(this, pimpl).get_or_add(nativeLabels)); return IntGauge{hdl}; @@ -177,7 +133,7 @@ DblGaugeFamily::DblGaugeFamily(Impl* ptr) : MetricFamily(upcast(ptr)) DblGauge DblGaugeFamily::GetOrAdd(Span labels) { - return withNativeLabels(labels, [this](auto nativeLabels) + return with_native_labels(labels, [this](auto nativeLabels) { auto hdl = opaque(deref(this, pimpl).get_or_add(nativeLabels)); return DblGauge{hdl}; diff --git a/src/telemetry/Gauge.h b/src/telemetry/Gauge.h index 8d4121f301..25ba22ee0b 100644 --- a/src/telemetry/Gauge.h +++ b/src/telemetry/Gauge.h @@ -107,6 +107,8 @@ public: class Impl; + using InstanceType = IntGauge; + IntGaugeFamily(const IntGaugeFamily&) noexcept = default; IntGaugeFamily& operator=(const IntGaugeFamily&) noexcept = default; @@ -209,6 +211,8 @@ public: class Impl; + using InstanceType = DblGauge; + DblGaugeFamily(const DblGaugeFamily&) noexcept = default; DblGaugeFamily& operator=(const DblGaugeFamily&) noexcept = default;