add observability infrastructure for cli subcmds

This commit is contained in:
Charles Hall 2024-10-10 14:30:13 -07:00
parent b93c39ee93
commit b03c2a15b3
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
5 changed files with 96 additions and 10 deletions

View file

@ -18,7 +18,7 @@ use opentelemetry_sdk::{
};
use strum::{AsRefStr, IntoStaticStr};
use tokio::time::Instant;
use tracing::Span;
use tracing::{subscriber::SetGlobalDefaultError, Span};
use tracing_flame::{FlameLayer, FlushGuard};
use tracing_opentelemetry::OtelData;
use tracing_subscriber::{
@ -469,3 +469,27 @@ pub(crate) async fn traceresponse_layer(req: Request, next: Next) -> Response {
resp
}
/// Set up observability for CLI-oriented subcommands.
///
/// Tracing spans and events will be sent to `stderr`.
pub(crate) fn init_for_cli(
log_format: LogFormat,
env_filter: EnvFilter,
) -> Result<(), SetGlobalDefaultError> {
let log_layer =
tracing_subscriber::fmt::Layer::new().with_writer(std::io::stderr);
let log_layer = match log_format {
LogFormat::Pretty => log_layer.pretty().boxed(),
LogFormat::Full => log_layer.boxed(),
LogFormat::Compact => log_layer.compact().boxed(),
LogFormat::Json => log_layer.json().boxed(),
};
let log_layer = log_layer.with_filter(env_filter);
let subscriber = Registry::default().with(log_layer);
tracing::subscriber::set_global_default(subscriber).map_err(Into::into)
}