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

Correctly identify gRPC requests in default on_response callback #278

Merged
merged 2 commits into from Jul 4, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion tower-http/src/trace/on_response.rs
Expand Up @@ -264,10 +264,23 @@ impl<B> OnResponse<B> for DefaultOnResponse {
fn status<B>(res: &Response<B>) -> Option<i32> {
use crate::classify::grpc_errors_as_failures::ParsedGrpcStatus;

// gRPC-over-HTTP2 uses the "application/grpc[+format]" content type, and gRPC-Web uses
// "application/grpc-web[+format]" or "application/grpc-web-text[+format]", where "format" is
// the message format, e.g. +proto, +json.
//
// So, valid grpc content types include (but are not limited to):
// - application/grpc
// - application/grpc+proto
// - application/grpc-web+proto
// - application/grpc-web-text+proto
//
// For simplicity, we simply check that the content type starts with "application/grpc".
let is_grpc = res
.headers()
.get(http::header::CONTENT_TYPE)
.map_or(false, |value| value == "application/grpc");
.map_or(false, |value| {
value.as_bytes().starts_with("application/grpc".as_bytes())
});

if is_grpc {
match crate::classify::grpc_errors_as_failures::classify_grpc_metadata(
Expand Down