2022-01-15 23:24:37 +00:00
|
|
|
use crate::settings::*;
|
2022-05-16 15:52:48 +00:00
|
|
|
use cfg_if::*;
|
2022-06-15 19:03:13 +00:00
|
|
|
use opentelemetry::sdk::*;
|
|
|
|
use opentelemetry::*;
|
2022-06-11 22:47:58 +00:00
|
|
|
use opentelemetry_otlp::WithExportConfig;
|
2022-06-08 01:31:05 +00:00
|
|
|
use std::path::*;
|
|
|
|
use tracing::*;
|
|
|
|
use tracing_appender::*;
|
|
|
|
use tracing_subscriber::prelude::*;
|
|
|
|
use tracing_subscriber::*;
|
2022-01-15 23:24:37 +00:00
|
|
|
|
|
|
|
pub struct VeilidLogs {
|
2022-06-08 01:31:05 +00:00
|
|
|
pub guard: Option<non_blocking::WorkerGuard>,
|
2022-01-15 23:24:37 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 15:45:30 +00:00
|
|
|
fn logfilter<T: AsRef<str>, V: AsRef<[T]>>(metadata: &Metadata, ignore_list: V) -> bool {
|
2022-06-08 01:31:05 +00:00
|
|
|
// Skip filtered targets
|
|
|
|
!match (metadata.target(), ignore_list.as_ref()) {
|
|
|
|
(path, ignore) if !ignore.is_empty() => {
|
|
|
|
// Check that the module path does not match any ignore filters
|
|
|
|
ignore.iter().any(|v| path.starts_with(v.as_ref()))
|
2022-05-16 15:52:48 +00:00
|
|
|
}
|
2022-06-08 01:31:05 +00:00
|
|
|
_ => false,
|
2022-05-16 15:52:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 23:24:37 +00:00
|
|
|
impl VeilidLogs {
|
2022-06-08 01:31:05 +00:00
|
|
|
pub fn setup(settings: Settings) -> Result<VeilidLogs, String> {
|
2022-01-15 23:24:37 +00:00
|
|
|
let settingsr = settings.read();
|
|
|
|
|
2022-06-08 01:31:05 +00:00
|
|
|
// Set up subscriber and layers
|
|
|
|
let mut ignore_list = Vec::<String>::new();
|
2022-02-01 03:47:17 +00:00
|
|
|
for ig in veilid_core::DEFAULT_LOG_IGNORE_LIST {
|
2022-06-08 01:31:05 +00:00
|
|
|
ignore_list.push(ig.to_owned());
|
2022-02-01 03:47:17 +00:00
|
|
|
}
|
2022-01-15 23:24:37 +00:00
|
|
|
|
2022-06-08 01:31:05 +00:00
|
|
|
let subscriber = Registry::default();
|
|
|
|
|
2022-06-11 22:47:58 +00:00
|
|
|
// Terminal logger
|
2022-06-08 01:31:05 +00:00
|
|
|
let subscriber = subscriber.with(if settingsr.logging.terminal.enabled {
|
2022-06-08 15:45:30 +00:00
|
|
|
let terminal_max_log_level: level_filters::LevelFilter =
|
|
|
|
convert_loglevel(settingsr.logging.terminal.level)
|
|
|
|
.to_tracing_level()
|
|
|
|
.into();
|
2022-06-08 13:33:41 +00:00
|
|
|
let ignore_list = ignore_list.clone();
|
2022-06-08 01:31:05 +00:00
|
|
|
Some(
|
|
|
|
fmt::Layer::new()
|
|
|
|
.compact()
|
|
|
|
.with_writer(std::io::stdout)
|
2022-06-08 15:45:30 +00:00
|
|
|
.with_filter(terminal_max_log_level)
|
2022-06-08 01:31:05 +00:00
|
|
|
.with_filter(filter::FilterFn::new(move |metadata| {
|
2022-06-08 15:45:30 +00:00
|
|
|
logfilter(metadata, &ignore_list)
|
2022-06-08 01:31:05 +00:00
|
|
|
})),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
});
|
|
|
|
|
2022-06-11 22:47:58 +00:00
|
|
|
// OpenTelemetry logger
|
|
|
|
let subscriber = subscriber.with(if settingsr.logging.otlp.enabled {
|
|
|
|
let otlp_max_log_level: level_filters::LevelFilter =
|
|
|
|
convert_loglevel(settingsr.logging.otlp.level)
|
|
|
|
.to_tracing_level()
|
|
|
|
.into();
|
2022-06-15 19:03:13 +00:00
|
|
|
let grpc_endpoint = settingsr.logging.otlp.grpc_endpoint.name.clone();
|
2022-06-11 22:47:58 +00:00
|
|
|
|
2022-06-29 14:13:49 +00:00
|
|
|
cfg_if! {
|
|
|
|
if #[cfg(feature="rt-async-std")] {
|
|
|
|
let exporter = opentelemetry_otlp::new_exporter()
|
|
|
|
.grpcio()
|
|
|
|
.with_endpoint(grpc_endpoint);
|
|
|
|
let batch = opentelemetry::runtime::AsyncStd;
|
|
|
|
} else if #[cfg(feature="rt-tokio")] {
|
|
|
|
let exporter = opentelemetry_otlp::new_exporter()
|
|
|
|
.tonic()
|
|
|
|
.with_endpoint(format!("http://{}", grpc_endpoint));
|
|
|
|
let batch = opentelemetry::runtime::Tokio;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 22:47:58 +00:00
|
|
|
let tracer = opentelemetry_otlp::new_pipeline()
|
|
|
|
.tracing()
|
2022-06-29 14:13:49 +00:00
|
|
|
.with_exporter(exporter)
|
2022-06-15 19:03:13 +00:00
|
|
|
.with_trace_config(opentelemetry::sdk::trace::config().with_resource(
|
|
|
|
Resource::new(vec![KeyValue::new(
|
|
|
|
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
|
|
|
|
format!(
|
|
|
|
"veilid_server:{}",
|
|
|
|
hostname::get()
|
|
|
|
.map(|s| s.to_string_lossy().into_owned())
|
|
|
|
.unwrap_or_else(|_| "unknown".to_owned())
|
|
|
|
),
|
|
|
|
)]),
|
|
|
|
))
|
2022-06-29 14:13:49 +00:00
|
|
|
.install_batch(batch)
|
2022-06-11 22:47:58 +00:00
|
|
|
.map_err(|e| format!("failed to install OpenTelemetry tracer: {}", e))?;
|
|
|
|
|
|
|
|
let ignore_list = ignore_list.clone();
|
|
|
|
Some(
|
|
|
|
tracing_opentelemetry::layer()
|
|
|
|
.with_tracer(tracer)
|
|
|
|
.with_filter(otlp_max_log_level)
|
|
|
|
.with_filter(filter::FilterFn::new(move |metadata| {
|
|
|
|
logfilter(metadata, &ignore_list)
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
});
|
|
|
|
|
|
|
|
// File logger
|
2022-06-08 01:31:05 +00:00
|
|
|
let mut guard = None;
|
|
|
|
let subscriber = subscriber.with(if settingsr.logging.file.enabled {
|
2022-06-08 15:45:30 +00:00
|
|
|
let file_max_log_level: level_filters::LevelFilter =
|
|
|
|
convert_loglevel(settingsr.logging.file.level)
|
|
|
|
.to_tracing_level()
|
|
|
|
.into();
|
2022-01-15 23:24:37 +00:00
|
|
|
|
2022-06-08 01:31:05 +00:00
|
|
|
let log_path = Path::new(&settingsr.logging.file.path);
|
|
|
|
let full_path = std::env::current_dir()
|
|
|
|
.unwrap_or(PathBuf::from(MAIN_SEPARATOR.to_string()))
|
|
|
|
.join(log_path);
|
|
|
|
let log_parent = full_path
|
|
|
|
.parent()
|
|
|
|
.unwrap_or(Path::new(&MAIN_SEPARATOR.to_string()))
|
|
|
|
.canonicalize()
|
|
|
|
.map_err(|e| {
|
|
|
|
format!(
|
|
|
|
"File log path parent does not exist: {} ({})",
|
|
|
|
settingsr.logging.file.path, e
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
let log_filename = full_path.file_name().ok_or(format!(
|
|
|
|
"File log filename not specified in path: {}",
|
|
|
|
settingsr.logging.file.path
|
|
|
|
))?;
|
|
|
|
|
|
|
|
let appender = tracing_appender::rolling::never(log_parent, Path::new(log_filename));
|
|
|
|
let (non_blocking_appender, non_blocking_guard) =
|
|
|
|
tracing_appender::non_blocking(appender);
|
|
|
|
guard = Some(non_blocking_guard);
|
|
|
|
|
2022-06-08 13:33:41 +00:00
|
|
|
let ignore_list = ignore_list.clone();
|
2022-06-08 01:31:05 +00:00
|
|
|
Some(
|
|
|
|
fmt::Layer::new()
|
|
|
|
.compact()
|
|
|
|
.with_writer(non_blocking_appender)
|
2022-06-08 15:45:30 +00:00
|
|
|
.with_filter(file_max_log_level)
|
2022-06-08 13:33:41 +00:00
|
|
|
.with_filter(filter::FilterFn::new(move |metadata| {
|
2022-06-08 15:45:30 +00:00
|
|
|
logfilter(metadata, &ignore_list)
|
2022-06-08 01:31:05 +00:00
|
|
|
})),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
});
|
|
|
|
|
2022-06-11 22:47:58 +00:00
|
|
|
// API logger
|
2022-06-08 01:31:05 +00:00
|
|
|
let subscriber = subscriber.with(if settingsr.logging.api.enabled {
|
|
|
|
// Get layer from veilid core, filtering is done by ApiTracingLayer automatically
|
|
|
|
Some(veilid_core::ApiTracingLayer::get())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
});
|
2022-01-15 23:24:37 +00:00
|
|
|
|
2022-06-11 22:47:58 +00:00
|
|
|
// Systemd Journal logger
|
2022-05-16 15:52:48 +00:00
|
|
|
cfg_if! {
|
|
|
|
if #[cfg(target_os = "linux")] {
|
2022-06-08 01:31:05 +00:00
|
|
|
let subscriber = subscriber.with(if settingsr.logging.system.enabled {
|
2022-06-08 13:33:41 +00:00
|
|
|
let ignore_list = ignore_list.clone();
|
2022-06-08 15:45:30 +00:00
|
|
|
let system_max_log_level: level_filters::LevelFilter = convert_loglevel(settingsr.logging.system.level).to_tracing_level().into();
|
|
|
|
Some(tracing_journald::layer().map_err(|e| format!("failed to set up journald logging: {}", e))?
|
|
|
|
.with_filter(system_max_log_level)
|
|
|
|
.with_filter(filter::FilterFn::new(move |metadata| {
|
|
|
|
logfilter(metadata, &ignore_list)
|
|
|
|
}))
|
|
|
|
)
|
2022-06-08 01:31:05 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
});
|
2022-05-16 15:52:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:31:05 +00:00
|
|
|
subscriber
|
|
|
|
.try_init()
|
|
|
|
.map_err(|e| format!("failed to initialize logging: {}", e))?;
|
2022-01-15 23:24:37 +00:00
|
|
|
|
2022-06-08 01:31:05 +00:00
|
|
|
Ok(VeilidLogs { guard })
|
2022-01-15 23:24:37 +00:00
|
|
|
}
|
|
|
|
}
|