diff --git a/README.md b/README.md index 66786138..2bdacac3 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,8 @@ Table of Contents * [lua\_kong\_load\_var\_index](#lua_kong_load_var_index) * [lua\_kong\_set\_static\_tag](#lua_kong_set_static_tag) * [lua\_kong\_error\_log\_request\_id](#lua_kong_error_log_request_id) +* [Variables](#variables) + * [$kong\_request\_id](#kong_request_id) * [Methods](#methods) * [resty.kong.tls.disable\_session\_reuse](#restykongtlsdisable_session_reuse) * [resty.kong.tls.get\_full\_client\_certificate\_chain](#restykongtlsget_full_client_certificate_chain) @@ -160,6 +162,16 @@ An error log line may look similar to the following: [Back to TOC](#table-of-contents) +Variables +========= + +$kong\_request\_id +------------------ +Unique request identifier generated from 16 pseudo-random bytes, in hexadecimal. +This variable is indexed. + +[Back to TOC](#table-of-contents) + Methods ======= diff --git a/config b/config index 8423d392..6c64b0e5 100644 --- a/config +++ b/config @@ -3,11 +3,12 @@ ngx_module_name=ngx_http_lua_kong_module ngx_module_srcs=" \ $ngx_addon_dir/src/ngx_http_lua_kong_grpc.c \ $ngx_addon_dir/src/ngx_http_lua_kong_ssl.c \ - $ngx_addon_dir/src/ngx_http_lua_kong_var.c \ + $ngx_addon_dir/src/ngx_http_lua_kong_var_index.c \ $ngx_addon_dir/src/ngx_http_lua_kong_tag.c \ $ngx_addon_dir/src/ngx_http_lua_kong_module.c \ $ngx_addon_dir/src/ngx_http_lua_kong_log.c \ $ngx_addon_dir/src/ngx_http_lua_kong_log_handler.c \ + $ngx_addon_dir/src/ngx_http_lua_kong_vars.c \ $ngx_addon_dir/src/ssl/ngx_lua_kong_ssl.c \ " diff --git a/src/ngx_http_lua_kong_common.h b/src/ngx_http_lua_kong_common.h index f3058165..525f1b84 100644 --- a/src/ngx_http_lua_kong_common.h +++ b/src/ngx_http_lua_kong_common.h @@ -52,4 +52,7 @@ ngx_http_lua_kong_ctx_t *ngx_http_lua_kong_get_module_ctx( char *ngx_http_lua_kong_error_log_init( ngx_conf_t *cf); +ngx_int_t +ngx_http_lua_kong_add_vars(ngx_conf_t *cf); + #endif /* _NGX_HTTP_LUA_KONG_COMMON_H_INCLUDED_ */ diff --git a/src/ngx_http_lua_kong_module.c b/src/ngx_http_lua_kong_module.c index 81c698d8..eae8c723 100644 --- a/src/ngx_http_lua_kong_module.c +++ b/src/ngx_http_lua_kong_module.c @@ -24,7 +24,7 @@ static char* ngx_http_lua_kong_merge_loc_conf(ngx_conf_t *cf, void *parent, void static ngx_http_module_t ngx_http_lua_kong_module_ctx = { - NULL, /* preconfiguration */ + ngx_http_lua_kong_add_vars, /* preconfiguration */ ngx_http_lua_kong_init, /* postconfiguration */ NULL, /* create main configuration */ diff --git a/src/ngx_http_lua_kong_var.c b/src/ngx_http_lua_kong_var_index.c similarity index 98% rename from src/ngx_http_lua_kong_var.c rename to src/ngx_http_lua_kong_var_index.c index ec36e5d4..eb9a8c29 100644 --- a/src/ngx_http_lua_kong_var.c +++ b/src/ngx_http_lua_kong_var_index.c @@ -72,7 +72,7 @@ static ngx_str_t default_vars[] = { ngx_string("server_addr"), ngx_string("server_port"), -/* --with-http_ssl_module */ + /* --with-http_ssl_module */ #if (NGX_SSL) ngx_string("ssl_cipher"), ngx_string("ssl_client_raw_cert"), @@ -86,6 +86,9 @@ static ngx_str_t default_vars[] = { ngx_string("upstream_http_upgrade"), ngx_string("upstream_status"), + /* lua-kong-module vars */ + ngx_string("kong_request_id"), + ngx_null_string }; diff --git a/src/ngx_http_lua_kong_vars.c b/src/ngx_http_lua_kong_vars.c new file mode 100644 index 00000000..5d180fc8 --- /dev/null +++ b/src/ngx_http_lua_kong_vars.c @@ -0,0 +1,81 @@ +/** + * Copyright 2019-2023 Kong Inc. + + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + + * http://www.apache.org/licenses/LICENSE-2.0 + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "ngx_http_lua_kong_common.h" + + +#define NGX_HTTP_LUA_KONG_RANDOM_COUNT 4 +#define NGX_HTTP_LUA_KONG_UINT32_HEX_LEN sizeof(uint32_t) * 2 + + +static ngx_int_t +ngx_http_lua_kong_variable_request_id(ngx_http_request_t *r, + ngx_http_variable_value_t *v, uintptr_t data) +{ + u_char *id; + uint32_t i, rnd; + + id = ngx_pnalloc(r->pool, + NGX_HTTP_LUA_KONG_RANDOM_COUNT * + NGX_HTTP_LUA_KONG_UINT32_HEX_LEN); + if (id == NULL) { + return NGX_ERROR; + } + + v->valid = 1; + v->no_cacheable = 0; + v->not_found = 0; + + v->len = NGX_HTTP_LUA_KONG_RANDOM_COUNT * + NGX_HTTP_LUA_KONG_UINT32_HEX_LEN; + v->data = id; + + for (i = 0; i < NGX_HTTP_LUA_KONG_RANDOM_COUNT; i++) { + rnd = (uint32_t) ngx_random(); + id = ngx_hex_dump(id, (u_char *) &rnd, sizeof(uint32_t)); + } + + return NGX_OK; +} + + +static ngx_http_variable_t ngx_http_lua_kong_variables[] = { + + { ngx_string("kong_request_id"), NULL, + ngx_http_lua_kong_variable_request_id, + 0, 0, 0 }, + + ngx_http_null_variable +}; + + +ngx_int_t +ngx_http_lua_kong_add_vars(ngx_conf_t *cf) +{ + ngx_http_variable_t *cv, *v; + + for (cv = ngx_http_lua_kong_variables; cv->name.len; cv++) { + v = ngx_http_add_variable(cf, &cv->name, cv->flags); + if (v == NULL) { + return NGX_ERROR; + } + + *v = *cv; + } + + return NGX_OK; +} diff --git a/t/009-error-log-append.t b/t/009-error-log-append.t index fff71039..7b97c306 100644 --- a/t/009-error-log-append.t +++ b/t/009-error-log-append.t @@ -233,3 +233,27 @@ GET /test [error] [crit] [alert] + + +=== TEST 9: $kong_request_id is appended correctly to error logs +--- http_config + lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;"; + lua_kong_error_log_request_id $kong_request_id; +--- config + location = /test { + content_by_lua_block { + ngx.log(ngx.INFO, "log_msg") + ngx.exit(200) + } + } +--- request +GET /test +--- error_code: 200 +--- error_log eval +qr/log_msg.*request_id: "[0-9a-f]{32}"$/ +--- no_error_log +[error] +[crit] +[alert] + + diff --git a/t/010-request-id.t b/t/010-request-id.t new file mode 100644 index 00000000..b1ed0a83 --- /dev/null +++ b/t/010-request-id.t @@ -0,0 +1,41 @@ +# vim:set ft= ts=4 sw=4 et: + +use Test::Nginx::Socket::Lua; +use Cwd qw(cwd); + +repeat_each(2); + +plan tests => repeat_each() * (blocks() * 5); + +my $pwd = cwd(); + +$ENV{TEST_NGINX_HTML_DIR} ||= html_dir(); + +no_long_string(); + +run_tests(); + +__DATA__ + +=== TEST 1: $kong_request_id works +--- config + location /t { + content_by_lua_block { + local rid = ngx.var.kong_request_id + assert(ngx.re.match(rid, "[0-9a-f]{32}")) + ngx.say("ok") + } + } + +--- request +GET /t +--- response_body_like +ok + +--- error_code: 200 +--- no_error_log +[error] +[crit] +[alert] + +