hrxi / tracing-loki

A tracing layer for shipping logs to Grafana Loki
Apache License 2.0
43 stars 20 forks source link

Content is always escaped? #15

Closed Christoph-AK closed 1 year ago

Christoph-AK commented 1 year ago

Hey there, first of all thanks so much for this! Such a big help in day to day logging needs.

I'm currently writing a quite involved interface that exchanges data between an ecommerce platform and an ERP system and makes heavy use of logging.

One thing I noticed is that all log lines that get sent to loki are fully escaped. Is this a Grafana thing, or are they escaped within this crate? I would love to view the logs unescaped for easier handling.

My setup (roughly):

    let (layer, task) = tracing_loki::layer(
        loki_url,
        vec![host, app, log_env].into_iter().collect(),
        Default::default(),
    )?;
    tracing_subscriber::registry()
        .with(layer)
        .with(tracing_subscriber::fmt::Layer::new())
        .with(tracing_subscriber::EnvFilter::from_str(
            "info,tiberius=error,hyper=error",
        )?)
        .init();

Example (a bit mangled to hammer down the point):


                let diff = format_text_diff(&jp.body_html, &sp.body_html);
                warn!(
                    "Beschreibungstext bei {} abweichend.
Abweichungen: {diff}

JTL: '{}',
Shopify: '{}'",
                    jp.handle, jp.body_html, sp.body_html
                );

What gets displayed in grafana:

"Beschreibungstext bei besser-grillen abweichend.\nAbweichungen: Gelöscht in Position 261:\t\t\" \"Hinzugefügt in Position 261:\t\" \"\n\nJTL: ....',\nShopify: ...'\n\nTrace:\n \n ...\n"

In the CLI the correct, unescaped output from the tracing_subscriber layer is shown.

For completeness: I made a small wrapper to include the backtrace in warnings. Shouldn't modiy any escaping tho.


#[macro_export(local_inner_macros)]
macro_rules! warn {
    // warn!("a {} event", "log")
    ($($arg:tt)+) => {{
        let mut a = std::format!($($arg)+);
        a += "\n\nTrace:\n";
        let b = std::backtrace::Backtrace::force_capture();
        let bs = b.to_string();
        a += &bs;
        log::warn!("{a}")
    }};
}
Christoph-AK commented 1 year ago

... as always: Trying to get it to run for hours, then write an issue, and find the solution 3 minutes later...

Using {app="ankertau", env="live-de", level=~"warn|error"} | json | line_format `{{.message}}` shows the correct, unescaped text. I'll try to find a more elegant way to display it, but for now it's working nicely.

image