Skip to content

Commit

Permalink
feat(variables): add new variable $kong_request_id (#70)
Browse files Browse the repository at this point in the history
Similar to `$request_id`, but from pseudo-random number source instead of OpenSSL's `RAND_bytes`
for better performance.

KAG-2734
---------

Co-authored-by: Datong Sun <datong.sun@konghq.com>
  • Loading branch information
chronolaw and dndx authored Oct 12, 2023
1 parent 2d42099 commit 4fbc3dd
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
=======

Expand Down
3 changes: 2 additions & 1 deletion config
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
"

Expand Down
3 changes: 3 additions & 0 deletions src/ngx_http_lua_kong_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
2 changes: 1 addition & 1 deletion src/ngx_http_lua_kong_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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
};

Expand Down
81 changes: 81 additions & 0 deletions src/ngx_http_lua_kong_vars.c
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 24 additions & 0 deletions t/009-error-log-append.t
Original file line number Diff line number Diff line change
Expand Up @@ -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]


41 changes: 41 additions & 0 deletions t/010-request-id.t
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 4fbc3dd

Please sign in to comment.