Skip to content

Commit

Permalink
perf(request-id): use proxy_set_header instead of `ngx.req.set_head…
Browse files Browse the repository at this point in the history
…er` (#11788)

Utilize the Nginx directive
`proxy_set_header X-Kong-Request-Id $kong_request_id` instead of Lua
call `set_header()` can enhance the RPS by ~2%
in the testing scenario where no plugins are enabled.

KAG-2814
  • Loading branch information
chobits authored and samugi committed Nov 15, 2023
1 parent 5345aa4 commit 2befdb0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
14 changes: 0 additions & 14 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ local log = ngx.log
local exit = ngx.exit
local exec = ngx.exec
local header = ngx.header
local set_header = ngx.req.set_header
local timer_at = ngx.timer.at
local subsystem = ngx.config.subsystem
local clear_header = ngx.req.clear_header
Expand Down Expand Up @@ -1329,9 +1328,6 @@ return {
end,
-- Only executed if the `router` module found a route and allows nginx to proxy it.
after = function(ctx)
local enabled_headers_upstream = kong.configuration.enabled_headers_upstream
local headers = constants.HEADERS

-- Nginx's behavior when proxying a request with an empty querystring
-- `/foo?` is to keep `$is_args` an empty string, hence effectively
-- stripping the empty querystring.
Expand Down Expand Up @@ -1426,16 +1422,6 @@ return {
if var.http_proxy_connection then
clear_header("Proxy-Connection")
end

-- X-Kong-Request-Id upstream header
local rid, rid_get_err = request_id_get()
if not rid then
log(WARN, "failed to get Request ID: ", rid_get_err)
end

if enabled_headers_upstream[headers.REQUEST_ID] and rid then
set_header(headers.REQUEST_ID, rid)
end
end
},
header_filter = {
Expand Down
18 changes: 18 additions & 0 deletions kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ server {
proxy_set_header X-Forwarded-Path $upstream_x_forwarded_path;
proxy_set_header X-Forwarded-Prefix $upstream_x_forwarded_prefix;
proxy_set_header X-Real-IP $remote_addr;
> if enabled_headers_upstream["X-Kong-Request-Id"] then
proxy_set_header X-Kong-Request-Id $kong_request_id;
> end
proxy_pass_header Server;
proxy_pass_header Date;
proxy_ssl_name $upstream_host;
Expand Down Expand Up @@ -187,6 +190,9 @@ server {
proxy_set_header X-Forwarded-Path $upstream_x_forwarded_path;
proxy_set_header X-Forwarded-Prefix $upstream_x_forwarded_prefix;
proxy_set_header X-Real-IP $remote_addr;
> if enabled_headers_upstream["X-Kong-Request-Id"] then
proxy_set_header X-Kong-Request-Id $kong_request_id;
> end
proxy_pass_header Server;
proxy_pass_header Date;
proxy_ssl_name $upstream_host;
Expand Down Expand Up @@ -218,6 +224,9 @@ server {
proxy_set_header X-Forwarded-Path $upstream_x_forwarded_path;
proxy_set_header X-Forwarded-Prefix $upstream_x_forwarded_prefix;
proxy_set_header X-Real-IP $remote_addr;
> if enabled_headers_upstream["X-Kong-Request-Id"] then
proxy_set_header X-Kong-Request-Id $kong_request_id;
> end
proxy_pass_header Server;
proxy_pass_header Date;
proxy_ssl_name $upstream_host;
Expand Down Expand Up @@ -249,6 +258,9 @@ server {
proxy_set_header X-Forwarded-Path $upstream_x_forwarded_path;
proxy_set_header X-Forwarded-Prefix $upstream_x_forwarded_prefix;
proxy_set_header X-Real-IP $remote_addr;
> if enabled_headers_upstream["X-Kong-Request-Id"] then
proxy_set_header X-Kong-Request-Id $kong_request_id;
> end
proxy_pass_header Server;
proxy_pass_header Date;
proxy_ssl_name $upstream_host;
Expand All @@ -273,6 +285,9 @@ server {
grpc_set_header X-Forwarded-Path $upstream_x_forwarded_path;
grpc_set_header X-Forwarded-Prefix $upstream_x_forwarded_prefix;
grpc_set_header X-Real-IP $remote_addr;
> if enabled_headers_upstream["X-Kong-Request-Id"] then
grpc_set_header X-Kong-Request-Id $kong_request_id;
> end
grpc_pass_header Server;
grpc_pass_header Date;
grpc_ssl_name $upstream_host;
Expand Down Expand Up @@ -316,6 +331,9 @@ server {
proxy_set_header X-Forwarded-Path $upstream_x_forwarded_path;
proxy_set_header X-Forwarded-Prefix $upstream_x_forwarded_prefix;
proxy_set_header X-Real-IP $remote_addr;
> if enabled_headers_upstream["X-Kong-Request-Id"] then
proxy_set_header X-Kong-Request-Id $kong_request_id;
> end
proxy_pass_header Server;
proxy_pass_header Date;
proxy_ssl_name $upstream_host;
Expand Down

0 comments on commit 2befdb0

Please sign in to comment.