Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logger to axum router #773

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ tokio = { workspace = true }
axum = { version = "0.7.4", features = ["ws"] }
futures-util = { workspace = true }
wgpu = { workspace = true }
http-body-util = "0.1.2"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
shared_memory = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod config;
pub mod logger;
pub mod middleware;
pub mod routes;
pub mod server;
pub mod state;
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use log::info;

mod config;
mod logger;
mod middleware;
mod routes;
mod server;
mod state;
Expand Down
77 changes: 77 additions & 0 deletions src/middleware.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use axum::{
body::Body,
extract::Request,
http::StatusCode,
middleware::Next,
response::{IntoResponse, Response},
};
use core::str;
use http_body_util::BodyExt;
use serde_json::Value;
use tracing::{enabled, trace, Level};

pub async fn body_logger_middleware(
request: Request,
next: Next,
) -> Result<impl IntoResponse, Response> {
if !enabled!(target: "live_compositor::log_request_body", Level::TRACE) {
return Ok(next.run(request).await);
}
let request = buffer_request_body(request).await?;
let response = next.run(request).await;
let response = buffer_response_body(response).await?;

Ok(response)
}

async fn buffer_request_body(request: Request) -> Result<Request, Response> {
let (parts, body) = request.into_parts();

let bytes = body
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if logging is enabled then do not read/restore that body

.collect()
.await
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())?
.to_bytes();

match serde_json::from_slice::<Value>(&bytes) {
Ok(body_json) => {
trace!(target: "live_compositor::log_request_body", method = ?parts.method, path = ?parts.uri, "Request body: {}", body_json);
}
Err(_) => match str::from_utf8(&bytes) {
Ok(body_str) => {
trace!(target: "live_compositor::log_request_body", method = ?parts.method, path = ?parts.uri, "Request body: {}", body_str);
}
Err(_) => {
trace!(target: "live_compositor::log_request_body", method = ?parts.method, path = ?parts.uri, "Request body: {:?}", bytes);
}
},
}

Ok(Request::from_parts(parts, Body::from(bytes)))
}

async fn buffer_response_body(response: Response) -> Result<Response, Response> {
let (parts, body) = response.into_parts();

let bytes = body
.collect()
.await
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())?
.to_bytes();

match serde_json::from_slice::<Value>(&bytes) {
Ok(body_json) => {
trace!(target: "live_compositor::log_request_body", status=?parts.status, "Response body: {}", body_json);
}
Err(_) => match str::from_utf8(&bytes) {
Ok(body_str) => {
trace!(target: "live_compositor::log_request_body", status=?parts.status, "Response body: {}", body_str);
}
Err(_) => {
trace!(target: "live_compositor::log_request_body", status=?parts.status, "Response body: {:?}", bytes);
}
},
}

Ok(Response::from_parts(parts, Body::from(bytes)))
}
3 changes: 3 additions & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use axum::{
async_trait,
extract::{rejection::JsonRejection, ws::WebSocketUpgrade, FromRequest, Request, State},
http::StatusCode,
middleware,
response::IntoResponse,
routing::{get, post},
Router,
Expand All @@ -17,6 +18,7 @@ use self::{
update_output::handle_keyframe_request, update_output::handle_output_update,
ws::handle_ws_upgrade,
};
use crate::middleware::body_logger_middleware;

mod register_request;
mod unregister_request;
Expand Down Expand Up @@ -75,6 +77,7 @@ pub fn routes(state: ApiState) -> Router {
"instance_id": state.config.instance_id
}))),
)
.layer(middleware::from_fn(body_logger_middleware))
.with_state(state)
}

Expand Down