Skip to content

Commit fc29d26

Browse files
committed
feat(vars): index upstream timing variables for FFI access
We have identified a ~5% RPS performance regression in the PR Kong/kong-ee#11452 Upon further investigation, we found that a big chunk of CPU time is spent on ngx.var variables access. We add upstream timing variables to the default indexed variables list, allowing efficient access through Kong's FFI interface. These variables track detailed timing information for upstream connections: * upstream_start_timestamp_us * upstream_connect_timestamp_us * upstream_request_timestamp_us * upstream_header_timestamp_us * upstream_response_timestamp_us These microsecond-precision timestamps enable detailed upstream latency analysis and troubleshooting of connection issues.
1 parent c90fe07 commit fc29d26

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/ngx_http_lua_kong_var_index.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ static ngx_str_t default_vars[] = {
9191
ngx_string("upstream_http_upgrade"),
9292
ngx_string("upstream_status"),
9393

94+
/* detailed upstream timing vars */
95+
ngx_string("upstream_start_timestamp_us"),
96+
ngx_string("upstream_connect_timestamp_us"),
97+
ngx_string("upstream_request_timestamp_us"),
98+
ngx_string("upstream_header_timestamp_us"),
99+
ngx_string("upstream_response_timestamp_us"),
100+
94101
/* lua-kong-module vars */
95102
ngx_string("kong_request_id"),
96103

t/006-default_indexed-var.t

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use Test::Nginx::Socket::Lua;
99

1010
repeat_each(2);
1111

12-
plan tests => repeat_each() * (blocks() * 8) + 10;
12+
plan tests => repeat_each() * (blocks() * 8) + 14;
1313

1414
#no_diff();
1515
#no_long_string();
@@ -444,3 +444,36 @@ get variable value 'false' by index
444444
[error]
445445
[crit]
446446
[alert]
447+
448+
=== TEST 14: upstream timing variables
449+
--- http_config
450+
lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;";
451+
lua_kong_load_var_index default;
452+
init_by_lua_block {
453+
require("resty.kong.var").patch_metatable()
454+
}
455+
456+
--- config
457+
location = /test {
458+
content_by_lua_block {
459+
ngx.say(ngx.var.upstream_start_timestamp_us, " ",
460+
ngx.var.upstream_connect_timestamp_us, " ",
461+
ngx.var.upstream_request_timestamp_us, " ",
462+
ngx.var.upstream_header_timestamp_us, " ",
463+
ngx.var.upstream_response_timestamp_us)
464+
}
465+
}
466+
--- request
467+
GET /test
468+
--- response_body
469+
nil nil nil nil nil
470+
--- error_log
471+
variable value is not found by index
472+
variable value is not found by index
473+
variable value is not found by index
474+
variable value is not found by index
475+
variable value is not found by index
476+
--- no_error_log
477+
[error]
478+
[crit]
479+
[alert]

0 commit comments

Comments
 (0)