-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
access log: add JSON logging mode #4693
Changes from all commits
bfe7744
031377f
1c754a5
f604562
cbdc6dd
8db57a7
6af83d5
bded5f2
ffd3358
7c777d3
ed1d0d8
4c7e000
216814f
777bd96
9064213
1d23323
cc0090e
ccaab7c
234a683
9f78381
f2b1173
633365a
d700a64
b8a19d8
6482235
53e066b
195c1be
7dfd316
9d0ab16
6dc6869
258a03d
76bc028
7f10f1a
a7d514c
034dffc
ee3f1dc
bc79d68
6273f4b
588a28f
85e05f9
a3e4410
91b1b80
0bf86f8
5aa43e2
d8a51e9
b87f2a4
6ef6456
1ffc475
e92a886
252df75
a060f50
e7ca4a7
d0221c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,16 +17,91 @@ Access logs are configured as part of the :ref:`HTTP connection manager config | |
Format rules | ||
------------ | ||
|
||
The access log format string contains either command operators or other characters interpreted as a | ||
plain string. The access log formatter does not make any assumptions about a new line separator, so one | ||
Access log formats contain command operators that extract the relevant data and insert it. | ||
They support two formats: :ref:`"format strings" <config_access_log_format_strings>` and | ||
:ref:`"format dictionaries" <config_access_log_format_dictionaries>`. In both cases, the command operators | ||
are used to extract the relevant data, which is then inserted into the specified log format. | ||
Only one access log format may be specified at a time. | ||
|
||
.. _config_access_log_format_strings: | ||
|
||
Format Strings | ||
-------------- | ||
|
||
Format strings are plain strings, specified using the ``format`` key. They may contain | ||
either command operators or other characters interpreted as a plain string. | ||
The access log formatter does not make any assumptions about a new line separator, so one | ||
has to specified as part of the format string. | ||
See the :ref:`default format <config_access_log_default_format>` for an example. | ||
Note that the access log line will contain a '-' character for every not set/empty value. | ||
|
||
The same format strings are used by different types of access logs (such as HTTP and TCP). Some | ||
.. _config_access_log_default_format: | ||
|
||
Default Format String | ||
--------------------- | ||
|
||
If custom format string is not specified, Envoy uses the following default format: | ||
|
||
.. code-block:: none | ||
|
||
[%START_TIME%] "%REQ(:METHOD)% %REQ(X-ENVOY-ORIGINAL-PATH?:PATH)% %PROTOCOL%" | ||
%RESPONSE_CODE% %RESPONSE_FLAGS% %BYTES_RECEIVED% %BYTES_SENT% %DURATION% | ||
%RESP(X-ENVOY-UPSTREAM-SERVICE-TIME)% "%REQ(X-FORWARDED-FOR)%" "%REQ(USER-AGENT)%" | ||
"%REQ(X-REQUEST-ID)%" "%REQ(:AUTHORITY)%" "%UPSTREAM_HOST%"\n | ||
|
||
Example of the default Envoy access log format: | ||
|
||
.. code-block:: none | ||
|
||
[2016-04-15T20:17:00.310Z] "POST /api/v1/locations HTTP/2" 204 - 154 0 226 100 "10.0.35.28" | ||
"nsq2http" "cc21d9b0-cf5c-432b-8c7e-98aeb7988cd2" "locations" "tcp://10.0.2.1:80" | ||
|
||
.. _config_access_log_format_dictionaries: | ||
|
||
Format Dictionaries | ||
------------------- | ||
|
||
Format dictionaries are dictionaries that specify a structured access log output format, | ||
specified using the ``json_format`` key. This allows logs to be output in a structured format | ||
such as JSON. | ||
Similar to format strings, command operators are evaluated and their values inserted into the format | ||
dictionary to construct the log output. | ||
|
||
For example, with the following format provided in the configuration: | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"config": { | ||
"json_format": { | ||
"protocol": "%PROTOCOL%", | ||
"duration": "%DURATION%", | ||
"my_custom_header": "%REQ(MY_CUSTOM_HEADER)%" | ||
} | ||
} | ||
} | ||
|
||
The following JSON object would be written to the log file: | ||
|
||
.. code-block:: json | ||
|
||
{"protocol": "HTTP/1.1", "duration": "123", "my_custom_header": "value_of_MY_CUSTOM_HEADER"} | ||
|
||
This allows you to specify a custom key for each command operator. | ||
|
||
Format dictionaries have the following restrictions: | ||
|
||
* The dictionary must map strings to strings (specifically, strings to command operators). Nesting is not currently supported. | ||
|
||
Command Operators | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, I think "command operator" is not the right terminology, but I see it was existing before this PR. I probably would have used "format expressions" or the like. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll see if it's pretty straightforward to change this. I personally prefer "format specifiers" since it lines up with the C terminology There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left this as is for now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, this was more just a non-actionable observation. |
||
----------------- | ||
|
||
Command operators are used to extract values that will be inserted into the access logs. | ||
The same operators are used by different types of access logs (such as HTTP and TCP). Some | ||
fields may have slightly different meanings, depending on what type of log it is. Differences | ||
are noted. | ||
|
||
Note that if a value is not set/empty, the logs will contain a '-' character. | ||
|
||
The following command operators are supported: | ||
|
||
.. _config_access_log_format_start_time: | ||
|
@@ -232,24 +307,3 @@ The following command operators are supported: | |
String value set on ssl connection socket for Server Name Indication (SNI) | ||
TCP | ||
String value set on ssl connection socket for Server Name Indication (SNI) | ||
|
||
.. _config_access_log_default_format: | ||
|
||
Default format | ||
-------------- | ||
|
||
If custom format is not specified, Envoy uses the following default format: | ||
|
||
.. code-block:: none | ||
|
||
[%START_TIME%] "%REQ(:METHOD)% %REQ(X-ENVOY-ORIGINAL-PATH?:PATH)% %PROTOCOL%" | ||
%RESPONSE_CODE% %RESPONSE_FLAGS% %BYTES_RECEIVED% %BYTES_SENT% %DURATION% | ||
%RESP(X-ENVOY-UPSTREAM-SERVICE-TIME)% "%REQ(X-FORWARDED-FOR)%" "%REQ(USER-AGENT)%" | ||
"%REQ(X-REQUEST-ID)%" "%REQ(:AUTHORITY)%" "%UPSTREAM_HOST%"\n | ||
|
||
Example of the default Envoy access log format: | ||
|
||
.. code-block:: none | ||
|
||
[2016-04-15T20:17:00.310Z] "POST /api/v1/locations HTTP/2" 204 - 154 0 226 100 "10.0.35.28" | ||
"nsq2http" "cc21d9b0-cf5c-432b-8c7e-98aeb7988cd2" "locations" "tcp://10.0.2.1:80" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,11 +71,20 @@ typedef std::shared_ptr<Instance> InstanceSharedPtr; | |
|
||
/** | ||
* Interface for access log formatter. | ||
* Formatters provide a complete access log output line for the given headers/trailers/stream. | ||
*/ | ||
class Formatter { | ||
public: | ||
virtual ~Formatter() {} | ||
|
||
/** | ||
* Return a formatted access log line. | ||
* @param request_headers supplies the request headers. | ||
* @param response_headers supplies the response headers. | ||
* @param response_trailers supplies the response trailers. | ||
* @param stream_info supplies the stream info. | ||
* @return std::string string containing the complete formatted access log line. | ||
*/ | ||
virtual std::string format(const Http::HeaderMap& request_headers, | ||
const Http::HeaderMap& response_headers, | ||
const Http::HeaderMap& response_trailers, | ||
|
@@ -84,5 +93,29 @@ class Formatter { | |
|
||
typedef std::unique_ptr<Formatter> FormatterPtr; | ||
|
||
/** | ||
* Interface for access log provider. | ||
* FormatterProviders extract information from the given headers/trailers/stream. | ||
*/ | ||
class FormatterProvider { | ||
public: | ||
virtual ~FormatterProvider() {} | ||
|
||
/** | ||
* Extract a value from the provided headers/trailers/stream. | ||
* @param request_headers supplies the request headers. | ||
* @param response_headers supplies the response headers. | ||
* @param response_trailers supplies the response trailers. | ||
* @param stream_info supplies the stream info. | ||
* @return std::string containing a single value extracted from the given headers/trailers/stream. | ||
*/ | ||
virtual std::string format(const Http::HeaderMap& request_headers, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a Doxygen comment here with args/return explanation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Felt like it was redundant to call out the args, so just added an explanation and then an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Envoy |
||
const Http::HeaderMap& response_headers, | ||
const Http::HeaderMap& response_trailers, | ||
const StreamInfo::StreamInfo& stream_info) const PURE; | ||
}; | ||
|
||
typedef std::unique_ptr<FormatterProvider> FormatterProviderPtr; | ||
|
||
} // namespace AccessLog | ||
} // namespace Envoy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment describing how this is interpreted? E.g. can we use the same custom access log formats in leaf strings, limitations on depth of nesting, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, this will be coming soon with the docs updates.