Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
performance_monitor.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include "../database_types.h"
8#include <chrono>
9#include <atomic>
10#include <mutex>
11#include <vector>
12#include <unordered_map>
13#include <memory>
14#include <thread>
15#include <condition_variable>
16#include <functional>
17#include <string>
18
20{
21 // Forward declaration for query_timer
22 class performance_monitor;
23
29 {
30 std::string query_hash;
31 std::chrono::steady_clock::time_point start_time;
32 std::chrono::steady_clock::time_point end_time;
33 std::chrono::microseconds execution_time{0};
34 size_t rows_affected = 0;
35 bool success = false;
36 std::string error_message;
38 };
39
45 {
46 std::atomic<size_t> total_connections{0};
47 std::atomic<size_t> active_connections{0};
48 std::atomic<size_t> idle_connections{0};
49 std::atomic<size_t> failed_connections{0};
50 std::atomic<std::chrono::microseconds> avg_acquisition_time{std::chrono::microseconds{0}};
51 std::atomic<std::chrono::microseconds> max_acquisition_time{std::chrono::microseconds{0}};
52 std::chrono::steady_clock::time_point last_update;
53
54 // Default constructor
55 connection_metrics() = default;
56
57 // Copy constructor
66
67 // Move constructor
69 total_connections(other.total_connections.load()),
70 active_connections(other.active_connections.load()),
71 idle_connections(other.idle_connections.load()),
72 failed_connections(other.failed_connections.load()),
73 avg_acquisition_time(other.avg_acquisition_time.load()),
74 max_acquisition_time(other.max_acquisition_time.load()),
75 last_update(std::move(other.last_update)) {}
76
77 // Copy assignment operator
79 if (this != &other) {
80 total_connections.store(other.total_connections.load());
81 active_connections.store(other.active_connections.load());
82 idle_connections.store(other.idle_connections.load());
83 failed_connections.store(other.failed_connections.load());
87 }
88 return *this;
89 }
90
91 // Move assignment operator
93 if (this != &other) {
94 total_connections.store(other.total_connections.load());
95 active_connections.store(other.active_connections.load());
96 idle_connections.store(other.idle_connections.load());
97 failed_connections.store(other.failed_connections.load());
98 avg_acquisition_time.store(other.avg_acquisition_time.load());
99 max_acquisition_time.store(other.max_acquisition_time.load());
100 last_update = std::move(other.last_update);
101 }
102 return *this;
103 }
104 };
105
111 {
112 // Query metrics
113 size_t total_queries = 0;
115 size_t failed_queries = 0;
116 std::chrono::microseconds avg_query_time{0};
117 std::chrono::microseconds min_query_time{0};
118 std::chrono::microseconds max_query_time{0};
119 double queries_per_second = 0.0;
120
121 // Connection metrics
125 std::chrono::microseconds avg_connection_time{0};
126
127 // Error rates
128 double error_rate = 0.0;
129 std::unordered_map<std::string, size_t> error_counts;
130
131 // Timestamps
132 std::chrono::steady_clock::time_point measurement_start;
133 std::chrono::steady_clock::time_point measurement_end;
134 };
135
141 {
142 public:
151
152 performance_alert(alert_type type, const std::string& message,
153 std::chrono::steady_clock::time_point timestamp);
154
155 alert_type type() const { return type_; }
156 const std::string& message() const { return message_; }
157 std::chrono::steady_clock::time_point timestamp() const { return timestamp_; }
158
159 private:
161 std::string message_;
162 std::chrono::steady_clock::time_point timestamp_;
163 };
164
170 {
171 public:
179 query_timer(const std::string& query, database_types db_type,
180 std::shared_ptr<performance_monitor> monitor);
181
182
183 ~query_timer();
184
185 void set_rows_affected(size_t rows) { metrics_.rows_affected = rows; }
186 void set_error(const std::string& error);
187
188 private:
190 std::chrono::steady_clock::time_point start_time_;
191 std::shared_ptr<performance_monitor> monitor_; // Sprint 3: Store monitor instance
192 };
193
199 {
200 public:
206
207
209
210 // Configuration
211 void set_monitoring_enabled(bool enabled) { monitoring_enabled_ = enabled; }
212 void set_metrics_retention_period(std::chrono::minutes period) { retention_period_ = period; }
213 void set_alert_thresholds(double error_rate_threshold,
214 std::chrono::microseconds latency_threshold);
215
216 // Metrics collection
217 void record_query_metrics(const query_metrics& metrics);
218 void record_connection_metrics(database_types db_type, const connection_metrics& metrics);
219 void record_slow_query(const std::string& query, std::chrono::microseconds execution_time);
220
221 // Metrics retrieval
224 std::vector<query_metrics> get_recent_queries(std::chrono::minutes window) const;
225 std::vector<query_metrics> get_slow_queries(std::chrono::microseconds threshold) const;
226
227 // Connection monitoring
228 void update_connection_count(database_types db_type, size_t active, size_t total);
230
231 // Alert system
232 void register_alert_handler(std::function<void(const performance_alert&)> handler);
233 std::vector<performance_alert> get_recent_alerts(std::chrono::minutes window) const;
234
235 // Cache management
236 void clear_metrics();
237 void cleanup_old_metrics();
238
239 // Dashboard support
240 std::string get_metrics_json() const;
241 std::string get_dashboard_html() const;
242
243 private:
244 // Internal methods
245 void cleanup_thread();
246 void check_thresholds();
247 void emit_alert(performance_alert::alert_type type, const std::string& message);
248 std::string calculate_query_hash(const std::string& query) const;
249
250 // Configuration
251 std::atomic<bool> monitoring_enabled_{true};
252 std::chrono::minutes retention_period_{60}; // 1 hour
253 double error_rate_threshold_ = 0.05; // 5%
254 std::chrono::microseconds latency_threshold_{1000000}; // 1 second
255
256 // Metrics storage
257 mutable std::mutex metrics_mutex_;
258 std::vector<query_metrics> query_history_;
259 std::unordered_map<database_types, connection_metrics> connection_metrics_;
260 std::vector<performance_alert> alerts_;
261
262 // Alert handlers
263 std::mutex handlers_mutex_;
264 std::vector<std::function<void(const performance_alert&)>> alert_handlers_;
265
266 // Background cleanup
267 std::atomic<bool> cleanup_running_{true};
268 std::thread cleanup_thread_;
269 std::condition_variable cleanup_cv_;
270 std::mutex cleanup_mutex_;
271
272 // Query caching for pattern analysis
273 std::unordered_map<std::string, size_t> query_patterns_;
274 std::unordered_map<std::string, std::chrono::microseconds> query_avg_times_;
275 };
276
282 {
283 public:
284 virtual ~metrics_exporter() = default;
285
286 virtual bool export_metrics(const performance_summary& summary) = 0;
287 virtual bool export_alerts(const std::vector<performance_alert>& alerts) = 0;
288 };
289
295 {
296 public:
297 prometheus_exporter(const std::string& endpoint, int port);
298
299 bool export_metrics(const performance_summary& summary) override;
300 bool export_alerts(const std::vector<performance_alert>& alerts) override;
301
302 std::string format_prometheus_metrics(const performance_summary& summary) const;
303
304 private:
305 std::string endpoint_;
306 [[maybe_unused]] int port_;
307 };
308
314 {
315 public:
316 dashboard_server(int port = 8080);
318
319 bool start();
320 void stop();
321
322 void set_custom_dashboard(const std::string& html_content);
323
324 private:
326 std::string handle_request(const std::string& path) const;
327
328 int port_;
329 std::atomic<bool> running_{false};
330 std::thread server_thread_;
331 std::string custom_dashboard_;
332 };
333
334 // Helper macros for automatic query timing
335 #define MONITOR_QUERY(query, db_type) \
336 database::monitoring::query_timer timer_(query, db_type)
337
338 #define MONITOR_QUERY_RESULT(rows) \
339 timer_.set_rows_affected(rows)
340
341 #define MONITOR_QUERY_ERROR(error) \
342 timer_.set_error(error)
343
344} // namespace database::monitoring
Simple HTTP server for performance dashboard.
std::string handle_request(const std::string &path) const
void set_custom_dashboard(const std::string &html_content)
Export metrics to external monitoring systems.
virtual bool export_metrics(const performance_summary &summary)=0
virtual bool export_alerts(const std::vector< performance_alert > &alerts)=0
Alert system for performance thresholds.
std::chrono::steady_clock::time_point timestamp() const
std::chrono::steady_clock::time_point timestamp_
performance_alert(alert_type type, const std::string &message, std::chrono::steady_clock::time_point timestamp)
Main performance monitoring system.
std::vector< std::function< void(const performance_alert &)> > alert_handlers_
void record_slow_query(const std::string &query, std::chrono::microseconds execution_time)
void record_connection_metrics(database_types db_type, const connection_metrics &metrics)
void set_alert_thresholds(double error_rate_threshold, std::chrono::microseconds latency_threshold)
std::string calculate_query_hash(const std::string &query) const
std::unordered_map< std::string, size_t > query_patterns_
std::vector< performance_alert > get_recent_alerts(std::chrono::minutes window) const
std::vector< query_metrics > get_slow_queries(std::chrono::microseconds threshold) const
void register_alert_handler(std::function< void(const performance_alert &)> handler)
connection_metrics get_connection_metrics(database_types db_type) const
performance_summary get_performance_summary() const
performance_monitor()
Constructor - now public for dependency injection.
std::unordered_map< database_types, connection_metrics > connection_metrics_
std::vector< performance_alert > alerts_
std::vector< query_metrics > get_recent_queries(std::chrono::minutes window) const
void emit_alert(performance_alert::alert_type type, const std::string &message)
void update_connection_count(database_types db_type, size_t active, size_t total)
std::unordered_map< std::string, std::chrono::microseconds > query_avg_times_
void record_query_metrics(const query_metrics &metrics)
void set_metrics_retention_period(std::chrono::minutes period)
Export metrics in Prometheus format.
std::string format_prometheus_metrics(const performance_summary &summary) const
bool export_alerts(const std::vector< performance_alert > &alerts) override
bool export_metrics(const performance_summary &summary) override
prometheus_exporter(const std::string &endpoint, int port)
RAII timer for measuring query execution time.
void set_error(const std::string &error)
std::shared_ptr< performance_monitor > monitor_
query_timer(const std::string &query, database_types db_type, std::shared_ptr< performance_monitor > monitor)
Constructor with explicit performance_monitor (recommended)
std::chrono::steady_clock::time_point start_time_
Defines the enumeration of supported database types.
database_types
Represents various database backends or modes.
@ none
No specific database type is set.
Metrics for database connection usage.
connection_metrics & operator=(connection_metrics &&other) noexcept
connection_metrics(connection_metrics &&other) noexcept
std::atomic< std::chrono::microseconds > max_acquisition_time
std::atomic< std::chrono::microseconds > avg_acquisition_time
std::chrono::steady_clock::time_point last_update
connection_metrics(const connection_metrics &other)
connection_metrics & operator=(const connection_metrics &other)
std::chrono::steady_clock::time_point measurement_end
std::unordered_map< std::string, size_t > error_counts
std::chrono::steady_clock::time_point measurement_start
Metrics for individual query execution.
std::chrono::steady_clock::time_point start_time
std::chrono::steady_clock::time_point end_time
std::chrono::microseconds execution_time