Attribute Macro span

Source
#[span]
Expand description

Trace the execution of a function.

See the guide for more details on tracing and span construction, including more advanced use cases.

§Examples

Note that debug_span, info_span, warn_span, and error_span use the same syntax.

Emitting an event with captured properties in the template:

#[emit::span!("executing with {x} and {y}")]
fn exec(x: i32, y: bool) {
    // Your code goes here
}

Emitting an event with captured properties after the template:

#[emit::span!("executing", x, y)]
fn exec(x: i32, y: bool) {
    // Your code goes here
}

Specifying control parameters before the template (in this example, mdl):

#[emit::span!(mdl: emit::path!("a::b"), "executing")]
fn exec(x: i32, y: bool) {
    // Your code goes here
}

§Syntax

(control_param),* template_literal, (property),*

where

  • control_param: A Rust field-value with a pre-determined identifier (see below).
  • template_literal: A template string literal.
  • property: A Rust field-value for a property to capture.

§Control parameters

This macro accepts the following optional control parameters:

nametypedescription
rtimpl emit::runtime::RuntimeThe runtime to emit the event through.
mdlimpl Into<emit::Path>The module the event belongs to. If unspecified the current module path is used.
whenimpl emit::FilterA filter to use instead of the one configured on the runtime.
guard-An identifier to bind an emit::SpanGuard to in the body of the span for manual completion.
ok_lvlstr or emit::LevelAssume the instrumented block returns a Result. Assign the event the given level when the result is Ok.
err_lvlstr or emit::LevelAssume the instrumented block returns a Result. Assign the event the given level when the result is Err and attach the error as the err property.
panic_lvlstr or emit::LevelDetect whether the function panics and use the given level if it does.
errimpl Fn(&E) -> TAssume the instrumented block returns a Result. Map the Err variant into a new type T that is str, &(dyn Error + 'static), or impl Error + 'static
setupimpl Fn() -> TInvoke the expression before creating the span, binding the result to a value that’s dropped at the end of the annotated function.

§Template literals

Templates are text literals that include regular text with holes. A hole is a point in the template where a property should be interpolated in.

  • template_literal: " (text | hole)* "
  • text: A fragment of plain text where { are escaped as {{ and } are escaped as }}.
  • hole: { property }
  • property: A Rust field-value expression.

The following are all examples of templates:

"some text"
 ├───────┘
 text
"some text and {x}"
 ├────────────┘ │
 text           property
"some {{text}} and {x: 42} and {y}"
 ├────────────────┘ ├───┘ └───┤ │
 text               property  │ property
                              text

See the guide for more details and examples of templates.

§Properties

Properties that appear within the template or after it are added to the emitted event. The identifier of the property is its key. Property capturing can be adjusted through the as_* attribute macros.

See the guide for more details on property capturing.