Add diesel tracing

This commit is contained in:
Rohan Sircar 2021-05-11 11:42:37 +05:30
parent 88430a3e51
commit 4dd9be5749
6 changed files with 39 additions and 11 deletions

23
Cargo.lock generated
View File

@ -55,6 +55,7 @@ dependencies = [
"custom_error", "custom_error",
"derive-new", "derive-new",
"diesel", "diesel",
"diesel-tracing",
"diesel_migrations", "diesel_migrations",
"dotenv", "dotenv",
"envy", "envy",
@ -889,10 +890,23 @@ dependencies = [
"byteorder", "byteorder",
"chrono", "chrono",
"diesel_derives", "diesel_derives",
"ipnetwork",
"libc",
"libsqlite3-sys", "libsqlite3-sys",
"r2d2", "r2d2",
] ]
[[package]]
name = "diesel-tracing"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528c21047ca2f10fa8978ee4f17acbeeae64acaf8912f5541a4741feb9db0ee4"
dependencies = [
"diesel",
"ipnetwork",
"tracing",
]
[[package]] [[package]]
name = "diesel_derives" name = "diesel_derives"
version = "1.4.1" version = "1.4.1"
@ -1366,6 +1380,15 @@ dependencies = [
"winreg", "winreg",
] ]
[[package]]
name = "ipnetwork"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02c3eaab3ac0ede60ffa41add21970a7df7d91772c03383aac6c2c3d53cc716b"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "isolang" name = "isolang"
version = "1.0.0" version = "1.0.0"

View File

@ -40,6 +40,7 @@ tracing-subscriber = { version = "0.2.18", features = ["fmt", "registry", "env-f
tracing-futures = "0.2.5" tracing-futures = "0.2.5"
tracing-actix-web = "0.2.1" tracing-actix-web = "0.2.1"
tracing-bunyan-formatter = "0.2.4" tracing-bunyan-formatter = "0.2.4"
diesel-tracing = { version = "0.1.4", features = ["sqlite"] }
[dependencies.build-info] [dependencies.build-info]
version = "=0.0.23" version = "=0.0.23"

View File

@ -6,7 +6,7 @@ use bcrypt::{hash, verify, DEFAULT_COST};
pub fn find_user_by_uid( pub fn find_user_by_uid(
uid: i32, uid: i32,
conn: &SqliteConnection, conn: &impl diesel::Connection<Backend = diesel::sqlite::Sqlite>,
) -> Result<Option<models::UserDto>, errors::DomainError> { ) -> Result<Option<models::UserDto>, errors::DomainError> {
use crate::schema::users::dsl::*; use crate::schema::users::dsl::*;
@ -21,7 +21,7 @@ pub fn find_user_by_uid(
pub fn _find_user_by_name( pub fn _find_user_by_name(
user_name: String, user_name: String,
conn: &SqliteConnection, conn: &impl diesel::Connection<Backend = diesel::sqlite::Sqlite>,
) -> Result<Option<models::UserDto>, errors::DomainError> { ) -> Result<Option<models::UserDto>, errors::DomainError> {
let maybe_user = query::_get_user_by_name(&user_name) let maybe_user = query::_get_user_by_name(&user_name)
.first::<models::UserDto>(conn) .first::<models::UserDto>(conn)
@ -31,7 +31,7 @@ pub fn _find_user_by_name(
} }
pub fn get_all( pub fn get_all(
conn: &SqliteConnection, conn: &impl diesel::Connection<Backend = diesel::sqlite::Sqlite>,
) -> Result<Vec<models::UserDto>, errors::DomainError> { ) -> Result<Vec<models::UserDto>, errors::DomainError> {
use crate::schema::users::dsl::*; use crate::schema::users::dsl::*;
Ok(users Ok(users
@ -42,7 +42,7 @@ pub fn get_all(
/// Run query using Diesel to insert a new database row and return the result. /// Run query using Diesel to insert a new database row and return the result.
pub fn insert_new_user( pub fn insert_new_user(
nu: models::NewUser, nu: models::NewUser,
conn: &SqliteConnection, conn: &impl diesel::Connection<Backend = diesel::sqlite::Sqlite>,
) -> Result<models::UserDto, errors::DomainError> { ) -> Result<models::UserDto, errors::DomainError> {
// It is common when using Diesel with Actix web to import schema-related // It is common when using Diesel with Actix web to import schema-related
// modules inside a function's scope (rather than the normal module's scope) // modules inside a function's scope (rather than the normal module's scope)
@ -63,7 +63,7 @@ pub fn insert_new_user(
pub fn verify_password( pub fn verify_password(
user_name: &str, user_name: &str,
given_password: &str, given_password: &str,
conn: &SqliteConnection, conn: &impl diesel::Connection<Backend = diesel::sqlite::Sqlite>,
) -> Result<bool, errors::DomainError> { ) -> Result<bool, errors::DomainError> {
use crate::schema::users::dsl::*; use crate::schema::users::dsl::*;
let password_hash = users let password_hash = users

View File

@ -1,6 +1,7 @@
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
use actix_demo::{AppConfig, AppData, EnvConfig, LoggerFormat}; use actix_demo::{AppConfig, AppData, EnvConfig, LoggerFormat};
use diesel::{r2d2::ConnectionManager, SqliteConnection}; use diesel::r2d2::ConnectionManager;
use diesel_tracing::sqlite::InstrumentedSqliteConnection;
use io::ErrorKind; use io::ErrorKind;
use std::io; use std::io;
use tracing::subscriber::set_global_default; use tracing::subscriber::set_global_default;
@ -32,7 +33,8 @@ async fn main() -> io::Result<()> {
let _ = setup_logger(env_config.logger_format)?; let _ = setup_logger(env_config.logger_format)?;
let connspec = &env_config.database_url; let connspec = &env_config.database_url;
let manager = ConnectionManager::<SqliteConnection>::new(connspec); let manager =
ConnectionManager::<InstrumentedSqliteConnection>::new(connspec);
let pool = r2d2::Pool::builder().build(manager).map_err(|err| { let pool = r2d2::Pool::builder().build(manager).map_err(|err| {
io::Error::new( io::Error::new(
ErrorKind::Other, ErrorKind::Other,

View File

@ -1,3 +1,4 @@
use diesel::prelude::*;
use diesel::r2d2::{self, ConnectionManager}; use diesel::r2d2::{self, ConnectionManager};
pub type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>; pub type DbPool = r2d2::Pool<
ConnectionManager<diesel_tracing::sqlite::InstrumentedSqliteConnection>,
>;

View File

@ -2,7 +2,6 @@ extern crate actix_demo;
use actix_demo::{AppConfig, AppData, EnvConfig}; use actix_demo::{AppConfig, AppData, EnvConfig};
use actix_web::test; use actix_web::test;
use actix_web::App; use actix_web::App;
use diesel::SqliteConnection;
use diesel::r2d2::{self, ConnectionManager}; use diesel::r2d2::{self, ConnectionManager};
use std::io; use std::io;
@ -72,7 +71,9 @@ pub async fn test_app() -> io::Result<
}); });
let connspec = ":memory:"; let connspec = ":memory:";
let manager = ConnectionManager::<SqliteConnection>::new(connspec); let manager = ConnectionManager::<
diesel_tracing::sqlite::InstrumentedSqliteConnection,
>::new(connspec);
let pool = r2d2::Pool::builder().build(manager).map_err(|err| { let pool = r2d2::Pool::builder().build(manager).map_err(|err| {
io::Error::new( io::Error::new(
ErrorKind::Other, ErrorKind::Other,