Skip to content

Commit 4fbc3dd

Browse files
chronolawdndx
andauthored
feat(variables): add new variable $kong_request_id (#70)
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 <[email protected]>
1 parent 2d42099 commit 4fbc3dd

File tree

8 files changed

+168
-3
lines changed

8 files changed

+168
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Table of Contents
1313
* [lua\_kong\_load\_var\_index](#lua_kong_load_var_index)
1414
* [lua\_kong\_set\_static\_tag](#lua_kong_set_static_tag)
1515
* [lua\_kong\_error\_log\_request\_id](#lua_kong_error_log_request_id)
16+
* [Variables](#variables)
17+
* [$kong\_request\_id](#kong_request_id)
1618
* [Methods](#methods)
1719
* [resty.kong.tls.disable\_session\_reuse](#restykongtlsdisable_session_reuse)
1820
* [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:
160162

161163
[Back to TOC](#table-of-contents)
162164

165+
Variables
166+
=========
167+
168+
$kong\_request\_id
169+
------------------
170+
Unique request identifier generated from 16 pseudo-random bytes, in hexadecimal.
171+
This variable is indexed.
172+
173+
[Back to TOC](#table-of-contents)
174+
163175
Methods
164176
=======
165177

config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ ngx_module_name=ngx_http_lua_kong_module
33
ngx_module_srcs=" \
44
$ngx_addon_dir/src/ngx_http_lua_kong_grpc.c \
55
$ngx_addon_dir/src/ngx_http_lua_kong_ssl.c \
6-
$ngx_addon_dir/src/ngx_http_lua_kong_var.c \
6+
$ngx_addon_dir/src/ngx_http_lua_kong_var_index.c \
77
$ngx_addon_dir/src/ngx_http_lua_kong_tag.c \
88
$ngx_addon_dir/src/ngx_http_lua_kong_module.c \
99
$ngx_addon_dir/src/ngx_http_lua_kong_log.c \
1010
$ngx_addon_dir/src/ngx_http_lua_kong_log_handler.c \
11+
$ngx_addon_dir/src/ngx_http_lua_kong_vars.c \
1112
$ngx_addon_dir/src/ssl/ngx_lua_kong_ssl.c \
1213
"
1314

src/ngx_http_lua_kong_common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@ ngx_http_lua_kong_ctx_t *ngx_http_lua_kong_get_module_ctx(
5252
char *ngx_http_lua_kong_error_log_init(
5353
ngx_conf_t *cf);
5454

55+
ngx_int_t
56+
ngx_http_lua_kong_add_vars(ngx_conf_t *cf);
57+
5558
#endif /* _NGX_HTTP_LUA_KONG_COMMON_H_INCLUDED_ */

src/ngx_http_lua_kong_module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static char* ngx_http_lua_kong_merge_loc_conf(ngx_conf_t *cf, void *parent, void
2424

2525

2626
static ngx_http_module_t ngx_http_lua_kong_module_ctx = {
27-
NULL, /* preconfiguration */
27+
ngx_http_lua_kong_add_vars, /* preconfiguration */
2828
ngx_http_lua_kong_init, /* postconfiguration */
2929

3030
NULL, /* create main configuration */

src/ngx_http_lua_kong_var.c renamed to src/ngx_http_lua_kong_var_index.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static ngx_str_t default_vars[] = {
7272
ngx_string("server_addr"),
7373
ngx_string("server_port"),
7474

75-
/* --with-http_ssl_module */
75+
/* --with-http_ssl_module */
7676
#if (NGX_SSL)
7777
ngx_string("ssl_cipher"),
7878
ngx_string("ssl_client_raw_cert"),
@@ -86,6 +86,9 @@ static ngx_str_t default_vars[] = {
8686
ngx_string("upstream_http_upgrade"),
8787
ngx_string("upstream_status"),
8888

89+
/* lua-kong-module vars */
90+
ngx_string("kong_request_id"),
91+
8992
ngx_null_string
9093
};
9194

src/ngx_http_lua_kong_vars.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright 2019-2023 Kong Inc.
3+
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
#include "ngx_http_lua_kong_common.h"
19+
20+
21+
#define NGX_HTTP_LUA_KONG_RANDOM_COUNT 4
22+
#define NGX_HTTP_LUA_KONG_UINT32_HEX_LEN sizeof(uint32_t) * 2
23+
24+
25+
static ngx_int_t
26+
ngx_http_lua_kong_variable_request_id(ngx_http_request_t *r,
27+
ngx_http_variable_value_t *v, uintptr_t data)
28+
{
29+
u_char *id;
30+
uint32_t i, rnd;
31+
32+
id = ngx_pnalloc(r->pool,
33+
NGX_HTTP_LUA_KONG_RANDOM_COUNT *
34+
NGX_HTTP_LUA_KONG_UINT32_HEX_LEN);
35+
if (id == NULL) {
36+
return NGX_ERROR;
37+
}
38+
39+
v->valid = 1;
40+
v->no_cacheable = 0;
41+
v->not_found = 0;
42+
43+
v->len = NGX_HTTP_LUA_KONG_RANDOM_COUNT *
44+
NGX_HTTP_LUA_KONG_UINT32_HEX_LEN;
45+
v->data = id;
46+
47+
for (i = 0; i < NGX_HTTP_LUA_KONG_RANDOM_COUNT; i++) {
48+
rnd = (uint32_t) ngx_random();
49+
id = ngx_hex_dump(id, (u_char *) &rnd, sizeof(uint32_t));
50+
}
51+
52+
return NGX_OK;
53+
}
54+
55+
56+
static ngx_http_variable_t ngx_http_lua_kong_variables[] = {
57+
58+
{ ngx_string("kong_request_id"), NULL,
59+
ngx_http_lua_kong_variable_request_id,
60+
0, 0, 0 },
61+
62+
ngx_http_null_variable
63+
};
64+
65+
66+
ngx_int_t
67+
ngx_http_lua_kong_add_vars(ngx_conf_t *cf)
68+
{
69+
ngx_http_variable_t *cv, *v;
70+
71+
for (cv = ngx_http_lua_kong_variables; cv->name.len; cv++) {
72+
v = ngx_http_add_variable(cf, &cv->name, cv->flags);
73+
if (v == NULL) {
74+
return NGX_ERROR;
75+
}
76+
77+
*v = *cv;
78+
}
79+
80+
return NGX_OK;
81+
}

t/009-error-log-append.t

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,27 @@ GET /test
233233
[error]
234234
[crit]
235235
[alert]
236+
237+
238+
=== TEST 9: $kong_request_id is appended correctly to error logs
239+
--- http_config
240+
lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;";
241+
lua_kong_error_log_request_id $kong_request_id;
242+
--- config
243+
location = /test {
244+
content_by_lua_block {
245+
ngx.log(ngx.INFO, "log_msg")
246+
ngx.exit(200)
247+
}
248+
}
249+
--- request
250+
GET /test
251+
--- error_code: 200
252+
--- error_log eval
253+
qr/log_msg.*request_id: "[0-9a-f]{32}"$/
254+
--- no_error_log
255+
[error]
256+
[crit]
257+
[alert]
258+
259+

t/010-request-id.t

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# vim:set ft= ts=4 sw=4 et:
2+
3+
use Test::Nginx::Socket::Lua;
4+
use Cwd qw(cwd);
5+
6+
repeat_each(2);
7+
8+
plan tests => repeat_each() * (blocks() * 5);
9+
10+
my $pwd = cwd();
11+
12+
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
13+
14+
no_long_string();
15+
16+
run_tests();
17+
18+
__DATA__
19+
20+
=== TEST 1: $kong_request_id works
21+
--- config
22+
location /t {
23+
content_by_lua_block {
24+
local rid = ngx.var.kong_request_id
25+
assert(ngx.re.match(rid, "[0-9a-f]{32}"))
26+
ngx.say("ok")
27+
}
28+
}
29+
30+
--- request
31+
GET /t
32+
--- response_body_like
33+
ok
34+
35+
--- error_code: 200
36+
--- no_error_log
37+
[error]
38+
[crit]
39+
[alert]
40+
41+

0 commit comments

Comments
 (0)