feat: add tracing

This commit is contained in:
Sandro Eiler 2024-02-04 13:48:31 +01:00
parent 653502c470
commit 93140e7d22
8 changed files with 431 additions and 174 deletions

View file

@ -13,23 +13,36 @@ struct FormData {
name: String,
}
async fn subscribe(State(pool): State<PgPool>, Form(form): Form<FormData>) {
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!(
#[tracing::instrument(
name = "Adding a new subscriber",
skip(form, pool),
fields(
request_id = %Uuid::new_v4(),
subscriber_email = %form.email,
subscriber_name = %form.name
)
)]
pub async fn subscribe(State(pool): State<PgPool>, Form(form): Form<FormData>) {
match insert_subscriber(&pool, &form).await {
Ok(_) => {
tracing::info!("Subscriber added to the database");
}
Err(_) => {
tracing::error!("Failed to add subscriber to the database");
}
}
}
#[tracing::instrument(
name = "Saving new subscriber details in the database",
skip(form, pool)
)]
pub async fn insert_subscriber(pool: &PgPool, form: &FormData) -> Result<(), sqlx::Error> {
sqlx::query!(
r#"
INSERT INTO subscriptions (id, email, name, subscribed_at)
VALUES ($1, $2, $3, $4)
"#,
INSERT INTO subscriptions (id, email, name, subscribed_at)
VALUES ($1, $2, $3, $4)
"#,
Uuid::new_v4(),
form.email,
form.name,
@ -37,23 +50,13 @@ 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)
.execute(pool)
.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
);
}
};
.map_err(|e| {
tracing::error!("Failed to execute query: {:?}", e);
e
})?;
Ok(())
}
pub fn routes_subscriptions(pool: PgPool) -> Router {