feat: add tracing

This commit is contained in:
Sandro Eiler 2024-01-30 21:43:32 +01:00
parent 1fdf2948fb
commit 653502c470
4 changed files with 162 additions and 2 deletions

View file

@ -1,3 +1,4 @@
use env_logger::Env;
use learn_axum::configuration::get_configuration;
use learn_axum::startup;
use sqlx::PgPool;
@ -5,7 +6,9 @@ use tokio::net::TcpListener;
#[tokio::main]
/// Entry point for the application.
/// Log level default can be overridden with the RUST_LOG environment variable.
async fn main() {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let configuration = get_configuration().expect("Failed to read configuration.");
let addr = format!("127.0.0.1:{}", configuration.application_port);
let listener = TcpListener::bind(addr).await.unwrap(); //.expect("Unable to bind to port");

View file

@ -14,7 +14,18 @@ struct FormData {
}
async fn subscribe(State(pool): State<PgPool>, Form(form): Form<FormData>) {
let _ = sqlx::query!(
let request_id = Uuid::new_v4();
tracing::info!(
"request_id {} - Adding '{}' '{}' as a new subscriber.",
request_id,
form.email,
form.name
);
tracing::info!(
"request_id {} - Saving new subscriber details in the database",
request_id
);
match sqlx::query!(
r#"
INSERT INTO subscriptions (id, email, name, subscribed_at)
VALUES ($1, $2, $3, $4)
@ -27,7 +38,22 @@ async fn subscribe(State(pool): State<PgPool>, Form(form): Form<FormData>) {
// We use `get_ref` to get an immutable reference to the `PgConnection`
// wrapped by `web::Data`.
.execute(&pool)
.await;
.await
{
Ok(_) => {
tracing::info!(
"request_id {} - New subscriber details have been saved",
request_id
);
}
Err(e) => {
tracing::error!(
"request_id {} - Failed to execute query: {:?}",
request_id,
e
);
}
};
}
pub fn routes_subscriptions(pool: PgPool) -> Router {