feat(log): database operations and API logs split
This commit is contained in:
parent
9336235b64
commit
3a0576ba48
@ -1,7 +1,6 @@
|
|||||||
use actix_web::{web, HttpResponse};
|
use actix_web::{web, HttpResponse};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use tracing::Instrument;
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
@ -10,24 +9,32 @@ pub struct FormData {
|
|||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(
|
||||||
|
name = "Adding a new subscriber",
|
||||||
|
// functions args isn't really relevant to the span
|
||||||
|
skip(form, db_conn_pool),
|
||||||
|
fields(
|
||||||
|
request_id = %Uuid::new_v4(),
|
||||||
|
subscriber_email = %form.email,
|
||||||
|
subscriber_name = %form.name
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub async fn subscribe_route(
|
pub async fn subscribe_route(
|
||||||
form: web::Form<FormData>,
|
form: web::Form<FormData>,
|
||||||
db_conn_pool: web::Data<PgPool>,
|
db_conn_pool: web::Data<PgPool>,
|
||||||
) -> HttpResponse {
|
) -> HttpResponse {
|
||||||
let request_id = Uuid::new_v4();
|
match insert_subscriber(&db_conn_pool, &form).await {
|
||||||
|
Ok(_) => HttpResponse::Ok().finish(),
|
||||||
|
Err(_) => HttpResponse::InternalServerError().finish(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let request_span = tracing::info_span!(
|
#[tracing::instrument(
|
||||||
"Adding new subscriber",
|
name = "Saving new subscriber details in the database",
|
||||||
%request_id,
|
skip(form, pool)
|
||||||
subscriber_email = %form.email,
|
)]
|
||||||
subscriber_name = %form.name
|
pub async fn insert_subscriber(pool: &PgPool, form: &FormData) -> Result<(), sqlx::Error> {
|
||||||
);
|
sqlx::query!(
|
||||||
|
|
||||||
let _request_span_guard = request_span.enter();
|
|
||||||
|
|
||||||
let query_span = tracing::info_span!("Adding new subscriber in PostgreSQL");
|
|
||||||
|
|
||||||
match sqlx::query!(
|
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO subscriptions (id, email, name, subscribed_at)
|
INSERT INTO subscriptions (id, email, name, subscribed_at)
|
||||||
VALUES ($1, $2, $3, $4)
|
VALUES ($1, $2, $3, $4)
|
||||||
@ -37,18 +44,14 @@ pub async fn subscribe_route(
|
|||||||
form.name,
|
form.name,
|
||||||
Utc::now()
|
Utc::now()
|
||||||
)
|
)
|
||||||
.execute(db_conn_pool.get_ref())
|
.execute(pool)
|
||||||
.instrument(query_span)
|
|
||||||
.await
|
.await
|
||||||
{
|
.map_err(|e| {
|
||||||
Ok(_) => HttpResponse::Ok().finish(),
|
tracing::error!("Failed to execute query: {:?}", e);
|
||||||
Err(err) => {
|
e
|
||||||
tracing::error!(
|
// Using the `?` operator to return early
|
||||||
"request_id {} - Failed to execute query: {:?}",
|
// if the function failed, returning a sqlx::Error
|
||||||
request_id,
|
// We will talk about error handling in depth later!
|
||||||
err
|
})?;
|
||||||
);
|
Ok(())
|
||||||
HttpResponse::InternalServerError().finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user