Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
central_collector.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
12#pragma once
13
18#include <shared_mutex>
19#include <unordered_map>
20#include <memory>
21#include <atomic>
22
23namespace kcenon { namespace monitoring {
24
36public:
37 static constexpr size_t DEFAULT_MAX_PROFILES = 10000;
38
43 explicit central_collector(size_t max_profiles = DEFAULT_MAX_PROFILES);
44
54 void receive_batch(const std::vector<metric_sample>& samples);
55
64 common::Result<performance_profile> get_profile(const std::string& operation_name) const;
65
73 std::unordered_map<std::string, performance_profile> get_all_profiles() const;
74
80 void clear();
81
88 size_t get_operation_count() const;
89
96 size_t get_total_sample_count() const {
97 return total_samples_.load(std::memory_order_relaxed);
98 }
99
113
115
116private:
122 std::atomic<std::chrono::steady_clock::rep> last_access_time;
123 std::mutex mutex; // Per-profile lock for sample aggregation
124
126 };
127
135 void process_sample(const metric_sample& sample);
136
142 void evict_lru();
143
144 mutable std::shared_mutex profiles_mutex_;
145 std::unordered_map<std::string, std::unique_ptr<profile_data>> profiles_;
147
148 // Statistics (atomic for thread-safe updates)
149 std::atomic<size_t> total_samples_{0};
150 std::atomic<size_t> batches_received_{0};
151 std::atomic<size_t> lru_evictions_{0};
152};
153
154}} // namespace kcenon::monitoring
Central collector for aggregating metrics from thread-local buffers.
size_t get_operation_count() const
Get the number of tracked operations.
std::unordered_map< std::string, std::unique_ptr< profile_data > > profiles_
void clear()
Clear all collected data.
size_t get_total_sample_count() const
Get total number of samples received.
central_collector(size_t max_profiles=DEFAULT_MAX_PROFILES)
Construct a central collector.
common::Result< performance_profile > get_profile(const std::string &operation_name) const
Get aggregated profile for an operation.
void receive_batch(const std::vector< metric_sample > &samples)
Receive a batch of samples from a thread-local buffer.
std::unordered_map< std::string, performance_profile > get_all_profiles() const
Get all aggregated profiles.
static constexpr size_t DEFAULT_MAX_PROFILES
void process_sample(const metric_sample &sample)
Process a single sample into a profile.
void evict_lru()
Evict least recently used profile.
Monitoring system specific error codes.
Lightweight performance profile types for aggregated metrics.
Result pattern type definitions for monitoring system.
Internal structure for profile data with LRU tracking.
std::atomic< std::chrono::steady_clock::rep > last_access_time
Get statistics about the collector.
size_t lru_evictions
Number of LRU evictions.
size_t batches_received
Total batches received.
size_t operation_count
Number of tracked operations.
Sample data structure for metric recording.
Lightweight performance profile for aggregated metrics.
Thread-local buffer for lock-free metric collection.