Skip to content

Commit

Permalink
instrument handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate committed Apr 20, 2022
1 parent a074776 commit 1d77474
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions atuin-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
17 changes: 16 additions & 1 deletion atuin-server/src/database.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -78,6 +78,7 @@ impl Postgres {

#[async_trait]
impl Database for Postgres {
#[instrument(skip_all)]
async fn get_session(&self, token: &str) -> Result<Session> {
let res: Option<Session> =
sqlx::query_as::<_, Session>("select * from sessions where token = $1")
Expand All @@ -92,6 +93,7 @@ impl Database for Postgres {
}
}

#[instrument(skip_all)]
async fn get_user(&self, username: &str) -> Result<User> {
let res: Option<User> =
sqlx::query_as::<_, User>("select * from users where username = $1")
Expand All @@ -106,6 +108,7 @@ impl Database for Postgres {
}
}

#[instrument(skip_all)]
async fn get_session_user(&self, token: &str) -> Result<User> {
let res: Option<User> = sqlx::query_as::<_, User>(
"select * from users
Expand All @@ -124,6 +127,7 @@ impl Database for Postgres {
}
}

#[instrument(skip_all)]
async fn count_history(&self, user: &User) -> Result<i64> {
let res: (i64,) = sqlx::query_as(
"select count(1) from history
Expand All @@ -136,6 +140,7 @@ impl Database for Postgres {
Ok(res.0)
}

#[instrument(skip_all)]
async fn count_history_range(
&self,
user: &User,
Expand All @@ -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<i64> {
let start = chrono::Utc.ymd(year, 1, 1).and_hms_nano(0, 0, 0, 0);
let end = start + RelativeDuration::years(1);
Expand All @@ -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<i64> {
let start = chrono::Utc
.ymd(month.year(), month.month(), 1)
Expand All @@ -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<i64> {
let start = chrono::Utc
.ymd(day.year(), day.month(), day.day())
Expand All @@ -208,6 +216,7 @@ impl Database for Postgres {
Ok(res)
}

#[instrument(skip_all)]
async fn list_history(
&self,
user: &User,
Expand Down Expand Up @@ -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?;

Expand Down Expand Up @@ -264,6 +274,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;
Expand All @@ -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;

Expand All @@ -300,6 +312,7 @@ impl Database for Postgres {
Ok(())
}

#[instrument(skip_all)]
async fn get_user_session(&self, u: &User) -> Result<Session> {
let res: Option<Session> =
sqlx::query_as::<_, Session>("select * from sessions where user_id = $1")
Expand All @@ -314,6 +327,7 @@ impl Database for Postgres {
}
}

#[instrument(skip_all)]
async fn oldest_history(&self, user: &User) -> Result<History> {
let res = sqlx::query_as::<_, History>(
"select * from history
Expand All @@ -328,6 +342,7 @@ impl Database for Postgres {
Ok(res)
}

#[instrument(skip_all)]
async fn calendar(
&self,
user: &User,
Expand Down
6 changes: 5 additions & 1 deletion atuin-server/src/handlers/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ 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};
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>,
Expand All @@ -21,6 +22,7 @@ pub async fn count(
}
}

#[instrument(skip_all, fields(user.id = user.id))]
pub async fn list(
req: Query<SyncHistoryRequest>,
user: User,
Expand Down Expand Up @@ -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<Vec<AddHistoryRequest>>,
user: User,
Expand Down Expand Up @@ -84,6 +87,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>>,
Expand Down
5 changes: 4 additions & 1 deletion atuin-server/src/handlers/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<String>,
db: Extension<Postgres>,
Expand All @@ -44,6 +45,7 @@ pub async fn get(
}))
}

#[instrument(skip_all)]
pub async fn register(
Json(register): Json<RegisterRequest>,
settings: Extension<Settings>,
Expand Down Expand Up @@ -91,6 +93,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>,
Expand Down
10 changes: 8 additions & 2 deletions atuin-server/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use axum::{
Extension, Router,
};
use eyre::Result;
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;

use super::{
database::{Database, Postgres},
Expand Down Expand Up @@ -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)),
)
}

0 comments on commit 1d77474

Please sign in to comment.