|
Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
|
Thread-safe log sampler with multiple strategy support. More...
#include <log_sampler.h>

Public Member Functions | |
| log_sampler (const sampling_config &config=sampling_config{}) | |
| Construct a sampler with the given configuration. | |
| ~log_sampler ()=default | |
| Destructor. | |
| log_sampler (const log_sampler &)=delete | |
| Copy constructor (deleted) | |
| log_sampler & | operator= (const log_sampler &)=delete |
| Copy assignment operator (deleted) | |
| log_sampler (log_sampler &&other) noexcept | |
| Move constructor. | |
| log_sampler & | operator= (log_sampler &&other) noexcept |
| Move assignment operator. | |
| bool | should_sample (const log_entry &entry) |
| Check if a log entry should be sampled (logged) | |
| bool | should_sample (log_level level, const std::string &message) |
| Check if a log entry should be sampled (without category) | |
| bool | should_sample (log_level level, const std::string &message, const std::optional< std::string > &category) |
| Check if a log entry should be sampled with category. | |
| void | set_config (const sampling_config &config) |
| Update the sampling configuration. | |
| sampling_config | get_config () const |
| Get the current configuration. | |
| sampling_stats | get_stats () const |
| Get sampling statistics. | |
| void | reset_stats () |
| Reset sampling statistics. | |
| bool | is_enabled () const |
| Check if sampling is enabled. | |
| void | set_enabled (bool enabled) |
| Enable or disable sampling. | |
| double | get_effective_rate () const |
| Get the current effective sampling rate. | |
Private Member Functions | |
| bool | should_bypass_level (log_level level) const |
| Check if a level should bypass sampling. | |
| bool | should_bypass_field (const log_entry &entry) const |
| Check if any field should bypass sampling. | |
| double | get_field_rate (const log_entry &entry) const |
| Get the sampling rate based on structured fields. | |
| double | get_category_rate (const std::string &category) const |
| Get the sampling rate for a category. | |
| bool | random_sample (double rate) |
| Perform random sampling decision. | |
| bool | rate_limit_sample () |
| Perform rate limiting sampling decision. | |
| bool | adaptive_sample () |
| Perform adaptive sampling decision. | |
| bool | hash_sample (const std::string &message, double rate) |
| Perform hash-based sampling decision. | |
| void | update_adaptive_rate () |
| Update adaptive sampling rate based on current throughput. | |
| std::uint64_t | xorshift64 () |
| Fast xorshift64 PRNG for random sampling. | |
Static Private Member Functions | |
| static std::uint64_t | fnv1a_hash (const std::string &str) |
| FNV-1a hash for message hashing. | |
Private Attributes | |
| sampling_config | config_ |
| std::shared_mutex | config_mutex_ |
| std::atomic< std::uint64_t > | total_count_ {0} |
| std::atomic< std::uint64_t > | sampled_count_ {0} |
| std::atomic< std::uint64_t > | dropped_count_ {0} |
| std::atomic< std::uint64_t > | bypassed_count_ {0} |
| std::atomic< std::uint64_t > | rng_state_ |
| std::atomic< std::uint64_t > | rate_limit_count_ {0} |
| std::atomic< std::chrono::steady_clock::time_point::rep > | rate_limit_window_start_ {0} |
| std::mutex | rate_limit_mutex_ |
| std::atomic< double > | effective_rate_ {1.0} |
| std::atomic< std::uint64_t > | adaptive_window_count_ {0} |
| std::atomic< std::chrono::steady_clock::time_point::rep > | adaptive_window_start_ {0} |
| std::atomic< bool > | is_throttling_ {false} |
Thread-safe log sampler with multiple strategy support.
The log_sampler class determines whether a log entry should be logged based on the configured sampling strategy. It maintains thread-safe statistics and supports runtime configuration updates.
Key features:
Definition at line 71 of file log_sampler.h.
|
explicit |
Construct a sampler with the given configuration.
| config | Sampling configuration |
Definition at line 16 of file log_sampler.cpp.
References adaptive_window_start_, config_, effective_rate_, kcenon::logger::sampling::sampling_config::rate, and rate_limit_window_start_.
|
default |
Destructor.
|
delete |
Copy constructor (deleted)
Samplers contain atomic state and should not be copied
|
noexcept |
Move constructor.
Definition at line 27 of file log_sampler.cpp.
|
nodiscardprivate |
Perform adaptive sampling decision.
Definition at line 364 of file log_sampler.cpp.
References effective_rate_, random_sample(), and update_adaptive_rate().
Referenced by should_sample(), and should_sample().


|
staticnodiscardprivate |
FNV-1a hash for message hashing.
| str | String to hash |
Definition at line 449 of file log_sampler.cpp.
Referenced by hash_sample().

|
nodiscardprivate |
Get the sampling rate for a category.
| category | Category name (empty for default rate) |
Definition at line 296 of file log_sampler.cpp.
References kcenon::logger::sampling::sampling_config::category_rates, config_, config_mutex_, and kcenon::logger::sampling::sampling_config::rate.
Referenced by should_sample(), and should_sample().

|
nodiscard |
Get the current configuration.
Definition at line 200 of file log_sampler.cpp.
References config_, and config_mutex_.
|
nodiscard |
Get the current effective sampling rate.
Definition at line 233 of file log_sampler.cpp.
References effective_rate_.
|
nodiscardprivate |
Get the sampling rate based on structured fields.
| entry | Log entry with fields to check |
Definition at line 259 of file log_sampler.cpp.
References config_, config_mutex_, kcenon::logger::sampling::sampling_config::field_rates, and kcenon::logger::log_entry::fields.
Referenced by should_sample().

|
nodiscard |
Get sampling statistics.
Definition at line 205 of file log_sampler.cpp.
References kcenon::logger::sampling::sampling_stats::bypassed_count, bypassed_count_, kcenon::logger::sampling::sampling_stats::dropped_count, dropped_count_, kcenon::logger::sampling::sampling_stats::effective_rate, effective_rate_, kcenon::logger::sampling::sampling_stats::is_throttling, is_throttling_, kcenon::logger::sampling::sampling_stats::sampled_count, sampled_count_, kcenon::logger::sampling::sampling_stats::total_count, and total_count_.
|
nodiscardprivate |
Perform hash-based sampling decision.
| message | Message to hash |
| rate | Sampling rate to use |
Definition at line 416 of file log_sampler.cpp.
References config_, config_mutex_, fnv1a_hash(), and kcenon::logger::sampling::sampling_config::hash_seed.
Referenced by should_sample(), and should_sample().


|
nodiscard |
Check if sampling is enabled.
Definition at line 223 of file log_sampler.cpp.
References config_, config_mutex_, and kcenon::logger::sampling::sampling_config::enabled.
|
delete |
Copy assignment operator (deleted)
|
noexcept |
Move assignment operator.
Definition at line 42 of file log_sampler.cpp.
|
nodiscardprivate |
Perform random sampling decision.
| rate | Sampling rate to use |
Definition at line 305 of file log_sampler.cpp.
References kcenon::logger::sampling::random, and xorshift64().
Referenced by adaptive_sample(), should_sample(), and should_sample().


|
nodiscardprivate |
Perform rate limiting sampling decision.
Definition at line 320 of file log_sampler.cpp.
References config_, config_mutex_, rate_limit_count_, rate_limit_mutex_, kcenon::logger::sampling::sampling_config::rate_limit_per_second, kcenon::logger::sampling::sampling_config::rate_limit_window_ms, and rate_limit_window_start_.
Referenced by should_sample(), and should_sample().

| void kcenon::logger::sampling::log_sampler::reset_stats | ( | ) |
Reset sampling statistics.
Definition at line 216 of file log_sampler.cpp.
References bypassed_count_, dropped_count_, sampled_count_, and total_count_.
| void kcenon::logger::sampling::log_sampler::set_config | ( | const sampling_config & | config | ) |
Update the sampling configuration.
| config | New configuration to apply |
Definition at line 194 of file log_sampler.cpp.
References config_, config_mutex_, effective_rate_, and kcenon::logger::sampling::sampling_config::rate.
| void kcenon::logger::sampling::log_sampler::set_enabled | ( | bool | enabled | ) |
Enable or disable sampling.
| enabled | true to enable, false to disable |
Definition at line 228 of file log_sampler.cpp.
References config_, config_mutex_, and kcenon::logger::sampling::sampling_config::enabled.
|
nodiscardprivate |
Check if any field should bypass sampling.
| entry | Log entry to check |
Definition at line 243 of file log_sampler.cpp.
References kcenon::logger::sampling::sampling_config::always_log_fields, config_, config_mutex_, and kcenon::logger::log_entry::fields.
Referenced by should_sample().

|
nodiscardprivate |
Check if a level should bypass sampling.
| level | Log level to check |
Definition at line 237 of file log_sampler.cpp.
References kcenon::logger::sampling::sampling_config::always_log_levels, config_, and config_mutex_.
Referenced by should_sample(), and should_sample().

|
nodiscard |
Check if a log entry should be sampled (logged)
| entry | The log entry to check |
This is the main entry point for sampling decisions. It considers:
Definition at line 61 of file log_sampler.cpp.
References kcenon::logger::sampling::adaptive, adaptive_sample(), bypassed_count_, kcenon::logger::log_entry::category, config_, config_mutex_, dropped_count_, kcenon::logger::sampling::sampling_config::enabled, get_category_rate(), get_field_rate(), kcenon::logger::sampling::hash_based, hash_sample(), kcenon::logger::log_entry::level, kcenon::logger::log_entry::message, kcenon::logger::sampling::random, random_sample(), kcenon::logger::sampling::sampling_config::rate, rate_limit_sample(), kcenon::logger::sampling::rate_limiting, sampled_count_, should_bypass_field(), should_bypass_level(), kcenon::logger::sampling::sampling_config::strategy, kcenon::logger::small_string< SSO_SIZE >::to_string(), and total_count_.
Referenced by should_sample().


|
nodiscard |
Check if a log entry should be sampled (without category)
| level | Log level |
| message | Log message (used for hash-based sampling) |
Definition at line 129 of file log_sampler.cpp.
References should_sample().

|
nodiscard |
Check if a log entry should be sampled with category.
| level | Log level |
| message | Log message |
| category | Optional category for category-specific rates |
Definition at line 134 of file log_sampler.cpp.
References kcenon::logger::sampling::adaptive, adaptive_sample(), bypassed_count_, config_, config_mutex_, dropped_count_, kcenon::logger::sampling::sampling_config::enabled, get_category_rate(), kcenon::logger::sampling::hash_based, hash_sample(), kcenon::logger::sampling::random, random_sample(), kcenon::logger::sampling::sampling_config::rate, rate_limit_sample(), kcenon::logger::sampling::rate_limiting, sampled_count_, should_bypass_level(), kcenon::logger::sampling::sampling_config::strategy, and total_count_.

|
private |
Update adaptive sampling rate based on current throughput.
Definition at line 373 of file log_sampler.cpp.
References kcenon::logger::sampling::sampling_config::adaptive_min_rate, kcenon::logger::sampling::sampling_config::adaptive_threshold, adaptive_window_count_, adaptive_window_start_, config_, config_mutex_, effective_rate_, is_throttling_, and kcenon::logger::sampling::sampling_config::rate.
Referenced by adaptive_sample().

|
nodiscardprivate |
Fast xorshift64 PRNG for random sampling.
Definition at line 440 of file log_sampler.cpp.
References rng_state_.
Referenced by random_sample().

|
private |
|
private |
Definition at line 284 of file log_sampler.h.
Referenced by log_sampler(), and update_adaptive_rate().
|
private |
Definition at line 271 of file log_sampler.h.
Referenced by get_stats(), reset_stats(), should_sample(), and should_sample().
|
private |
Definition at line 264 of file log_sampler.h.
Referenced by get_category_rate(), get_config(), get_field_rate(), hash_sample(), is_enabled(), log_sampler(), rate_limit_sample(), set_config(), set_enabled(), should_bypass_field(), should_bypass_level(), should_sample(), should_sample(), and update_adaptive_rate().
|
mutableprivate |
Definition at line 265 of file log_sampler.h.
Referenced by get_category_rate(), get_config(), get_field_rate(), hash_sample(), is_enabled(), rate_limit_sample(), set_config(), set_enabled(), should_bypass_field(), should_bypass_level(), should_sample(), should_sample(), and update_adaptive_rate().
|
private |
Definition at line 270 of file log_sampler.h.
Referenced by get_stats(), reset_stats(), should_sample(), and should_sample().
|
private |
Definition at line 282 of file log_sampler.h.
Referenced by adaptive_sample(), get_effective_rate(), get_stats(), log_sampler(), set_config(), and update_adaptive_rate().
|
private |
Definition at line 285 of file log_sampler.h.
Referenced by get_stats(), and update_adaptive_rate().
|
private |
|
private |
Definition at line 279 of file log_sampler.h.
Referenced by rate_limit_sample().
|
private |
Definition at line 278 of file log_sampler.h.
Referenced by log_sampler(), and rate_limit_sample().
|
private |
Definition at line 274 of file log_sampler.h.
Referenced by xorshift64().
|
private |
Definition at line 269 of file log_sampler.h.
Referenced by get_stats(), reset_stats(), should_sample(), and should_sample().
|
private |
Definition at line 268 of file log_sampler.h.
Referenced by get_stats(), reset_stats(), should_sample(), and should_sample().