From 1d77474d4eaf9fce23bfb29950e3b61d6ce7ffbb Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Wed, 20 Apr 2022 21:15:58 +0100 Subject: [PATCH] instrument handlers --- Cargo.lock | 3 +++ atuin-server/Cargo.toml | 2 ++ atuin-server/src/database.rs | 17 ++++++++++++++++- atuin-server/src/handlers/history.rs | 6 +++++- atuin-server/src/handlers/user.rs | 5 ++++- atuin-server/src/router.rs | 10 ++++++++-- 6 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7889e48553a..7fed6667a90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -167,6 +167,8 @@ dependencies = [ "sodiumoxide", "sqlx", "tokio", + "tower", + "tower-http", "tracing", "uuid", "whoami", @@ -2476,6 +2478,7 @@ dependencies = [ "tower", "tower-layer", "tower-service", + "tracing", ] [[package]] diff --git a/atuin-server/Cargo.toml b/atuin-server/Cargo.toml index 92f4bd03b59..33f5f7cd75a 100644 --- a/atuin-server/Cargo.toml +++ b/atuin-server/Cargo.toml @@ -31,3 +31,5 @@ axum = "0.5" http = "0.2" fs-err = "2.7" chronoutil = "0.2.3" +tower = "0.4" +tower-http = { version = "0.2", features = ["trace"] } diff --git a/atuin-server/src/database.rs b/atuin-server/src/database.rs index 99447e6e3c5..1ebcf21ed36 100644 --- a/atuin-server/src/database.rs +++ b/atuin-server/src/database.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; use std::collections::HashMap; -use tracing::debug; +use tracing::{debug, instrument}; use eyre::{eyre, Result}; use sqlx::postgres::PgPoolOptions; @@ -78,6 +78,7 @@ impl Postgres { #[async_trait] impl Database for Postgres { + #[instrument(skip_all)] async fn get_session(&self, token: &str) -> Result { let res: Option = sqlx::query_as::<_, Session>("select * from sessions where token = $1") @@ -92,6 +93,7 @@ impl Database for Postgres { } } + #[instrument(skip_all)] async fn get_user(&self, username: &str) -> Result { let res: Option = sqlx::query_as::<_, User>("select * from users where username = $1") @@ -106,6 +108,7 @@ impl Database for Postgres { } } + #[instrument(skip_all)] async fn get_session_user(&self, token: &str) -> Result { let res: Option = sqlx::query_as::<_, User>( "select * from users @@ -124,6 +127,7 @@ impl Database for Postgres { } } + #[instrument(skip_all)] async fn count_history(&self, user: &User) -> Result { let res: (i64,) = sqlx::query_as( "select count(1) from history @@ -136,6 +140,7 @@ impl Database for Postgres { Ok(res.0) } + #[instrument(skip_all)] async fn count_history_range( &self, user: &User, @@ -158,6 +163,7 @@ impl Database for Postgres { } // Count the history for a given year + #[instrument(skip_all)] async fn count_history_year(&self, user: &User, year: i32) -> Result { let start = chrono::Utc.ymd(year, 1, 1).and_hms_nano(0, 0, 0, 0); let end = start + RelativeDuration::years(1); @@ -169,6 +175,7 @@ impl Database for Postgres { } // Count the history for a given month + #[instrument(skip_all)] async fn count_history_month(&self, user: &User, month: chrono::NaiveDate) -> Result { let start = chrono::Utc .ymd(month.year(), month.month(), 1) @@ -194,6 +201,7 @@ impl Database for Postgres { } // Count the history for a given day + #[instrument(skip_all)] async fn count_history_day(&self, user: &User, day: chrono::NaiveDate) -> Result { let start = chrono::Utc .ymd(day.year(), day.month(), day.day()) @@ -208,6 +216,7 @@ impl Database for Postgres { Ok(res) } + #[instrument(skip_all)] async fn list_history( &self, user: &User, @@ -235,6 +244,7 @@ impl Database for Postgres { Ok(res) } + #[instrument(skip_all)] async fn add_history(&self, history: &[NewHistory]) -> Result<()> { let mut tx = self.pool.begin().await?; @@ -264,6 +274,7 @@ impl Database for Postgres { Ok(()) } + #[instrument(skip_all)] async fn add_user(&self, user: &NewUser) -> Result { let email: &str = &user.email; let username: &str = &user.username; @@ -284,6 +295,7 @@ impl Database for Postgres { Ok(res.0) } + #[instrument(skip_all)] async fn add_session(&self, session: &NewSession) -> Result<()> { let token: &str = &session.token; @@ -300,6 +312,7 @@ impl Database for Postgres { Ok(()) } + #[instrument(skip_all)] async fn get_user_session(&self, u: &User) -> Result { let res: Option = sqlx::query_as::<_, Session>("select * from sessions where user_id = $1") @@ -314,6 +327,7 @@ impl Database for Postgres { } } + #[instrument(skip_all)] async fn oldest_history(&self, user: &User) -> Result { let res = sqlx::query_as::<_, History>( "select * from history @@ -328,6 +342,7 @@ impl Database for Postgres { Ok(res) } + #[instrument(skip_all)] async fn calendar( &self, user: &User, diff --git a/atuin-server/src/handlers/history.rs b/atuin-server/src/handlers/history.rs index a0b8144e9ef..80811ed8b7e 100644 --- a/atuin-server/src/handlers/history.rs +++ b/atuin-server/src/handlers/history.rs @@ -2,7 +2,7 @@ use axum::extract::Query; use axum::{extract::Path, Extension, Json}; use http::StatusCode; use std::collections::HashMap; -use tracing::{debug, error}; +use tracing::{debug, error, instrument}; use crate::database::{Database, Postgres}; use crate::models::{NewHistory, User}; @@ -10,6 +10,7 @@ use atuin_common::api::*; use crate::calendar::{TimePeriod, TimePeriodInfo}; +#[instrument(skip_all, fields(user.id = user.id))] pub async fn count( user: User, db: Extension, @@ -21,6 +22,7 @@ pub async fn count( } } +#[instrument(skip_all, fields(user.id = user.id))] pub async fn list( req: Query, user: User, @@ -56,6 +58,7 @@ pub async fn list( Ok(Json(SyncHistoryResponse { history })) } +#[instrument(skip_all, fields(user.id = user.id))] pub async fn add( Json(req): Json>, user: User, @@ -84,6 +87,7 @@ pub async fn add( Ok(()) } +#[instrument(skip_all, fields(user.id = user.id))] pub async fn calendar( Path(focus): Path, Query(params): Query>, diff --git a/atuin-server/src/handlers/user.rs b/atuin-server/src/handlers/user.rs index 06a0c576b9e..ef31f83987c 100644 --- a/atuin-server/src/handlers/user.rs +++ b/atuin-server/src/handlers/user.rs @@ -6,7 +6,7 @@ use axum::extract::Path; use axum::{Extension, Json}; use http::StatusCode; use sodiumoxide::crypto::pwhash::argon2id13; -use tracing::{debug, error}; +use tracing::{debug, error, instrument}; use uuid::Uuid; use crate::database::{Database, Postgres}; @@ -27,6 +27,7 @@ pub fn verify_str(secret: &str, verify: &str) -> bool { } } +#[instrument(skip_all, fields(user.username = username.as_str()))] pub async fn get( Path(username): Path, db: Extension, @@ -44,6 +45,7 @@ pub async fn get( })) } +#[instrument(skip_all)] pub async fn register( Json(register): Json, settings: Extension, @@ -91,6 +93,7 @@ pub async fn register( } } +#[instrument(skip_all, fields(user.username = login.username.as_str()))] pub async fn login( login: Json, db: Extension, diff --git a/atuin-server/src/router.rs b/atuin-server/src/router.rs index 146cc991610..9b525e05a8f 100644 --- a/atuin-server/src/router.rs +++ b/atuin-server/src/router.rs @@ -7,6 +7,8 @@ use axum::{ Extension, Router, }; use eyre::Result; +use tower::ServiceBuilder; +use tower_http::trace::TraceLayer; use super::{ database::{Database, Postgres}, @@ -65,6 +67,10 @@ pub fn router(postgres: Postgres, settings: Settings) -> Router { .route("/register", post(handlers::user::register)) .route("/login", post(handlers::user::login)) .fallback(teapot.into_service()) - .layer(Extension(postgres)) - .layer(Extension(settings)) + .layer( + ServiceBuilder::new() + .layer(TraceLayer::new_for_http()) + .layer(Extension(postgres)) + .layer(Extension(settings)), + ) }