2022-03-28 04:27:07 +00:00
|
|
|
use crate::config::{LogFormat, OpenTelemetry, Tracing};
|
2021-12-19 18:59:51 +00:00
|
|
|
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;
|
2023-09-05 02:51:27 +00:00
|
|
|
use tracing_subscriber::{layer::SubscriberExt, registry::LookupSpan, Layer, Registry};
|
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());
|
|
|
|
|
2023-09-05 02:51:27 +00:00
|
|
|
let format_layer = tracing_subscriber::fmt::layer();
|
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 {
|
2023-01-29 17:57:59 +00:00
|
|
|
println!("Starting console on {address}");
|
2022-04-11 20:17:54 +00:00
|
|
|
|
2022-03-22 03:41:51 +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)
|
2022-03-22 03:41:51 +00:00
|
|
|
.spawn();
|
|
|
|
|
|
|
|
let subscriber = subscriber.with(console_layer);
|
|
|
|
|
2022-03-28 04:27:07 +00:00
|
|
|
with_subscriber(subscriber, &tracing.opentelemetry)
|
2022-03-22 03:41:51 +00:00
|
|
|
} else {
|
2022-03-28 04:27:07 +00:00
|
|
|
with_subscriber(subscriber, &tracing.opentelemetry)
|
2022-03-22 03:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
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<()>
|
2022-03-22 03:41:51 +00:00
|
|
|
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(())
|
|
|
|
}
|