9 breaking releases

new 0.23.0 Sep 30, 2024
0.21.0 Aug 1, 2024
0.20.0 Jun 27, 2024
0.3.0 Feb 5, 2024
0.1.0 Dec 4, 2023

#559 in Asynchronous

Download history 1397/week @ 2024-06-16 1116/week @ 2024-06-23 1570/week @ 2024-06-30 251/week @ 2024-07-07 491/week @ 2024-07-14 495/week @ 2024-07-21 1066/week @ 2024-07-28 1229/week @ 2024-08-04 901/week @ 2024-08-11 1162/week @ 2024-08-18 998/week @ 2024-08-25 1494/week @ 2024-09-01 950/week @ 2024-09-08 924/week @ 2024-09-15 1377/week @ 2024-09-22 1188/week @ 2024-09-29

4,653 downloads per month
Used in 32 crates (4 directly)

MIT/Apache

275KB
4K SLoC

Rate-limited logging for frequent events

Often we want to tell the user about an event that is undesirable when it happens, but which can happen very frequently. In that case, we don't want to log "There was a problem!" a thousand times per hour. Instead, we'd like our logs to look more like "connecting to the guard X: error (problem occurred 1310/2000 times in the last hour)"

This crate is part of arti, and is not adapted for use outside of it: it assumes that your logging system is tracing, and that you are using tor_rtcompat for your asynchronous runtime.

Setup

Before you can use this crate, you need to call install_runtime, or messages won't be collected.

Example

use tor_log_ratelim::log_ratelim;
# use std::num::ParseIntError;
pub fn parse_u8(source: &str, s: &str) -> u8 {
    let r: Result<u8, ParseIntError> = s.parse();
    log_ratelim!(
        // The activity we were performing
        "parsing an integer from {}", source;
        // A Result to decide whether it succeeded 
        r; 
        // An error message to report on failure, with rate limiting,
        // after some time has elapsed.
        // The error itself is always reported.
        Err(_) => WARN, "Had to use default";
        // A success message to report (without rate limiting)
        // on every success.
        Ok(v) => TRACE, "Got {}", v;
    );
    r.unwrap_or(0)
}

The above example might produce WARN outputs more or less like these:

WARN: Parsing an integer from cache: error (Problem occurred 7/10 times in the last minute): Had to use default: invalid digit found in string"
WARN: Parsing an integer from cache: error (Problem occurred 81/92 times in the last 5 minutes): Had to use default: number too large to fit in target type"
WARN: Parsing an integer from cache: now working (Problem occurred 0/106 times in the last 5 minutes)

and TRACE outputs like these:

TRACE: Parsing an integer from cache: Got 7
TRACE: Parsing an integer from cache: Got 14
TRACE: Parsing an integer from the network: Got 2

For more information on log_ratelim!, and simpler ways to invoke it, see its documentation.

Dependencies

~2.1–3.5MB
~63K SLoC