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

@ -7,7 +7,10 @@ use std::path::PathBuf;
use clap::{Parser, Subcommand};
use crate::error;
use crate::{
config::{default_tracing_filter, EnvFilterClone, LogFormat},
error, observability,
};
mod serve;
@ -51,6 +54,21 @@ pub(crate) struct ConfigArg {
pub(crate) config: Option<PathBuf>,
}
/// Observability arguments for CLI subcommands
#[derive(clap::Args)]
struct ObservabilityArgs {
/// Log format
#[clap(long, default_value_t = LogFormat::Full)]
log_format: LogFormat,
/// Log filter
///
/// For information about the syntax, see here:
/// <https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives>
#[clap(long, default_value_t = default_tracing_filter())]
log_filter: EnvFilterClone,
}
#[derive(clap::Args)]
pub(crate) struct ServeArgs {
#[clap(flatten)]
@ -59,9 +77,23 @@ pub(crate) struct ServeArgs {
impl Args {
pub(crate) async fn run(self) -> Result<(), error::Main> {
if let Some((format, filter)) = self.command.cli_observability_args() {
observability::init_for_cli(format, filter.into())?;
}
match self.command {
Command::Serve(args) => serve::run(args).await?,
}
Ok(())
}
}
impl Command {
fn cli_observability_args(&self) -> Option<(LogFormat, EnvFilterClone)> {
// All subcommands other than `serve` should return `Some`. Keep these
// match arms sorted by the enum variant name.
match self {
Command::Serve(_) => None,
}
}
}