From 3a961bc52417edcf907a64c80b3be82a5b565184 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Fri, 11 Nov 2022 15:37:33 +0530 Subject: [PATCH] feat: add tracing log identifier to each HTTP route handler --- src/api/v1/account/mod.rs | 6 ++++++ src/api/v1/auth.rs | 3 +++ src/api/v1/pages.rs | 17 +++++++++++++++++ src/deploy.rs | 3 ++- src/meta.rs | 2 ++ src/pages/auth/login.rs | 2 ++ src/pages/auth/mod.rs | 2 +- src/pages/auth/register.rs | 2 ++ src/pages/dash/home.rs | 3 +-- src/pages/mod.rs | 1 + src/serve.rs | 1 + 11 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/api/v1/pages.rs diff --git a/src/api/v1/account/mod.rs b/src/api/v1/account/mod.rs index d4a8a4e..c2716e2 100644 --- a/src/api/v1/account/mod.rs +++ b/src/api/v1/account/mod.rs @@ -58,6 +58,7 @@ pub struct Username { path = "crate::V1_API_ROUTES.account.update_username", wrap = "super::get_auth_middleware()" )] +#[tracing::instrument(name = "Update username", skip(ctx, payload, id))] async fn set_username( id: Identity, payload: web::Json, @@ -74,6 +75,7 @@ async fn set_username( } #[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.account.username_exists")] +#[tracing::instrument(name = "Check if username exists", skip(ctx, payload))] async fn username_exists( payload: web::Json, ctx: AppCtx, @@ -82,6 +84,7 @@ async fn username_exists( } #[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.account.email_exists")] +#[tracing::instrument(name = "Check if email exists", skip(ctx, payload))] pub async fn email_exists( payload: web::Json, ctx: AppCtx, @@ -94,6 +97,7 @@ pub async fn email_exists( path = "crate::V1_API_ROUTES.account.update_email", wrap = "super::get_auth_middleware()" )] +#[tracing::instrument(name = "Update email", skip(ctx, payload, id))] async fn set_email( id: Identity, payload: web::Json, @@ -108,6 +112,7 @@ async fn set_email( path = "crate::V1_API_ROUTES.account.delete", wrap = "super::get_auth_middleware()" )] +#[tracing::instrument(name = "Delete account", skip(ctx, payload, id))] async fn delete_account( id: Identity, payload: web::Json, @@ -124,6 +129,7 @@ async fn delete_account( path = "crate::V1_API_ROUTES.account.update_password", wrap = "super::get_auth_middleware()" )] +#[tracing::instrument(name = "Update user password", skip(ctx, payload, id))] async fn update_user_password( id: Identity, ctx: AppCtx, diff --git a/src/api/v1/auth.rs b/src/api/v1/auth.rs index 8b2c1eb..d604d08 100644 --- a/src/api/v1/auth.rs +++ b/src/api/v1/auth.rs @@ -30,12 +30,14 @@ pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(signout); } #[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.auth.register")] +#[tracing::instrument(name = "Register new user", skip(ctx, payload))] async fn register(payload: web::Json, ctx: AppCtx) -> ServiceResult { ctx.register(&payload).await?; Ok(HttpResponse::Ok()) } #[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.auth.login")] +#[tracing::instrument(name = "Login", skip(ctx, payload, id, query))] async fn login( id: Identity, payload: web::Json, @@ -59,6 +61,7 @@ async fn login( path = "crate::V1_API_ROUTES.auth.logout", wrap = "super::get_auth_middleware()" )] +#[tracing::instrument(name = "Sign out", skip(id))] async fn signout(id: Identity) -> impl Responder { use actix_auth_middleware::GetLoginRoute; diff --git a/src/api/v1/pages.rs b/src/api/v1/pages.rs new file mode 100644 index 0000000..e504959 --- /dev/null +++ b/src/api/v1/pages.rs @@ -0,0 +1,17 @@ +/* + * Copyright (C) 2022 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + diff --git a/src/deploy.rs b/src/deploy.rs index 719a8f6..836be01 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -45,6 +45,7 @@ pub struct DeployEvent { } #[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.deploy.update")] +#[tracing::instrument(name = "Update webpages", skip(payload, ctx))] async fn update(payload: web::Json, ctx: AppCtx) -> ServiceResult { let payload = payload.into_inner(); ctx.update_site(&payload.secret, Some(payload.branch)) @@ -81,12 +82,12 @@ impl DeployInfo { } #[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.deploy.info")] +#[tracing::instrument(name = "Get webpage deploy info", skip(payload, ctx))] async fn deploy_info( payload: web::Json, ctx: AppCtx, ) -> ServiceResult { if let Ok(page) = ctx.db.get_site_from_secret(&payload.secret).await { - // if let Some(page) = find_page(&payload.secret, &ctx) { let resp = DeployInfo::from_page(&Page::from_site(&ctx.settings, page))?; Ok(HttpResponse::Ok().json(resp)) } else { diff --git a/src/meta.rs b/src/meta.rs index 32e1be7..997b3b4 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -44,6 +44,7 @@ pub mod routes { /// emits build details of the binary #[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.meta.build_details")] +#[tracing::instrument(name = "Fetch Build Details", skip(ctx))] async fn build_details(ctx: AppCtx) -> impl Responder { let build = BuildDetails { version: VERSION, @@ -61,6 +62,7 @@ pub struct Health { /// checks all components of the system #[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.meta.health")] +#[tracing::instrument(name = "Fetch health", skip(ctx))] async fn health(ctx: crate::AppCtx) -> impl Responder { let res = Health { db: ctx.db.ping().await, diff --git a/src/pages/auth/login.rs b/src/pages/auth/login.rs index 64bdb8f..988f017 100644 --- a/src/pages/auth/login.rs +++ b/src/pages/auth/login.rs @@ -61,6 +61,7 @@ impl Login { } #[actix_web_codegen_const_routes::get(path = "PAGES.auth.login")] +#[tracing::instrument(name = "Serve login page", skip(ctx))] pub async fn get_login(ctx: AppCtx) -> impl Responder { let login = Login::page(&ctx.settings); let html = ContentType::html(); @@ -73,6 +74,7 @@ pub fn services(cfg: &mut web::ServiceConfig) { } #[actix_web_codegen_const_routes::post(path = "PAGES.auth.login")] +#[tracing::instrument(name = "Web UI Login", skip(id, payload, query, ctx))] pub async fn login_submit( id: Identity, payload: web::Form, diff --git a/src/pages/auth/mod.rs b/src/pages/auth/mod.rs index 1aed55c..5343e89 100644 --- a/src/pages/auth/mod.rs +++ b/src/pages/auth/mod.rs @@ -42,13 +42,13 @@ pub fn services(cfg: &mut web::ServiceConfig) { path = "PAGES.auth.logout", wrap = "super::get_auth_middleware()" )] +#[tracing::instrument(name = "Sign out", skip(id))] async fn signout(id: Identity) -> impl Responder { use actix_auth_middleware::GetLoginRoute; if id.identity().is_some() { id.forget(); } - println!("received signout"); HttpResponse::Found() .append_header((http::header::LOCATION, PAGES.get_login_route(None))) .finish() diff --git a/src/pages/auth/register.rs b/src/pages/auth/register.rs index 9ecef00..32330a6 100644 --- a/src/pages/auth/register.rs +++ b/src/pages/auth/register.rs @@ -58,6 +58,7 @@ impl Register { } #[actix_web_codegen_const_routes::get(path = "PAGES.auth.register")] +#[tracing::instrument(name = "Serve registration page", skip(ctx))] pub async fn get_register(ctx: AppCtx) -> impl Responder { let login = Register::page(&ctx.settings); let html = ContentType::html(); @@ -70,6 +71,7 @@ pub fn services(cfg: &mut web::ServiceConfig) { } #[actix_web_codegen_const_routes::post(path = "PAGES.auth.register")] +#[tracing::instrument(name = "Process web UI registration", skip(ctx))] pub async fn register_submit( payload: web::Form, ctx: AppCtx, diff --git a/src/pages/dash/home.rs b/src/pages/dash/home.rs index b5b1ca9..9188002 100644 --- a/src/pages/dash/home.rs +++ b/src/pages/dash/home.rs @@ -16,11 +16,9 @@ */ use std::cell::RefCell; -use actix_identity::Identity; use actix_web::http::header::ContentType; use tera::Context; -use crate::api::v1::RedirectQuery; use crate::ctx::api::v1::auth::Login as LoginPayload; use crate::pages::errors::*; use crate::settings::Settings; @@ -63,6 +61,7 @@ impl Home { } #[actix_web_codegen_const_routes::get(path = "PAGES.dash.home")] +#[tracing::instrument(name = "Dashboard homepage", skip(ctx))] pub async fn get_home(ctx: AppCtx) -> impl Responder { let home = Home::page(&ctx.settings); let html = ContentType::html(); diff --git a/src/pages/mod.rs b/src/pages/mod.rs index 2f2654b..6c817eb 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -165,6 +165,7 @@ impl Home { } #[actix_web_codegen_const_routes::get(path = "PAGES.home")] +#[tracing::instrument(name = "Dashboard homepage", skip(id, ctx))] pub async fn home(ctx: AppCtx, id: Identity) -> impl Responder { if id.identity().is_none() { let home = Home::page(&ctx.settings); diff --git a/src/serve.rs b/src/serve.rs index 63619dc..1385af0 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -34,6 +34,7 @@ pub mod routes { } #[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.serve.catch_all")] +#[tracing::instrument(name = "Serve webpages", skip(req, ctx))] async fn index(req: HttpRequest, ctx: AppCtx) -> ServiceResult { let c = req.connection_info(); let mut host = c.host();