mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
Move duplicated code to detail header
This commit is contained in:
parent
2a21f2903a
commit
0b665ee130
5 changed files with 164 additions and 154 deletions
|
@ -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<NativeIntCounter>;
|
||||
|
||||
using NativeDblCounter = ct::dbl_counter;
|
||||
|
||||
using NativeDblCounterFamily = ct::metric_family_impl<NativeDblCounter>;
|
||||
|
||||
auto& deref(IntCounter::Impl* ptr)
|
||||
template <>
|
||||
struct PimplTrait<IntCounter::Impl>
|
||||
{
|
||||
return *reinterpret_cast<NativeIntCounter*>(ptr);
|
||||
}
|
||||
using Native = ct::int_counter;
|
||||
using Oqaque = IntCounter::Impl;
|
||||
using NativeFamily = ct::metric_family_impl<Native>;
|
||||
};
|
||||
|
||||
auto& deref(IntCounterFamily*, MetricFamily::Impl* ptr)
|
||||
template <>
|
||||
struct PimplTrait<ct::int_counter> : PimplTrait<IntCounter::Impl> { };
|
||||
|
||||
template <>
|
||||
struct PimplTrait<IntCounterFamily::Impl>
|
||||
{
|
||||
auto basePtr = reinterpret_cast<NativeMetricFamily*>(ptr);
|
||||
return *static_cast<NativeIntCounterFamily*>(basePtr);
|
||||
}
|
||||
using Native = typename PimplTrait<IntCounter::Impl>::NativeFamily;
|
||||
using Oqaque = IntCounterFamily::Impl;
|
||||
};
|
||||
|
||||
auto upcast(IntCounterFamily::Impl* ptr)
|
||||
template <>
|
||||
struct PimplTrait<DblCounter::Impl>
|
||||
{
|
||||
auto native = reinterpret_cast<NativeIntCounterFamily*>(ptr);
|
||||
auto basePtr = static_cast<NativeMetricFamily*>(native);
|
||||
return reinterpret_cast<MetricFamily::Impl*>(basePtr);
|
||||
}
|
||||
using Native = ct::dbl_counter;
|
||||
using Oqaque = DblCounter::Impl;
|
||||
using NativeFamily = ct::metric_family_impl<Native>;
|
||||
};
|
||||
|
||||
auto opaque(NativeIntCounter* ptr)
|
||||
template <>
|
||||
struct PimplTrait<ct::dbl_counter> : PimplTrait<DblCounter::Impl> { };
|
||||
|
||||
template <>
|
||||
struct PimplTrait<DblCounterFamily::Impl>
|
||||
{
|
||||
return reinterpret_cast<IntCounter::Impl*>(ptr);
|
||||
}
|
||||
|
||||
auto& deref(DblCounter::Impl* ptr)
|
||||
{
|
||||
return *reinterpret_cast<ct::dbl_counter*>(ptr);
|
||||
}
|
||||
|
||||
auto& deref(DblCounterFamily*, MetricFamily::Impl* ptr)
|
||||
{
|
||||
auto basePtr = reinterpret_cast<NativeMetricFamily*>(ptr);
|
||||
return *static_cast<NativeDblCounterFamily*>(basePtr);
|
||||
}
|
||||
|
||||
auto upcast(DblCounterFamily::Impl* ptr)
|
||||
{
|
||||
auto native = reinterpret_cast<NativeDblCounterFamily*>(ptr);
|
||||
auto basePtr = static_cast<NativeMetricFamily*>(native);
|
||||
return reinterpret_cast<MetricFamily::Impl*>(basePtr);
|
||||
}
|
||||
|
||||
auto opaque(NativeDblCounter* ptr)
|
||||
{
|
||||
return reinterpret_cast<DblCounter::Impl*>(ptr);
|
||||
}
|
||||
|
||||
template <class F>
|
||||
auto withNativeLabels(Span<const LabelView> 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<ct::label_view> buf;
|
||||
for ( auto x : xs )
|
||||
buf.emplace_back(x.first, x.second);
|
||||
return continuation(Span{buf});
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
using Native = typename PimplTrait<DblCounter::Impl>::NativeFamily;
|
||||
using Oqaque = DblCounterFamily::Impl;
|
||||
};
|
||||
|
||||
// -- IntCounter ---------------------------------------------------------------
|
||||
|
||||
|
@ -122,7 +78,7 @@ IntCounterFamily::IntCounterFamily(Impl* ptr) : MetricFamily(upcast(ptr))
|
|||
|
||||
IntCounter IntCounterFamily::GetOrAdd(Span<const LabelView> 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<const LabelView> 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};
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
90
src/telemetry/Detail.h
Normal file
90
src/telemetry/Detail.h
Normal file
|
@ -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 <cstdint>
|
||||
#include <initializer_list>
|
||||
#include <type_traits>
|
||||
|
||||
#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 <class T>
|
||||
struct PimplTrait;
|
||||
|
||||
template <class T, class NativeType = typename PimplTrait<T>::Native>
|
||||
auto& deref(T* ptr)
|
||||
{
|
||||
return *reinterpret_cast<NativeType*>(ptr);
|
||||
}
|
||||
|
||||
template <class Family>
|
||||
auto& deref(Family*, MetricFamily::Impl* ptr)
|
||||
{
|
||||
using InstanceType = typename Family::InstanceType;
|
||||
using ImplType = typename InstanceType::Impl;
|
||||
using NativeType = typename PimplTrait<ImplType>::NativeFamily;
|
||||
return *reinterpret_cast<NativeType*>(ptr);
|
||||
}
|
||||
|
||||
template <class T, class OpaqueType = typename PimplTrait<T>::Oqaque>
|
||||
auto opaque(T* ptr)
|
||||
{
|
||||
return reinterpret_cast<OpaqueType*>(ptr);
|
||||
}
|
||||
|
||||
template <class Family>
|
||||
auto opaque(const Family*, MetricFamily::Impl* ptr)
|
||||
{
|
||||
using InstanceType = typename Family::InstanceType;
|
||||
using ImplType = typename InstanceType::Impl;
|
||||
using OpaqueType = typename PimplTrait<ImplType>::NativeFamily;
|
||||
return reinterpret_cast<OpaqueType*>(ptr);
|
||||
}
|
||||
|
||||
template <class T, class Native = typename PimplTrait<T>::Native>
|
||||
auto upcast(T* ptr)
|
||||
{
|
||||
auto native = reinterpret_cast<Native*>(ptr);
|
||||
auto base_ptr = static_cast<caf::telemetry::metric_family*>(native);
|
||||
return reinterpret_cast<MetricFamily::Impl*>(base_ptr);
|
||||
}
|
||||
|
||||
template <class F>
|
||||
auto with_native_labels(Span<const LabelView> 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<ct::label_view> buf;
|
||||
for ( auto x : xs )
|
||||
buf.emplace_back(x.first, x.second);
|
||||
return continuation(Span{buf});
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace zeek::telemetry
|
|
@ -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<NativeIntGauge>;
|
||||
|
||||
using NativeDblGauge = ct::dbl_gauge;
|
||||
|
||||
using NativeDblGaugeFamily = ct::metric_family_impl<NativeDblGauge>;
|
||||
|
||||
auto& deref(IntGauge::Impl* ptr)
|
||||
template <>
|
||||
struct PimplTrait<IntGauge::Impl>
|
||||
{
|
||||
return *reinterpret_cast<NativeIntGauge*>(ptr);
|
||||
}
|
||||
using Native = ct::int_gauge;
|
||||
using Oqaque = IntGauge::Impl;
|
||||
using NativeFamily = ct::metric_family_impl<Native>;
|
||||
};
|
||||
|
||||
auto& deref(IntGaugeFamily*, MetricFamily::Impl* ptr)
|
||||
template <>
|
||||
struct PimplTrait<ct::int_gauge> : PimplTrait<IntGauge::Impl> { };
|
||||
|
||||
template <>
|
||||
struct PimplTrait<IntGaugeFamily::Impl>
|
||||
{
|
||||
auto basePtr = reinterpret_cast<NativeMetricFamily*>(ptr);
|
||||
return *static_cast<NativeIntGaugeFamily*>(basePtr);
|
||||
}
|
||||
using Native = typename PimplTrait<IntGauge::Impl>::NativeFamily;
|
||||
using Oqaque = IntGaugeFamily::Impl;
|
||||
};
|
||||
|
||||
auto upcast(IntGaugeFamily::Impl* ptr)
|
||||
template <>
|
||||
struct PimplTrait<DblGauge::Impl>
|
||||
{
|
||||
auto native = reinterpret_cast<NativeIntGaugeFamily*>(ptr);
|
||||
auto basePtr = static_cast<NativeMetricFamily*>(native);
|
||||
return reinterpret_cast<MetricFamily::Impl*>(basePtr);
|
||||
}
|
||||
using Native = ct::dbl_gauge;
|
||||
using Oqaque = DblGauge::Impl;
|
||||
using NativeFamily = ct::metric_family_impl<Native>;
|
||||
};
|
||||
|
||||
auto opaque(NativeIntGauge* ptr)
|
||||
template <>
|
||||
struct PimplTrait<ct::dbl_gauge> : PimplTrait<DblGauge::Impl> { };
|
||||
|
||||
template <>
|
||||
struct PimplTrait<DblGaugeFamily::Impl>
|
||||
{
|
||||
return reinterpret_cast<IntGauge::Impl*>(ptr);
|
||||
}
|
||||
|
||||
auto& deref(DblGauge::Impl* ptr)
|
||||
{
|
||||
return *reinterpret_cast<ct::dbl_gauge*>(ptr);
|
||||
}
|
||||
|
||||
auto& deref(DblGaugeFamily*, MetricFamily::Impl* ptr)
|
||||
{
|
||||
auto basePtr = reinterpret_cast<NativeMetricFamily*>(ptr);
|
||||
return *static_cast<NativeDblGaugeFamily*>(basePtr);
|
||||
}
|
||||
|
||||
auto upcast(DblGaugeFamily::Impl* ptr)
|
||||
{
|
||||
auto native = reinterpret_cast<NativeDblGaugeFamily*>(ptr);
|
||||
auto basePtr = static_cast<NativeMetricFamily*>(native);
|
||||
return reinterpret_cast<MetricFamily::Impl*>(basePtr);
|
||||
}
|
||||
|
||||
auto opaque(NativeDblGauge* ptr)
|
||||
{
|
||||
return reinterpret_cast<DblGauge::Impl*>(ptr);
|
||||
}
|
||||
|
||||
template <class F>
|
||||
auto withNativeLabels(Span<const LabelView> 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<ct::label_view> buf;
|
||||
for ( auto x : xs )
|
||||
buf.emplace_back(x.first, x.second);
|
||||
return continuation(Span{buf});
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
using Native = typename PimplTrait<DblGauge::Impl>::NativeFamily;
|
||||
using Oqaque = DblGaugeFamily::Impl;
|
||||
};
|
||||
|
||||
// -- IntGauge ---------------------------------------------------------------
|
||||
|
||||
|
@ -137,7 +93,7 @@ IntGaugeFamily::IntGaugeFamily(Impl* ptr) : MetricFamily(upcast(ptr))
|
|||
|
||||
IntGauge IntGaugeFamily::GetOrAdd(Span<const LabelView> 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<const LabelView> 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};
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue