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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#[cfg(feature = "opentelemetry")]
pub mod telemetry;
#[cfg(feature = "opentelemetry")]
pub use opentelemetry_sdk::trace::SdkTracerProvider;
#[cfg(feature = "tracing-loki")]
mod loki;
use tracing_subscriber::{
EnvFilter, Layer, Registry, layer::SubscriberExt, util::SubscriberInitExt,
};
/// Telemetry handle
#[derive(bon::Builder)]
#[builder(finish_fn(vis = "", name = build_internal))]
pub struct Tracing {
#[builder(field = vec![tracing_subscriber::fmt::layer().boxed()])]
layers: Vec<Box<dyn Layer<Registry> + Sync + Send>>,
#[cfg(feature = "tracing-loki")]
#[builder(setters(vis = "", name = loki_internal))]
pub loki_task: tracing_loki::BackgroundTask,
#[cfg(feature = "opentelemetry")]
#[builder(setters(vis = "", name = otel_internal))]
pub otel_provider: opentelemetry_sdk::trace::SdkTracerProvider,
}
// Define a custom finishing function as a method on the `UserBuilder`.
// The builder's state must implement the `IsComplete` trait.
// See details about it in the tip below this example.
impl<S: tracing_builder::IsComplete> TracingBuilder<S> {
pub fn build(self, config: &crate::Monitoring) -> Tracing {
// Delegate to `build_internal()` to get the instance of user.
let mut tracing = self.build_internal();
let layers = std::mem::take(&mut tracing.layers);
tracing_subscriber::registry()
.with(layers)
.with(
EnvFilter::try_from_default_env()
.unwrap_or_else(|_| config.log_level.to_string().into()),
)
.try_init()
.ok();
tracing
}
}
#[cfg(test)]
mod tests {
use crate::{AppConfig, Environment, Monitoring};
use super::*;
#[tokio::test]
async fn build() {
let config = Monitoring {
log_level: "error".to_string(),
opentelemetry_endpoint: "http://localhost:4317".into(),
loki_endpoint: "http://localhost:3100".into(),
};
let app_config = AppConfig {
name: "test".into(),
version: "1.0.0".into(),
env: Environment::Development,
port: 6969,
};
let tracing = Tracing::builder().opentelemetry(&app_config, &config);
assert!(tracing.is_ok());
let tracing = tracing.unwrap().loki(&app_config, &config);
assert!(tracing.is_ok());
}
}
|