Skip to content

Commit

Permalink
Add and implement HttpMakeClassifier and GrpcMakeClassifier type aliases
Browse files Browse the repository at this point in the history
Fixes: #233
  • Loading branch information
TofuWaffles authored and jplatte committed Jan 14, 2024
1 parent f47f2f6 commit 469bdac
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
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
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

0 comments on commit 469bdac

Please sign in to comment.