Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
thread_local_buffer.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
22#include <vector>
23#include <string>
24#include <chrono>
25#include <memory>
26#include <functional>
27
28namespace kcenon { namespace monitoring {
29
30// Forward declaration
31class central_collector;
32
37 std::string operation_name;
38 std::chrono::nanoseconds duration;
39 bool success;
40 std::chrono::steady_clock::time_point timestamp;
41
42 metric_sample() = default;
43 metric_sample(const std::string& name,
44 std::chrono::nanoseconds dur,
45 bool succ)
46 : operation_name(name)
47 , duration(dur)
48 , success(succ)
49 , timestamp(std::chrono::steady_clock::now())
50 {}
51};
52
67public:
68 static constexpr size_t DEFAULT_CAPACITY = 256;
69
76 std::shared_ptr<central_collector> collector = nullptr);
77
82
83 // Non-copyable, non-movable (thread-local resource)
88
98 bool record(const metric_sample& sample);
99
109 bool record_auto_flush(const metric_sample& sample);
110
119 size_t flush();
120
125 size_t size() const { return write_index_; }
126
131 bool is_full() const { return write_index_ >= capacity_; }
132
137 size_t capacity() const { return capacity_; }
138
143 void set_collector(std::shared_ptr<central_collector> collector) {
144 collector_ = collector;
145 }
146
150 struct stats {
151 size_t total_records{0};
152 size_t total_flushes{0};
153 size_t auto_flushes{0};
154 };
155
160 const stats& get_stats() const { return stats_; }
161
162private:
163 std::vector<metric_sample> buffer_; // Pre-allocated ring buffer
164 size_t capacity_;
165 size_t write_index_{0}; // Single-writer, no atomic needed
166 std::shared_ptr<central_collector> collector_;
168};
169
170}} // namespace kcenon::monitoring
Thread-local buffer for lock-free metric collection.
~thread_local_buffer()
Destructor - flushes any remaining samples.
size_t flush()
Flush buffered samples to central collector.
thread_local_buffer & operator=(thread_local_buffer &&)=delete
std::shared_ptr< central_collector > collector_
size_t size() const
Get current number of buffered samples.
thread_local_buffer & operator=(const thread_local_buffer &)=delete
bool record_auto_flush(const metric_sample &sample)
Record a metric sample with automatic flush on overflow.
bool is_full() const
Check if buffer is full.
const stats & get_stats() const
Get buffer statistics.
size_t capacity() const
Get buffer capacity.
void set_collector(std::shared_ptr< central_collector > collector)
Set the central collector.
thread_local_buffer(thread_local_buffer &&)=delete
thread_local_buffer(const thread_local_buffer &)=delete
thread_local_buffer(size_t capacity=DEFAULT_CAPACITY, std::shared_ptr< central_collector > collector=nullptr)
Construct a thread-local buffer.
bool record(const metric_sample &sample)
Record a metric sample (lock-free)
Sample data structure for metric recording.
std::chrono::steady_clock::time_point timestamp
metric_sample(const std::string &name, std::chrono::nanoseconds dur, bool succ)
Get statistics about buffer operations.
size_t auto_flushes
Flushes triggered by auto_flush.