Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
unified_config.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
23#pragma once
24
25#include <chrono>
26#include <optional>
27#include <string>
28#include <vector>
29
30namespace kcenon::common::config {
31
38 size_t pool_size = 0; // 0 means auto-detect
39
41 std::string queue_type = "lockfree";
42
44 size_t max_queue_size = 10000;
45
47 std::string thread_name_prefix = "worker";
48};
49
56 std::string level = "info";
57
59 std::vector<std::string> writers = {"console"};
60
62 bool async = true;
63
65 size_t buffer_size = 8192;
66
68 std::string file_path = "./logs/app.log";
69
71 size_t max_file_size = 10 * 1024 * 1024; // 10MB
72
74 size_t max_backup_files = 5;
75
77 std::string format_pattern = "[%Y-%m-%d %H:%M:%S.%e] [%l] [%t] %v";
78};
79
86 bool enabled = false;
87
89 double sampling_rate = 0.1;
90
92 std::string exporter = "otlp";
93
95 std::string endpoint = "http://localhost:4317";
96};
97
104 bool enabled = true;
105
107 std::chrono::milliseconds metrics_interval{5000};
108
110 std::chrono::milliseconds health_check_interval{30000};
111
114
116 uint16_t prometheus_port = 9090;
117
119 std::string prometheus_path = "/metrics";
120};
121
128 size_t min_size = 5;
129
131 size_t max_size = 20;
132
134 std::chrono::milliseconds idle_timeout{60000};
135
137 std::chrono::milliseconds acquire_timeout{5000};
138};
139
146 std::string backend;
147
149 std::string connection_string;
150
153
155 bool log_queries = false;
156
158 std::chrono::milliseconds slow_query_threshold{1000};
159};
160
167 bool enabled = true;
168
170 std::string version = "1.3";
171
173 std::string cert_path;
174
176 std::string key_path;
177
179 std::string ca_path;
180
182 bool verify_peer = true;
183};
184
192
194 std::string compression = "lz4";
195
197 size_t buffer_size = 65536;
198
200 std::chrono::milliseconds connect_timeout{5000};
201
203 std::chrono::milliseconds io_timeout{30000};
204
206 std::chrono::milliseconds keepalive_interval{15000};
207
209 size_t max_connections = 10000;
210};
211
243
257constexpr const char* ENV_PREFIX = "UNIFIED_";
258
264 std::string path;
265
267 std::string description;
268
270 bool hot_reloadable = false;
271
273 std::string env_var;
274
276 std::vector<std::string> allowed_values;
277};
278
283inline std::vector<field_metadata> get_config_metadata() {
284 return {
285 // Thread configuration
286 {"thread.pool_size", "Number of worker threads (0 for auto)", false,
287 "UNIFIED_THREAD_POOL_SIZE", {}},
288 {"thread.queue_type", "Task queue type", false,
289 "UNIFIED_THREAD_QUEUE_TYPE", {"mutex", "lockfree", "bounded"}},
290 {"thread.max_queue_size", "Maximum task queue size", false,
291 "UNIFIED_THREAD_MAX_QUEUE_SIZE", {}},
292
293 // Logger configuration
294 {"logger.level", "Log level", true,
295 "UNIFIED_LOGGER_LEVEL",
296 {"trace", "debug", "info", "warn", "error", "critical", "off"}},
297 {"logger.async", "Enable async logging", false,
298 "UNIFIED_LOGGER_ASYNC", {}},
299 {"logger.buffer_size", "Async buffer size", false,
300 "UNIFIED_LOGGER_BUFFER_SIZE", {}},
301 {"logger.file_path", "Log file path", true,
302 "UNIFIED_LOGGER_FILE_PATH", {}},
303
304 // Monitoring configuration
305 {"monitoring.enabled", "Enable monitoring", false,
306 "UNIFIED_MONITORING_ENABLED", {}},
307 {"monitoring.metrics_interval", "Metrics collection interval (ms)", true,
308 "UNIFIED_MONITORING_METRICS_INTERVAL_MS", {}},
309 {"monitoring.tracing.enabled", "Enable distributed tracing", false,
310 "UNIFIED_MONITORING_TRACING_ENABLED", {}},
311 {"monitoring.tracing.sampling_rate", "Trace sampling rate", true,
312 "UNIFIED_MONITORING_TRACING_SAMPLING_RATE", {}},
313
314 // Database configuration
315 {"database.backend", "Database backend type", false,
316 "UNIFIED_DATABASE_BACKEND",
317 {"postgresql", "mysql", "sqlite", "mongodb", "redis"}},
318 {"database.connection_string", "Database connection string", false,
319 "UNIFIED_DATABASE_CONNECTION_STRING", {}},
320 {"database.pool.min_size", "Minimum pool size", false,
321 "UNIFIED_DATABASE_POOL_MIN_SIZE", {}},
322 {"database.pool.max_size", "Maximum pool size", false,
323 "UNIFIED_DATABASE_POOL_MAX_SIZE", {}},
324
325 // Network configuration
326 {"network.tls.enabled", "Enable TLS", false,
327 "UNIFIED_NETWORK_TLS_ENABLED", {}},
328 {"network.tls.version", "TLS version", false,
329 "UNIFIED_NETWORK_TLS_VERSION", {"1.2", "1.3"}},
330 {"network.compression", "Compression algorithm", false,
331 "UNIFIED_NETWORK_COMPRESSION", {"none", "lz4", "gzip", "deflate", "zstd"}},
332 {"network.buffer_size", "I/O buffer size", false,
333 "UNIFIED_NETWORK_BUFFER_SIZE", {}},
334 };
335}
336
342inline bool is_hot_reloadable(const std::string& field_path) {
343 static const std::vector<std::string> hot_reloadable_fields = {
344 "logger.level",
345 "logger.file_path",
346 "monitoring.metrics_interval",
347 "monitoring.tracing.sampling_rate",
348 };
349
350 for (const auto& field : hot_reloadable_fields) {
351 if (field_path == field) {
352 return true;
353 }
354 }
355 return false;
356}
357
358} // namespace kcenon::common::config
std::vector< field_metadata > get_config_metadata()
Get metadata for all configuration fields.
constexpr const char * ENV_PREFIX
Environment variable prefix for configuration overrides.
bool is_hot_reloadable(const std::string &field_path)
Check if a configuration field supports hot-reload.
Database system configuration.
bool log_queries
Enable query logging.
std::string connection_string
Connection string or URI.
std::string backend
Database backend: "postgresql", "mysql", "sqlite", "mongodb", "redis".
pool_config pool
Connection pool configuration.
std::chrono::milliseconds slow_query_threshold
Slow query threshold.
Configuration field metadata for validation and documentation.
std::vector< std::string > allowed_values
Allowed values (for enum-like fields)
bool hot_reloadable
Whether the field can be hot-reloaded.
std::string description
Human-readable description.
std::string path
Field path (e.g., "logger.level")
std::string env_var
Environment variable name (if applicable)
Logging system configuration.
bool async
Enable async logging.
size_t max_file_size
Maximum file size in bytes (for rotating_file)
std::vector< std::string > writers
List of writers: "console", "file", "rotating_file", "network", "json".
std::string file_path
Log file path (for file writers)
size_t max_backup_files
Maximum number of backup files (for rotating_file)
size_t buffer_size
Async buffer size in bytes.
std::string format_pattern
Log format pattern.
std::string level
Log level: "trace", "debug", "info", "warn", "error", "critical", "off".
Monitoring system configuration.
std::chrono::milliseconds metrics_interval
Metrics collection interval.
uint16_t prometheus_port
Prometheus metrics port (0 to disable)
std::chrono::milliseconds health_check_interval
Health check interval.
std::string prometheus_path
Prometheus metrics path.
tracing_config tracing
Tracing configuration.
Network system configuration.
std::chrono::milliseconds connect_timeout
Connection timeout.
size_t max_connections
Maximum concurrent connections (server)
std::chrono::milliseconds io_timeout
Read/write timeout.
size_t buffer_size
Send/receive buffer size.
tls_config tls
TLS configuration.
std::string compression
Compression type: "none", "lz4", "gzip", "deflate", "zstd".
std::chrono::milliseconds keepalive_interval
Keep-alive interval.
Database connection pool configuration.
size_t max_size
Maximum pool size.
size_t min_size
Minimum pool size.
std::chrono::milliseconds acquire_timeout
Connection acquisition timeout.
std::chrono::milliseconds idle_timeout
Idle connection timeout.
std::string queue_type
Queue type: "mutex", "lockfree", "bounded".
size_t pool_size
Number of worker threads (default: hardware concurrency)
size_t max_queue_size
Maximum queue size (for bounded queue)
std::string thread_name_prefix
Thread naming prefix.
std::string version
TLS version: "1.2", "1.3".
std::string key_path
Private key file path.
bool verify_peer
Verify peer certificate.
std::string cert_path
Certificate file path.
std::string ca_path
CA certificate path (for client verification)
Distributed tracing configuration.
std::string endpoint
Exporter endpoint.
std::string exporter
Exporter type: "otlp", "jaeger", "zipkin", "console".
double sampling_rate
Sampling rate (0.0 to 1.0)
Root configuration structure for the unified system.
network_config network
Network system configuration.
logger_config logger
Logger system configuration.
static unified_config defaults()
Create a configuration with all default values.
thread_config thread
Thread system configuration.
monitoring_config monitoring
Monitoring system configuration.
database_config database
Database system configuration.