tracing_subscriber: add additional fields for printing to json fmt subscriber #2943
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
Closes #1531
There are scenarios where one wants to add some information to logs emitted by
FmtSubscriber
, for example adding OpenTelemetry trace ID and span ID.Solution
This adds an extension which all other subscribers can fill with any fields and then the formatting subscriber can emit these fields alongside the normal ones.
The extension currently wraps a
HashMap<String, String>
, but it could also feasibly be something likeHashMap<String, Box<dyn Value>>
but the flexibility of having other types than string permissible heredidn't seem to offset the additional allocations for thisString
also needs an allocation. Otherwise we could useHashMap<String, serde_json::Value>
if this would be considered to be json-specific but I don't think we should go that way now.Currently, only the
Json
formatter prints these fields as other ones do not print span attributes at all.