Porting from slog to tracing and span shenanigans

Hi,
I have a project that used slog and I'm trying use tracing instead.

With slog, I use the Logger::new() function in order to put some fields value that I wanted in all the logs with this logger.

let logger = Logger::new(o!("my_key" => some_value));
info!(logger, "this log will have fields my_key printed as well");

Thinking about how to achieve that with tracing I though that Span could be used for that. But then I read about Span::enter() and async code and it looks like really convoluted for the user.

Am I missing something to achieve what I want or do I need this instrument(span) thing?

The easiest solution will be using #[tracing::instrument], which just does the right thing with async fn. To put an entire future inside a span, you do want Instrument::instrument.

The important thing to note is that tracing spans are always used in a scoped manner, whereas slog-scope is optional and generally not preferred. A superpower that this gives tracing spans is that they know how long they're active, meaning you can do profiling off of them.

You don't pass around spans in tracing the way you might with slog; you just use the contextual stack of what spans have been entered.

1 Like

reading on the attribute macro and it makes things clearer indeed. Ta!