tracer

Macro tracer 

Source
macro_rules! tracer {
    ($name:ident) => { ... };
    () => { ... };
}
Expand description

Creates a static OpenTelemetry tracer for instrumentation.

This macro creates a LazyLock<BoxedTracer> static that initializes on first use from the global tracer provider. Declare at crate or module root, then import where needed.

§Prerequisites

The global tracer provider must be initialized before any spans are created. Typically done in main() before calling into application code:

fn main() {
    let provider = opentelemetry_otlp::new_pipeline()
        .tracing()
        .with_exporter(opentelemetry_otlp::new_exporter().tonic())
        .install_batch(opentelemetry_sdk::runtime::Tokio)
        .expect("Failed to initialize tracer");

    opentelemetry::global::set_tracer_provider(provider);

    run_app();  // Now TRACER can be used

    opentelemetry::global::shutdown_tracer_provider();
}

§Variants

§Default tracer (no arguments)

Creates a TRACER static using the crate’s package name as the instrumentation scope:

// In lib.rs or main.rs
otel::tracer!();

// Creates: pub(crate) static TRACER: LazyLock<BoxedTracer>
// Scope name: env!("CARGO_PKG_NAME")

§Named tracer

Creates a tracer with a custom name suffix, useful for subsystems:

// In src/client/mod.rs
otel::tracer!(lsp_client);

// Creates: pub(crate) static LSP_CLIENT_TRACER: LazyLock<BoxedTracer>
// Scope name: "{CARGO_PKG_NAME}.lsp_client"

§Tracer Configuration

Each tracer is configured with:

PropertyValue
Scope nameCrate name, or {crate}.{subsystem} for named tracers
VersionCARGO_PKG_VERSION
Schema URLhttps://opentelemetry.io/schemas/{OTEL_SPEC_VERSION}

§Multiple Tracers

Use named tracers to separate instrumentation by subsystem. This makes it easy to filter traces by scope in your observability backend:

// src/lib.rs - default tracer for general use
otel::tracer!();

// src/client/mod.rs - client subsystem
otel::tracer!(client);

// src/server/mod.rs - server subsystem
otel::tracer!(server);

Use explicit tracer syntax in span! to select which tracer to use:

use crate::client::CLIENT_TRACER;

fn send_request() {
    let (_cx, _guard) = otel::span!(@CLIENT_TRACER, "request.send");
}

§Example: Full Setup

// src/lib.rs
otel::tracer!();

pub mod engine;
// src/engine.rs
use crate::TRACER;

pub fn run() {
    let (_cx, _guard) = otel::span!("engine.run");
    // ...
}