near_o11y/
macros.rs

1/// Internal use only.
2#[macro_export]
3macro_rules! handler_span {
4    (target: $target:expr, level: $lvl:expr, $msg:expr, $($extra_fields:tt)*) => {{
5        let WithSpanContext { msg, context, .. } = $msg;
6        let span = tracing::span!(
7            target: $target,
8            $lvl,
9            "handle",
10            handler = near_o11y::macros::type_name_of(&msg),
11            actor = near_o11y::macros::last_component_of_name(std::any::type_name::<Self>()),
12            $($extra_fields)*)
13        .entered();
14        <tracing::span::Span as near_o11y::OpenTelemetrySpanExt>::set_parent(&span, context);
15        (span, msg)
16    }};
17}
18
19/// A macro that lets attach `handle()` functions to the tracing context that
20/// generated the actix message being processed.
21/// Creates a DEBUG-level span with the handler type name and the message type name as attributes.
22#[macro_export]
23macro_rules! handler_debug_span {
24    (target: $target:expr, $msg:expr) => {
25        $crate::handler_span!(target: $target, level: tracing::Level::DEBUG, $msg, )
26    };
27    (target: $target:expr, $msg:expr, $($extra_fields:tt)*) => {
28        $crate::handler_span!(target: $target, level: tracing::Level::DEBUG, $msg, $($extra_fields)*)
29    };
30}
31
32/// A macro that lets attach `handle()` functions to the tracing context that
33/// generated the actix message being processed.
34/// Creates a TRACE-level span with the handler type name and the message type name as attributes.
35#[macro_export]
36macro_rules! handler_trace_span {
37    (target: $target:expr, $msg:expr) => {
38        $crate::handler_span!(target: $target, level: tracing::Level::TRACE, $msg, )
39    };
40    (target: $target:expr, $msg:expr, $($extra_fields:tt)*) => {
41        $crate::handler_span!(target: $target, level: tracing::Level::TRACE, $msg, $($extra_fields)*)
42    };
43}
44
45/// For internal use by `handler_span!`.
46/// Given 'abc::bcd::cde' returns 'cde'.
47/// Given 'abc' returns 'abc'.
48pub fn last_component_of_name(name: &str) -> &str {
49    name.rsplit_once("::").map_or(name, |(_, name)| name)
50}
51
52/// For internal use by `handler_span!`.
53/// Returns the last component of the name of type `T`.
54pub fn type_name_of<T>(_: &T) -> &str {
55    last_component_of_name(std::any::type_name::<T>())
56}