From 8e3d48d7eb108e4191228251e00e213abab34811 Mon Sep 17 00:00:00 2001 From: Louis-Marie Baer Date: Thu, 30 May 2024 17:57:56 +0200 Subject: [PATCH] feat: add basic tracing --- Cargo.toml | 1 + src/main.rs | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7d43403..1d86455 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,3 +33,4 @@ confy = "0.6" url = {version="2.5.0", features=["serde"]} serde = { version = "1", features = ["derive"]} serde_json = "1" +tracing = "0.1" diff --git a/src/main.rs b/src/main.rs index 04004a7..a0e784c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use axum::{ use reqwest::{Client, Url}; use serde::{Deserialize, Serialize}; use serde_json::Value; +use tracing::{debug, info, warn}; #[derive(Serialize, Deserialize)] struct Config { listen_address: Ipv4Addr, @@ -35,7 +36,9 @@ impl Default for Config { #[tokio::main] async fn main() -> Result<(), Box> { + info!("reading configuration file..."); let config: Config = confy::load_path("/etc/api-formatter/config.toml").expect("check that the directory /etc/api-formatter exist and that the program is ran with sufficient permission"); + info!("creating client"); let state = AppState { client: Client::new(), }; @@ -43,25 +46,32 @@ async fn main() -> Result<(), Box> { let listener = tokio::net::TcpListener::bind(format!("{}:{}", config.listen_address, config.listen_port)) .await?; + info!("Listening to {}", config.listen_address); axum::serve(listener, route.into_make_service()).await?; Ok(()) } async fn handler(State(state): State, request: Request) -> impl IntoResponse { - match state + info!("new request ! "); + debug!("The request received {:?}", request); + let req = state .client .request(request.method().to_owned(), request.uri().to_string()) .headers(request.headers().to_owned()) .body(to_bytes(request.into_body(), usize::MAX).await.unwrap()) .send() - .await - { + .await; + debug!("The request that will be make by the proxy {:?}", req); + match req { Ok(rep) => format_resp(rep).await.into_response(), - Err(err) => ( - axum::http::StatusCode::INTERNAL_SERVER_ERROR, - err.to_string(), - ) - .into_response(), + Err(err) => { + warn!("request had an error: {}", err); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + err.to_string(), + ) + .into_response() + } } } @@ -69,6 +79,7 @@ async fn format_resp(rep: reqwest::Response) -> impl IntoResponse { let headers = rep.headers().to_owned(); let status = rep.status(); if let Ok(mut body) = rep.json::().await { + debug!("pretty formatting the response: {}", body); format_json_fields_into_readable_output(&mut body); (status, headers, body.to_string()).into_response() } else {