Adding custom fields to tracing events

I'm using the tracing crate for a project. I'd like to add some custom fields to every event; hostname, pid, and so forth.

I've been bouncing of the documentation for a bit and have come to the conclusion that the only way I can do this is to write a custom formatter much like I see in tracing-bunyan-formatter. Before I go through that exercise, is there an easier way to drop in a few new fields? The pretty format is, well, pretty and I know I won't come close if left to my own devices.

The correct way to handle this in tracing is that you enter some span! with the appropriate fields. The info of the span is then attached to the span trace of each event inside of that span.

This can be done either with the span! macros, or with #[instrument(skip_all, fields(hostname = req.host(), pid = getpid(), so_forth))] on a function.

If you just want the fields on the event, you can just use field = value (or ?field shorthands) on the event macro itself.

2 Likes

I had some additional challenges because I'm using async. Once I worked through that, I have it exactly like I want. Thank you, @CAD97!

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.