veilid/veilid-server/src/veilid_logs.rs

209 lines
7.6 KiB
Rust
Raw Normal View History

2022-01-15 23:24:37 +00:00
use crate::settings::*;
2022-07-07 03:15:51 +00:00
use crate::*;
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-07-01 16:13:52 +00:00
use parking_lot::*;
use std::collections::BTreeMap;
2022-06-08 01:31:05 +00:00
use std::path::*;
2022-07-01 16:13:52 +00:00
use std::sync::Arc;
2022-06-08 01:31:05 +00:00
use tracing_appender::*;
use tracing_subscriber::prelude::*;
use tracing_subscriber::*;
2022-01-15 23:24:37 +00:00
2022-07-01 16:13:52 +00:00
struct VeilidLogsInner {
_guard: Option<non_blocking::WorkerGuard>,
filters: BTreeMap<&'static str, veilid_core::VeilidLayerFilter>,
2022-01-15 23:24:37 +00:00
}
2022-07-01 16:13:52 +00:00
#[derive(Clone)]
pub struct VeilidLogs {
inner: Arc<Mutex<VeilidLogsInner>>,
2022-05-16 15:52:48 +00:00
}
2022-01-15 23:24:37 +00:00
impl VeilidLogs {
2022-07-07 03:15:51 +00:00
pub fn setup(settings: Settings) -> EyreResult<VeilidLogs> {
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 subscriber = Registry::default();
2022-07-01 16:13:52 +00:00
let mut layers = Vec::new();
let mut filters = BTreeMap::new();
2022-06-08 01:31:05 +00:00
2022-07-14 20:57:34 +00:00
// Error layer
// XXX: Spantrace capture causes rwlock deadlocks/crashes
// XXX:
//layers.push(tracing_error::ErrorLayer::default().boxed());
2022-06-11 22:47:58 +00:00
// Terminal logger
2022-07-01 16:13:52 +00:00
if settingsr.logging.terminal.enabled {
let filter = veilid_core::VeilidLayerFilter::new(
convert_loglevel(settingsr.logging.terminal.level),
None,
);
let layer = fmt::Layer::new()
.compact()
.with_writer(std::io::stdout)
.with_filter(filter.clone());
filters.insert("terminal", filter);
layers.push(layer.boxed());
}
2022-06-08 01:31:05 +00:00
2022-06-11 22:47:58 +00:00
// OpenTelemetry logger
2022-07-01 16:13:52 +00:00
if settingsr.logging.otlp.enabled {
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-07-07 03:15:51 +00:00
.wrap_err("failed to install OpenTelemetry tracer")?;
2022-06-11 22:47:58 +00:00
2022-07-01 16:13:52 +00:00
let filter = veilid_core::VeilidLayerFilter::new(
convert_loglevel(settingsr.logging.otlp.level),
None,
);
let layer = tracing_opentelemetry::layer()
.with_tracer(tracer)
.with_filter(filter.clone());
filters.insert("otlp", filter);
layers.push(layer.boxed());
}
2022-06-11 22:47:58 +00:00
// File logger
2022-06-08 01:31:05 +00:00
let mut guard = None;
2022-07-01 16:13:52 +00:00
if settingsr.logging.file.enabled {
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()
2022-07-07 03:15:51 +00:00
.wrap_err(format!(
"File log path parent does not exist: {}",
settingsr.logging.file.path
))?;
let log_filename = full_path.file_name().ok_or(eyre!(
2022-06-08 01:31:05 +00:00
"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-07-01 16:13:52 +00:00
let filter = veilid_core::VeilidLayerFilter::new(
convert_loglevel(settingsr.logging.file.level),
None,
);
let layer = fmt::Layer::new()
.compact()
.with_writer(non_blocking_appender)
.with_filter(filter.clone());
filters.insert("file", filter);
layers.push(layer.boxed());
}
2022-06-08 01:31:05 +00:00
2022-06-11 22:47:58 +00:00
// API logger
2022-07-01 16:13:52 +00:00
if settingsr.logging.api.enabled {
let filter = veilid_core::VeilidLayerFilter::new(
convert_loglevel(settingsr.logging.api.level),
None,
);
let layer = veilid_core::ApiTracingLayer::get().with_filter(filter.clone());
filters.insert("api", filter);
layers.push(layer.boxed());
}
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-07-01 16:13:52 +00:00
if settingsr.logging.system.enabled {
let filter = veilid_core::VeilidLayerFilter::new(
convert_loglevel(settingsr.logging.system.level),
None,
);
2022-07-07 03:15:51 +00:00
let layer =tracing_journald::layer().wrap_err("failed to set up journald logging")?
2022-07-01 16:13:52 +00:00
.with_filter(filter.clone());
filters.insert("system", filter);
layers.push(layer.boxed());
}
2022-05-16 15:52:48 +00:00
}
}
2022-07-01 16:13:52 +00:00
let subscriber = subscriber.with(layers);
2022-06-08 01:31:05 +00:00
subscriber
.try_init()
2022-07-07 03:15:51 +00:00
.wrap_err("failed to initialize logging")?;
2022-01-15 23:24:37 +00:00
2022-07-01 16:13:52 +00:00
Ok(VeilidLogs {
inner: Arc::new(Mutex::new(VeilidLogsInner {
_guard: guard,
filters,
})),
})
}
pub fn change_log_level(
&self,
layer: String,
log_level: veilid_core::VeilidConfigLogLevel,
) -> Result<(), veilid_core::VeilidAPIError> {
// get layer to change level on
let layer = if layer == "all" { "".to_owned() } else { layer };
// change log level on appropriate layer
let inner = self.inner.lock();
if layer.is_empty() {
// Change all layers
for f in inner.filters.values() {
f.set_max_level(log_level);
}
} else {
// Change a specific layer
let f = match inner.filters.get(layer.as_str()) {
Some(f) => f,
None => {
return Err(veilid_core::VeilidAPIError::InvalidArgument {
context: "change_log_level".to_owned(),
argument: "layer".to_owned(),
value: layer,
});
}
};
f.set_max_level(log_level);
}
Ok(())
2022-01-15 23:24:37 +00:00
}
}