feat: add tracing log identifier to each HTTP route handler

This commit is contained in:
Aravinth Manivannan 2022-11-11 15:37:33 +05:30
parent 0b2db58483
commit 3a961bc524
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
11 changed files with 38 additions and 4 deletions

View File

@ -58,6 +58,7 @@ pub struct Username {
path = "crate::V1_API_ROUTES.account.update_username", path = "crate::V1_API_ROUTES.account.update_username",
wrap = "super::get_auth_middleware()" wrap = "super::get_auth_middleware()"
)] )]
#[tracing::instrument(name = "Update username", skip(ctx, payload, id))]
async fn set_username( async fn set_username(
id: Identity, id: Identity,
payload: web::Json<Username>, payload: web::Json<Username>,
@ -74,6 +75,7 @@ async fn set_username(
} }
#[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.account.username_exists")] #[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( async fn username_exists(
payload: web::Json<AccountCheckPayload>, payload: web::Json<AccountCheckPayload>,
ctx: AppCtx, ctx: AppCtx,
@ -82,6 +84,7 @@ async fn username_exists(
} }
#[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.account.email_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( pub async fn email_exists(
payload: web::Json<AccountCheckPayload>, payload: web::Json<AccountCheckPayload>,
ctx: AppCtx, ctx: AppCtx,
@ -94,6 +97,7 @@ pub async fn email_exists(
path = "crate::V1_API_ROUTES.account.update_email", path = "crate::V1_API_ROUTES.account.update_email",
wrap = "super::get_auth_middleware()" wrap = "super::get_auth_middleware()"
)] )]
#[tracing::instrument(name = "Update email", skip(ctx, payload, id))]
async fn set_email( async fn set_email(
id: Identity, id: Identity,
payload: web::Json<Email>, payload: web::Json<Email>,
@ -108,6 +112,7 @@ async fn set_email(
path = "crate::V1_API_ROUTES.account.delete", path = "crate::V1_API_ROUTES.account.delete",
wrap = "super::get_auth_middleware()" wrap = "super::get_auth_middleware()"
)] )]
#[tracing::instrument(name = "Delete account", skip(ctx, payload, id))]
async fn delete_account( async fn delete_account(
id: Identity, id: Identity,
payload: web::Json<Password>, payload: web::Json<Password>,
@ -124,6 +129,7 @@ async fn delete_account(
path = "crate::V1_API_ROUTES.account.update_password", path = "crate::V1_API_ROUTES.account.update_password",
wrap = "super::get_auth_middleware()" wrap = "super::get_auth_middleware()"
)] )]
#[tracing::instrument(name = "Update user password", skip(ctx, payload, id))]
async fn update_user_password( async fn update_user_password(
id: Identity, id: Identity,
ctx: AppCtx, ctx: AppCtx,

View File

@ -30,12 +30,14 @@ pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(signout); cfg.service(signout);
} }
#[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.auth.register")] #[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<Register>, ctx: AppCtx) -> ServiceResult<impl Responder> { async fn register(payload: web::Json<Register>, ctx: AppCtx) -> ServiceResult<impl Responder> {
ctx.register(&payload).await?; ctx.register(&payload).await?;
Ok(HttpResponse::Ok()) Ok(HttpResponse::Ok())
} }
#[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.auth.login")] #[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( async fn login(
id: Identity, id: Identity,
payload: web::Json<Login>, payload: web::Json<Login>,
@ -59,6 +61,7 @@ async fn login(
path = "crate::V1_API_ROUTES.auth.logout", path = "crate::V1_API_ROUTES.auth.logout",
wrap = "super::get_auth_middleware()" wrap = "super::get_auth_middleware()"
)] )]
#[tracing::instrument(name = "Sign out", skip(id))]
async fn signout(id: Identity) -> impl Responder { async fn signout(id: Identity) -> impl Responder {
use actix_auth_middleware::GetLoginRoute; use actix_auth_middleware::GetLoginRoute;

17
src/api/v1/pages.rs Normal file
View File

@ -0,0 +1,17 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/

View File

@ -45,6 +45,7 @@ pub struct DeployEvent {
} }
#[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.deploy.update")] #[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<DeployEvent>, ctx: AppCtx) -> ServiceResult<impl Responder> { async fn update(payload: web::Json<DeployEvent>, ctx: AppCtx) -> ServiceResult<impl Responder> {
let payload = payload.into_inner(); let payload = payload.into_inner();
ctx.update_site(&payload.secret, Some(payload.branch)) 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")] #[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( async fn deploy_info(
payload: web::Json<DeploySecret>, payload: web::Json<DeploySecret>,
ctx: AppCtx, ctx: AppCtx,
) -> ServiceResult<impl Responder> { ) -> ServiceResult<impl Responder> {
if let Ok(page) = ctx.db.get_site_from_secret(&payload.secret).await { 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))?; let resp = DeployInfo::from_page(&Page::from_site(&ctx.settings, page))?;
Ok(HttpResponse::Ok().json(resp)) Ok(HttpResponse::Ok().json(resp))
} else { } else {

View File

@ -44,6 +44,7 @@ pub mod routes {
/// emits build details of the binary /// emits build details of the binary
#[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.meta.build_details")] #[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 { async fn build_details(ctx: AppCtx) -> impl Responder {
let build = BuildDetails { let build = BuildDetails {
version: VERSION, version: VERSION,
@ -61,6 +62,7 @@ pub struct Health {
/// checks all components of the system /// checks all components of the system
#[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.meta.health")] #[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 { async fn health(ctx: crate::AppCtx) -> impl Responder {
let res = Health { let res = Health {
db: ctx.db.ping().await, db: ctx.db.ping().await,

View File

@ -61,6 +61,7 @@ impl Login {
} }
#[actix_web_codegen_const_routes::get(path = "PAGES.auth.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 { pub async fn get_login(ctx: AppCtx) -> impl Responder {
let login = Login::page(&ctx.settings); let login = Login::page(&ctx.settings);
let html = ContentType::html(); 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")] #[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( pub async fn login_submit(
id: Identity, id: Identity,
payload: web::Form<LoginPayload>, payload: web::Form<LoginPayload>,

View File

@ -42,13 +42,13 @@ pub fn services(cfg: &mut web::ServiceConfig) {
path = "PAGES.auth.logout", path = "PAGES.auth.logout",
wrap = "super::get_auth_middleware()" wrap = "super::get_auth_middleware()"
)] )]
#[tracing::instrument(name = "Sign out", skip(id))]
async fn signout(id: Identity) -> impl Responder { async fn signout(id: Identity) -> impl Responder {
use actix_auth_middleware::GetLoginRoute; use actix_auth_middleware::GetLoginRoute;
if id.identity().is_some() { if id.identity().is_some() {
id.forget(); id.forget();
} }
println!("received signout");
HttpResponse::Found() HttpResponse::Found()
.append_header((http::header::LOCATION, PAGES.get_login_route(None))) .append_header((http::header::LOCATION, PAGES.get_login_route(None)))
.finish() .finish()

View File

@ -58,6 +58,7 @@ impl Register {
} }
#[actix_web_codegen_const_routes::get(path = "PAGES.auth.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 { pub async fn get_register(ctx: AppCtx) -> impl Responder {
let login = Register::page(&ctx.settings); let login = Register::page(&ctx.settings);
let html = ContentType::html(); 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")] #[actix_web_codegen_const_routes::post(path = "PAGES.auth.register")]
#[tracing::instrument(name = "Process web UI registration", skip(ctx))]
pub async fn register_submit( pub async fn register_submit(
payload: web::Form<RegisterPayload>, payload: web::Form<RegisterPayload>,
ctx: AppCtx, ctx: AppCtx,

View File

@ -16,11 +16,9 @@
*/ */
use std::cell::RefCell; use std::cell::RefCell;
use actix_identity::Identity;
use actix_web::http::header::ContentType; use actix_web::http::header::ContentType;
use tera::Context; use tera::Context;
use crate::api::v1::RedirectQuery;
use crate::ctx::api::v1::auth::Login as LoginPayload; use crate::ctx::api::v1::auth::Login as LoginPayload;
use crate::pages::errors::*; use crate::pages::errors::*;
use crate::settings::Settings; use crate::settings::Settings;
@ -63,6 +61,7 @@ impl Home {
} }
#[actix_web_codegen_const_routes::get(path = "PAGES.dash.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 { pub async fn get_home(ctx: AppCtx) -> impl Responder {
let home = Home::page(&ctx.settings); let home = Home::page(&ctx.settings);
let html = ContentType::html(); let html = ContentType::html();

View File

@ -165,6 +165,7 @@ impl Home {
} }
#[actix_web_codegen_const_routes::get(path = "PAGES.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 { pub async fn home(ctx: AppCtx, id: Identity) -> impl Responder {
if id.identity().is_none() { if id.identity().is_none() {
let home = Home::page(&ctx.settings); let home = Home::page(&ctx.settings);

View File

@ -34,6 +34,7 @@ pub mod routes {
} }
#[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.serve.catch_all")] #[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<impl Responder> { async fn index(req: HttpRequest, ctx: AppCtx) -> ServiceResult<impl Responder> {
let c = req.connection_info(); let c = req.connection_info();
let mut host = c.host(); let mut host = c.host();