Skip to content

Commit

Permalink
syn2mas: disable logging of slow statements
Browse files Browse the repository at this point in the history
  • Loading branch information
reivilibre committed Feb 1, 2025
1 parent df67ce5 commit acf4cd0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
20 changes: 17 additions & 3 deletions crates/cli/src/commands/syn2mas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sqlx::{postgres::PgConnectOptions, types::Uuid, Connection, Either, PgConnec
use syn2mas::{synapse_config, LockedMasDatabase, MasWriter, SynapseReader};
use tracing::{error, info_span, warn, Instrument};

use crate::util::database_connection_from_config;
use crate::util::{database_connection_from_config_with_options, DatabaseConnectOptions};

/// The exit code used by `syn2mas check` and `syn2mas migrate` when there are
/// errors preventing migration.
Expand Down Expand Up @@ -111,7 +111,13 @@ impl Options {

let config = DatabaseConfig::extract_or_default(figment)?;

let mut mas_connection = database_connection_from_config(&config).await?;
let mut mas_connection = database_connection_from_config_with_options(
&config,
&DatabaseConnectOptions {
log_slow_statements: false,
},
)
.await?;

MIGRATOR
.run(&mut mas_connection)
Expand Down Expand Up @@ -220,7 +226,15 @@ impl Options {
let reader = SynapseReader::new(&mut syn_conn, true).await?;
let mut writer_mas_connections = Vec::with_capacity(NUM_WRITER_CONNECTIONS);
for _ in 0..NUM_WRITER_CONNECTIONS {
writer_mas_connections.push(database_connection_from_config(&config).await?);
writer_mas_connections.push(
database_connection_from_config_with_options(
&config,
&DatabaseConnectOptions {
log_slow_statements: false,
},
)
.await?,
);
}
let writer = MasWriter::new(mas_connection, writer_mas_connections).await?;

Expand Down
38 changes: 33 additions & 5 deletions crates/cli/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ pub async fn templates_from_config(

fn database_connect_options_from_config(
config: &DatabaseConfig,
opts: &DatabaseConnectOptions,
) -> Result<PgConnectOptions, anyhow::Error> {
let options = if let Some(uri) = config.uri.as_deref() {
uri.parse()
Expand Down Expand Up @@ -301,17 +302,19 @@ fn database_connect_options_from_config(
None => options,
};

let options = options
.log_statements(LevelFilter::Debug)
.log_slow_statements(LevelFilter::Warn, Duration::from_millis(100));
let mut options = options.log_statements(LevelFilter::Debug);

if opts.log_slow_statements {
options = options.log_slow_statements(LevelFilter::Warn, Duration::from_millis(100));
}

Ok(options)
}

/// Create a database connection pool from the configuration
#[tracing::instrument(name = "db.connect", skip_all, err(Debug))]
pub async fn database_pool_from_config(config: &DatabaseConfig) -> Result<PgPool, anyhow::Error> {
let options = database_connect_options_from_config(config)?;
let options = database_connect_options_from_config(config, &Default::default())?;
PgPoolOptions::new()
.max_connections(config.max_connections.into())
.min_connections(config.min_connections)
Expand All @@ -323,12 +326,37 @@ pub async fn database_pool_from_config(config: &DatabaseConfig) -> Result<PgPool
.context("could not connect to the database")
}

pub struct DatabaseConnectOptions {
pub log_slow_statements: bool,
}

impl Default for DatabaseConnectOptions {
fn default() -> Self {
Self {
log_slow_statements: true,
}
}
}

/// Create a single database connection from the configuration
#[tracing::instrument(name = "db.connect", skip_all, err(Debug))]
pub async fn database_connection_from_config(
config: &DatabaseConfig,
) -> Result<PgConnection, anyhow::Error> {
database_connect_options_from_config(config)?
database_connect_options_from_config(config, &Default::default())?
.connect()
.await
.context("could not connect to the database")
}

/// Create a single database connection from the configuration,
/// with specific options.
#[tracing::instrument(name = "db.connect", skip_all, err(Debug))]
pub async fn database_connection_from_config_with_options(
config: &DatabaseConfig,
options: &DatabaseConnectOptions,
) -> Result<PgConnection, anyhow::Error> {
database_connect_options_from_config(config, options)?
.connect()
.await
.context("could not connect to the database")
Expand Down

0 comments on commit acf4cd0

Please sign in to comment.