24 explicit constexpr Counter(std::string_view
name, std::string_view
help =
"") noexcept
31 m_value.fetch_add(val, std::memory_order_relaxed);
35 [[nodiscard]] uint64_t
get() const noexcept {
36 return m_value.load(std::memory_order_relaxed);
41 m_value.store(0, std::memory_order_relaxed);
44 [[nodiscard]] std::string_view
name() const noexcept {
return m_name; }
45 [[nodiscard]] std::string_view
help() const noexcept {
return m_help; }
48 std::string_view m_name;
49 std::string_view m_help;
50 std::atomic<uint64_t> m_value{0};
57 explicit constexpr Gauge(std::string_view
name, std::string_view
help =
"") noexcept
62 void set(int64_t val)
noexcept {
63 m_value.store(val, std::memory_order_relaxed);
68 m_value.fetch_add(val, std::memory_order_relaxed);
73 m_value.fetch_sub(val, std::memory_order_relaxed);
77 [[nodiscard]] int64_t
get() const noexcept {
78 return m_value.load(std::memory_order_relaxed);
81 [[nodiscard]] std::string_view
name() const noexcept {
return m_name; }
82 [[nodiscard]] std::string_view
help() const noexcept {
return m_help; }
85 std::string_view m_name;
86 std::string_view m_help;
87 std::atomic<int64_t> m_value{0};
93template <
size_t NumBuckets = 8>
97 std::string_view
name,
98 std::array<double, NumBuckets> bounds,
99 std::string_view
help =
""
101 : m_name(
name), m_bounds(bounds), m_help(
help)
107 m_count.fetch_add(1, std::memory_order_relaxed);
109 for (
size_t i = 0; i < NumBuckets; ++i) {
110 if (val <= m_bounds[i]) {
111 m_buckets[i].fetch_add(1, std::memory_order_relaxed);
116 [[nodiscard]] uint64_t
count() const noexcept {
117 return m_count.load(std::memory_order_relaxed);
121 return index < NumBuckets ? m_buckets[index].load(std::memory_order_relaxed) : 0;
125 return index < NumBuckets ? m_bounds[index] : 0.0;
128 [[nodiscard]] std::string_view
name() const noexcept {
return m_name; }
129 [[nodiscard]] std::string_view
help() const noexcept {
return m_help; }
132 std::string_view m_name;
133 std::array<double, NumBuckets> m_bounds;
134 std::string_view m_help;
135 std::array<std::atomic<uint64_t>, NumBuckets> m_buckets{};
136 std::atomic<uint64_t> m_count{0};
142 if (buf.size() < 64)
return 0;
143 int len = std::snprintf(
144 buf.data(), buf.size(),
145 "# HELP %.*s %.*s\n# TYPE %.*s counter\n%.*s %lu\n",
146 static_cast<int>(c.name().size()), c.name().data(),
147 static_cast<int>(c.help().size()), c.help().data(),
148 static_cast<int>(c.name().size()), c.name().data(),
149 static_cast<int>(c.name().size()), c.name().data(),
150 static_cast<unsigned long>(c.get())
152 return len > 0 ?
static_cast<size_t>(len) : 0;
158 if (buf.size() < 64)
return 0;
159 int len = std::snprintf(
160 buf.data(), buf.size(),
161 "# HELP %.*s %.*s\n# TYPE %.*s gauge\n%.*s %ld\n",
162 static_cast<int>(g.name().size()), g.name().data(),
163 static_cast<int>(g.help().size()), g.help().data(),
164 static_cast<int>(g.name().size()), g.name().data(),
165 static_cast<int>(g.name().size()), g.name().data(),
166 static_cast<long>(g.get())
168 return len > 0 ?
static_cast<size_t>(len) : 0;
Atomic 64-bit monotonically increasing counter.
Definition Metrics.hpp:22
void increment(uint64_t val=1) noexcept
Increment counter value.
Definition Metrics.hpp:30
constexpr Counter(std::string_view name, std::string_view help="") noexcept
Definition Metrics.hpp:24
uint64_t get() const noexcept
Current counter value.
Definition Metrics.hpp:35
std::string_view name() const noexcept
Definition Metrics.hpp:44
void reset() noexcept
Reset counter to zero.
Definition Metrics.hpp:40
std::string_view help() const noexcept
Definition Metrics.hpp:45
Atomic 64-bit signed gauge metric representing instantaneous level.
Definition Metrics.hpp:55
constexpr Gauge(std::string_view name, std::string_view help="") noexcept
Definition Metrics.hpp:57
int64_t get() const noexcept
Current gauge value.
Definition Metrics.hpp:77
void decrement(int64_t val=1) noexcept
Decrement gauge.
Definition Metrics.hpp:72
void increment(int64_t val=1) noexcept
Increment gauge.
Definition Metrics.hpp:67
void set(int64_t val) noexcept
Set gauge to an absolute value.
Definition Metrics.hpp:62
std::string_view name() const noexcept
Definition Metrics.hpp:81
std::string_view help() const noexcept
Definition Metrics.hpp:82
Statically bucketed latency distribution histogram.
Definition Metrics.hpp:94
uint64_t count() const noexcept
Definition Metrics.hpp:116
uint64_t bucketCount(size_t index) const noexcept
Definition Metrics.hpp:120
void observe(double val) noexcept
Record an observed value.
Definition Metrics.hpp:106
double bucketBound(size_t index) const noexcept
Definition Metrics.hpp:124
std::string_view help() const noexcept
Definition Metrics.hpp:129
constexpr Histogram(std::string_view name, std::array< double, NumBuckets > bounds, std::string_view help="") noexcept
Definition Metrics.hpp:96
std::string_view name() const noexcept
Definition Metrics.hpp:128
size_t formatPrometheusCounter(const Counter &c, std::span< char > buf) noexcept
Format counter in Prometheus exposition text format into a char buffer.
Definition Metrics.hpp:141
size_t formatPrometheusGauge(const Gauge &g, std::span< char > buf) noexcept
Format gauge in Prometheus exposition text format into a char buffer.
Definition Metrics.hpp:157
Definition FlightRecorder.hpp:16