Skip to content

Commit 86b3ffb

Browse files
committed
add tests
1 parent a86cdf6 commit 86b3ffb

File tree

2 files changed

+217
-57
lines changed

2 files changed

+217
-57
lines changed

src/ngx_http_apisix_module.c

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -854,12 +854,12 @@ ngx_http_apisix_error_log_handler(ngx_http_request_t *r, u_char *buf, size_t len
854854

855855
loc_conf = ngx_http_get_module_loc_conf(r, ngx_http_apisix_module);
856856
if (loc_conf->request_id_var_index == NGX_CONF_UNSET) {
857-
return buf;
857+
return buf;
858858
}
859859

860860
request_id_var = ngx_http_get_indexed_variable(r, loc_conf->request_id_var_index);
861861
if (request_id_var == NULL || request_id_var->not_found) {
862-
return buf;
862+
return buf;
863863
}
864864
buf = ngx_snprintf(buf, len, ", request_id: \"%v\"", request_id_var);
865865
return buf;
@@ -875,23 +875,23 @@ ngx_http_apisix_error_log_handler(ngx_http_request_t *r, u_char *buf, size_t len
875875
static u_char*
876876
ngx_http_apisix_combined_error_log_handler(ngx_http_request_t *r, ngx_http_request_t *sr, u_char *buf, size_t len)
877877
{
878-
u_char *p;
879-
ngx_http_apisix_ctx_t *ctx;
878+
u_char *p;
879+
ngx_http_apisix_ctx_t *ctx;
880880

881-
ctx = ngx_http_apisix_get_module_ctx(r);
882-
if (ctx == NULL || ctx->orig_log_handler == NULL) {
883-
return buf;
884-
}
881+
ctx = ngx_http_apisix_get_module_ctx(r);
882+
if (ctx == NULL || ctx->orig_log_handler == NULL) {
883+
return buf;
884+
}
885885

886-
//Get the original log message
887-
p = ctx->orig_log_handler(r, sr, buf, len);
888-
//p - buf calculates the number of bytes written by the original log handler into the buffer.
889-
//len -= (p - buf) reduces the remaining buffer length by the amount already used.
890-
len -= p-buf;
886+
//Get the original log message
887+
p = ctx->orig_log_handler(r, sr, buf, len);
888+
//p - buf calculates the number of bytes written by the original log handler into the buffer.
889+
//len -= (p - buf) reduces the remaining buffer length by the amount already used.
890+
len -= p-buf;
891891

892-
//Apisix log handler
893-
buf = ngx_http_apisix_error_log_handler(r, buf, len);
894-
return buf;
892+
//Apisix log handler
893+
buf = ngx_http_apisix_error_log_handler(r, buf, len);
894+
return buf;
895895
}
896896

897897

@@ -900,47 +900,47 @@ ngx_http_apisix_combined_error_log_handler(ngx_http_request_t *r, ngx_http_reque
900900
static ngx_int_t
901901
ngx_http_apisix_replace_error_log_handler(ngx_http_request_t *r)
902902
{
903-
ngx_http_apisix_ctx_t *ctx;
903+
ngx_http_apisix_ctx_t *ctx;
904904

905-
ctx = ngx_http_apisix_get_module_ctx(r);
906-
if (ctx == NULL) {
907-
return NGX_OK;
908-
}
905+
ctx = ngx_http_apisix_get_module_ctx(r);
906+
if (ctx == NULL) {
907+
return NGX_OK;
908+
}
909909

910-
if (r->log_handler == NULL){
911-
return NGX_DECLINED;
912-
}
910+
if (r->log_handler == NULL){
911+
return NGX_DECLINED;
912+
}
913913

914-
/*
915-
* Store the original log handler in ctx->orig_log_handler, replace
916-
* it with the combined log handler, which will execute the original
917-
* handler's logic in addition to our own.
918-
*/
919-
ctx->orig_log_handler = r->log_handler;
920-
r->log_handler = ngx_http_apisix_combined_error_log_handler;
914+
/*
915+
* Store the original log handler in ctx->orig_log_handler, replace
916+
* it with the combined log handler, which will execute the original
917+
* handler's logic in addition to our own.
918+
*/
919+
ctx->orig_log_handler = r->log_handler;
920+
r->log_handler = ngx_http_apisix_combined_error_log_handler;
921921

922-
return NGX_DECLINED;
922+
return NGX_DECLINED;
923923
}
924924

925925
//This function is part of postconfiguration passed to module context and will override the post_read_phase with custom log handler.
926926
// It extracts the pointer to log handler from the post read phase handlers and then override that with new function address.
927927
char *
928928
ngx_http_apisix_error_log_init(ngx_conf_t *cf)
929929
{
930-
ngx_http_handler_pt *h;
931-
ngx_http_core_main_conf_t *cmcf;
930+
ngx_http_handler_pt *h;
931+
ngx_http_core_main_conf_t *cmcf;
932932

933-
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
934-
h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers);
935-
if (h == NULL) {
936-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
937-
"failed setting error log handler");
938-
return NGX_CONF_ERROR;
939-
}
933+
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
934+
h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers);
935+
if (h == NULL) {
936+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
937+
"failed setting error log handler");
938+
return NGX_CONF_ERROR;
939+
}
940940

941-
*h = ngx_http_apisix_replace_error_log_handler;
941+
*h = ngx_http_apisix_replace_error_log_handler;
942942

943-
return NGX_CONF_OK;
943+
return NGX_CONF_OK;
944944
}
945945

946946
// This function does the translation of the configuration file to the internal representation.
@@ -949,22 +949,22 @@ char *
949949
ngx_http_apisix_error_log_request_id(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
950950
{
951951
ngx_str_t *value;
952-
ngx_http_apisix_loc_conf_t *loc_conf = conf;
952+
ngx_http_apisix_loc_conf_t *loc_conf = conf;
953953

954-
value = cf->args->elts;
955-
if (value[1].data[0] != '$') {
956-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid variable name \"%V\"", &value[1]);
957-
return NGX_CONF_ERROR;
958-
}
954+
value = cf->args->elts;
955+
if (value[1].data[0] != '$') {
956+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid variable name \"%V\"", &value[1]);
957+
return NGX_CONF_ERROR;
958+
}
959959

960-
value[1].len--;
961-
value[1].data++;
960+
value[1].len--;
961+
value[1].data++;
962962

963-
loc_conf->request_id_var_index = ngx_http_get_variable_index(cf, &value[1]);
964-
if (loc_conf->request_id_var_index == NGX_ERROR) {
965-
return NGX_CONF_ERROR;
966-
}
963+
loc_conf->request_id_var_index = ngx_http_get_variable_index(cf, &value[1]);
964+
if (loc_conf->request_id_var_index == NGX_ERROR) {
965+
return NGX_CONF_ERROR;
966+
}
967967

968-
return NGX_CONF_OK;
968+
return NGX_CONF_OK;
969969
}
970970

t/request-id-err-log.t

Lines changed: 161 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,170 @@ __DATA__
1515
}
1616
}
1717
--- request
18-
GET /test
18+
GET /t
1919
--- error_log eval
2020
qr/log_msg.*request_id: "1234"$/
2121
--- no_error_log
2222
[error]
2323
[crit]
2424
[alert]
25+
26+
27+
28+
=== TEST 2: request_id in error log set when a runtime error occurs
29+
--- config
30+
location /t {
31+
set $request_id_new 1234;
32+
apisix_request_id_var $request_id_new;
33+
content_by_lua_block {
34+
error("error_message")
35+
}
36+
}
37+
--- request
38+
GET /t
39+
--- error_code: 500
40+
--- error_log eval
41+
qr/.*request_id: "1234".*$/
42+
43+
44+
45+
=== TEST 3: scoping: value is appended correctly to error logs
46+
based on the location where the directive is defined
47+
--- config
48+
location /append_req_id {
49+
set $req_id_a 123456;
50+
apisix_request_id_var $req_id_a;
51+
content_by_lua_block {
52+
ngx.log(ngx.INFO, "log_msg")
53+
ngx.exit(200)
54+
}
55+
}
56+
location /append_method {
57+
set $req_id_b 654321;
58+
apisix_request_id_var $req_id_b;
59+
60+
content_by_lua_block {
61+
ngx.log(ngx.INFO, "log_msg")
62+
ngx.exit(200)
63+
}
64+
}
65+
--- pipelined_requests eval
66+
["GET /append_req_id", "GET /append_method"]
67+
--- error_code eval
68+
[200, 200, 200]
69+
--- error_log eval
70+
[ 'request_id: "123456"', 'request_id: "654321"' ]
71+
--- no_error_log
72+
[error]
73+
[crit]
74+
[alert]
75+
76+
77+
78+
=== TEST 4: scoping: value is NOT appended to error logs for the location where the directive is NOT defined
79+
--- config
80+
location /append {
81+
set $req_id 123456;
82+
apisix_request_id_var $req_id;
83+
content_by_lua_block {
84+
ngx.log(ngx.ERR, "log_msg")
85+
ngx.exit(200)
86+
}
87+
}
88+
location /no_append {
89+
content_by_lua_block {
90+
ngx.log(ngx.INFO, "log_msg")
91+
ngx.exit(200)
92+
}
93+
}
94+
--- request
95+
GET /no_append
96+
--- error_code: 200
97+
--- no_error_log eval
98+
qr/log_msg.*request_id/
99+
100+
101+
102+
=== TEST 5: scoping: value is appended correctly to error logs when the directive is in the main configuration
103+
--- http_config
104+
apisix_request_id_var $req_id;
105+
--- config
106+
set $req_id 123456;
107+
location = /test {
108+
content_by_lua_block {
109+
ngx.log(ngx.INFO, "log_msg")
110+
ngx.exit(200)
111+
}
112+
}
113+
--- request
114+
GET /test
115+
--- error_code: 200
116+
--- error_log eval
117+
qr/log_msg.*request_id: "123456"$/
118+
--- no_error_log
119+
[error]
120+
[crit]
121+
[alert]
122+
123+
124+
125+
=== TEST 6: scoping: value is appended correctly to error logs and the local directive overrides the global one
126+
--- http_config
127+
apisix_request_id_var $req_id_global;
128+
--- config
129+
set $req_id_global global;
130+
set $req_id_local local;
131+
132+
location = /test {
133+
apisix_request_id_var $req_id_local;
134+
content_by_lua_block {
135+
ngx.log(ngx.INFO, "log_msg")
136+
ngx.exit(200)
137+
}
138+
}
139+
--- request
140+
GET /test
141+
--- error_code: 200
142+
--- error_log eval
143+
qr/log_msg.*request_id: "local"$/
144+
--- no_error_log eval
145+
qr/log_msg.*request_id: "global"$/
146+
147+
148+
149+
=== TEST 7: Request ID variable changes are applied to the error log output
150+
--- config
151+
location = /test {
152+
set $my_var "";
153+
apisix_request_id_var $my_var;
154+
rewrite_by_lua_block {
155+
ngx.log(ngx.INFO, "rewrite_0")
156+
ngx.var.my_var = "changed_in_rewrite"
157+
ngx.log(ngx.INFO, "rewrite_1")
158+
ngx.var.my_var = "changed_in_rewrite_2"
159+
ngx.log(ngx.INFO, "rewrite_2")
160+
}
161+
access_by_lua_block {
162+
ngx.log(ngx.INFO, "access_0")
163+
ngx.var.my_var = "changed_in_access"
164+
ngx.log(ngx.INFO, "access_1")
165+
ngx.var.my_var = "changed_in_access_2"
166+
ngx.log(ngx.INFO, "access_2")
167+
ngx.exit(200)
168+
}
169+
}
170+
--- request
171+
GET /test
172+
--- error_log eval
173+
[
174+
qr/rewrite_0.*request_id: ""$/,
175+
qr/rewrite_1.*request_id: "changed_in_rewrite"$/,
176+
qr/rewrite_2.*request_id: "changed_in_rewrite_2"$/,
177+
qr/access_0.*request_id: "changed_in_rewrite_2"$/,
178+
qr/access_1.*request_id: "changed_in_access"$/,
179+
qr/access_2.*request_id: "changed_in_access_2"$/,
180+
]
181+
--- no_error_log
182+
[error]
183+
[crit]
184+
[alert]

0 commit comments

Comments
 (0)