Metrics system scaffold

This commit is contained in:
2025-08-28 17:04:53 -04:00
parent ed6e6ea9fe
commit 9c89eba6c8
2 changed files with 127 additions and 0 deletions

64
src/metric.cpp Normal file
View File

@@ -0,0 +1,64 @@
#include "metric.hpp"
#include <mutex>
#include <string_view>
#include <thread>
#include <unordered_map>
#include <vector>
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<std::pair<std::string, std::string>> labels;
};
struct PerThreadState {
std::unordered_map<MetricKey, Counter::State> counters;
std::unordered_map<MetricKey, Histogram::State> histograms;
};
static std::unordered_map<std::thread::id, PerThreadState> perThreadState;
struct ThreadInit {
ThreadInit() {
std::unique_lock<std::mutex> _{mutex};
perThreadState[std::this_thread::get_id()] = {};
}
~ThreadInit() {
std::unique_lock<std::mutex> _{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<Counter>::create(
std::initializer_list<std::pair<std::string_view, std::string_view>>) {}
template <>
Gauge Family<Gauge>::create(
std::initializer_list<std::pair<std::string_view, std::string_view>>) {}
template <>
Histogram Family<Histogram>::create(
std::initializer_list<std::pair<std::string_view, std::string_view>>) {}
Family<Counter> create_counter(std::string_view name, std::string_view help) {}
Family<Gauge> create_gauge(std::string_view name, std::string_view help) {}
Family<Histogram> create_histogram(std::string_view name, std::string_view help,
std::initializer_list<double> buckets) {}
std::span<std::string_view> render(ArenaAllocator &) {}
} // namespace metric

63
src/metric.hpp Normal file
View File

@@ -0,0 +1,63 @@
#pragma once
#include "arena_allocator.hpp"
#include <initializer_list>
#include <span>
#include <string_view>
#include <type_traits>
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 <class T> struct Family {
static_assert(std::is_same_v<T, Counter> || std::is_same_v<T, Gauge> ||
std::is_same_v<T, Histogram>);
T create(
std::initializer_list<std::pair<std::string_view, std::string_view>>);
private:
friend struct Metric;
std::string_view name;
std::string_view help;
};
// All std::string_view's should point to static memory
Family<Counter> create_counter(std::string_view name, std::string_view help);
Family<Gauge> create_gauge(std::string_view name, std::string_view help);
Family<Histogram> create_histogram(std::string_view name, std::string_view help,
std::initializer_list<double> buckets);
std::span<std::string_view> render(ArenaAllocator &);
} // namespace metric