diff --git a/contrib/generic_proxy/filters/network/source/access_log.cc b/contrib/generic_proxy/filters/network/source/access_log.cc index 1e1f70feb868..a4f0c4cbda33 100644 --- a/contrib/generic_proxy/filters/network/source/access_log.cc +++ b/contrib/generic_proxy/filters/network/source/access_log.cc @@ -41,6 +41,31 @@ class StringValueFormatterProvider : public FormatterProvider { absl::optional max_length_; }; +class GenericStatusCodeFormatterProvider : public FormatterProvider { +public: + GenericStatusCodeFormatterProvider() = default; + + // FormatterProvider + absl::optional formatWithContext(const FormatterContext& context, + const StreamInfo::StreamInfo&) const override { + if (context.response_ == nullptr) { + return absl::nullopt; + } + + const int code = context.response_->status().code(); + return std::to_string(code); + } + ProtobufWkt::Value formatValueWithContext(const FormatterContext& context, + const StreamInfo::StreamInfo&) const override { + if (context.response_ == nullptr) { + return ValueUtil::nullValue(); + } + + const int code = context.response_->status().code(); + return ValueUtil::numberValue(code); + } +}; + class SimpleCommandParser : public CommandParser { public: using ProviderFunc = @@ -139,6 +164,13 @@ class SimpleCommandParser : public CommandParser { return std::string(optional_view.value()); }); }}, + // A formatter for the response status code. This supports the case where the response + // code is minus value and will override the common RESPONSE_CODE formatter for generic + // proxy. + {"RESPONSE_CODE", + [](absl::string_view, absl::optional) -> FormatterProviderPtr { + return std::make_unique(); + }}, }); } }; diff --git a/contrib/generic_proxy/filters/network/source/interface/stream.h b/contrib/generic_proxy/filters/network/source/interface/stream.h index fcd7debb7660..a345be85261b 100644 --- a/contrib/generic_proxy/filters/network/source/interface/stream.h +++ b/contrib/generic_proxy/filters/network/source/interface/stream.h @@ -247,7 +247,7 @@ using StatusCode = absl::StatusCode; struct StreamStatus { public: StreamStatus() = default; - StreamStatus(uint32_t code, bool ok) : code_(code), ok_(ok) {} + StreamStatus(int code, bool ok) : code_(code), ok_(ok) {} // Returns true if the status indicates success. This will be used for tracing, logging // or stats purposes. @@ -255,10 +255,10 @@ struct StreamStatus { // Returns the status code value. The code will be used for tracing, logging or stats // purposes. The specific code value is application protocol specific. - ABSL_MUST_USE_RESULT uint32_t code() const { return code_; } + ABSL_MUST_USE_RESULT int code() const { return code_; } private: - uint32_t code_{}; + int code_{}; bool ok_{true}; }; diff --git a/contrib/generic_proxy/filters/network/test/fake_codec.h b/contrib/generic_proxy/filters/network/test/fake_codec.h index 702abb47d25e..e5e241075efb 100644 --- a/contrib/generic_proxy/filters/network/test/fake_codec.h +++ b/contrib/generic_proxy/filters/network/test/fake_codec.h @@ -152,7 +152,7 @@ class FakeStreamCodecFactory : public CodecFactory { } // Additional 4 bytes for status. encoding_buffer_.writeBEInt(body.size() + 4); - encoding_buffer_.writeBEInt(static_cast(typed_response->status_.code())); + encoding_buffer_.writeBEInt(typed_response->status_.code()); encoding_buffer_.add(body); callback.onEncodingSuccess(encoding_buffer_, response.frameFlags().endStream()); @@ -160,8 +160,7 @@ class FakeStreamCodecFactory : public CodecFactory { ResponsePtr respond(Status status, absl::string_view, const Request&) override { auto response = std::make_unique(); - response->status_ = {static_cast(status.code()), - status.code() == absl::StatusCode::kOk}; + response->status_ = {status.raw_code(), status.ok()}; response->message_ = status.message(); response->protocol_ = "fake_protocol_for_test"; return response; @@ -192,7 +191,7 @@ class FakeStreamCodecFactory : public CodecFactory { } auto response = std::make_unique(); - response->status_ = {static_cast(status_code), + response->status_ = {status_code, static_cast(status_code) == absl::StatusCode::kOk}; response->protocol_ = std::string(result[0]); for (absl::string_view pair_str : absl::StrSplit(result[2], ';', absl::SkipEmpty())) { diff --git a/contrib/generic_proxy/filters/network/test/proxy_test.cc b/contrib/generic_proxy/filters/network/test/proxy_test.cc index 2fe8b80c98b9..82fc88c7e17e 100644 --- a/contrib/generic_proxy/filters/network/test/proxy_test.cc +++ b/contrib/generic_proxy/filters/network/test/proxy_test.cc @@ -658,8 +658,7 @@ TEST_F(FilterTest, ActiveStreamSendLocalReply) { EXPECT_CALL(*server_codec_, respond(_, _, _)) .WillOnce(Invoke([&](Status status, absl::string_view, const Request&) -> ResponsePtr { auto response = std::make_unique(); - response->status_ = {static_cast(status.code()), - status.code() == StatusCode::kOk}; + response->status_ = {static_cast(status.code()), status.code() == StatusCode::kOk}; response->message_ = status.message(); return response; }));