1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

Better tracing spans

This commit is contained in:
Quentin Gliech
2023-01-04 16:02:42 +01:00
parent 09a567ab17
commit a7883618be
12 changed files with 101 additions and 24 deletions

View File

@ -15,7 +15,7 @@
use clap::Parser;
use mas_config::{ConfigurationSection, RootConfig};
use rand::SeedableRng;
use tracing::info;
use tracing::{info, info_span};
#[derive(Parser, Debug)]
pub(super) struct Options {
@ -40,6 +40,8 @@ impl Options {
use Subcommand as SC;
match &self.subcommand {
SC::Dump => {
let _span = info_span!("cli.config.dump").entered();
let config: RootConfig = root.load_config()?;
serde_yaml::to_writer(std::io::stdout(), &config)?;
@ -47,11 +49,15 @@ impl Options {
Ok(())
}
SC::Check => {
let _span = info_span!("cli.config.check").entered();
let _config: RootConfig = root.load_config()?;
info!(path = ?root.config, "Configuration file looks good");
Ok(())
}
SC::Generate => {
let _span = info_span!("cli.config.generate").entered();
// XXX: we should disallow SeedableRng::from_entropy
let rng = rand_chacha::ChaChaRng::from_entropy();
let config = RootConfig::load_and_generate(rng).await?;

View File

@ -16,6 +16,7 @@ use anyhow::Context;
use clap::Parser;
use mas_config::DatabaseConfig;
use mas_storage::MIGRATOR;
use tracing::{info_span, Instrument};
use crate::util::database_from_config;
@ -33,12 +34,14 @@ enum Subcommand {
impl Options {
pub async fn run(&self, root: &super::Options) -> anyhow::Result<()> {
let _span = info_span!("cli.database.migrate").entered();
let config: DatabaseConfig = root.load_config()?;
let pool = database_from_config(&config).await?;
// Run pending migrations
MIGRATOR
.run(&pool)
.instrument(info_span!("db.migrate"))
.await
.context("could not run migrations")?;

View File

@ -19,7 +19,7 @@ use mas_handlers::HttpClientFactory;
use mas_http::HttpServiceExt;
use tokio::io::AsyncWriteExt;
use tower::{Service, ServiceExt};
use tracing::info;
use tracing::{info, info_span};
use crate::util::policy_factory_from_config;
@ -74,6 +74,7 @@ impl Options {
json: false,
url,
} => {
let _span = info_span!("cli.debug.http").entered();
let mut client = http_client_factory.client("cli-debug-http").await?;
let request = hyper::Request::builder()
.uri(url)
@ -98,6 +99,7 @@ impl Options {
json: true,
url,
} => {
let _span = info_span!("cli.debug.http").entered();
let mut client = http_client_factory
.client("cli-debug-http")
.await?
@ -122,6 +124,7 @@ impl Options {
}
SC::Policy => {
let _span = info_span!("cli.debug.policy").entered();
let config: PolicyConfig = root.load_config()?;
info!("Loading and compiling the policy module");
let policy_factory = policy_factory_from_config(&config).await?;

View File

@ -25,7 +25,7 @@ use mas_storage::{
};
use oauth2_types::scope::Scope;
use rand::SeedableRng;
use tracing::{info, warn};
use tracing::{info, info_span, warn};
use crate::util::{database_from_config, password_manager_from_config};
@ -193,6 +193,9 @@ impl Options {
match &self.subcommand {
SC::SetPassword { username, password } => {
let _span =
info_span!("cli.manage.set_password", user.username = %username).entered();
let database_config: DatabaseConfig = root.load_config()?;
let passwords_config: PasswordsConfig = root.load_config()?;
@ -221,6 +224,13 @@ impl Options {
}
SC::VerifyEmail { username, email } => {
let _span = info_span!(
"cli.manage.verify_email",
user.username = username,
user_email.email = email
)
.entered();
let config: DatabaseConfig = root.load_config()?;
let pool = database_from_config(&config).await?;
let mut txn = pool.begin().await?;
@ -245,6 +255,8 @@ impl Options {
}
SC::ImportClients { update } => {
let _span = info_span!("cli.manage.import_clients").entered();
let config: RootConfig = root.load_config()?;
let pool = database_from_config(&config.database).await?;
let encrypter = config.secrets.encrypter();
@ -303,6 +315,13 @@ impl Options {
client_secret,
signing_alg,
} => {
let _span = info_span!(
"cli.manage.add_oauth_upstream",
upstream_oauth_provider.issuer = issuer,
upstream_oauth_provider.client_id = client_id,
)
.entered();
let config: RootConfig = root.load_config()?;
let encrypter = config.secrets.encrypter();
let pool = database_from_config(&config.database).await?;

View File

@ -24,7 +24,7 @@ use mas_router::UrlBuilder;
use mas_storage::MIGRATOR;
use mas_tasks::TaskQueue;
use tokio::signal::unix::SignalKind;
use tracing::{info, warn};
use tracing::{info, info_span, warn, Instrument};
use crate::util::{
database_from_config, mailer_from_config, password_manager_from_config,
@ -45,6 +45,7 @@ pub(super) struct Options {
impl Options {
#[allow(clippy::too_many_lines)]
pub async fn run(&self, root: &super::Options) -> anyhow::Result<()> {
let span = info_span!("cli.run.init").entered();
let config: RootConfig = root.load_config()?;
// Connect to the database
@ -55,6 +56,7 @@ impl Options {
info!("Running pending migrations");
MIGRATOR
.run(&pool)
.instrument(info_span!("db.migrate"))
.await
.context("could not run migrations")?;
}
@ -186,6 +188,8 @@ impl Options {
.with_signal(SignalKind::terminate())?
.with_signal(SignalKind::interrupt())?;
span.exit();
mas_listener::server::run_servers(servers, shutdown).await;
Ok(())

View File

@ -17,6 +17,7 @@ use clap::Parser;
use mas_storage::Clock;
use mas_templates::Templates;
use rand::SeedableRng;
use tracing::info_span;
#[derive(Parser, Debug)]
pub(super) struct Options {
@ -38,6 +39,8 @@ impl Options {
use Subcommand as SC;
match &self.subcommand {
SC::Check { path } => {
let _span = info_span!("cli.templates.check").entered();
let clock = Clock::default();
// XXX: we should disallow SeedableRng::from_entropy
let mut rng = rand_chacha::ChaChaRng::from_entropy();

View File

@ -110,6 +110,7 @@ pub async fn templates_from_config(
Templates::load(config.path.clone(), url_builder.clone()).await
}
#[tracing::instrument(name = "db.connect", skip_all, err(Debug))]
pub async fn database_from_config(config: &DatabaseConfig) -> Result<PgPool, anyhow::Error> {
let mut options = match &config.options {
DatabaseConnectConfig::Uri { uri } => uri