Skip to content

Commit

Permalink
feat: enable L4 stream logging (#5768)
Browse files Browse the repository at this point in the history
  • Loading branch information
bisakhmondal committed Dec 13, 2021
1 parent 02d32c9 commit 71c256b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ stream {
lua_ssl_trusted_certificate {* ssl.ssl_trusted_certificate *};
{% end %}
# for stream logs, off by default
{% if stream.enable_access_log == true then %}
log_format main escape={* stream.access_log_format_escape *} '{* stream.access_log_format *}';
access_log {* stream.access_log *} main buffer=16384 flush=3;
{% end %}
# stream configuration snippet starts
{% if stream_configuration_snippet then %}
{* stream_configuration_snippet *}
Expand Down
5 changes: 5 additions & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ nginx_config: # config for render the template to generate n
# - TEST_ENV

stream:
enable_access_log: false # enable access log or not, default false
access_log: logs/access_stream.log

This comment has been minimized.

Copy link
@membphis

membphis Dec 13, 2021

Member

the plugin log-rotate, does it support this log file?

access_log_format: "$remote_addr [$time_local] $protocol $status $bytes_sent $bytes_received $session_time"
# create your custom log format by visiting http://nginx.org/en/docs/varindex.html
access_log_format_escape: default # allows setting json or default characters escaping in variables
lua_shared_dict:
etcd-cluster-health-check-stream: 10m
lrucache-lock-stream: 10m
Expand Down
37 changes: 37 additions & 0 deletions t/cli/test_access_log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,40 @@ fi

make stop
echo "passed: should find upstream scheme"

# check stream logs
echo '
apisix:
stream_proxy: # UDP proxy
udp:
- "127.0.0.1:9200"
nginx_config:
stream:
enable_access_log: true
access_log_format: "$remote_addr $protocol test_stream_access_log_format"
' > conf/config.yaml

make init

grep "test_stream_access_log_format" conf/nginx.conf > /dev/null
if [ ! $? -eq 0 ]; then
echo "failed: stream access_log_format in nginx.conf doesn't change"
exit 1
fi
echo "passed: stream access_log_format in nginx.conf is ok"

# check if logs are being written
make run
sleep 0.1
# sending single udp packet
echo -n "hello" | nc -4u -w0 localhost 9200
sleep 4
tail -n 1 logs/access_stream.log > output.log

if ! grep '127.0.0.1 UDP test_stream_access_log_format' output.log; then
echo "failed: should have found udp log entry"
cat output.log
exit 1
fi
echo "passed: logs are being dumped for stream proxy"

0 comments on commit 71c256b

Please sign in to comment.