tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.19k stars 677 forks source link

Skip recording `Span` fields when not requested #2881

Open Swatinem opened 5 months ago

Swatinem commented 5 months ago

In particular for the Json formatter, one can disable output of spans, in which case it is not necessary to record all of the span fields, as that is potentially expensive.

Motivation

Creating Spans is a lot more frequent than capturing Events (in particular with #[instrument]). In some profiling I have done, it shows that the overhead of tracing is quite high, in particular recording of all the Span fields.

Solution

There is already a mechanism to disable outputting all the spans of Events, at least for the Json formatter. However that was still recording all the fields, even though they are never being used. This will now skip that.

mladedav commented 5 months ago

Hi, why isn't not setting the fields on the span sufficient? Either by not naming them or using #[instrument(skip_all)].

Do you want your json logs to contain only the name of the span while other subscribers such as otel contains all the fields? Or am I missing the intended use case?

Swatinem commented 5 months ago

Do you want your json logs to contain only the name of the span while other subscribers such as otel contains all the fields?

Yes pretty much. I have another subscriber / layer which should record all the fields, but I don’t care to see them in the JSON output.