#tracing #loki #tracing-layer #builder

tracing-loki-fmt

Grafana Loki tracing layer

3 releases

new 0.1.2 May 5, 2025
0.1.1 May 5, 2025
0.1.0 May 5, 2025

#4 in #loki

GPL-3.0-or-later

20KB
329 lines

Layer that pushes logs formatted with tracing-subscriber into Grafana Loki and attaching all fields as Loki's structured metadata.

It combines both log readability and convenient manipulation (filtering | field = "value", aggregation, etc) without the need for parsing.

Example

use eyre::Result;
use std::time::Duration;
use tokio::spawn;
use tokio::time::sleep;
use tracing::{info, info_span};
use tracing_loki_fmt::Builder;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{fmt, registry};

#[tokio::main]
async fn main() -> Result<()> {
    let builder = Builder::new(
        "http://grafana.proxmox/loki/api/v1/push",
        fmt::layer().without_time(),
    )?;
    let (layer, task) = builder
        .add_label("this_is_label", "test456")
        .add_label("job", "test")
        .add_field("this_is_static_field", "Test6666")
        .build();
    spawn(task.run());

    registry()
        .with(fmt::layer())
        .with(layer)
        .init();

    let _span1 = info_span!("span1", hello = "world").entered();
    let _span2 = info_span!("span2", world = "test").entered();

    info!(test = 123, test1 = "456", "hello world");

    sleep(Duration::from_secs(15)).await;

    Ok(())
}

Dependencies

~8–21MB
~286K SLoC