-
Notifications
You must be signed in to change notification settings - Fork 721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Syntax for recording errors in events and spans #3067
Comments
Instead of a special symbol, could we perhaps specialise on the Another idea could be a wrapper struct. Anything that implements |
This assumes that no one ever will want two errors in one event.
This is how
|
Perhaps that could be a first step into this direction to make it easier to use and not require these obscure casts? |
Feature Request
Crates
tracing-core
,tracing
Motivation
Tracing supports recording a type implementing
std::error::Error
with a methodVisit::record_error
. Currently, one way to record such type inevent
orspan
macros is by writing something similar toOne could bring
std::error::Error
into scope asStdError
, but even then it can be considered cumbersome to write.Also, users might have to use different syntax depending on type of
error
:I consider this too explicit. I do not see any reason why
Debug
andDisplay
would have a dedicated syntax, whileError
requires spelling out the type cast every time, especially considering that errors are added to traces as frequently as any other value (at least for me that's the case).It might be valuable to gather data on whether recording errors with
... as &dyn std::error::Error
is the most common way, or are there any popular alternatives.Why not just use the
?
or%
for errors?In my opinion, if
Visit
has a method specifically for recording errors, then that feature should be available for users inspan
andevent
macros, which are the basic blocks for defining spans and traces. For logging subscribers this is probably not that big of a difference, but for other subscribers that might collect statistics or other data, this distinction might be important.Proposal
I propose to introduce syntax for recording errors in
event
andspan
macros intracing
and addErrorValue
totracing-core
as an analogous totracing_core::field::DebugValue
andtracing_core::field::DisplayValue
.Possible syntax candidates are:
Sigil
#
, aka pound sign/hashtag/octothorpSigil
^
, aka caretSigil
@
, aka at signKeyword
There is a limited number of symbols available in rust. One alternative is to use a keyword like
err
ore
.Keyword and a sigil
Mixing is also an option, e.g.
e%
:Other, most likely invalid candidates
~
, aka tilde, is currently disallowed in Rust when writing~expr
to help users who might think this is a unary bitwise not operator, recommending to use!
instead, thus makingerror!(~err)
fail before macro expansion,!
, aka exclamation mark, might clash with Rust's negation operator inerror!(error = !error)
Alternatives
Don't add anything
Currently there is one other way to record an error using therecord_error
method - using theinstrument
macro.Edit: I was wrong, there's an open PR to implement that #3057.
Thus, one alternative is to not add additional support for recording errors in
event
andspan
macros.Function
error_as_dyn
or traitErrorExt
tracing
could recommend users to cast the error to&dyn StdError
with a function. This option results in less jarring syntax than manually casting, but still not as concise as a single sigil or keyword.It could also be possible to define trait
ErrorExt
with methodas_dyn
, but I failed at figuring out the lifetime bounds. As far as I know there's not such trait or utility function in standard library.Wait until
valuable
is stabilizedPersonally, I've never used the
valuable
feature, but from what I understand it could remove the need for a special syntax by allowing users to implementvaluable::Valuable
for their own error's, or implement it for all errors (if possible).Docs
Write examples, blog posts, documentation, etc. on how to structure programs to better utilize current tracing features for logging errors. Of course, this is a good thing to do, regardless of the future of this issue.
The text was updated successfully, but these errors were encountered: