* enable tracing on server

* fmt

* instrument handlers
This commit is contained in:
Conrad Ludgate 2022-04-21 18:07:33 +01:00 committed by GitHub
parent 98d70fbf76
commit 9085485a4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 121 additions and 8 deletions

68
Cargo.lock generated
View File

@ -22,6 +22,15 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.53" version = "0.1.53"
@ -82,6 +91,7 @@ dependencies = [
"tabwriter", "tabwriter",
"termion", "termion",
"tokio", "tokio",
"tracing-subscriber",
"tui", "tui",
"unicode-width", "unicode-width",
] ]
@ -149,7 +159,6 @@ dependencies = [
"eyre", "eyre",
"fs-err", "fs-err",
"http", "http",
"log",
"rand 0.8.5", "rand 0.8.5",
"rust-crypto", "rust-crypto",
"serde", "serde",
@ -158,6 +167,9 @@ dependencies = [
"sodiumoxide", "sodiumoxide",
"sqlx", "sqlx",
"tokio", "tokio",
"tower",
"tower-http",
"tracing",
"uuid", "uuid",
"whoami", "whoami",
] ]
@ -1167,6 +1179,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
[[package]]
name = "matchers"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
dependencies = [
"regex-automata",
]
[[package]] [[package]]
name = "matches" name = "matches"
version = "0.1.9" version = "0.1.9"
@ -1725,6 +1746,9 @@ name = "regex-automata"
version = "0.1.10" version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax",
]
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
@ -2004,6 +2028,15 @@ dependencies = [
"digest 0.10.3", "digest 0.10.3",
] ]
[[package]]
name = "sharded-slab"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31"
dependencies = [
"lazy_static",
]
[[package]] [[package]]
name = "shellexpand" name = "shellexpand"
version = "2.1.0" version = "2.1.0"
@ -2290,6 +2323,15 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "thread_local"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180"
dependencies = [
"once_cell",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.1.43" version = "0.1.43"
@ -2436,6 +2478,7 @@ dependencies = [
"tower", "tower",
"tower-layer", "tower-layer",
"tower-service", "tower-service",
"tracing",
] ]
[[package]] [[package]]
@ -2481,6 +2524,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90442985ee2f57c9e1b548ee72ae842f4a9a20e3f417cc38dbc5dc684d9bb4ee" checksum = "90442985ee2f57c9e1b548ee72ae842f4a9a20e3f417cc38dbc5dc684d9bb4ee"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"valuable",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bc28f93baff38037f64e6f43d34cfa1605f27a49c34e8a04c5e78b0babf2596"
dependencies = [
"ansi_term",
"lazy_static",
"matchers",
"regex",
"sharded-slab",
"thread_local",
"tracing",
"tracing-core",
] ]
[[package]] [[package]]
@ -2586,6 +2646,12 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.15" version = "0.2.15"

View File

@ -59,6 +59,17 @@ clap = { version = "3.1.10", features = ["derive"] }
clap_complete = "3.1.1" clap_complete = "3.1.1"
fs-err = "2.7" fs-err = "2.7"
[dependencies.tracing-subscriber]
version = "0.3"
default-features = false
features = [
"ansi",
"fmt",
"registry",
"env-filter",
]
[profile.release] [profile.release]
lto = "fat" lto = "fat"
codegen-units = 1 codegen-units = 1

View File

@ -11,7 +11,7 @@ repository = "https://github.com/ellie/atuin"
[dependencies] [dependencies]
atuin-common = { path = "../atuin-common", version = "0.8.1" } atuin-common = { path = "../atuin-common", version = "0.8.1" }
log = "0.4" tracing = "0.1"
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
eyre = "0.6" eyre = "0.6"
uuid = { version = "0.8", features = ["v4"] } uuid = { version = "0.8", features = ["v4"] }
@ -31,3 +31,5 @@ axum = "0.5"
http = "0.2" http = "0.2"
fs-err = "2.7" fs-err = "2.7"
chronoutil = "0.2.3" chronoutil = "0.2.3"
tower = "0.4"
tower-http = { version = "0.2", features = ["trace"] }

View File

@ -1,5 +1,6 @@
use async_trait::async_trait; use async_trait::async_trait;
use std::collections::HashMap; use std::collections::HashMap;
use tracing::{debug, instrument};
use sqlx::{postgres::PgPoolOptions, Result}; use sqlx::{postgres::PgPoolOptions, Result};
@ -77,6 +78,7 @@ impl Postgres {
#[async_trait] #[async_trait]
impl Database for Postgres { impl Database for Postgres {
#[instrument(skip_all)]
async fn get_session(&self, token: &str) -> Result<Session> { async fn get_session(&self, token: &str) -> Result<Session> {
sqlx::query_as::<_, Session>("select * from sessions where token = $1") sqlx::query_as::<_, Session>("select * from sessions where token = $1")
.bind(token) .bind(token)
@ -84,6 +86,7 @@ impl Database for Postgres {
.await .await
} }
#[instrument(skip_all)]
async fn get_user(&self, username: &str) -> Result<User> { async fn get_user(&self, username: &str) -> Result<User> {
sqlx::query_as::<_, User>("select * from users where username = $1") sqlx::query_as::<_, User>("select * from users where username = $1")
.bind(username) .bind(username)
@ -91,6 +94,7 @@ impl Database for Postgres {
.await .await
} }
#[instrument(skip_all)]
async fn get_session_user(&self, token: &str) -> Result<User> { async fn get_session_user(&self, token: &str) -> Result<User> {
sqlx::query_as::<_, User>( sqlx::query_as::<_, User>(
"select * from users "select * from users
@ -103,6 +107,7 @@ impl Database for Postgres {
.await .await
} }
#[instrument(skip_all)]
async fn count_history(&self, user: &User) -> Result<i64> { async fn count_history(&self, user: &User) -> Result<i64> {
// The cache is new, and the user might not yet have a cache value. // 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 // 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) Ok(res.0)
} }
#[instrument(skip_all)]
async fn count_history_cached(&self, user: &User) -> Result<i64> { async fn count_history_cached(&self, user: &User) -> Result<i64> {
let res: (i32,) = sqlx::query_as( let res: (i32,) = sqlx::query_as(
"select total from total_history_count_user "select total from total_history_count_user
@ -131,6 +137,7 @@ impl Database for Postgres {
Ok(res.0 as i64) Ok(res.0 as i64)
} }
#[instrument(skip_all)]
async fn count_history_range( async fn count_history_range(
&self, &self,
user: &User, user: &User,
@ -153,6 +160,7 @@ impl Database for Postgres {
} }
// Count the history for a given year // Count the history for a given year
#[instrument(skip_all)]
async fn count_history_year(&self, user: &User, year: i32) -> Result<i64> { 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 start = chrono::Utc.ymd(year, 1, 1).and_hms_nano(0, 0, 0, 0);
let end = start + RelativeDuration::years(1); let end = start + RelativeDuration::years(1);
@ -164,6 +172,7 @@ impl Database for Postgres {
} }
// Count the history for a given month // Count the history for a given month
#[instrument(skip_all)]
async fn count_history_month(&self, user: &User, month: chrono::NaiveDate) -> Result<i64> { async fn count_history_month(&self, user: &User, month: chrono::NaiveDate) -> Result<i64> {
let start = chrono::Utc let start = chrono::Utc
.ymd(month.year(), month.month(), 1) .ymd(month.year(), month.month(), 1)
@ -189,6 +198,7 @@ impl Database for Postgres {
} }
// Count the history for a given day // Count the history for a given day
#[instrument(skip_all)]
async fn count_history_day(&self, user: &User, day: chrono::NaiveDate) -> Result<i64> { async fn count_history_day(&self, user: &User, day: chrono::NaiveDate) -> Result<i64> {
let start = chrono::Utc let start = chrono::Utc
.ymd(day.year(), day.month(), day.day()) .ymd(day.year(), day.month(), day.day())
@ -203,6 +213,7 @@ impl Database for Postgres {
Ok(res) Ok(res)
} }
#[instrument(skip_all)]
async fn list_history( async fn list_history(
&self, &self,
user: &User, user: &User,
@ -230,6 +241,7 @@ impl Database for Postgres {
Ok(res) Ok(res)
} }
#[instrument(skip_all)]
async fn add_history(&self, history: &[NewHistory]) -> Result<()> { async fn add_history(&self, history: &[NewHistory]) -> Result<()> {
let mut tx = self.pool.begin().await?; let mut tx = self.pool.begin().await?;
@ -259,6 +271,7 @@ impl Database for Postgres {
Ok(()) Ok(())
} }
#[instrument(skip_all)]
async fn add_user(&self, user: &NewUser) -> Result<i64> { async fn add_user(&self, user: &NewUser) -> Result<i64> {
let email: &str = &user.email; let email: &str = &user.email;
let username: &str = &user.username; let username: &str = &user.username;
@ -279,6 +292,7 @@ impl Database for Postgres {
Ok(res.0) Ok(res.0)
} }
#[instrument(skip_all)]
async fn add_session(&self, session: &NewSession) -> Result<()> { async fn add_session(&self, session: &NewSession) -> Result<()> {
let token: &str = &session.token; let token: &str = &session.token;
@ -295,6 +309,7 @@ impl Database for Postgres {
Ok(()) Ok(())
} }
#[instrument(skip_all)]
async fn get_user_session(&self, u: &User) -> Result<Session> { async fn get_user_session(&self, u: &User) -> Result<Session> {
sqlx::query_as::<_, Session>("select * from sessions where user_id = $1") sqlx::query_as::<_, Session>("select * from sessions where user_id = $1")
.bind(u.id) .bind(u.id)
@ -302,6 +317,7 @@ impl Database for Postgres {
.await .await
} }
#[instrument(skip_all)]
async fn oldest_history(&self, user: &User) -> Result<History> { async fn oldest_history(&self, user: &User) -> Result<History> {
let res = sqlx::query_as::<_, History>( let res = sqlx::query_as::<_, History>(
"select * from history "select * from history
@ -316,6 +332,7 @@ impl Database for Postgres {
Ok(res) Ok(res)
} }
#[instrument(skip_all)]
async fn calendar( async fn calendar(
&self, &self,
user: &User, user: &User,

View File

@ -2,6 +2,7 @@ use axum::extract::Query;
use axum::{extract::Path, Extension, Json}; use axum::{extract::Path, Extension, Json};
use http::StatusCode; use http::StatusCode;
use std::collections::HashMap; use std::collections::HashMap;
use tracing::{debug, error, instrument};
use crate::database::{Database, Postgres}; use crate::database::{Database, Postgres};
use crate::models::{NewHistory, User}; use crate::models::{NewHistory, User};
@ -9,6 +10,7 @@ use atuin_common::api::*;
use crate::calendar::{TimePeriod, TimePeriodInfo}; use crate::calendar::{TimePeriod, TimePeriodInfo};
#[instrument(skip_all, fields(user.id = user.id))]
pub async fn count( pub async fn count(
user: User, user: User,
db: Extension<Postgres>, db: Extension<Postgres>,
@ -27,6 +29,7 @@ pub async fn count(
} }
} }
#[instrument(skip_all, fields(user.id = user.id))]
pub async fn list( pub async fn list(
req: Query<SyncHistoryRequest>, req: Query<SyncHistoryRequest>,
user: User, user: User,
@ -62,6 +65,7 @@ pub async fn list(
Ok(Json(SyncHistoryResponse { history })) Ok(Json(SyncHistoryResponse { history }))
} }
#[instrument(skip_all, fields(user.id = user.id))]
pub async fn add( pub async fn add(
Json(req): Json<Vec<AddHistoryRequest>>, Json(req): Json<Vec<AddHistoryRequest>>,
user: User, user: User,
@ -90,6 +94,7 @@ pub async fn add(
Ok(()) Ok(())
} }
#[instrument(skip_all, fields(user.id = user.id))]
pub async fn calendar( pub async fn calendar(
Path(focus): Path<String>, Path(focus): Path<String>,
Query(params): Query<HashMap<String, u64>>, Query(params): Query<HashMap<String, u64>>,

View File

@ -6,6 +6,7 @@ use axum::extract::Path;
use axum::{Extension, Json}; use axum::{Extension, Json};
use http::StatusCode; use http::StatusCode;
use sodiumoxide::crypto::pwhash::argon2id13; use sodiumoxide::crypto::pwhash::argon2id13;
use tracing::{debug, error, instrument};
use uuid::Uuid; use uuid::Uuid;
use crate::database::{Database, Postgres}; 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( pub async fn get(
Path(username): Path<String>, Path(username): Path<String>,
db: Extension<Postgres>, db: Extension<Postgres>,
@ -48,6 +50,7 @@ pub async fn get(
})) }))
} }
#[instrument(skip_all)]
pub async fn register( pub async fn register(
Json(register): Json<RegisterRequest>, Json(register): Json<RegisterRequest>,
settings: Extension<Settings>, 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( pub async fn login(
login: Json<LoginRequest>, login: Json<LoginRequest>,
db: Extension<Postgres>, db: Extension<Postgres>,

View File

@ -8,9 +8,6 @@ use eyre::{Context, Result};
use crate::settings::Settings; use crate::settings::Settings;
#[macro_use]
extern crate log;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;

View File

@ -7,6 +7,8 @@ use axum::{
Extension, Router, Extension, Router,
}; };
use eyre::Result; use eyre::Result;
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;
use super::{ use super::{
database::{Database, Postgres}, database::{Database, Postgres},
@ -65,6 +67,10 @@ pub fn router(postgres: Postgres, settings: Settings) -> Router {
.route("/register", post(handlers::user::register)) .route("/register", post(handlers::user::register))
.route("/login", post(handlers::user::login)) .route("/login", post(handlers::user::login))
.fallback(teapot.into_service()) .fallback(teapot.into_service())
.layer(Extension(postgres)) .layer(
.layer(Extension(settings)) ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
.layer(Extension(postgres))
.layer(Extension(settings)),
)
} }

View File

@ -1,3 +1,5 @@
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use clap::Parser; use clap::Parser;
use eyre::{Context, Result}; use eyre::{Context, Result};
@ -21,7 +23,10 @@ pub enum Cmd {
impl Cmd { impl Cmd {
pub async fn run(self) -> Result<()> { pub async fn run(self) -> Result<()> {
pretty_env_logger::init(); tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_default_env())
.init();
let settings = Settings::new().wrap_err("could not load server settings")?; let settings = Settings::new().wrap_err("could not load server settings")?;