From 1a97e630c86beed48fec4f171730d194cedc09db Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Fri, 20 Oct 2023 14:58:57 +0800 Subject: [PATCH] perf(request-id): use `proxy_set_header` instead of `ngx.req.set_header` (#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 --- kong/runloop/handler.lua | 14 -------------- kong/templates/nginx_kong.lua | 18 ++++++++++++++++++ spec/fixtures/custom_nginx.template | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/kong/runloop/handler.lua b/kong/runloop/handler.lua index 0ce909baecb6..41d26d5e135e 100644 --- a/kong/runloop/handler.lua +++ b/kong/runloop/handler.lua @@ -36,7 +36,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 @@ -1473,9 +1472,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. @@ -1557,16 +1553,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 }, response = { diff --git a/kong/templates/nginx_kong.lua b/kong/templates/nginx_kong.lua index 84330518720a..7aa5e5b7246d 100644 --- a/kong/templates/nginx_kong.lua +++ b/kong/templates/nginx_kong.lua @@ -205,6 +205,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; @@ -236,6 +239,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; @@ -267,6 +273,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; @@ -298,6 +307,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; @@ -322,6 +334,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; @@ -356,6 +371,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; diff --git a/spec/fixtures/custom_nginx.template b/spec/fixtures/custom_nginx.template index 22402199ad4f..8987e935f2ae 100644 --- a/spec/fixtures/custom_nginx.template +++ b/spec/fixtures/custom_nginx.template @@ -218,6 +218,9 @@ http { 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; @@ -249,6 +252,9 @@ http { 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; @@ -279,6 +285,9 @@ http { proxy_set_header X-Forwarded-Port $upstream_x_forwarded_port; 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; @@ -309,6 +318,9 @@ http { proxy_set_header X-Forwarded-Port $upstream_x_forwarded_port; 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; @@ -367,6 +379,9 @@ http { 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;