tracing-core: correctly use features for tracing macro #2739
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
An alternative to #2655.
Currently,
tracing-core
defines atracing
feature which is never used, because it was intended only for expansion inside a macro, but the#[cfg(feature = "tracing")]
attribute is expanded and is evaluated based on whether the consuming crate (in this case eitheraxum
oraxum-extra
) has the feature enabled.Solution
Based on the feature, we can either expand the macro to a
tracing::event!
call or we can just expand to nothing. This way, the feature works as intended.One difference between this and removing the feature is that with this change, if either
tracing
ortracing-extra
enables thetracing
feature, it will transitively enable the same feature intracing-core
so extractors from both crates will have their rejections logged.If we instead remove the feature from
tracing-core
, each crate'stracing
feature will be independent which may result in some extractors being logged and others not. So although it gives more control to the user, I personally think the behavior may be surprising.Also, removing the feature is a breaking change while this is not.