Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
Metrics.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <array>
10#include <atomic>
11#include <cstddef>
12#include <cstdint>
13#include <cstdio>
14#include <cstring>
15#include <span>
16#include <string_view>
17
18namespace corium::profiler {
19
22class Counter {
23public:
24 explicit constexpr Counter(std::string_view name, std::string_view help = "") noexcept
25 : m_name(name), m_help(help)
26 {}
27
30 void increment(uint64_t val = 1) noexcept {
31 m_value.fetch_add(val, std::memory_order_relaxed);
32 }
33
35 [[nodiscard]] uint64_t get() const noexcept {
36 return m_value.load(std::memory_order_relaxed);
37 }
38
40 void reset() noexcept {
41 m_value.store(0, std::memory_order_relaxed);
42 }
43
44 [[nodiscard]] std::string_view name() const noexcept { return m_name; }
45 [[nodiscard]] std::string_view help() const noexcept { return m_help; }
46
47private:
48 std::string_view m_name;
49 std::string_view m_help;
50 std::atomic<uint64_t> m_value{0};
51};
52
55class Gauge {
56public:
57 explicit constexpr Gauge(std::string_view name, std::string_view help = "") noexcept
58 : m_name(name), m_help(help)
59 {}
60
62 void set(int64_t val) noexcept {
63 m_value.store(val, std::memory_order_relaxed);
64 }
65
67 void increment(int64_t val = 1) noexcept {
68 m_value.fetch_add(val, std::memory_order_relaxed);
69 }
70
72 void decrement(int64_t val = 1) noexcept {
73 m_value.fetch_sub(val, std::memory_order_relaxed);
74 }
75
77 [[nodiscard]] int64_t get() const noexcept {
78 return m_value.load(std::memory_order_relaxed);
79 }
80
81 [[nodiscard]] std::string_view name() const noexcept { return m_name; }
82 [[nodiscard]] std::string_view help() const noexcept { return m_help; }
83
84private:
85 std::string_view m_name;
86 std::string_view m_help;
87 std::atomic<int64_t> m_value{0};
88};
89
93template <size_t NumBuckets = 8>
94class Histogram {
95public:
96 constexpr Histogram(
97 std::string_view name,
98 std::array<double, NumBuckets> bounds,
99 std::string_view help = ""
100 ) noexcept
101 : m_name(name), m_bounds(bounds), m_help(help)
102 {}
103
106 void observe(double val) noexcept {
107 m_count.fetch_add(1, std::memory_order_relaxed);
108
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);
112 }
113 }
114 }
115
116 [[nodiscard]] uint64_t count() const noexcept {
117 return m_count.load(std::memory_order_relaxed);
118 }
119
120 [[nodiscard]] uint64_t bucketCount(size_t index) const noexcept {
121 return index < NumBuckets ? m_buckets[index].load(std::memory_order_relaxed) : 0;
122 }
123
124 [[nodiscard]] double bucketBound(size_t index) const noexcept {
125 return index < NumBuckets ? m_bounds[index] : 0.0;
126 }
127
128 [[nodiscard]] std::string_view name() const noexcept { return m_name; }
129 [[nodiscard]] std::string_view help() const noexcept { return m_help; }
130
131private:
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};
137};
138
141inline size_t formatPrometheusCounter(const Counter& c, std::span<char> buf) noexcept {
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())
151 );
152 return len > 0 ? static_cast<size_t>(len) : 0;
153}
154
157inline size_t formatPrometheusGauge(const Gauge& g, std::span<char> buf) noexcept {
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())
167 );
168 return len > 0 ? static_cast<size_t>(len) : 0;
169}
170
171} // namespace corium::profiler
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