2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-23 03:41:23 +00:00
pict-rs/src/init_tracing.rs

92 lines
2.9 KiB
Rust
Raw Normal View History

2022-03-28 04:27:07 +00:00
use crate::config::{LogFormat, OpenTelemetry, Tracing};
use console_subscriber::ConsoleLayer;
2021-10-21 00:28:40 +00:00
use opentelemetry::{
sdk::{propagation::TraceContextPropagator, Resource},
KeyValue,
};
use opentelemetry_otlp::WithExportConfig;
use tracing::subscriber::set_global_default;
use tracing_error::ErrorLayer;
use tracing_log::LogTracer;
2021-12-17 02:46:46 +00:00
use tracing_subscriber::{
2022-03-28 04:27:07 +00:00
fmt::format::FmtSpan, layer::SubscriberExt, registry::LookupSpan, Layer, Registry,
2021-12-17 02:46:46 +00:00
};
2021-10-21 00:28:40 +00:00
2022-03-29 01:47:46 +00:00
pub(super) fn init_tracing(tracing: &Tracing) -> color_eyre::Result<()> {
color_eyre::install()?;
2021-10-21 00:28:40 +00:00
LogTracer::init()?;
opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
2022-03-28 04:27:07 +00:00
let format_layer =
tracing_subscriber::fmt::layer().with_span_events(FmtSpan::NEW | FmtSpan::CLOSE);
2021-12-17 02:46:46 +00:00
2022-03-28 04:27:07 +00:00
match tracing.logging.format {
LogFormat::Compact => with_format(format_layer.compact(), tracing),
LogFormat::Json => with_format(format_layer.json(), tracing),
LogFormat::Normal => with_format(format_layer, tracing),
LogFormat::Pretty => with_format(format_layer.pretty(), tracing),
}
}
2022-03-29 01:47:46 +00:00
fn with_format<F>(format_layer: F, tracing: &Tracing) -> color_eyre::Result<()>
2022-03-28 04:27:07 +00:00
where
F: Layer<Registry> + Send + Sync,
{
let format_layer = format_layer.with_filter(tracing.logging.targets.targets.clone());
2021-10-21 00:28:40 +00:00
let subscriber = Registry::default()
.with(format_layer)
.with(ErrorLayer::default());
2022-03-28 04:27:07 +00:00
if let Some(address) = tracing.console.address {
2022-04-07 02:40:49 +00:00
println!("Starting console on {}", address);
2022-04-11 20:17:54 +00:00
let console_layer = ConsoleLayer::builder()
2022-03-28 04:27:07 +00:00
.event_buffer_capacity(tracing.console.buffer_capacity)
.server_addr(address)
.spawn();
let subscriber = subscriber.with(console_layer);
2022-03-28 04:27:07 +00:00
with_subscriber(subscriber, &tracing.opentelemetry)
} else {
2022-03-28 04:27:07 +00:00
with_subscriber(subscriber, &tracing.opentelemetry)
}
}
2022-02-01 16:10:52 +00:00
2022-03-29 01:47:46 +00:00
fn with_subscriber<S>(subscriber: S, otel: &OpenTelemetry) -> color_eyre::Result<()>
where
S: SubscriberExt + Send + Sync,
for<'a> S: LookupSpan<'a>,
{
2022-03-28 04:27:07 +00:00
if let Some(url) = otel.url.as_ref() {
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_trace_config(
opentelemetry::sdk::trace::config().with_resource(Resource::new(vec![
KeyValue::new("service.name", otel.service_name.clone()),
])),
)
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(url.as_str()),
)
.install_batch(opentelemetry::runtime::Tokio)?;
2021-10-21 00:28:40 +00:00
2021-12-17 02:46:46 +00:00
let otel_layer = tracing_opentelemetry::layer()
.with_tracer(tracer)
2022-03-28 04:27:07 +00:00
.with_filter(otel.targets.as_ref().targets.clone());
2021-10-21 00:28:40 +00:00
let subscriber = subscriber.with(otel_layer);
set_global_default(subscriber)?;
} else {
set_global_default(subscriber)?;
}
Ok(())
}