Skip to content

Adaptive Log Sampling

AdaptiveSampledLogger wraps any structlog logger and automatically adjusts per-level sampling rates to stay within a target log volume — ideal for high-throughput loops like BRPOP retry workers.

Quick Start

Python
from obskit import AdaptiveSampledLogger

logger = AdaptiveSampledLogger(
    "retry_worker",
    target_logs_per_second=50,   # aim for ≤ 50 log lines/s
)

while True:
    event = redis.brpop("retry_queue", timeout=1)
    if event:
        logger.info("event_retried", event_id=event["id"])   # throttled
        logger.warning("retry_limit_near", count=...)        # always logged
        logger.error("retry_failed", error=...)              # always logged

Default sampling rates

Level Default rate
debug 1 %
info 10 %
warning 100 %
error 100 %
critical 100 %

Rates are multiplied by the adaptive factor target_lps / actual_lps, then clamped to [min_sample_rate, max_sample_rate].

Parameters

Parameter Default Description
name Logger name
target_logs_per_second 100 Target throughput
min_sample_rate 0.001 Floor — at least 0.1 % of messages
max_sample_rate 1.0 Ceiling — never exceed 100 %
adjustment_interval 10.0 Seconds between rate recalculations

Fixed-rate variant

For simpler use-cases, SampledLogger applies a static rate without adaptive adjustment:

Python
from obskit.logging.sampling import SampledLogger, SamplingConfig

cfg = SamplingConfig(debug_rate=0.01, info_rate=0.05)
logger = SampledLogger("my_service", config=cfg)

API Reference

obskit.logging.sampling.AdaptiveSampledLogger

Bases: SampledLogger

Logger with adaptive sampling based on log volume.

Automatically adjusts sampling rates based on current load.

Example

logger = AdaptiveSampledLogger( name="adaptive_service", target_logs_per_second=100, )

Sampling rate adjusts automatically based on volume

logger.info("high_volume_event")

Source code in src/obskit/logging/sampling.py
Python
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
class AdaptiveSampledLogger(SampledLogger):
    """
    Logger with adaptive sampling based on log volume.

    Automatically adjusts sampling rates based on current load.

    Example:
        logger = AdaptiveSampledLogger(
            name="adaptive_service",
            target_logs_per_second=100,
        )

        # Sampling rate adjusts automatically based on volume
        logger.info("high_volume_event")
    """

    def __init__(
        self,
        name: str,
        target_logs_per_second: float = 100,
        min_sample_rate: float = 0.001,
        max_sample_rate: float = 1.0,
        adjustment_interval: float = 10.0,
        **kwargs,
    ):
        """
        Initialize adaptive sampled logger.

        Args:
            name: Logger name
            target_logs_per_second: Target log volume
            min_sample_rate: Minimum sample rate
            max_sample_rate: Maximum sample rate
            adjustment_interval: Seconds between rate adjustments
        """
        super().__init__(name, **kwargs)

        self.target_lps = target_logs_per_second
        self.min_rate = min_sample_rate
        self.max_rate = max_sample_rate
        self.adjustment_interval = adjustment_interval

        self._current_rate = 1.0
        self._log_count_in_window = 0
        self._window_start = time.time()
        self._adaptive_lock = threading.Lock()

    def _maybe_adjust_rate(self):
        """Adjust sampling rate based on current volume."""
        now = time.time()
        with self._adaptive_lock:
            elapsed = now - self._window_start

            if elapsed >= self.adjustment_interval:
                # Calculate current rate
                current_lps = self._log_count_in_window / elapsed

                if current_lps > 0:
                    # Calculate desired rate
                    desired_rate = self.target_lps / current_lps

                    # Apply bounds and smoothing
                    new_rate = self._current_rate * 0.7 + desired_rate * 0.3
                    self._current_rate = max(self.min_rate, min(self.max_rate, new_rate))

                # Reset window
                self._log_count_in_window = 0
                self._window_start = now

                _get_logger().debug(
                    "adaptive_sampling_adjusted",
                    logger=self.name,
                    new_rate=self._current_rate,
                    current_lps=current_lps,
                )

    def _get_sample_rate(self, level: str, event: str) -> float:
        """Get adaptive sample rate."""
        base_rate = super()._get_sample_rate(level, event)
        with self._adaptive_lock:
            return base_rate * self._current_rate

    def _log(self, level: str, event: str, **kwargs):
        """Log with adaptive sampling."""
        with self._adaptive_lock:
            self._log_count_in_window += 1
        self._maybe_adjust_rate()
        super()._log(level, event, **kwargs)

__init__

Python
__init__(
    name: str,
    target_logs_per_second: float = 100,
    min_sample_rate: float = 0.001,
    max_sample_rate: float = 1.0,
    adjustment_interval: float = 10.0,
    **kwargs,
)

Initialize adaptive sampled logger.

Parameters:

Name Type Description Default
name str

Logger name

required
target_logs_per_second float

Target log volume

100
min_sample_rate float

Minimum sample rate

0.001
max_sample_rate float

Maximum sample rate

1.0
adjustment_interval float

Seconds between rate adjustments

10.0
Source code in src/obskit/logging/sampling.py
Python
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
def __init__(
    self,
    name: str,
    target_logs_per_second: float = 100,
    min_sample_rate: float = 0.001,
    max_sample_rate: float = 1.0,
    adjustment_interval: float = 10.0,
    **kwargs,
):
    """
    Initialize adaptive sampled logger.

    Args:
        name: Logger name
        target_logs_per_second: Target log volume
        min_sample_rate: Minimum sample rate
        max_sample_rate: Maximum sample rate
        adjustment_interval: Seconds between rate adjustments
    """
    super().__init__(name, **kwargs)

    self.target_lps = target_logs_per_second
    self.min_rate = min_sample_rate
    self.max_rate = max_sample_rate
    self.adjustment_interval = adjustment_interval

    self._current_rate = 1.0
    self._log_count_in_window = 0
    self._window_start = time.time()
    self._adaptive_lock = threading.Lock()

obskit.logging.sampling.SampledLogger

Logger with intelligent sampling.

Reduces log volume while maintaining visibility for important events.

Example

logger = SampledLogger( name="high_volume_service", config=SamplingConfig( debug_rate=0.01, info_rate=0.1, dedupe_window_seconds=60, ) )

Only 10% of info logs will be emitted

logger.info("routine_operation", data="value")

Errors are always logged

logger.error("something_failed", error="details")

Mark as important to always log

logger.info("important_event", _important=True)

Source code in src/obskit/logging/sampling.py
Python
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
class SampledLogger:
    """
    Logger with intelligent sampling.

    Reduces log volume while maintaining visibility for important events.

    Example:
        logger = SampledLogger(
            name="high_volume_service",
            config=SamplingConfig(
                debug_rate=0.01,
                info_rate=0.1,
                dedupe_window_seconds=60,
            )
        )

        # Only 10% of info logs will be emitted
        logger.info("routine_operation", data="value")

        # Errors are always logged
        logger.error("something_failed", error="details")

        # Mark as important to always log
        logger.info("important_event", _important=True)
    """

    def __init__(
        self, name: str, config: SamplingConfig | None = None, base_logger: Any | None = None
    ):
        """
        Initialize sampled logger.

        Args:
            name: Logger name
            config: Sampling configuration
            base_logger: Underlying logger (defaults to structlog)
        """
        self.name = name
        self.config = config or SamplingConfig()
        self._logger = base_logger or structlog.get_logger(name)

        # Track for deduplication — protected by _dedup_lock
        self._recent_logs: dict[str, float] = {}  # dedupe_key -> last_logged_time
        self._occurrence_counts: dict[str, int] = defaultdict(int)
        self._dedup_lock = threading.Lock()

        # Track sampling stats
        self._sampled_count: dict[str, int] = defaultdict(int)
        self._dropped_count: dict[str, int] = defaultdict(int)

        # Call counter for periodic cleanup (every 1000 calls instead of 1% random)
        self._log_count: int = 0

    def _get_sample_rate(self, level: str, event: str) -> float:
        """Get sample rate for a log level/event."""
        # Check custom rules
        if event in self.config.custom_rules:
            return self.config.custom_rules[event].sample_rate

        # Default rates by level
        rates = {
            "debug": self.config.debug_rate,
            "info": self.config.info_rate,
            "warning": self.config.warning_rate,
            "error": self.config.error_rate,
            "critical": self.config.critical_rate,
        }
        return rates.get(level, 1.0)

    def _get_dedupe_key(self, level: str, event: str, **kwargs: Any) -> str:
        """Generate deduplication key for a log."""
        # Include level, event, and key kwargs in dedupe key
        key_parts = [level, event]

        # Add important kwargs to key
        for k in sorted(kwargs.keys()):
            if not k.startswith("_"):
                v = kwargs[k]
                if isinstance(v, (str, int, float, bool)):
                    key_parts.append(f"{k}={v}")

        key_string = "|".join(key_parts)
        return hashlib.sha256(key_string.encode(), usedforsecurity=False).hexdigest()[
            :16
        ]  # NOSONAR

    def _should_log(
        self,
        level: str,
        event: str,
        duration_seconds: float | None = None,
        important: bool = False,
        **kwargs: Any,
    ) -> tuple[bool, str]:
        """
        Determine if a log should be emitted.

        Returns:
            (should_log, reason)
        """
        # Always log list
        if event in self.config.always_log_events:
            return True, "always_log_event"

        # Never log list
        if event in self.config.never_log_events:
            return False, "never_log_event"

        # Important flag bypasses sampling
        if important:
            return True, "marked_important"

        # Slow operations always logged
        if duration_seconds and duration_seconds >= self.config.slow_threshold_seconds:
            return True, "slow_operation"

        # Single lock acquisition covers occurrence counting, dedup check,
        # sampling decision, and last-logged update atomically.  This avoids
        # the double-lock pattern (two separate acquisitions) that doubled lock
        # pressure on the hot path and left a TOCTOU window between them.
        dedupe_key = self._get_dedupe_key(level, event, **kwargs)
        now = time.time()
        sample_rate = self._get_sample_rate(level, event)
        with self._dedup_lock:
            self._occurrence_counts[dedupe_key] += 1
            count = self._occurrence_counts[dedupe_key]

            if count <= self.config.always_log_first_n:
                return True, "first_occurrences"

            last_logged = self._recent_logs.get(dedupe_key)
            if last_logged is not None and now - last_logged < self.config.dedupe_window_seconds:
                return False, "deduplicated"

            if random.random() > sample_rate:  # NOSONAR
                return False, "sampled_out"

            self._recent_logs[dedupe_key] = now
        return True, "sampled_in"

    def _cleanup_recent(self):
        """Clean up old entries from recent logs."""
        now = time.time()
        cutoff = now - self.config.dedupe_window_seconds * 2

        with self._dedup_lock:
            keys_to_remove = [k for k, v in self._recent_logs.items() if v < cutoff]
            for k in keys_to_remove:
                del self._recent_logs[k]

    def _log(self, level: str, event: str, **kwargs):
        """Internal log method with sampling."""
        # Extract special kwargs
        duration = kwargs.pop("_duration", None)
        important = kwargs.pop("_important", False)

        should_log, reason = self._should_log(level, event, duration, important, **kwargs)

        # Single lock acquisition: increment per-level counter AND flush to
        # the global stats dict atomically.  Two separate acquisitions left a
        # window where get_sampling_stats() could observe a partially-updated
        # state (local dict incremented but global dict not yet updated), and
        # also risked RuntimeError if a new level key was inserted while
        # get_stats() was iterating the dict without a lock.
        with _sampling_stats_lock:
            if should_log:
                self._sampled_count[level] += 1
            else:
                self._dropped_count[level] += 1
            sampled = sum(self._sampled_count.values())
            dropped = sum(self._dropped_count.values())
            _sampling_stats[self.name]["sampled"] = sampled
            _sampling_stats[self.name]["dropped"] = dropped

        if should_log:
            # Add sampling metadata
            kwargs["_sampling_reason"] = reason

            log_method = getattr(self._logger, level)
            log_method(event, **kwargs)

        # Periodic cleanup — run every 1000 calls instead of 1% random
        self._log_count += 1
        if self._log_count % 1000 == 0:
            self._cleanup_recent()

    def debug(self, event: str, **kwargs):
        """Log debug message."""
        self._log("debug", event, **kwargs)

    def info(self, event: str, **kwargs):
        """Log info message."""
        self._log("info", event, **kwargs)

    def warning(self, event: str, **kwargs):
        """Log warning message."""
        self._log("warning", event, **kwargs)

    def error(self, event: str, **kwargs):
        """Log error message."""
        self._log("error", event, **kwargs)

    def critical(self, event: str, **kwargs):
        """Log critical message."""
        self._log("critical", event, **kwargs)

    def exception(self, event: str, **kwargs):
        """Log exception (always logged)."""
        kwargs["_important"] = True
        self._log("error", event, exc_info=True, **kwargs)

    def bind(self, **kwargs):
        """Bind context to logger."""
        bound_logger = self._logger.bind(**kwargs)
        new_sampled = SampledLogger(self.name, self.config, bound_logger)
        # Share the same dedup state AND the same lock so bound loggers
        # don't race with the parent on the shared dicts.
        new_sampled._recent_logs = self._recent_logs
        new_sampled._occurrence_counts = self._occurrence_counts
        new_sampled._dedup_lock = self._dedup_lock
        return new_sampled

    def get_stats(self) -> dict[str, Any]:
        """Get sampling statistics."""
        total_sampled = sum(self._sampled_count.values())
        total_dropped = sum(self._dropped_count.values())
        total = total_sampled + total_dropped

        return {
            "logger_name": self.name,
            "total_logs": total,
            "sampled": total_sampled,
            "dropped": total_dropped,
            "effective_rate": total_sampled / total if total > 0 else 0,
            "by_level": {
                "sampled": dict(self._sampled_count),
                "dropped": dict(self._dropped_count),
            },
        }

__init__

Python
__init__(
    name: str,
    config: SamplingConfig | None = None,
    base_logger: Any | None = None,
)

Initialize sampled logger.

Parameters:

Name Type Description Default
name str

Logger name

required
config SamplingConfig | None

Sampling configuration

None
base_logger Any | None

Underlying logger (defaults to structlog)

None
Source code in src/obskit/logging/sampling.py
Python
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def __init__(
    self, name: str, config: SamplingConfig | None = None, base_logger: Any | None = None
):
    """
    Initialize sampled logger.

    Args:
        name: Logger name
        config: Sampling configuration
        base_logger: Underlying logger (defaults to structlog)
    """
    self.name = name
    self.config = config or SamplingConfig()
    self._logger = base_logger or structlog.get_logger(name)

    # Track for deduplication — protected by _dedup_lock
    self._recent_logs: dict[str, float] = {}  # dedupe_key -> last_logged_time
    self._occurrence_counts: dict[str, int] = defaultdict(int)
    self._dedup_lock = threading.Lock()

    # Track sampling stats
    self._sampled_count: dict[str, int] = defaultdict(int)
    self._dropped_count: dict[str, int] = defaultdict(int)

    # Call counter for periodic cleanup (every 1000 calls instead of 1% random)
    self._log_count: int = 0

debug

Python
debug(event: str, **kwargs)

Log debug message.

Source code in src/obskit/logging/sampling.py
Python
262
263
264
def debug(self, event: str, **kwargs):
    """Log debug message."""
    self._log("debug", event, **kwargs)

info

Python
info(event: str, **kwargs)

Log info message.

Source code in src/obskit/logging/sampling.py
Python
266
267
268
def info(self, event: str, **kwargs):
    """Log info message."""
    self._log("info", event, **kwargs)

warning

Python
warning(event: str, **kwargs)

Log warning message.

Source code in src/obskit/logging/sampling.py
Python
270
271
272
def warning(self, event: str, **kwargs):
    """Log warning message."""
    self._log("warning", event, **kwargs)

error

Python
error(event: str, **kwargs)

Log error message.

Source code in src/obskit/logging/sampling.py
Python
274
275
276
def error(self, event: str, **kwargs):
    """Log error message."""
    self._log("error", event, **kwargs)

critical

Python
critical(event: str, **kwargs)

Log critical message.

Source code in src/obskit/logging/sampling.py
Python
278
279
280
def critical(self, event: str, **kwargs):
    """Log critical message."""
    self._log("critical", event, **kwargs)

exception

Python
exception(event: str, **kwargs)

Log exception (always logged).

Source code in src/obskit/logging/sampling.py
Python
282
283
284
285
def exception(self, event: str, **kwargs):
    """Log exception (always logged)."""
    kwargs["_important"] = True
    self._log("error", event, exc_info=True, **kwargs)

bind

Python
bind(**kwargs)

Bind context to logger.

Source code in src/obskit/logging/sampling.py
Python
287
288
289
290
291
292
293
294
295
296
def bind(self, **kwargs):
    """Bind context to logger."""
    bound_logger = self._logger.bind(**kwargs)
    new_sampled = SampledLogger(self.name, self.config, bound_logger)
    # Share the same dedup state AND the same lock so bound loggers
    # don't race with the parent on the shared dicts.
    new_sampled._recent_logs = self._recent_logs
    new_sampled._occurrence_counts = self._occurrence_counts
    new_sampled._dedup_lock = self._dedup_lock
    return new_sampled

get_stats

Python
get_stats() -> dict[str, Any]

Get sampling statistics.

Source code in src/obskit/logging/sampling.py
Python
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
def get_stats(self) -> dict[str, Any]:
    """Get sampling statistics."""
    total_sampled = sum(self._sampled_count.values())
    total_dropped = sum(self._dropped_count.values())
    total = total_sampled + total_dropped

    return {
        "logger_name": self.name,
        "total_logs": total,
        "sampled": total_sampled,
        "dropped": total_dropped,
        "effective_rate": total_sampled / total if total > 0 else 0,
        "by_level": {
            "sampled": dict(self._sampled_count),
            "dropped": dict(self._dropped_count),
        },
    }

obskit.logging.sampling.SamplingConfig dataclass

Configuration for log sampling.

Source code in src/obskit/logging/sampling.py
Python
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@dataclass
class SamplingConfig:
    """Configuration for log sampling."""

    # Default sample rates by level
    debug_rate: float = 0.01  # 1%
    info_rate: float = 0.1  # 10%
    warning_rate: float = 1.0  # 100%
    error_rate: float = 1.0  # 100%
    critical_rate: float = 1.0  # 100%

    # Always log slow operations (threshold in seconds)
    slow_threshold_seconds: float = 1.0

    # Dedupe similar logs within this window
    dedupe_window_seconds: float = 60.0

    # Always log first N occurrences of a unique log
    always_log_first_n: int = 3

    # Custom rules by event name
    custom_rules: dict[str, SamplingRule] = field(default_factory=dict)

    # Events to always log (bypass sampling)
    always_log_events: set[str] = field(default_factory=set)

    # Events to never log
    never_log_events: set[str] = field(default_factory=set)