Skip to content

Commit

Permalink
router: add two header formatter operators (#11242)
Browse files Browse the repository at this point in the history
This commit updates the router's header formatter to support
the RESPONSE_FLAGS and RESPONSE_CODE_DETAILS operators.

Signed-off-by: Spencer Lewis <slewis@squareup.com>
  • Loading branch information
spenceral authored May 20, 2020
1 parent 95c3b41 commit 72b930b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/root/configuration/http/http_conn_man/headers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,11 @@ Supported variable names are:
key: "x-request-start"
value: "%START_TIME(%s.%3f)%"
append: true
%RESPONSE_FLAGS%
Additional details about the response or connection, if any. Possible values and their meanings
are listed in the access log formatter :ref:`documentation<config_access_log_format_response_flags>`.

%RESPONSE_CODE_DETAILS%
Response code details provides additional information about the HTTP response code, such as
who set it (the upstream or envoy) and why.
2 changes: 2 additions & 0 deletions docs/root/version_history/current.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ New Features
* request_id: added to :ref:`always_set_request_id_in_response setting <envoy_v3_api_field_extensions.filters.network.http_connection_manager.v3.HttpConnectionManager.always_set_request_id_in_response>`
to set :ref:`x-request-id <config_http_conn_man_headers_x-request-id>` header in response even if
tracing is not forced.
* router: add support for RESPONSE_FLAGS and RESPONSE_CODE_DETAILS :ref:`header formatters
<config_http_conn_man_headers_custom_request_headers>`.
* router: more fine grained internal redirect configs are added to the :ref`internal_redirect_policy
<envoy_api_field_router.RouterAction.internal_redirect_policy>` field.
* runtime: add new gauge :ref:`deprecated_feature_seen_since_process_start <runtime_stats>` that gets reset across hot restarts.
Expand Down
11 changes: 11 additions & 0 deletions source/common/router/header_formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@ StreamInfoHeaderFormatter::StreamInfoHeaderFormatter(absl::string_view field_nam
} else if (field_name == "HOSTNAME") {
std::string hostname = Envoy::AccessLog::AccessLogFormatUtils::getHostname();
field_extractor_ = [hostname](const StreamInfo::StreamInfo&) { return hostname; };
} else if (field_name == "RESPONSE_FLAGS") {
field_extractor_ = [](const StreamInfo::StreamInfo& stream_info) {
return StreamInfo::ResponseFlagUtils::toShortString(stream_info);
};
} else if (field_name == "RESPONSE_CODE_DETAILS") {
field_extractor_ = [](const StreamInfo::StreamInfo& stream_info) -> std::string {
if (stream_info.responseCodeDetails().has_value()) {
return stream_info.responseCodeDetails().value();
}
return "";
};
} else {
throw EnvoyException(fmt::format("field '{}' not supported as custom header", field_name));
}
Expand Down
8 changes: 8 additions & 0 deletions test/common/router/header_formatter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@ TEST(HeaderParserTest, TestParseInternal) {
{"%PER_REQUEST_STATE(testing)%", {"test_value"}, {}},
{"%REQ(x-request-id)%", {"123"}, {}},
{"%START_TIME%", {"2018-04-03T23:06:09.123Z"}, {}},
{"%RESPONSE_FLAGS%", {"LR"}, {}},
{"%RESPONSE_CODE_DETAILS%", {"via_upstream"}, {}},

// Unescaped %
{"%", {}, {"Invalid header configuration. Un-escaped % at position 0"}},
Expand Down Expand Up @@ -875,6 +877,12 @@ TEST(HeaderParserTest, TestParseInternal) {
ON_CALL(stream_info, filterState()).WillByDefault(ReturnRef(filter_state));
ON_CALL(Const(stream_info), filterState()).WillByDefault(ReturnRef(*filter_state));

ON_CALL(stream_info, hasResponseFlag(StreamInfo::ResponseFlag::LocalReset))
.WillByDefault(Return(true));

absl::optional<std::string> rc_details{"via_upstream"};
ON_CALL(stream_info, responseCodeDetails()).WillByDefault(ReturnRef(rc_details));

for (const auto& test_case : test_cases) {
Protobuf::RepeatedPtrField<envoy::config::core::v3::HeaderValueOption> to_add;
envoy::config::core::v3::HeaderValueOption* header = to_add.Add();
Expand Down

0 comments on commit 72b930b

Please sign in to comment.