Telemetry

This commit is contained in:
Alphonse Paix
2025-08-22 08:14:59 +02:00
parent 709bd28a8c
commit f1290d0bc5
11 changed files with 239 additions and 46 deletions

View File

@@ -4,32 +4,53 @@ use serde::Deserialize;
use sqlx::PgPool;
use uuid::Uuid;
#[tracing::instrument(
name = "Adding a new subscriber",
skip(connection, form),
fields(
subscriber_email = %form.email,
subscriber_name = %form.name
)
)]
pub async fn subscribe(
State(connection): State<PgPool>,
form: Form<FormData>,
) -> impl IntoResponse {
match sqlx::query!(
if insert_subscriber(&connection, &form).await.is_err() {
StatusCode::INTERNAL_SERVER_ERROR
} else {
StatusCode::OK
}
}
#[tracing::instrument(
name = "Saving new subscriber details in the database",
skip(connection, form)
)]
pub async fn insert_subscriber(
connection: &PgPool,
form: &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,
Utc::now()
)
.execute(&connection)
.execute(connection)
.await
{
Ok(_) => StatusCode::OK,
Err(e) => {
eprintln!("Failed to execute query: {}", e);
StatusCode::INTERNAL_SERVER_ERROR
}
}
.map_err(|e| {
tracing::error!("Failed to execute query: {:?}", e);
e
})?;
Ok(())
}
#[derive(Deserialize)]
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct FormData {
name: String,