Tracing fallible functions
The ok_lvl
and err_lvl
control parameters can be applied to span macros to assign a level based on whether the annotated function returned Ok
or Err
:
Event {
mdl: "my_app",
tpl: "wait a bit",
extent: Some(
"2024-06-12T21:43:03.556361000Z".."2024-06-12T21:43:03.661164000Z",
),
props: {
"lvl": info,
"evt_kind": span,
"span_name": "wait a bit",
"trace_id": 6a3fc0e46bfa1da71537e39e3bf1942c,
"span_id": f5bcc5821c6c3227,
"sleep_ms": 100,
},
}
Event {
mdl: "my_app",
tpl: "wait a bit",
extent: Some(
"2024-06-12T21:43:03.661850000Z".."2024-06-12T21:43:03.661986000Z",
),
props: {
"lvl": error,
"err": Custom {
kind: Other,
error: "the wait is too long",
},
"evt_kind": span,
"span_name": "wait a bit",
"trace_id": 3226b70b45ff90f92f4feccee4325d4d,
"span_id": 3702ba2429f9a7b7,
"sleep_ms": 1200,
},
}
Mapping error types
Attaching errors to spans requires they're either &str
, &(dyn std::error::Error + 'static)
, or impl std::error::Error
. Error types like anyhow::Error
don't satisfy these requirements so need to be mapped.
The err
control parameter can be used to map the error type of a fallible span into one that can be captured:
The err
control parameter accepts an expression that implements Fn(&E) -> U
, which can either be provided as a closure inline, or as an external function like emit::err::as_ref
in the above example.
If your error type can't be mapped, you can also fall back to just providing a static string description as the error value:
Panics
If a function annotated with #[span]
panics, it will emit an event with an error level and an err
property indicating a panic was observed. The panic_lvl
control parameter can be used to specify a different level in case of panics.