Skip to content

Commit b971b19

Browse files
feat: support apisix_request_id variable with request-id plugin (#12931)
1 parent d89cb86 commit b971b19

File tree

8 files changed

+105
-11
lines changed

8 files changed

+105
-11
lines changed

apisix/cli/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ local _M = {
110110
enable_access_log = false,
111111
access_log = "logs/access_stream.log",
112112
-- luacheck: push max code line length 300
113-
access_log_format = "$remote_addr [$time_local] $protocol $status $bytes_sent $bytes_received $session_time",
113+
access_log_format = "$remote_addr [$time_local] $protocol $status $bytes_sent $bytes_received $session_time $apisix_request_id",
114114
-- luacheck: pop
115115
access_log_format_escape = "default",
116116
lua_shared_dict = {

apisix/cli/ngx_tpl.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ http {
640640
allow all;
641641
{%end%}
642642
643+
{% if use_apisix_base then %}
644+
set $apisix_request_id $request_id;
645+
lua_error_log_request_id $apisix_request_id;
646+
{% end %}
643647
location /apisix/admin {
644648
content_by_lua_block {
645649
apisix.http_admin()
@@ -819,6 +823,11 @@ http {
819823
set $llm_completion_tokens '0';
820824
821825
826+
{% if use_apisix_base then %}
827+
set $apisix_request_id $request_id;
828+
lua_error_log_request_id $apisix_request_id;
829+
{% end %}
830+
822831
access_by_lua_block {
823832
apisix.http_access_phase()
824833
}

apisix/core/ctx.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ do
234234
upstream_connection = true,
235235
upstream_uri = true,
236236
llm_content_risk_level = true,
237+
apisix_request_id = true,
237238

238239
request_type = true,
239240
apisix_upstream_response_time = true,

apisix/plugins/request-id.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ function _M.rewrite(conf, ctx)
112112
if conf.include_in_response then
113113
ctx["request-id-" .. conf.header_name] = uuid_val
114114
end
115+
if ctx.var.apisix_request_id then
116+
ctx.var.apisix_request_id = uuid_val
117+
end
115118
end
116119

117120
function _M.header_filter(conf, ctx)

t/APISIX.pm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,9 @@ _EOC_
870870
set \$apisix_upstream_response_time \$upstream_response_time;
871871
access_log $apisix_home/t/servroot/logs/access.log main;
872872
873+
set \$apisix_request_id \$request_id;
874+
lua_error_log_request_id \$apisix_request_id;
875+
873876
access_by_lua_block {
874877
-- wait for etcd sync
875878
ngx.sleep($wait_etcd_sync)

t/cli/test_access_log.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ if [ `grep -c '"client_ip": "127.0.0.1"' output.log` -eq '0' ]; then
129129
exit 1
130130
fi
131131

132+
if [ `grep -E "^{[^}]+}$" output.log` -eq '0' ]; then
133+
echo "failed: invalid JSON log in access log"
134+
exit 1
135+
fi
136+
132137
if [ `grep -c 'main escape=json' conf/nginx.conf` -eq '0' ]; then
133138
echo "failed: not found \"escape=json\" in conf/nginx.conf"
134139
exit 1
@@ -138,6 +143,30 @@ make stop
138143

139144
echo "passed: access log with JSON format"
140145

146+
147+
# access log with default log format
148+
echo '
149+
nginx_config:
150+
http:
151+
enable_access_log: true
152+
' > conf/config.yaml
153+
154+
make init
155+
make run
156+
sleep 0.1
157+
curl http://127.0.0.1:9080/hello2
158+
sleep 4
159+
tail -n 1 logs/access.log > output.log
160+
161+
if [ `grep -E '[0-9|.]+ - - \[[^]]+\] [0-9|.:]+ "[^"]+" [0-9]+ [0-9]+ [0|.]+ "-" "[^"]+" - - - "[^"]+" ".{36}"' output.log` -eq '0' ]; then
162+
echo "failed: access log don't match default log format"
163+
exit 1
164+
fi
165+
166+
make stop
167+
168+
echo "passed: access log with default format"
169+
141170
# access log with unset llm_token JSON format
142171

143172
echo '

t/node/upstream-discovery-dynamic.t

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ GET /t
121121
--- grep_error_log eval
122122
qr/upstream: \S+, host: \S+/
123123
--- grep_error_log_out
124-
upstream: "http://127.0.0.1:1111/", host: "a.myhost.com"
125-
upstream: "http://127.0.0.1:1111/", host: "a.myhost.com"
126-
upstream: "http://127.0.0.1:2222/", host: "b.myhost.com"
127-
upstream: "http://127.0.0.1:2222/", host: "b.myhost.com"
128-
upstream: "http://127.0.0.1:1111/", host: "a.myhost.com"
129-
upstream: "http://127.0.0.1:2222/", host: "b.myhost.com"
130-
upstream: "http://127.0.0.1:2222/", host: "b.myhost.com"
131-
upstream: "http://127.0.0.1:1111/", host: "a.myhost.com"
132-
upstream: "http://127.0.0.1:2222/", host: "b.myhost.com"
133-
upstream: "http://127.0.0.1:1111/", host: "a.myhost.com"
124+
upstream: "http://127.0.0.1:1111/", host: "a.myhost.com",
125+
upstream: "http://127.0.0.1:1111/", host: "a.myhost.com",
126+
upstream: "http://127.0.0.1:2222/", host: "b.myhost.com",
127+
upstream: "http://127.0.0.1:2222/", host: "b.myhost.com",
128+
upstream: "http://127.0.0.1:1111/", host: "a.myhost.com",
129+
upstream: "http://127.0.0.1:2222/", host: "b.myhost.com",
130+
upstream: "http://127.0.0.1:2222/", host: "b.myhost.com",
131+
upstream: "http://127.0.0.1:1111/", host: "a.myhost.com",
132+
upstream: "http://127.0.0.1:2222/", host: "b.myhost.com",
133+
upstream: "http://127.0.0.1:1111/", host: "a.myhost.com",

t/plugin/request-id3.t

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,52 @@ X-Request-Id
152152
--- wait: 5
153153
--- response_body
154154
true
155+
156+
157+
158+
=== TEST 4: enable request-id plugin
159+
--- yaml_config
160+
--- config
161+
location /t {
162+
content_by_lua_block {
163+
local t = require("lib.test_admin").test
164+
local code, body = t('/apisix/admin/routes/1',
165+
ngx.HTTP_PUT,
166+
[[{
167+
"plugins": {
168+
"request-id": {
169+
"header_name": "X-Request-Id"
170+
}
171+
},
172+
"upstream": {
173+
"nodes": {
174+
"127.0.0.1:1999": 1
175+
},
176+
"type": "roundrobin"
177+
},
178+
"uri": "/opentracing"
179+
}]]
180+
)
181+
if code >= 300 then
182+
ngx.status = code
183+
end
184+
ngx.say(body)
185+
}
186+
}
187+
--- response_body
188+
passed
189+
190+
191+
192+
=== TEST 5: check request-id
193+
--- request
194+
GET /opentracing
195+
--- more_headers
196+
X-Request-Id: abctesting
197+
--- grep_error_log eval
198+
qr/request_id: "abctesting"/
199+
--- grep_error_log_out
200+
request_id: "abctesting"
201+
request_id: "abctesting"
202+
request_id: "abctesting"
203+
--- error_code: 502

0 commit comments

Comments
 (0)