diff --git a/src/metric.cpp b/src/metric.cpp new file mode 100644 index 0000000..0689aca --- /dev/null +++ b/src/metric.cpp @@ -0,0 +1,64 @@ +#include "metric.hpp" +#include +#include +#include +#include +#include + +namespace metric { +struct Counter::State {}; +struct Gauge::State {}; +struct Histogram::State {}; +struct Metric { + + static std::mutex mutex; + + struct MetricKey { + std::string_view name; + std::vector> labels; + }; + + struct PerThreadState { + std::unordered_map counters; + std::unordered_map histograms; + }; + + static std::unordered_map perThreadState; + + struct ThreadInit { + ThreadInit() { + std::unique_lock _{mutex}; + perThreadState[std::this_thread::get_id()] = {}; + } + ~ThreadInit() { + std::unique_lock _{mutex}; + perThreadState.erase(std::this_thread::get_id()); + } + }; + static thread_local ThreadInit thread_init; +}; + +void Counter::inc(double x) {} +void Gauge::inc(double x) {} +void Gauge::dec(double x) {} +void Gauge::set(double x) {} +void Histogram::observe(double x) {} + +template <> +Counter Family::create( + std::initializer_list>) {} +template <> +Gauge Family::create( + std::initializer_list>) {} +template <> +Histogram Family::create( + std::initializer_list>) {} + +Family create_counter(std::string_view name, std::string_view help) {} +Family create_gauge(std::string_view name, std::string_view help) {} +Family create_histogram(std::string_view name, std::string_view help, + std::initializer_list buckets) {} + +std::span render(ArenaAllocator &) {} + +} // namespace metric diff --git a/src/metric.hpp b/src/metric.hpp new file mode 100644 index 0000000..25ae7fa --- /dev/null +++ b/src/metric.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include "arena_allocator.hpp" +#include +#include +#include +#include + +namespace metric { + +struct Counter { + void inc(double = 1.0); + +private: + Counter(); + friend struct Metric; + struct State; + State *p; +}; + +struct Gauge { + void inc(double = 1.0); + void dec(double = 1.0); + void set(double); + +private: + Gauge(); + friend struct Metric; + struct State; + State *p; +}; + +struct Histogram { + void observe(double); + +private: + Histogram(); + friend struct Metric; + struct State; + State *p; +}; + +template struct Family { + static_assert(std::is_same_v || std::is_same_v || + std::is_same_v); + T create( + std::initializer_list>); + +private: + friend struct Metric; + std::string_view name; + std::string_view help; +}; + +// All std::string_view's should point to static memory +Family create_counter(std::string_view name, std::string_view help); +Family create_gauge(std::string_view name, std::string_view help); +Family create_histogram(std::string_view name, std::string_view help, + std::initializer_list buckets); + +std::span render(ArenaAllocator &); + +} // namespace metric