Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracing #315

Merged
merged 3 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion Cargo.lock

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

11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ clap = { version = "3.1.10", features = ["derive"] }
clap_complete = "3.1.1"
fs-err = "2.7"


[dependencies.tracing-subscriber]
version = "0.3"
default-features = false
features = [
"ansi",
"fmt",
"registry",
"env-filter",
]

[profile.release]
lto = "fat"
codegen-units = 1
Expand Down
4 changes: 3 additions & 1 deletion atuin-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = "https://github.com/ellie/atuin"
[dependencies]
atuin-common = { path = "../atuin-common", version = "0.8.1" }

log = "0.4"
tracing = "0.1"
chrono = { version = "0.4", features = ["serde"] }
eyre = "0.6"
uuid = { version = "0.8", features = ["v4"] }
Expand All @@ -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: 17 additions & 0 deletions atuin-server/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use async_trait::async_trait;
use std::collections::HashMap;
use tracing::{debug, instrument};

use sqlx::{postgres::PgPoolOptions, Result};

Expand Down Expand Up @@ -77,20 +78,23 @@ impl Postgres {

#[async_trait]
impl Database for Postgres {
#[instrument(skip_all)]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasoning for skip_all is that it's usually better form to opt_in which fields you want to log (for PII as well as just for noisy structs like db handles).

In tracing 0.2, this will be the default: tokio-rs/tracing#651

async fn get_session(&self, token: &str) -> Result<Session> {
sqlx::query_as::<_, Session>("select * from sessions where token = $1")
.bind(token)
.fetch_one(&self.pool)
.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)
.fetch_one(&self.pool)
.await
}

#[instrument(skip_all)]
async fn get_session_user(&self, token: &str) -> Result<User> {
sqlx::query_as::<_, User>(
"select * from users
Expand All @@ -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
Expand All @@ -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
Expand All @@ -131,6 +137,7 @@ impl Database for Postgres {
Ok(res.0 as i64)
}

#[instrument(skip_all)]
async fn count_history_range(
&self,
user: &User,
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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())
Expand All @@ -203,6 +213,7 @@ impl Database for Postgres {
Ok(res)
}

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

Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -295,13 +309,15 @@ 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)
.fetch_one(&self.pool)
.await
}

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

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

#[instrument(skip_all)]
pub async fn register(
Json(register): Json<RegisterRequest>,
settings: Extension<Settings>,
Expand Down Expand Up @@ -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>,
Expand Down
3 changes: 0 additions & 3 deletions atuin-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ use eyre::{Context, Result};

use crate::settings::Settings;

#[macro_use]
extern crate log;

#[macro_use]
extern crate serde_derive;

Expand Down
Loading