Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter is a sharded middleware that limits the rate of log entries per level.
func NewRateLimiter ¶
NewRateLimiter creates a new sharded RateLimiter for a specific log level.
func (*RateLimiter) Delete ¶ added in v0.1.7
func (rl *RateLimiter) Delete(level lx.LevelType)
Delete removes a rate limit for a specific level.
func (*RateLimiter) Get ¶ added in v0.1.7
Get retrieves the current rate limit settings for a level.
func (*RateLimiter) Handle ¶
func (rl *RateLimiter) Handle(e *lx.Entry) error
Handle processes a log entry and enforces rate limiting.
func (*RateLimiter) Set ¶
func (rl *RateLimiter) Set(level lx.LevelType, count int, interval time.Duration) *RateLimiter
Set configures a rate limit for a specific log level.
type Sampling ¶
type Sampling struct {
// contains filtered or unexported fields
}
Sampling is a middleware that randomly samples log entries based on a rate per level. It allows logs to pass through with a specified probability, tracking rejected logs in stats. Thread-safe with a mutex for concurrent access to rates and stats maps.
func NewSampling ¶
NewSampling creates a new Sampling middleware for a specific log level. It initializes the middleware with a sampling rate for the given level, allowing further configuration via the Set method. Example:
sampler := NewSampling(lx.LevelDebug, 0.1) // Sample 10% of Debug logs
logger := ll.New("app").Enable().Use(sampler)
logger.Debug("Test") // Passes with 10% probability
func (*Sampling) GetStats ¶
GetStats returns a copy of the sampling statistics. It provides the count of rejected logs per level, ensuring thread-safety with a read lock. The returned map is safe for external use without affecting internal state. Example:
stats := sampler.GetStats() // Returns map of rejected log counts by level
func (*Sampling) Handle ¶
Handle processes a log entry and applies sampling based on the level's rate. It generates a random number and compares it to the level's sampling rate, allowing the log if the random number is less than or equal to the rate. Rejected logs increment the stats counter. Returns an error for rejected logs. Thread-safe with a mutex for stats updates. Example (internal usage):
err := sampler.Handle(&lx.Entry{Level: lx.LevelDebug}) // Returns error if rejected
func (*Sampling) Set ¶
Set configures a sampling rate for a specific log level. It adds or updates the sampling rate (0.0 to 1.0) for the given level, where 0.0 rejects all logs and 1.0 allows all logs. Thread-safe with a mutex. Returns the Sampling instance for chaining. Example:
sampler := NewSampling(lx.LevelDebug, 0.1) sampler.Set(lx.LevelInfo, 0.5) // Sample 50% of Info logs