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 SetRequestId and PropagateRequestId middleware #150

Merged
merged 13 commits into from
Nov 22, 2021
4 changes: 3 additions & 1 deletion tower-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
be used instead. ([#170])
- Add `ServiceBuilderExt` which adds methods to `tower::ServiceBuilder` for
adding middleware from tower-http.
- Add `SetRequestId` and `PropagateRequestId` middleware ([#150])

[#124]: https://github.com/tower-rs/tower-http/pull/124
[#148]: https://github.com/tower-rs/tower-http/pull/148
[#150]: https://github.com/tower-rs/tower-http/pull/150
[#156]: https://github.com/tower-rs/tower-http/pull/156
[#166]: https://github.com/tower-rs/tower-http/pull/166
[#148]: https://github.com/tower-rs/tower-http/pull/148
[#170]: https://github.com/tower-rs/tower-http/pull/170

# 0.1.2 (November 13, 2021)
Expand Down
5 changes: 4 additions & 1 deletion tower-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ base64 = { version = "0.13", optional = true }
iri-string = { version = "0.4", optional = true }
mime = { version = "0.3", optional = true, default_features = false }
mime_guess = { version = "2", optional = true, default_features = false }
percent-encoding = { version = "2.1.0", optional = true }
tokio = { version = "1", optional = true, default_features = false }
tokio-util = { version = "0.6", optional = true, default_features = false, features = ["io"] }
tower = { version = "0.4.1", optional = true }
tracing = { version = "0.1", default_features = false, optional = true }
percent-encoding = { version = "2.1.0", optional = true }

[dev-dependencies]
bytes = "1"
Expand All @@ -43,6 +43,7 @@ once_cell = "1"
tokio = { version = "1", features = ["full"] }
tower = { version = "0.4.10", features = ["buffer", "util", "retry", "make", "timeout"] }
tracing-subscriber = "0.2"
uuid = { version = "0.8", features = ["v4"] }

[features]
default = []
Expand All @@ -59,6 +60,7 @@ full = [
"metrics",
"propagate-header",
"redirect",
"request-id",
"sensitive-headers",
"set-header",
"trace",
Expand All @@ -75,6 +77,7 @@ map-response-body = []
metrics = ["tokio/time"]
propagate-header = []
redirect = []
request-id = []
sensitive-headers = []
set-header = []
trace = ["tracing"]
Expand Down
77 changes: 76 additions & 1 deletion tower-http/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use tower_layer::Stack;
/// # service.ready().await.unwrap().call(Request::new(Body::empty())).await.unwrap();
/// # }
/// ```
pub trait ServiceBuilderExt<L>: crate::sealed::Sealed<L> {
pub trait ServiceBuilderExt<L>: crate::sealed::Sealed<L> + Sized {
/// Propagate a header from the request to the response.
///
/// See [`tower_http::propagate_header`] for more details.
Expand Down Expand Up @@ -302,6 +302,58 @@ pub trait ServiceBuilderExt<L>: crate::sealed::Sealed<L> {
header_name: HeaderName,
make: M,
) -> ServiceBuilder<Stack<crate::set_header::SetResponseHeaderLayer<M>, L>>;

/// Add request id header and extension.
///
/// See [`tower_http::request_id`] for more details.
///
/// [`tower_http::request_id`]: crate::request_id
#[cfg(feature = "request-id")]
#[cfg_attr(docsrs, doc(cfg(feature = "request-id")))]
fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> ServiceBuilder<Stack<crate::request_id::SetRequestIdLayer<M>, L>>
where
M: crate::request_id::MakeRequestId;

/// TODO
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "request-id")]
#[cfg_attr(docsrs, doc(cfg(feature = "request-id")))]
fn set_x_request_id<M>(
self,
make_request_id: M,
) -> ServiceBuilder<Stack<crate::request_id::SetRequestIdLayer<M>, L>>
where
M: crate::request_id::MakeRequestId,
{
self.set_request_id(
HeaderName::from_static(crate::request_id::X_REQUEST_ID),
make_request_id,
)
}

/// Propgate request ids from requests to responses.
///
/// See [`tower_http::request_id`] for more details.
///
/// [`tower_http::request_id`]: crate::request_id
#[cfg(feature = "request-id")]
#[cfg_attr(docsrs, doc(cfg(feature = "request-id")))]
fn propagate_request_id(
self,
header_name: HeaderName,
) -> ServiceBuilder<Stack<crate::request_id::PropagateRequestIdLayer, L>>;

/// TODO
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "request-id")]
#[cfg_attr(docsrs, doc(cfg(feature = "request-id")))]
fn propagate_x_request_id(
self,
) -> ServiceBuilder<Stack<crate::request_id::PropagateRequestIdLayer, L>> {
self.propagate_request_id(HeaderName::from_static(crate::request_id::X_REQUEST_ID))
}
}

impl<L> crate::sealed::Sealed<L> for ServiceBuilder<L> {}
Expand Down Expand Up @@ -502,4 +554,27 @@ impl<L> ServiceBuilderExt<L> for ServiceBuilder<L> {
make,
))
}

#[cfg(feature = "request-id")]
fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> ServiceBuilder<Stack<crate::request_id::SetRequestIdLayer<M>, L>>
where
M: crate::request_id::MakeRequestId,
{
self.layer(crate::request_id::SetRequestIdLayer::new(
header_name,
make_request_id,
))
}

#[cfg(feature = "request-id")]
fn propagate_request_id(
self,
header_name: HeaderName,
) -> ServiceBuilder<Stack<crate::request_id::PropagateRequestIdLayer, L>> {
self.layer(crate::request_id::PropagateRequestIdLayer::new(header_name))
}
}
4 changes: 4 additions & 0 deletions tower-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ pub mod metrics;
#[cfg_attr(docsrs, doc(cfg(feature = "cors")))]
pub mod cors;

#[cfg(feature = "request-id")]
#[cfg_attr(docsrs, doc(cfg(feature = "request-id")))]
pub mod request_id;

pub mod classify;
pub mod services;

Expand Down
Loading