145 lines
4.1 KiB
C++
145 lines
4.1 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Observability/Context.hpp>
|
|
#include <Void/Observability/Hooks.hpp>
|
|
#include <Void/Time/Clock.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Void::Observability
|
|
{
|
|
|
|
struct MetricsLimits {
|
|
std::size_t max_instruments{256};
|
|
std::size_t max_series_per_instrument{1024};
|
|
std::size_t max_total_series{4096};
|
|
std::size_t max_labels_per_point{8};
|
|
std::size_t max_key_bytes{128};
|
|
std::size_t max_value_bytes{256};
|
|
std::size_t max_producer_shards{32};
|
|
std::size_t max_storage_bytes{32ULL * 1024ULL * 1024ULL};
|
|
|
|
[[nodiscard]] Void::Result::Result<void> validate() const;
|
|
};
|
|
|
|
[[nodiscard]] inline std::vector<double> default_duration_buckets_seconds()
|
|
{
|
|
return {0.0001, 0.0005, 0.001, 0.005, 0.01, 0.025, 0.05,
|
|
0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0};
|
|
}
|
|
|
|
struct InstrumentSpec {
|
|
std::string name{};
|
|
std::string unit{};
|
|
MetricKind kind{MetricKind::counter};
|
|
std::vector<std::string> label_keys{};
|
|
std::vector<double> bucket_bounds{};
|
|
};
|
|
|
|
struct InstrumentHandle {
|
|
std::size_t index{0};
|
|
std::uint64_t registry_id{0};
|
|
|
|
[[nodiscard]] constexpr bool valid() const noexcept
|
|
{
|
|
return registry_id != 0;
|
|
}
|
|
};
|
|
|
|
struct HistogramSnapshot {
|
|
std::vector<double> bounds{};
|
|
std::vector<std::uint64_t> bucket_counts{};
|
|
std::uint64_t count{0};
|
|
double sum{0.0};
|
|
};
|
|
|
|
struct SeriesSnapshot {
|
|
std::vector<std::pair<std::string, std::string>> labels{};
|
|
bool is_overflow{false};
|
|
std::uint64_t counter_value{0};
|
|
bool counter_saturated{false};
|
|
double gauge_value{0.0};
|
|
bool has_gauge_sample{false};
|
|
Void::Time::MonotonicClock::time_point gauge_timestamp{};
|
|
HistogramSnapshot histogram{};
|
|
};
|
|
|
|
struct InstrumentSnapshot {
|
|
InstrumentSpec spec{};
|
|
std::vector<SeriesSnapshot> series{};
|
|
std::uint64_t dropped_series{0};
|
|
std::uint64_t overflow_events{0};
|
|
};
|
|
|
|
struct MetricsSnapshot {
|
|
std::uint64_t epoch{1};
|
|
std::vector<InstrumentSnapshot> instruments{};
|
|
std::uint64_t dropped_series{0};
|
|
std::uint64_t overflow_events{0};
|
|
std::uint64_t rejected_invalid_points{0};
|
|
std::uint64_t shared_shard_fallbacks{0};
|
|
std::uint64_t unregistered_hook_points{0};
|
|
std::size_t shard_count{0};
|
|
std::size_t estimated_bytes{0};
|
|
};
|
|
|
|
/// Thread-safe, sharded, cardinality- and byte-bounded metric registry with
|
|
/// frozen schemas and cumulative snapshot export.
|
|
class MetricRegistry
|
|
{
|
|
public:
|
|
[[nodiscard]] static Void::Result::Result<MetricRegistry> create(
|
|
MetricsLimits limits = {},
|
|
const Void::Time::MonotonicClock& clock =
|
|
Void::Time::SystemMonotonicClock::instance());
|
|
|
|
MetricRegistry(MetricRegistry&&) noexcept = default;
|
|
MetricRegistry& operator=(MetricRegistry&&) noexcept = default;
|
|
MetricRegistry(const MetricRegistry&) = delete;
|
|
MetricRegistry& operator=(const MetricRegistry&) = delete;
|
|
~MetricRegistry();
|
|
|
|
[[nodiscard]] Void::Result::Result<InstrumentHandle>
|
|
register_instrument(InstrumentSpec spec);
|
|
|
|
[[nodiscard]] Void::Result::Result<void> add_counter(
|
|
InstrumentHandle handle,
|
|
std::uint64_t delta,
|
|
std::span<const AttributeView> attributes = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<void> set_gauge(
|
|
InstrumentHandle handle,
|
|
double value,
|
|
std::span<const AttributeView> attributes = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<void> observe_histogram(
|
|
InstrumentHandle handle,
|
|
double value,
|
|
std::span<const AttributeView> attributes = {});
|
|
|
|
[[nodiscard]] MetricsSnapshot snapshot() const;
|
|
void reset_epoch();
|
|
|
|
[[nodiscard]] ObservationHooks make_hooks() const noexcept;
|
|
[[nodiscard]] std::shared_ptr<const void> keepalive() const noexcept;
|
|
[[nodiscard]] const MetricsLimits& limits() const noexcept;
|
|
|
|
private:
|
|
struct Impl;
|
|
explicit MetricRegistry(std::shared_ptr<Impl> impl) noexcept;
|
|
|
|
std::shared_ptr<Impl> impl_;
|
|
};
|
|
|
|
} // namespace Void::Observability
|