mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 15:51:23 +01:00
add observability infrastructure for cli subcmds
This commit is contained in:
parent
b93c39ee93
commit
b03c2a15b3
5 changed files with 96 additions and 10 deletions
34
src/cli.rs
34
src/cli.rs
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue