tracing icon indicating copy to clipboard operation
tracing copied to clipboard

tracing: add record! macro

Open marcusirgens opened this issue 4 months ago • 0 comments

Add a macro that provides a more consise way of recording a new value for a field.

Motivation

The ability to record updated values for a span is very useful. However, it can be a bit verbose, and for what I assume is the most common scenario, adding fields to the current span, the update can quickly capture four lines in an already-busy function.

Here's an example from a code base I'm contributing to, with some names changed, using a helper function to format a time::Instant value inside of a tracing::Span::record call:

impl RetroEncabulator for AcmeEncabulator {
    #[tracing::instrument(fields(request.start = tracing::field::Empty, request.deadline = tracing::field::Empty))]
    async fn prevent_sidefumbling(&self, foo: Bar, baz: Qux) {
        /* omitted for brevity */

        tracing::Span::current().record(
            "request.start",
            tracing::field::display(display_instant(start)),
        );
        tracing::Span::current().record(
            "request.deadline",
            tracing::field::display(display_instant(deadline)),
        );

        /* ... etc */
    }
}

Solution

My proposal is to add a way to record a field on the current span. This PR has one such implementation, which, using the code sample above, gives:

impl RetroEncabulator for AcmeEncabulator {
    #[tracing::instrument(fields(request.start = tracing::field::Empty, request.deadline = tracing::field::Empty))]
    async fn prevent_sidefumbling(&self, foo: Bar, baz: Qux) {
        /* omitted for brevity */

        tracing::record!("request.start", %display_instant(start));
        tracing::record!("request.deadline", %display_instant(deadline));

        /* ... etc */
    }
}

I'm open to other suggestions, of course, and if the maintainers agree that this is something that should be solved in the tracing package, I need some help figuring out where such an implementation belongs. This example implementation "feels" right as tracing::field::record!, but I've yet to fully understand how macro exports work.

marcusirgens avatar Apr 03 '24 16:04 marcusirgens