Rate limiting with log tracing in Rust

216 views Asked by At

My codebase has a lot of error message logging using the tracing crate. Does the log tracing crate in Rust have any rate limited error messaging facilities or do we have to make our own wrapper?

1

There are 1 answers

0
kmdreko On

Behavior like that would depend on the Subscriber you are using, but I don't know of any that do rate-limiting. Normally one doesn't want their logs to be thrown out.

You'd probably have to make your own. Here's how to go about it:

  • If you are using tracing-subscriber (or at least if your output method has a Layer implementation), you can create a Filter and put it on your layer. You would keep track of your limiting metrics in enabled() and return false to reject them. This is probably the easiest and most composable solution if available.
  • If you are using some other Subscriber, you must wrap and forward all methods to the inner subscriber except in enabled() where you would do your logic like above.

Before going for a rate-based approach, you should double-check if other unnecessary logs could be filtered out (if your existing output supports it).