-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add middleware to log the request/response payloads (#304)
It can be useful when developing an application to be able to see the request/response bodies as they are seen by the server. Add an Axum middleware to do this. Because the middleware buffers the entire request/response, it is disabled by default. If we add a mechanism to provide different defaults per environment, this middleware may be enabled in non-prod environments by default in the future.
- Loading branch information
1 parent
2d6d4ec
commit 20b5eea
Showing
8 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/service/http/middleware/tracing.rs → src/service/http/middleware/tracing/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
//! Middleware to log the request/response payloads. Logs at the debug level. | ||
use crate::app::context::AppContext; | ||
use crate::error::RoadsterResult; | ||
use crate::service::http::middleware::Middleware; | ||
use axum::body::{Body, Bytes}; | ||
use axum::extract::{FromRef, Request}; | ||
use axum::http::StatusCode; | ||
use axum::middleware::Next; | ||
use axum::response::{IntoResponse, Response}; | ||
use axum::{middleware, Router}; | ||
use http_body_util::BodyExt; | ||
use serde_derive::{Deserialize, Serialize}; | ||
use tracing::debug; | ||
use validator::Validate; | ||
|
||
#[derive(Debug, Clone, Default, Validate, Serialize, Deserialize)] | ||
#[serde(rename_all = "kebab-case", default)] | ||
#[non_exhaustive] | ||
pub struct ReqResLoggingConfig {} | ||
|
||
pub struct RequestLoggingMiddleware; | ||
impl<S> Middleware<S> for RequestLoggingMiddleware | ||
where | ||
S: Clone + Send + Sync + 'static, | ||
AppContext: FromRef<S>, | ||
{ | ||
fn name(&self) -> String { | ||
"request-response-logging".to_string() | ||
} | ||
|
||
fn enabled(&self, state: &S) -> bool { | ||
AppContext::from_ref(state) | ||
.config() | ||
.service | ||
.http | ||
.custom | ||
.middleware | ||
.request_response_logging | ||
.common | ||
.enabled(state) | ||
} | ||
|
||
fn priority(&self, state: &S) -> i32 { | ||
AppContext::from_ref(state) | ||
.config() | ||
.service | ||
.http | ||
.custom | ||
.middleware | ||
.request_response_logging | ||
.common | ||
.priority | ||
} | ||
|
||
fn install(&self, router: Router, _state: &S) -> RoadsterResult<Router> { | ||
let router = router.layer(middleware::from_fn(log_req_res_bodies)); | ||
|
||
Ok(router) | ||
} | ||
} | ||
|
||
// https://github.com/tokio-rs/axum/blob/main/examples/consume-body-in-extractor-or-middleware/src/main.rs | ||
async fn log_req_res_bodies(request: Request, next: Next) -> Result<impl IntoResponse, Response> { | ||
// Log the request body | ||
let (parts, body) = request.into_parts(); | ||
let bytes = log_body(body, "request").await?; | ||
let request = Request::from_parts(parts, Body::from(bytes)); | ||
|
||
// Handle the request | ||
let response = next.run(request).await; | ||
|
||
// Log the response body | ||
let (parts, body) = response.into_parts(); | ||
let bytes = log_body(body, "response").await?; | ||
let response = Response::from_parts(parts, Body::from(bytes)); | ||
|
||
// Return the response | ||
Ok(response) | ||
} | ||
|
||
async fn log_body(body: Body, msg: &str) -> Result<Bytes, Response> { | ||
// This only works if the body is not a long-running stream | ||
let bytes = body | ||
.collect() | ||
.await | ||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())? | ||
.to_bytes(); | ||
|
||
debug!(body = ?bytes, msg); | ||
|
||
Ok(bytes) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::config::app_config::AppConfig; | ||
use rstest::rstest; | ||
|
||
#[rstest] | ||
#[case(false, Some(true), true)] | ||
#[case(false, Some(false), false)] | ||
#[cfg_attr(coverage_nightly, coverage(off))] | ||
fn enabled( | ||
#[case] default_enable: bool, | ||
#[case] enable: Option<bool>, | ||
#[case] expected_enabled: bool, | ||
) { | ||
// Arrange | ||
let mut config = AppConfig::test(None).unwrap(); | ||
config.service.http.custom.middleware.default_enable = default_enable; | ||
config | ||
.service | ||
.http | ||
.custom | ||
.middleware | ||
.request_response_logging | ||
.common | ||
.enable = enable; | ||
|
||
let context = AppContext::test(Some(config), None, None).unwrap(); | ||
|
||
let middleware = RequestLoggingMiddleware; | ||
|
||
// Act/Assert | ||
assert_eq!(middleware.enabled(&context), expected_enabled); | ||
} | ||
|
||
#[rstest] | ||
#[case(None, 0)] | ||
#[case(Some(1234), 1234)] | ||
#[cfg_attr(coverage_nightly, coverage(off))] | ||
fn priority(#[case] override_priority: Option<i32>, #[case] expected_priority: i32) { | ||
// Arrange | ||
let mut config = AppConfig::test(None).unwrap(); | ||
if let Some(priority) = override_priority { | ||
config | ||
.service | ||
.http | ||
.custom | ||
.middleware | ||
.request_response_logging | ||
.common | ||
.priority = priority; | ||
} | ||
|
||
let context = AppContext::test(Some(config), None, None).unwrap(); | ||
|
||
let middleware = RequestLoggingMiddleware; | ||
|
||
// Act/Assert | ||
assert_eq!(middleware.priority(&context), expected_priority); | ||
} | ||
} |