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

Make GrpcErrorsAsFailures more flexible #189

Merged
merged 5 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 9 additions & 1 deletion examples/tonic-key-value-store/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use tokio_stream::{
use tonic::{async_trait, body::BoxBody, transport::Channel, Code, Request, Response, Status};
use tower::{BoxError, Service, ServiceBuilder};
use tower_http::{
classify::{GrpcCode, GrpcErrorsAsFailures, SharedClassifier},
compression::CompressionLayer,
decompression::DecompressionLayer,
sensitive_headers::SetSensitiveHeadersLayer,
Expand Down Expand Up @@ -165,6 +166,12 @@ async fn serve_forever(listener: TcpListener) -> Result<(), Box<dyn std::error::
// Build our tonic `Service`
let service = key_value_store_server::KeyValueStoreServer::new(ServerImpl { db, tx });

// Response classifier that doesn't consider `Ok`, `Invalid Argument`, or `Not Found` as
// failures
let classifier = GrpcErrorsAsFailures::new()
.with_success(GrpcCode::InvalidArgument)
.with_success(GrpcCode::NotFound);

// Build our middleware stack
let layer = ServiceBuilder::new()
// Set a timeout
Expand All @@ -175,7 +182,8 @@ async fn serve_forever(listener: TcpListener) -> Result<(), Box<dyn std::error::
.layer(SetSensitiveHeadersLayer::new(once(header::AUTHORIZATION)))
// Log all requests and responses
.layer(
TraceLayer::new_for_grpc().make_span_with(DefaultMakeSpan::new().include_headers(true)),
TraceLayer::new(SharedClassifier::new(classifier))
.make_span_with(DefaultMakeSpan::new().include_headers(true)),
)
.into_inner();

Expand Down
2 changes: 2 additions & 0 deletions tower-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
adding middleware from tower-http.
- Add `SetRequestId` and `PropagateRequestId` middleware ([#150])
- Add `LatencyUnit::Seconds` for formatting latencies as seconds.
- Support customizing which status codes are considered failures by `GrpcErrorsAsFailures` ([#189])

[#124]: https://github.com/tower-rs/tower-http/pull/124
[#148]: https://github.com/tower-rs/tower-http/pull/148
Expand All @@ -44,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#172]: https://github.com/tower-rs/tower-http/pull/172
[#182]: https://github.com/tower-rs/tower-http/pull/182
[#187]: https://github.com/tower-rs/tower-http/pull/187
[#189]: https://github.com/tower-rs/tower-http/pull/189

# 0.1.2 (November 13, 2021)

Expand Down
1 change: 1 addition & 0 deletions tower-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ keywords = ["io", "async", "futures", "service", "http"]
rust-version = "1.51"

[dependencies]
bitflags = "1.0"
bytes = "1"
futures-core = "0.3"
futures-util = { version = "0.3", default_features = false, features = [] }
Expand Down
Loading