diff options
| author | Conrad Ludgate <conrad.ludgate@truelayer.com> | 2022-04-21 18:07:33 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-21 18:07:33 +0100 |
| commit | 9085485a4f8a6be76d1ac8a8b7a7b65bdf83aa24 (patch) | |
| tree | df3760f5b1467359f9d331d1acfd09763f377221 /atuin-server/src | |
| parent | treat popos as ubuntu (#319) (diff) | |
| download | atuin-9085485a4f8a6be76d1ac8a8b7a7b65bdf83aa24.zip | |
tracing (#315)
* enable tracing on server
* fmt
* instrument handlers
Diffstat (limited to 'atuin-server/src')
| -rw-r--r-- | atuin-server/src/database.rs | 17 | ||||
| -rw-r--r-- | atuin-server/src/handlers/history.rs | 5 | ||||
| -rw-r--r-- | atuin-server/src/handlers/user.rs | 4 | ||||
| -rw-r--r-- | atuin-server/src/lib.rs | 3 | ||||
| -rw-r--r-- | atuin-server/src/router.rs | 10 |
5 files changed, 34 insertions, 5 deletions
diff --git a/atuin-server/src/database.rs b/atuin-server/src/database.rs index efde86a3..2d3f8be1 100644 --- a/atuin-server/src/database.rs +++ b/atuin-server/src/database.rs @@ -1,5 +1,6 @@ use async_trait::async_trait; use std::collections::HashMap; +use tracing::{debug, instrument}; use sqlx::{postgres::PgPoolOptions, Result}; @@ -77,6 +78,7 @@ impl Postgres { #[async_trait] impl Database for Postgres { + #[instrument(skip_all)] async fn get_session(&self, token: &str) -> Result<Session> { sqlx::query_as::<_, Session>("select * from sessions where token = $1") .bind(token) @@ -84,6 +86,7 @@ impl Database for Postgres { .await } + #[instrument(skip_all)] async fn get_user(&self, username: &str) -> Result<User> { sqlx::query_as::<_, User>("select * from users where username = $1") .bind(username) @@ -91,6 +94,7 @@ impl Database for Postgres { .await } + #[instrument(skip_all)] async fn get_session_user(&self, token: &str) -> Result<User> { sqlx::query_as::<_, User>( "select * from users @@ -103,6 +107,7 @@ impl Database for Postgres { .await } + #[instrument(skip_all)] async fn count_history(&self, user: &User) -> Result<i64> { // The cache is new, and the user might not yet have a cache value. // They will have one as soon as they post up some new history, but handle that @@ -119,6 +124,7 @@ impl Database for Postgres { Ok(res.0) } + #[instrument(skip_all)] async fn count_history_cached(&self, user: &User) -> Result<i64> { let res: (i32,) = sqlx::query_as( "select total from total_history_count_user @@ -131,6 +137,7 @@ impl Database for Postgres { Ok(res.0 as i64) } + #[instrument(skip_all)] async fn count_history_range( &self, user: &User, @@ -153,6 +160,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<i64> { let start = chrono::Utc.ymd(year, 1, 1).and_hms_nano(0, 0, 0, 0); let end = start + RelativeDuration::years(1); @@ -164,6 +172,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<i64> { let start = chrono::Utc .ymd(month.year(), month.month(), 1) @@ -189,6 +198,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<i64> { let start = chrono::Utc .ymd(day.year(), day.month(), day.day()) @@ -203,6 +213,7 @@ impl Database for Postgres { Ok(res) } + #[instrument(skip_all)] async fn list_history( &self, user: &User, @@ -230,6 +241,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?; @@ -259,6 +271,7 @@ impl Database for Postgres { Ok(()) } + #[instrument(skip_all)] async fn add_user(&self, user: &NewUser) -> Result<i64> { let email: &str = &user.email; let username: &str = &user.username; @@ -279,6 +292,7 @@ impl Database for Postgres { Ok(res.0) } + #[instrument(skip_all)] async fn add_session(&self, session: &NewSession) -> Result<()> { let token: &str = &session.token; @@ -295,6 +309,7 @@ impl Database for Postgres { Ok(()) } + #[instrument(skip_all)] async fn get_user_session(&self, u: &User) -> Result<Session> { sqlx::query_as::<_, Session>("select * from sessions where user_id = $1") .bind(u.id) @@ -302,6 +317,7 @@ impl Database for Postgres { .await } + #[instrument(skip_all)] async fn oldest_history(&self, user: &User) -> Result<History> { let res = sqlx::query_as::<_, History>( "select * from history @@ -316,6 +332,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 4fa2a962..b7112526 100644 --- a/atuin-server/src/handlers/history.rs +++ b/atuin-server/src/handlers/history.rs @@ -2,6 +2,7 @@ use axum::extract::Query; use axum::{extract::Path, Extension, Json}; use http::StatusCode; use std::collections::HashMap; +use tracing::{debug, error, instrument}; use crate::database::{Database, Postgres}; use crate::models::{NewHistory, User}; @@ -9,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<Postgres>, @@ -27,6 +29,7 @@ pub async fn count( } } +#[instrument(skip_all, fields(user.id = user.id))] pub async fn list( req: Query<SyncHistoryRequest>, user: User, @@ -62,6 +65,7 @@ pub async fn list( Ok(Json(SyncHistoryResponse { history })) } +#[instrument(skip_all, fields(user.id = user.id))] pub async fn add( Json(req): Json<Vec<AddHistoryRequest>>, user: User, @@ -90,6 +94,7 @@ pub async fn add( Ok(()) } +#[instrument(skip_all, fields(user.id = user.id))] pub async fn calendar( Path(focus): Path<String>, Query(params): Query<HashMap<String, u64>>, diff --git a/atuin-server/src/handlers/user.rs b/atuin-server/src/handlers/user.rs index 42e4aa33..a9a48fdc 100644 --- a/atuin-server/src/handlers/user.rs +++ b/atuin-server/src/handlers/user.rs @@ -6,6 +6,7 @@ use axum::extract::Path; use axum::{Extension, Json}; use http::StatusCode; use sodiumoxide::crypto::pwhash::argon2id13; +use tracing::{debug, error, instrument}; use uuid::Uuid; use crate::database::{Database, Postgres}; @@ -26,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<String>, db: Extension<Postgres>, @@ -48,6 +50,7 @@ pub async fn get( })) } +#[instrument(skip_all)] pub async fn register( Json(register): Json<RegisterRequest>, settings: Extension<Settings>, @@ -95,6 +98,7 @@ pub async fn register( } } +#[instrument(skip_all, fields(user.username = login.username.as_str()))] pub async fn login( login: Json<LoginRequest>, db: Extension<Postgres>, diff --git a/atuin-server/src/lib.rs b/atuin-server/src/lib.rs index 02b3fdae..8811986e 100644 --- a/atuin-server/src/lib.rs +++ b/atuin-server/src/lib.rs @@ -9,9 +9,6 @@ use eyre::{Context, Result}; use crate::settings::Settings; #[macro_use] -extern crate log; - -#[macro_use] extern crate serde_derive; pub mod auth; diff --git a/atuin-server/src/router.rs b/atuin-server/src/router.rs index 146cc991..9b525e05 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)), + ) } |
