-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtracing_setup.rs
More file actions
51 lines (41 loc) · 1.4 KB
/
tracing_setup.rs
File metadata and controls
51 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::{env, io};
use tracing::Level;
use tracing_loki::url::Url;
use tracing_loki::BackgroundTask;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{filter, fmt};
use warp::hyper::Client;
fn build_loki_layer(url: &str) -> (tracing_loki::Layer, BackgroundTask) {
tracing_loki::layer(
Url::parse(url).unwrap(),
vec![("host".into(), "rusty_controller".into())]
.into_iter()
.collect(),
vec![].into_iter().collect(),
)
.unwrap()
}
pub async fn setup_loki() {
let filter = filter::Targets::new()
.with_target("rusty_controller", Level::TRACE)
.with_default(Level::WARN);
let registry = tracing_subscriber::registry()
.with(filter)
.with(fmt::layer().with_writer(io::stdout));
let http = Client::new();
let mut is_url_provided = true;
let loki_base_url = env::var("LOKI_BASE_URL").unwrap_or_else(|_| {
is_url_provided = false;
"http://127.0.0.1:3100".into()
});
if !is_url_provided && http.get(loki_base_url.parse().unwrap()).await.is_err() {
registry.init();
tracing::warn!("Couldn't connect to Loki. Continuing without it.");
return;
}
let (layer, task) = build_loki_layer(&loki_base_url);
registry.with(layer).init();
tokio::spawn(task);
tracing::info!("Loki initialized");
}