Skip to content

Commit 2546bcd

Browse files
authored
formatter: add formal full name for some commands (envoyproxy#41670)
Commit Message: formatter: add formal full name for some commands Additional Description: To make the naming more clear. And we can also to support request trailers in the future. Risk Level: low. Testing: unit. Docs Changes: n/a. Release Notes: added. Signed-off-by: WangBaiping <wbphub@gmail.com>
1 parent 2a33d4e commit 2546bcd

5 files changed

Lines changed: 63 additions & 22 deletions

File tree

changelogs/current.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ new_features:
105105
change: |
106106
Added support for not-equal operator for access log filter rules, in
107107
:ref:`ComparisonFilter <envoy_v3_api_msg_config.accesslog.v3.ComparisonFilter>`.
108+
- area: formatter
109+
change: |
110+
Added support for the following new access log formatters:
111+
- ``%REQUEST_HEADER(X?Y):Z%`` as full name version of ``%REQ(X?Y):Z%``
112+
- ``%RESPONSE_HEADER(X?Y):Z%`` as full name version of ``%RESP(X?Y):Z%``
113+
- ``%RESPONSE_TRAILER(X?Y):Z%`` as full name version of ``%TRAILER(X?Y):Z%``
114+
This mainly to provide a more consistent naming scheme for users to understand and use.
108115
- area: lua
109116
change: |
110117
Added ``drainConnectionUponCompletion()`` to the Lua filter stream info API. This allows Lua scripts

docs/root/configuration/observability/access_log/usage.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ If custom format string is not specified, Envoy uses the following default forma
4444

4545
.. code-block:: none
4646
47-
[%START_TIME%] "%REQ(:METHOD)% %REQ(X-ENVOY-ORIGINAL-PATH?:PATH)% %PROTOCOL%"
47+
[%START_TIME%] "%REQUEST_HEADER(:METHOD)% %REQUEST_HEADER(X-ENVOY-ORIGINAL-PATH?:PATH)% %PROTOCOL%"
4848
%RESPONSE_CODE% %RESPONSE_FLAGS% %BYTES_RECEIVED% %BYTES_SENT% %DURATION%
49-
%RESP(X-ENVOY-UPSTREAM-SERVICE-TIME)% "%REQ(X-FORWARDED-FOR)%" "%REQ(USER-AGENT)%"
50-
"%REQ(X-REQUEST-ID)%" "%REQ(:AUTHORITY)%" "%UPSTREAM_HOST%"\n
49+
%RESPONSE_HEADER(X-ENVOY-UPSTREAM-SERVICE-TIME)% "%REQUEST_HEADER(X-FORWARDED-FOR)%" "%REQUEST_HEADER(USER-AGENT)%"
50+
"%REQUEST_HEADER(X-REQUEST-ID)%" "%REQUEST_HEADER(:AUTHORITY)%" "%UPSTREAM_HOST%"\n
5151
5252
Example of the default Envoy access log format:
5353

@@ -75,7 +75,7 @@ For example, with the following format provided in the configuration as ``json_f
7575
"json_format": {
7676
"protocol": "%PROTOCOL%",
7777
"duration": "%DURATION%",
78-
"my_custom_header": "%REQ(MY_CUSTOM_HEADER)%"
78+
"my_custom_header": "%REQUEST_HEADER(MY_CUSTOM_HEADER)%"
7979
}
8080
}
8181
}
@@ -835,8 +835,8 @@ UDP
835835
An identifier for the stream (HTTP request, long-live HTTP2 stream, TCP connection, etc.). It can be used to
836836
cross-reference TCP access logs across multiple log sinks, or to cross-reference timer-based reports for the same connection.
837837
Different with %CONNECTION_ID%, the identifier should be unique across multiple instances or between restarts.
838-
And it's value should be same with %REQ(X-REQUEST-ID)% for HTTP request.
839-
This should be used to replace %CONNECTION_ID% and %REQ(X-REQUEST-ID)% in most cases.
838+
And it's value should be same with %REQUEST_HEADER(X-REQUEST-ID)% for HTTP request.
839+
This should be used to replace %CONNECTION_ID% and %REQUEST_HEADER(X-REQUEST-ID)% in most cases.
840840

841841
%GRPC_STATUS(X)%
842842
`gRPC status code <https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto>`_ formatted according to the optional parameter ``X``, which can be ``CAMEL_STRING``, ``SNAKE_STRING`` and ``NUMBER``.
@@ -848,7 +848,7 @@ UDP
848848

849849
.. _config_access_log_format_req:
850850

851-
%REQ(X?Y):Z%
851+
%REQUEST_HEADER(X?Y):Z%/%REQ(X?Y):Z%
852852
HTTP
853853
An HTTP request header where X is the main HTTP header, Y is the alternative one, and Z is an
854854
optional parameter denoting string truncation up to Z characters long. The value is taken from
@@ -858,16 +858,16 @@ UDP
858858
TCP/UDP
859859
Not implemented ("-").
860860

861-
%RESP(X?Y):Z%
861+
%RESPONSE_HEADER(X?Y):Z%/%RESP(X?Y):Z%
862862
HTTP
863-
Same as **%REQ(X?Y):Z%** but taken from HTTP response headers.
863+
Same as **%REQUEST_HEADER(X?Y):Z%** but taken from HTTP response headers.
864864

865865
TCP/UDP
866866
Not implemented ("-").
867867

868-
%TRAILER(X?Y):Z%
868+
%RESPONSE_TRAILER(X?Y):Z%/%TRAILER(X?Y):Z%
869869
HTTP
870-
Same as **%REQ(X?Y):Z%** but taken from HTTP response trailers.
870+
Same as **%REQUEST_HEADER(X?Y):Z%** but taken from HTTP response trailers.
871871

872872
TCP/UDP
873873
Not implemented ("-").

source/common/formatter/http_formatter_context.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ namespace Envoy {
1010
namespace Formatter {
1111

1212
static constexpr absl::string_view DEFAULT_FORMAT =
13-
"[%START_TIME%] \"%REQ(:METHOD)% %REQ(X-ENVOY-ORIGINAL-PATH?:PATH)% %PROTOCOL%\" "
14-
"%RESPONSE_CODE% %RESPONSE_FLAGS% %BYTES_RECEIVED% %BYTES_SENT% %DURATION% "
15-
"%RESP(X-ENVOY-UPSTREAM-SERVICE-TIME)% "
16-
"\"%REQ(X-FORWARDED-FOR)%\" \"%REQ(USER-AGENT)%\" \"%REQ(X-REQUEST-ID)%\" "
17-
"\"%REQ(:AUTHORITY)%\" \"%UPSTREAM_HOST%\"\n";
13+
"[%START_TIME%] "
14+
"\"%REQUEST_HEADER(:METHOD)% %REQUEST_HEADER(X-ENVOY-ORIGINAL-PATH?:PATH)% %PROTOCOL%\" "
15+
"%RESPONSE_CODE% "
16+
"%RESPONSE_FLAGS% "
17+
"%BYTES_RECEIVED% "
18+
"%BYTES_SENT% "
19+
"%DURATION% "
20+
"%RESPONSE_HEADER(X-ENVOY-UPSTREAM-SERVICE-TIME)% "
21+
"\"%REQUEST_HEADER(X-FORWARDED-FOR)%\" "
22+
"\"%REQUEST_HEADER(USER-AGENT)%\" "
23+
"\"%REQUEST_HEADER(X-REQUEST-ID)%\" "
24+
"\"%REQUEST_HEADER(:AUTHORITY)%\" "
25+
"\"%UPSTREAM_HOST%\"\n";
1826

1927
absl::StatusOr<FormatterPtr> HttpSubstitutionFormatUtils::defaultSubstitutionFormatter() {
2028
// It is possible that failed to parse the default format string if the required formatters

source/common/formatter/http_specific_formatter.cc

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,23 +388,47 @@ const BuiltInHttpCommandParser::FormatterProviderLookupTbl&
388388
BuiltInHttpCommandParser::getKnownFormatters() {
389389
CONSTRUCT_ON_FIRST_USE(
390390
FormatterProviderLookupTbl,
391-
{{"REQ",
391+
{{"REQ", // Same as REQUEST_HEADER and used for backward compatibility.
392392
{CommandSyntaxChecker::PARAMS_REQUIRED | CommandSyntaxChecker::LENGTH_ALLOWED,
393393
[](absl::string_view format, absl::optional<size_t> max_length) {
394394
auto result = SubstitutionFormatUtils::parseSubcommandHeaders(format);
395395
THROW_IF_NOT_OK_REF(result.status());
396396
return std::make_unique<RequestHeaderFormatter>(result.value().first,
397397
result.value().second, max_length);
398398
}}},
399-
{"RESP",
399+
{"REQUEST_HEADER",
400+
{CommandSyntaxChecker::PARAMS_REQUIRED | CommandSyntaxChecker::LENGTH_ALLOWED,
401+
[](absl::string_view format, absl::optional<size_t> max_length) {
402+
auto result = SubstitutionFormatUtils::parseSubcommandHeaders(format);
403+
THROW_IF_NOT_OK_REF(result.status());
404+
return std::make_unique<RequestHeaderFormatter>(result.value().first,
405+
result.value().second, max_length);
406+
}}},
407+
{"RESP", // Same as RESPONSE_HEADER and used for backward compatibility.
400408
{CommandSyntaxChecker::PARAMS_REQUIRED | CommandSyntaxChecker::LENGTH_ALLOWED,
401409
[](absl::string_view format, absl::optional<size_t> max_length) {
402410
auto result = SubstitutionFormatUtils::parseSubcommandHeaders(format);
403411
THROW_IF_NOT_OK_REF(result.status());
404412
return std::make_unique<ResponseHeaderFormatter>(result.value().first,
405413
result.value().second, max_length);
406414
}}},
407-
{"TRAILER",
415+
{"RESPONSE_HEADER",
416+
{CommandSyntaxChecker::PARAMS_REQUIRED | CommandSyntaxChecker::LENGTH_ALLOWED,
417+
[](absl::string_view format, absl::optional<size_t> max_length) {
418+
auto result = SubstitutionFormatUtils::parseSubcommandHeaders(format);
419+
THROW_IF_NOT_OK_REF(result.status());
420+
return std::make_unique<ResponseHeaderFormatter>(result.value().first,
421+
result.value().second, max_length);
422+
}}},
423+
{"TRAILER", // Same as RESPONSE_TRAILER and used for backward compatibility.
424+
{CommandSyntaxChecker::PARAMS_REQUIRED | CommandSyntaxChecker::LENGTH_ALLOWED,
425+
[](absl::string_view format, absl::optional<size_t> max_length) {
426+
auto result = SubstitutionFormatUtils::parseSubcommandHeaders(format);
427+
THROW_IF_NOT_OK_REF(result.status());
428+
return std::make_unique<ResponseTrailerFormatter>(result.value().first,
429+
result.value().second, max_length);
430+
}}},
431+
{"RESPONSE_TRAILER",
408432
{CommandSyntaxChecker::PARAMS_REQUIRED | CommandSyntaxChecker::LENGTH_ALLOWED,
409433
[](absl::string_view format, absl::optional<size_t> max_length) {
410434
auto result = SubstitutionFormatUtils::parseSubcommandHeaders(format);

test/common/formatter/substitution_formatter_test.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4249,14 +4249,16 @@ TEST(SubstitutionFormatterTest, CompositeFormatterSuccess) {
42494249
{
42504250
NiceMock<StreamInfo::MockStreamInfo> stream_info;
42514251
const std::string format = "{{%PROTOCOL%}} %RESP(not_exist)%++%RESP(test)% "
4252-
"%REQ(FIRST?SECOND)% %RESP(FIRST?SECOND)%"
4253-
"\t@%TRAILER(THIRD)%@\t%TRAILER(TEST?TEST-2)%[]";
4252+
"%REQ(FIRST?SECOND)%/%REQUEST_HEADER(FIRST?SECOND)% "
4253+
"%RESP(FIRST?SECOND)%/%RESPONSE_HEADER(FIRST?SECOND)% "
4254+
"\t@%TRAILER(THIRD)%@\t%TRAILER(TEST?TEST-2)%[] "
4255+
"%RESPONSE_TRAILER(THIRD)%";
42544256
FormatterPtr formatter = *FormatterImpl::create(format, false);
42554257

42564258
absl::optional<Http::Protocol> protocol = Http::Protocol::Http11;
42574259
EXPECT_CALL(stream_info, protocol()).WillRepeatedly(Return(protocol));
42584260

4259-
EXPECT_EQ("{{HTTP/1.1}} -++test GET PUT\t@POST@\ttest-2[]",
4261+
EXPECT_EQ("{{HTTP/1.1}} -++test GET/GET PUT/PUT \t@POST@\ttest-2[] POST",
42604262
formatter->format(formatter_context, stream_info));
42614263
}
42624264

0 commit comments

Comments
 (0)