Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Revolyssup committed Dec 2, 2024
1 parent 86b3ffb commit 3e53a25
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
30 changes: 15 additions & 15 deletions src/ngx_http_apisix_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,20 +849,20 @@ ngx_http_apisix_is_ntls_enabled(ngx_http_conf_ctx_t *conf_ctx)
static u_char*
ngx_http_apisix_error_log_handler(ngx_http_request_t *r, u_char *buf, size_t len)
{
ngx_http_variable_value_t *request_id_var;
ngx_http_apisix_loc_conf_t *loc_conf;
ngx_http_variable_value_t *request_id_var;
ngx_http_apisix_loc_conf_t *loc_conf;

loc_conf = ngx_http_get_module_loc_conf(r, ngx_http_apisix_module);
if (loc_conf->request_id_var_index == NGX_CONF_UNSET) {
return buf;
}
loc_conf = ngx_http_get_module_loc_conf(r, ngx_http_apisix_module);
if (loc_conf->request_id_var_index == NGX_CONF_UNSET) {
return buf;
}

request_id_var = ngx_http_get_indexed_variable(r, loc_conf->request_id_var_index);
if (request_id_var == NULL || request_id_var->not_found) {
return buf;
}
buf = ngx_snprintf(buf, len, ", request_id: \"%v\"", request_id_var);
return buf;
request_id_var = ngx_http_get_indexed_variable(r, loc_conf->request_id_var_index);
if (request_id_var == NULL || request_id_var->not_found) {
return buf;
}
buf = ngx_snprintf(buf, len, ", request_id: \"%v\"", request_id_var);
return buf;
}


Expand Down Expand Up @@ -904,7 +904,7 @@ ngx_http_apisix_replace_error_log_handler(ngx_http_request_t *r)

ctx = ngx_http_apisix_get_module_ctx(r);
if (ctx == NULL) {
return NGX_OK;
return NGX_ERROR;
}

if (r->log_handler == NULL){
Expand Down Expand Up @@ -935,8 +935,8 @@ ngx_http_apisix_error_log_init(ngx_conf_t *cf)
if (h == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"failed setting error log handler");
return NGX_CONF_ERROR;
}
return NGX_CONF_ERROR;
}

*h = ngx_http_apisix_replace_error_log_handler;

Expand Down
45 changes: 27 additions & 18 deletions t/request-id-err-log.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,49 @@ qr/.*request_id: "1234".*$/



=== TEST 3: scoping: value is appended correctly to error logs
based on the location where the directive is defined
=== TEST 3: scoping: value is appended correctly to error logs based on the location where the directive is defined
--- config
location /append_req_id {
set $req_id_a 123456;
apisix_request_id_var $req_id_a;
location = /append_method {
set $req_id_b 654321;
apisix_request_id_var $req_id_b;

content_by_lua_block {
ngx.log(ngx.INFO, "log_msg")
ngx.exit(200)
}
}
location /append_method {
set $req_id_b 654321;
apisix_request_id_var $req_id_b;

location = /append_req_id {
set $req_id_a 123456;
apisix_request_id_var $req_id_a;
content_by_lua_block {
ngx.log(ngx.INFO, "log_msg")
ngx.exit(200)
}
}
--- pipelined_requests eval
["GET /append_req_id", "GET /append_method"]
--- error_code eval
[200, 200, 200]
--- request
GET /append_method
--- error_log eval
[ 'request_id: "123456"', 'request_id: "654321"' ]
qr/log_msg.*request_id: "654321"$/
--- no_error_log
[error]
[crit]
[alert]



=== TEST 4: Send request to different location
--- request
GET /append_req_id
--- error_log eval
qr/log_msg.*request_id: "123456"$/
--- no_error_log
[error]
[crit]
[alert]



=== TEST 4: scoping: value is NOT appended to error logs for the location where the directive is NOT defined
=== TEST 5: scoping: value is NOT appended to error logs for the location where the directive is NOT defined
--- config
location /append {
set $req_id 123456;
Expand All @@ -99,7 +108,7 @@ qr/log_msg.*request_id/



=== TEST 5: scoping: value is appended correctly to error logs when the directive is in the main configuration
=== TEST 6: scoping: value is appended correctly to error logs when the directive is in the main configuration
--- http_config
apisix_request_id_var $req_id;
--- config
Expand All @@ -122,7 +131,7 @@ qr/log_msg.*request_id: "123456"$/



=== TEST 6: scoping: value is appended correctly to error logs and the local directive overrides the global one
=== TEST 7: scoping: value is appended correctly to error logs and the local directive overrides the global one
--- http_config
apisix_request_id_var $req_id_global;
--- config
Expand All @@ -146,7 +155,7 @@ qr/log_msg.*request_id: "global"$/



=== TEST 7: Request ID variable changes are applied to the error log output
=== TEST 8: Request ID variable changes are applied to the error log output
--- config
location = /test {
set $my_var "";
Expand Down

0 comments on commit 3e53a25

Please sign in to comment.