Skip to content

Commit

Permalink
feat: Allow configuring which req/res body content types to log (#407)
Browse files Browse the repository at this point in the history
Some req/res body content types are more useful to log than others. For
example, it's not generally helpful to log `image/x-icon` or
`text/javascript`. However, it can still be very useful for debugging to
log things like `application/json` or
`application/x-www-form-urlencoded`.

Allow configuring which content types to allow to be logged. By default,
all content types are allowed. Currently, only exact matches of content
types are supported, so using `text/*` will not match `text/plain`, for
example.

Closes #390
  • Loading branch information
spencewenski authored Oct 11, 2024
1 parent f8d2a22 commit 53d7f02
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 22 deletions.
15 changes: 10 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rust-version = "1.74.1"

[features]
default = ["sidekiq", "db-sql", "open-api", "jwt-ietf", "cli", "otel"]
http = ["dep:axum-extra", "dep:tower", "dep:tower-http"]
http = ["dep:axum-extra", "dep:tower", "dep:tower-http", "dep:http-body-util", "dep:mime"]
open-api = ["http", "dep:aide", "dep:schemars"]
sidekiq = ["dep:rusty-sidekiq", "dep:bb8", "dep:num_cpus"]
db-sql = ["dep:sea-orm", "dep:sea-orm-migration"]
Expand Down Expand Up @@ -54,12 +54,13 @@ tracing-opentelemetry = { version = "0.27.0", features = ["metrics"], optional =
# `axum` is not optional because we use the `FromRef` trait pretty extensively, even in parts of
# the code that wouldn't otherwise need `axum`.
axum = { workspace = true, features = ["macros"] }
axum-extra = { version = "0.9.0", features = ["typed-header", "cookie"], optional = true }
tower = { version = "0.5.0", optional = true }
tower-http = { version = "0.6.0", features = ["trace", "timeout", "request-id", "util", "normalize-path", "sensitive-headers", "catch-panic", "compression-full", "decompression-full", "limit", "cors"], optional = true }
axum-extra = { workspace = true, features = ["typed-header", "cookie"], optional = true }
tower = { workspace = true, optional = true }
tower-http = { workspace = true, features = ["trace", "timeout", "request-id", "util", "normalize-path", "sensitive-headers", "catch-panic", "compression-full", "decompression-full", "limit", "cors"], optional = true }
aide = { workspace = true, features = ["axum", "redoc", "scalar", "macros"], optional = true }
schemars = { workspace = true, optional = true }
http-body-util = "0.1.0"
http-body-util = { version = "0.1.0", optional = true }
mime = { workspace = true, optional = true }

# DB
sea-orm = { workspace = true, features = ["debug-print", "runtime-tokio-rustls", "sqlx-postgres", "macros"], optional = true }
Expand Down Expand Up @@ -138,7 +139,11 @@ async-trait = "0.1.74"
# Controllers
aide = { version = "0.13.4", features = ["axum"] }
axum = "0.7.4"
axum-extra = "0.9.0"
tower-http = "0.6.0"
tower = "0.5.0"
schemars = "0.8.16"
mime = "0.3.17"

# DB
sea-orm = { version = "1.0.1" }
Expand Down
4 changes: 2 additions & 2 deletions examples/leptos-ssr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ leptos_axum = { version = "0.6.14", optional = true }
leptos_meta = { version = "0.6.14" }
leptos_router = { version = "0.6.14" }
leptos_config = { version = "0.6.14" }
tower = { version = "0.5.0", features = ["full"], optional = true }
tower-http = { version = "0.5", features = ["full"], optional = true }
tower = { workspace = true, features = ["full"], optional = true }
tower-http = { workspace = true, features = ["full"], optional = true }
wasm-bindgen = "=0.2.93"

# Defines a size-optimized profile for the WASM bundle in release mode
Expand Down
5 changes: 5 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ help:
# Run all of our unit tests.
test:
cargo nextest run --all-features --no-fail-fast

test-doc:
cargo test --doc --all-features --no-fail-fast

# Run all of our unit tests.
test-all: test test-doc

# Run all of our unit tests whenever files in the repo change.
test-watch:
cargo watch -s 'just test'
Expand Down
1 change: 0 additions & 1 deletion src/error/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub enum AxumError {
#[error(transparent)]
ToStrError(#[from] axum::http::header::ToStrError),

#[cfg(feature = "jwt")]
#[error(transparent)]
TypedHeaderRejection(#[from] axum_extra::typed_header::TypedHeaderRejection),

Expand Down
17 changes: 17 additions & 0 deletions src/error/mime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::error::Error;

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum MimeError {
#[error(transparent)]
FromStr(#[from] mime::FromStrError),

#[error(transparent)]
Other(#[from] Box<dyn std::error::Error + Send + Sync>),
}

impl From<mime::FromStrError> for Error {
fn from(value: mime::FromStrError) -> Self {
Self::Mime(MimeError::from(value))
}
}
8 changes: 8 additions & 0 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub mod axum;
pub mod config;
#[cfg(feature = "email")]
pub mod email;
#[cfg(feature = "http")]
pub mod mime;
pub mod other;
pub mod reqwest;
pub mod serde;
Expand All @@ -21,6 +23,8 @@ use crate::error::auth::AuthError;
use crate::error::axum::AxumError;
#[cfg(feature = "email")]
use crate::error::email::EmailError;
#[cfg(feature = "http")]
use crate::error::mime::MimeError;
use crate::error::other::OtherError;
use crate::error::reqwest::ReqwestError;
use crate::error::serde::SerdeError;
Expand Down Expand Up @@ -86,6 +90,10 @@ pub enum Error {
#[error(transparent)]
Axum(#[from] AxumError),

#[cfg(feature = "http")]
#[error(transparent)]
Mime(#[from] MimeError),

#[cfg(feature = "grpc")]
#[error(transparent)]
Tonic(#[from] TonicError),
Expand Down
Loading

0 comments on commit 53d7f02

Please sign in to comment.