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

[trace] Add default generic parameters to ResponseBody and ResponseFuture #455

Merged
merged 3 commits into from
Jan 14, 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: 4 additions & 1 deletion tower-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Added

- None.
- **trace:** Add default generic parameters for `ResponseBody` and `ResponseFuture` ([#455])
- **trace:** Add type aliases `HttpMakeClassifier` and `GrpcMakeClassifier` ([#455])

## Changed

Expand All @@ -23,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- None.

[#455]: https://github.com/tower-rs/tower-http/pull/455

# 0.5.0 (November 21, 2023)

## Changed
Expand Down
13 changes: 4 additions & 9 deletions tower-http/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use tower::ServiceBuilder;

#[cfg(feature = "trace")]
use crate::classify::{GrpcErrorsAsFailures, ServerErrorsAsFailures, SharedClassifier};

#[allow(unused_imports)]
use http::header::HeaderName;
#[allow(unused_imports)]
Expand Down Expand Up @@ -126,7 +123,7 @@ pub trait ServiceBuilderExt<L>: crate::sealed::Sealed<L> + Sized {
#[cfg(feature = "trace")]
fn trace_for_http(
self,
) -> ServiceBuilder<Stack<crate::trace::TraceLayer<SharedClassifier<ServerErrorsAsFailures>>, L>>;
) -> ServiceBuilder<Stack<crate::trace::TraceLayer<crate::trace::HttpMakeClassifier>, L>>;

/// High level tracing that classifies responses using gRPC headers.
///
Expand All @@ -140,7 +137,7 @@ pub trait ServiceBuilderExt<L>: crate::sealed::Sealed<L> + Sized {
#[cfg(feature = "trace")]
fn trace_for_grpc(
self,
) -> ServiceBuilder<Stack<crate::trace::TraceLayer<SharedClassifier<GrpcErrorsAsFailures>>, L>>;
) -> ServiceBuilder<Stack<crate::trace::TraceLayer<crate::trace::GrpcMakeClassifier>, L>>;

/// Follow redirect resposes using the [`Standard`] policy.
///
Expand Down Expand Up @@ -427,16 +424,14 @@ impl<L> ServiceBuilderExt<L> for ServiceBuilder<L> {
#[cfg(feature = "trace")]
fn trace_for_http(
self,
) -> ServiceBuilder<Stack<crate::trace::TraceLayer<SharedClassifier<ServerErrorsAsFailures>>, L>>
{
) -> ServiceBuilder<Stack<crate::trace::TraceLayer<crate::trace::HttpMakeClassifier>, L>> {
self.layer(crate::trace::TraceLayer::new_for_http())
}

#[cfg(feature = "trace")]
fn trace_for_grpc(
self,
) -> ServiceBuilder<Stack<crate::trace::TraceLayer<SharedClassifier<GrpcErrorsAsFailures>>, L>>
{
) -> ServiceBuilder<Stack<crate::trace::TraceLayer<crate::trace::GrpcMakeClassifier>, L>> {
self.layer(crate::trace::TraceLayer::new_for_grpc())
}

Expand Down
4 changes: 2 additions & 2 deletions tower-http/src/trace/body.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{OnBodyChunk, OnEos, OnFailure};
use super::{DefaultOnBodyChunk, DefaultOnEos, DefaultOnFailure, OnBodyChunk, OnEos, OnFailure};
use crate::classify::ClassifyEos;
use http_body::{Body, Frame};
use pin_project_lite::pin_project;
Expand All @@ -14,7 +14,7 @@ pin_project! {
/// Response body for [`Trace`].
///
/// [`Trace`]: super::Trace
pub struct ResponseBody<B, C, OnBodyChunk, OnEos, OnFailure> {
pub struct ResponseBody<B, C, OnBodyChunk = DefaultOnBodyChunk, OnEos = DefaultOnEos, OnFailure = DefaultOnFailure> {
#[pin]
pub(crate) inner: B,
pub(crate) classify_eos: Option<C>,
Expand Down
7 changes: 5 additions & 2 deletions tower-http/src/trace/future.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use super::{OnBodyChunk, OnEos, OnFailure, OnResponse, ResponseBody};
use super::{
DefaultOnBodyChunk, DefaultOnEos, DefaultOnFailure, DefaultOnResponse, OnBodyChunk, OnEos,
OnFailure, OnResponse, ResponseBody,
};
use crate::classify::{ClassifiedResponse, ClassifyResponse};
use http::Response;
use http_body::Body;
Expand All @@ -15,7 +18,7 @@ pin_project! {
/// Response future for [`Trace`].
///
/// [`Trace`]: super::Trace
pub struct ResponseFuture<F, C, OnResponse, OnBodyChunk, OnEos, OnFailure> {
pub struct ResponseFuture<F, C, OnResponse = DefaultOnResponse, OnBodyChunk = DefaultOnBodyChunk, OnEos = DefaultOnEos, OnFailure = DefaultOnFailure> {
#[pin]
pub(crate) inner: F,
pub(crate) span: Span,
Expand Down
6 changes: 3 additions & 3 deletions tower-http/src/trace/layer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
DefaultMakeSpan, DefaultOnBodyChunk, DefaultOnEos, DefaultOnFailure, DefaultOnRequest,
DefaultOnResponse, Trace,
DefaultOnResponse, GrpcMakeClassifier, HttpMakeClassifier, Trace,
};
use crate::classify::{
GrpcErrorsAsFailures, MakeClassifier, ServerErrorsAsFailures, SharedClassifier,
Expand Down Expand Up @@ -176,7 +176,7 @@ impl<M, MakeSpan, OnRequest, OnResponse, OnBodyChunk, OnEos, OnFailure>
}
}

impl TraceLayer<SharedClassifier<ServerErrorsAsFailures>> {
impl TraceLayer<HttpMakeClassifier> {
/// Create a new [`TraceLayer`] using [`ServerErrorsAsFailures`] which supports classifying
/// regular HTTP responses based on the status code.
pub fn new_for_http() -> Self {
Expand All @@ -192,7 +192,7 @@ impl TraceLayer<SharedClassifier<ServerErrorsAsFailures>> {
}
}

impl TraceLayer<SharedClassifier<GrpcErrorsAsFailures>> {
impl TraceLayer<GrpcMakeClassifier> {
/// Create a new [`TraceLayer`] using [`GrpcErrorsAsFailures`] which supports classifying
/// gRPC responses and streams based on the `grpc-status` header.
pub fn new_for_grpc() -> Self {
Expand Down
11 changes: 10 additions & 1 deletion tower-http/src/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,16 @@ pub use self::{
on_response::{DefaultOnResponse, OnResponse},
service::Trace,
};
use crate::LatencyUnit;
use crate::{
classify::{GrpcErrorsAsFailures, ServerErrorsAsFailures, SharedClassifier},
LatencyUnit,
};

/// MakeClassifier for HTTP requests.
pub type HttpMakeClassifier = SharedClassifier<ServerErrorsAsFailures>;

/// MakeClassifier for gRPC requests.
pub type GrpcMakeClassifier = SharedClassifier<GrpcErrorsAsFailures>;

macro_rules! event_dynamic_lvl {
( $(target: $target:expr,)? $(parent: $parent:expr,)? $lvl:expr, $($tt:tt)* ) => {
Expand Down
8 changes: 4 additions & 4 deletions tower-http/src/trace/service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{
DefaultMakeSpan, DefaultOnBodyChunk, DefaultOnEos, DefaultOnFailure, DefaultOnRequest,
DefaultOnResponse, MakeSpan, OnBodyChunk, OnEos, OnFailure, OnRequest, OnResponse,
ResponseBody, ResponseFuture, TraceLayer,
DefaultOnResponse, GrpcMakeClassifier, HttpMakeClassifier, MakeSpan, OnBodyChunk, OnEos,
OnFailure, OnRequest, OnResponse, ResponseBody, ResponseFuture, TraceLayer,
};
use crate::classify::{
GrpcErrorsAsFailures, MakeClassifier, ServerErrorsAsFailures, SharedClassifier,
Expand Down Expand Up @@ -207,7 +207,7 @@ impl<S, M, MakeSpan, OnRequest, OnResponse, OnBodyChunk, OnEos, OnFailure>
impl<S>
Trace<
S,
SharedClassifier<ServerErrorsAsFailures>,
HttpMakeClassifier,
DefaultMakeSpan,
DefaultOnRequest,
DefaultOnResponse,
Expand Down Expand Up @@ -235,7 +235,7 @@ impl<S>
impl<S>
Trace<
S,
SharedClassifier<GrpcErrorsAsFailures>,
GrpcMakeClassifier,
DefaultMakeSpan,
DefaultOnRequest,
DefaultOnResponse,
Expand Down