1
0
Fork 0

Add dev-only query logging support

This PR adds query logging support as an optional feature.
It is only allowed during development/debug builds, and will abort when
used during a `--release` build.

For this feature to be fully activated you also need to se an
environment variable `QUERY_LOGGER=1` to activate the debug log-level
for this crate, else there will be no output.

The reason for this PR is that sometimes it is useful to be able to see
the generated queries, like when debugging an issue, or trying to
optimize a query. Currently i always added this code when needed, but
having this a part of the code could benifit other developers too who
maybe need this.
Dieser Commit ist enthalten in:
BlackDex 2022-12-03 18:25:59 +01:00
Ursprung 8feed2916f
Commit 1b64b9e164
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 58C80A2AA6C765E1
5 geänderte Dateien mit 46 neuen und 8 gelöschten Zeilen

11
Cargo.lock generiert
Datei anzeigen

@ -650,6 +650,16 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "diesel_logger"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a22b1f4804a69ed8954910b2ab30dedc759665e0284e57db95eef4a7b5edffb"
dependencies = [
"diesel",
"log",
]
[[package]] [[package]]
name = "diesel_migrations" name = "diesel_migrations"
version = "2.0.0" version = "2.0.0"
@ -3180,6 +3190,7 @@ dependencies = [
"data-encoding", "data-encoding",
"data-url", "data-url",
"diesel", "diesel",
"diesel_logger",
"diesel_migrations", "diesel_migrations",
"dotenvy", "dotenvy",
"email_address", "email_address",

Datei anzeigen

@ -24,6 +24,11 @@ vendored_openssl = ["openssl/vendored"]
# Enable MiMalloc memory allocator to replace the default malloc # Enable MiMalloc memory allocator to replace the default malloc
# This can improve performance for Alpine builds # This can improve performance for Alpine builds
enable_mimalloc = ["mimalloc"] enable_mimalloc = ["mimalloc"]
# This is a development dependency, and should only be used during development!
# It enables the usage of the diesel_logger crate, which is able to output the generated queries.
# You also need to set an env variable `QUERY_LOGGER=1` to fully activate this so you do not have to re-compile
# if you want to turn off the logging for a specific run.
query_logger = ["diesel_logger"]
# Enable unstable features, requires nightly # Enable unstable features, requires nightly
# Currently only used to enable rusts official ip support # Currently only used to enable rusts official ip support
@ -70,6 +75,7 @@ serde_json = "1.0.87"
# A safe, extensible ORM and Query builder # A safe, extensible ORM and Query builder
diesel = { version = "2.0.2", features = ["chrono", "r2d2"] } diesel = { version = "2.0.2", features = ["chrono", "r2d2"] }
diesel_migrations = "2.0.0" diesel_migrations = "2.0.0"
diesel_logger = { version = "0.2.0", optional = true }
# Bundled SQLite # Bundled SQLite
libsqlite3-sys = { version = "0.25.2", features = ["bundled"], optional = true } libsqlite3-sys = { version = "0.25.2", features = ["bundled"], optional = true }

Datei anzeigen

@ -9,20 +9,25 @@ fn main() {
println!("cargo:rustc-cfg=mysql"); println!("cargo:rustc-cfg=mysql");
#[cfg(feature = "postgresql")] #[cfg(feature = "postgresql")]
println!("cargo:rustc-cfg=postgresql"); println!("cargo:rustc-cfg=postgresql");
#[cfg(feature = "query_logger")]
println!("cargo:rustc-cfg=query_logger");
#[cfg(not(any(feature = "sqlite", feature = "mysql", feature = "postgresql")))] #[cfg(not(any(feature = "sqlite", feature = "mysql", feature = "postgresql")))]
compile_error!( compile_error!(
"You need to enable one DB backend. To build with previous defaults do: cargo build --features sqlite" "You need to enable one DB backend. To build with previous defaults do: cargo build --features sqlite"
); );
#[cfg(all(not(debug_assertions), feature = "query_logger"))]
compile_error!("Query Logging is only allowed during development, it is not intented for production usage!");
// Support $BWRS_VERSION for legacy compatibility, but default to $VW_VERSION. // Support $BWRS_VERSION for legacy compatibility, but default to $VW_VERSION.
// If neither exist, read from git. // If neither exist, read from git.
let maybe_vaultwarden_version = let maybe_vaultwarden_version =
env::var("VW_VERSION").or_else(|_| env::var("BWRS_VERSION")).or_else(|_| version_from_git_info()); env::var("VW_VERSION").or_else(|_| env::var("BWRS_VERSION")).or_else(|_| version_from_git_info());
if let Ok(version) = maybe_vaultwarden_version { if let Ok(version) = maybe_vaultwarden_version {
println!("cargo:rustc-env=VW_VERSION={}", version); println!("cargo:rustc-env=VW_VERSION={version}");
println!("cargo:rustc-env=CARGO_PKG_VERSION={}", version); println!("cargo:rustc-env=CARGO_PKG_VERSION={version}");
} }
} }
@ -47,29 +52,29 @@ fn version_from_git_info() -> Result<String, std::io::Error> {
// the current commit doesn't have an associated tag // the current commit doesn't have an associated tag
let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok(); let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
if let Some(ref exact) = exact_tag { if let Some(ref exact) = exact_tag {
println!("cargo:rustc-env=GIT_EXACT_TAG={}", exact); println!("cargo:rustc-env=GIT_EXACT_TAG={exact}");
} }
// The last available tag, equal to exact_tag when // The last available tag, equal to exact_tag when
// the current commit is tagged // the current commit is tagged
let last_tag = run(&["git", "describe", "--abbrev=0", "--tags"])?; let last_tag = run(&["git", "describe", "--abbrev=0", "--tags"])?;
println!("cargo:rustc-env=GIT_LAST_TAG={}", last_tag); println!("cargo:rustc-env=GIT_LAST_TAG={last_tag}");
// The current branch name // The current branch name
let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?; let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?;
println!("cargo:rustc-env=GIT_BRANCH={}", branch); println!("cargo:rustc-env=GIT_BRANCH={branch}");
// The current git commit hash // The current git commit hash
let rev = run(&["git", "rev-parse", "HEAD"])?; let rev = run(&["git", "rev-parse", "HEAD"])?;
let rev_short = rev.get(..8).unwrap_or_default(); let rev_short = rev.get(..8).unwrap_or_default();
println!("cargo:rustc-env=GIT_REV={}", rev_short); println!("cargo:rustc-env=GIT_REV={rev_short}");
// Combined version // Combined version
if let Some(exact) = exact_tag { if let Some(exact) = exact_tag {
Ok(exact) Ok(exact)
} else if &branch != "main" && &branch != "master" { } else if &branch != "main" && &branch != "master" {
Ok(format!("{}-{} ({})", last_tag, rev_short, branch)) Ok(format!("{last_tag}-{rev_short} ({branch})"))
} else { } else {
Ok(format!("{}-{}", last_tag, rev_short)) Ok(format!("{last_tag}-{rev_short}"))
} }
} }

Datei anzeigen

@ -182,12 +182,20 @@ macro_rules! generate_connections {
}; };
} }
#[cfg(not(query_logger))]
generate_connections! { generate_connections! {
sqlite: diesel::sqlite::SqliteConnection, sqlite: diesel::sqlite::SqliteConnection,
mysql: diesel::mysql::MysqlConnection, mysql: diesel::mysql::MysqlConnection,
postgresql: diesel::pg::PgConnection postgresql: diesel::pg::PgConnection
} }
#[cfg(query_logger)]
generate_connections! {
sqlite: diesel_logger::LoggingConnection<diesel::sqlite::SqliteConnection>,
mysql: diesel_logger::LoggingConnection<diesel::mysql::MysqlConnection>,
postgresql: diesel_logger::LoggingConnection<diesel::pg::PgConnection>
}
impl DbConnType { impl DbConnType {
pub fn from_url(url: &str) -> Result<DbConnType, Error> { pub fn from_url(url: &str) -> Result<DbConnType, Error> {
// Mysql // Mysql

Datei anzeigen

@ -171,6 +171,13 @@ fn init_logging(level: log::LevelFilter) -> Result<(), fern::InitError> {
log::LevelFilter::Off log::LevelFilter::Off
}; };
let diesel_logger_level: log::LevelFilter =
if cfg!(feature = "query_logger") && std::env::var("QUERY_LOGGER").is_ok() {
log::LevelFilter::Debug
} else {
log::LevelFilter::Off
};
let mut logger = fern::Dispatch::new() let mut logger = fern::Dispatch::new()
.level(level) .level(level)
// Hide unknown certificate errors if using self-signed // Hide unknown certificate errors if using self-signed
@ -191,6 +198,7 @@ fn init_logging(level: log::LevelFilter) -> Result<(), fern::InitError> {
.level_for("cookie_store", log::LevelFilter::Off) .level_for("cookie_store", log::LevelFilter::Off)
// Variable level for trust-dns used by reqwest // Variable level for trust-dns used by reqwest
.level_for("trust_dns_proto", trust_dns_level) .level_for("trust_dns_proto", trust_dns_level)
.level_for("diesel_logger", diesel_logger_level)
.chain(std::io::stdout()); .chain(std::io::stdout());
// Enable smtp debug logging only specifically for smtp when need. // Enable smtp debug logging only specifically for smtp when need.