Tracing::instrument(err) -- anyhow::Result or something else?

instrument in tracing - Rust has a very cool feature of instrument(err) in that for a Result<T, E> it emits an event if and only if is an error.

Generally, I use anyhow::Result for these types of things; but I am wondering if there is something more json-y / works better with tracing than anyhow::Result ?

I would normally let the error bubble up to somewhere higher and explicitly match on it there. The anyhow::Error type is great, but because it erases the type you are often reduced to printing it as a string or trying to downcast it to a known error type so you can log more structured messages.

I am willing to give up matching on the error. However, I still want structured messages. Is there something like "structured-anyhow" ? One can imagine a Result<T, JsonValue> almost satisfying this.

I am willing to give up the match on error since most of my error handling code is either: (1) die or (2) retry after delay. However, for ease of debugging, I do want structured messages.

@Michael-F-Bryan : Also, I don't know if you got pinged/notified for the followup question in Rust logger package? - #5 by zeroexcuses -- i.e. is there a way to do your setup but locally instead of depending on Datadog / Elastic.

It might be easier to roll your own solution. Otherwise, you could emit more structured error logs when the error is first encountered.

Also, it's worth asking yourself what kind of structured information you want out of these errors. Try to think of a couple use cases and work your way backwards.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.