Attribute Macro instrument

#[instrument]
Expand description

An attribute macro designed to eliminate boilerplate code.

This macro automatically creates a span for the annotated function. The span name defaults to the function name but can be customized by passing a string literal as an argument using the name parameter.

The #[trace] attribute requires a local parent context to function correctly. Ensure that the function annotated with #[trace] is called within a local context of a Span, which is established by invoking the Span::set_local_parent() method.

§Arguments

  • name - The name of the span. Defaults to the full path of the function.
  • short_name - Whether to use the function name without path as the span name. Defaults to false.
  • properties - A list of key-value pairs to be added as properties to the span. The value can be a format string, where the function arguments are accessible. Defaults to {}.

§Examples

use veecle_telemetry::instrument;

#[veecle_telemetry::instrument]
fn simple() {
    // ...
}

#[veecle_telemetry::instrument(short_name = true)]
async fn simple_async() {
    // ...
}

#[veecle_telemetry::instrument(properties = { "k1": "v1", "a": 2 })]
async fn properties(a: u64) {
    // ...
}

The code snippets above will be expanded to:


fn simple() {
    let __guard__ = Span::new("example::simple", &[]).entered();
    // ...
}

async fn simple_async() {
    veecle_telemetry::future::FutureExt::with_span(
        async move {
            // ...
        },
        veecle_telemetry::Span::new("simple_async", &[]),
    )
    .await
}

async fn properties(a: u64) {
    veecle_telemetry::future::FutureExt::with_span(
        async move {
            // ...
        },
        veecle_telemetry::Span::new("example::properties", &[
            KeyValue::new("k1", "v1"),
            KeyValue::new("a", 2),
        ]),
    )
    .await
}