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
Prev Previous commit
Next Next commit
change things around a bit
  • Loading branch information
davidpdrsn committed Nov 19, 2021
commit d3e9d3fd28ad360824ec24e5da315fe76a63cf7a
1 change: 0 additions & 1 deletion tower-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ 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 }
uuid = { version = "0.8", features = ["v4"], optional = true }

[dev-dependencies]
bytes = "1"
Expand Down
69 changes: 68 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,53 @@ 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>>;

/// TODO
#[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>> {
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
#[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 +549,24 @@ 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>> {
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))
}
}
Loading