#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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 validate() const; }; [[nodiscard]] inline std::vector 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 label_keys{}; std::vector 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 bounds{}; std::vector bucket_counts{}; std::uint64_t count{0}; double sum{0.0}; }; struct SeriesSnapshot { std::vector> 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 series{}; std::uint64_t dropped_series{0}; std::uint64_t overflow_events{0}; }; struct MetricsSnapshot { std::uint64_t epoch{1}; std::vector 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 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 register_instrument(InstrumentSpec spec); [[nodiscard]] Void::Result::Result add_counter( InstrumentHandle handle, std::uint64_t delta, std::span attributes = {}); [[nodiscard]] Void::Result::Result set_gauge( InstrumentHandle handle, double value, std::span attributes = {}); [[nodiscard]] Void::Result::Result observe_histogram( InstrumentHandle handle, double value, std::span attributes = {}); [[nodiscard]] MetricsSnapshot snapshot() const; void reset_epoch(); [[nodiscard]] ObservationHooks make_hooks() const noexcept; [[nodiscard]] std::shared_ptr keepalive() const noexcept; [[nodiscard]] const MetricsLimits& limits() const noexcept; private: struct Impl; explicit MetricRegistry(std::shared_ptr impl) noexcept; std::shared_ptr impl_; }; } // namespace Void::Observability