Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add config to control write all status to x-upsream-apisix-status #6392

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8ac287c
feat: add config to control write all status to x-upsream-apisix-status
Feb 21, 2022
e41138e
doc: add doc
Feb 21, 2022
fa58fdc
Merge branch 'master' into feature/resp-all-upstream-status
Feb 21, 2022
6b9dd1c
fix: modify config.json
Feb 21, 2022
f696391
fix: modify doc
Feb 21, 2022
b2bf96c
fix: remove datadog config.json
Feb 21, 2022
15dcb93
fix: modify test case & fix some error
Feb 21, 2022
d285758
fix: modify test case. remove duplicate code
Feb 21, 2022
5b24a19
merge:maser
Feb 21, 2022
f1e280a
fix: remove ;
Feb 21, 2022
2b82f96
fix: rename config name
Feb 21, 2022
363f4ad
fix:remove datalog from config.json
Feb 21, 2022
627cacf
fix: modify test case
Feb 22, 2022
b236178
fix: test case
Feb 22, 2022
737bbbf
Merge branch 'master' into feature/resp-all-upstream-status
Feb 22, 2022
b57f265
fix: test case
Feb 22, 2022
af58a93
fix: test case
Feb 22, 2022
f033c0a
fix: test case
Feb 22, 2022
12dc467
fix: test case
Feb 22, 2022
a102aa6
fix: fix test case
Feb 22, 2022
6e0c097
fix: when return 200 do not log
Feb 23, 2022
840daa5
fix: test case
Feb 23, 2022
2691ab3
feat: add log
Feb 23, 2022
d86e765
fix: add log
Feb 23, 2022
babb0d2
fix: add log
Feb 23, 2022
922cc84
fix: add test
Feb 23, 2022
5b3c2c0
fix: modify
Feb 23, 2022
6009c2b
fix: test case
Feb 23, 2022
6b9e435
fix: add log
Feb 23, 2022
bcd4112
fix: add test case
Feb 23, 2022
cb07135
fix: remove log
Feb 23, 2022
07db3ac
fix: format code
Feb 23, 2022
8a07c89
fix: modify test case
Feb 23, 2022
f0b7400
fix: modify test case
Feb 23, 2022
6264b2b
fix: modify
Feb 23, 2022
0f06ba1
fix: modify resp status
Feb 23, 2022
aec91c8
docs: modify docs
Feb 23, 2022
f8aabe1
Merge branch 'master' into feature/resp-all-upstream-status
Feb 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,27 @@ end


local function set_resp_upstream_status(up_status)
core.response.set_header("X-APISIX-Upstream-Status", up_status)
core.log.info("X-APISIX-Upstream-Status: ", up_status)
local_conf = core.config.local_conf()

if local_conf.apisix and local_conf.apisix.show_upstream_status_in_response_header then
core.response.set_header("X-APISIX-Upstream-Status", up_status)
elseif #up_status == 3 then
if tonumber(up_status) >= 500 and tonumber(up_status) <= 599 then
core.response.set_header("X-APISIX-Upstream-Status", up_status)
end
elseif #up_status > 3 then
-- the up_status can be "502, 502" or "502, 502 : "
local last_status
if str_byte(up_status, -1) == str_byte(" ") then
last_status = str_sub(up_status, -6, -3)
else
last_status = str_sub(up_status, -3)
end

if tonumber(last_status) >= 500 and tonumber(last_status) <= 599 then
core.response.set_header("X-APISIX-Upstream-Status", up_status)
end
end
end


Expand All @@ -589,23 +608,8 @@ function _M.http_header_filter_phase()
core.response.set_header("Server", ver_header)

local up_status = get_var("upstream_status")
if up_status and #up_status == 3
and tonumber(up_status) >= 500
and tonumber(up_status) <= 599
then
if up_status then
set_resp_upstream_status(up_status)
elseif up_status and #up_status > 3 then
-- the up_status can be "502, 502" or "502, 502 : "
local last_status
if str_byte(up_status, -1) == str_byte(" ") then
last_status = str_sub(up_status, -6, -3)
else
last_status = str_sub(up_status, -3)
end

if tonumber(last_status) >= 500 and tonumber(last_status) <= 599 then
set_resp_upstream_status(up_status)
end
end

common_phase("header_filter")
Expand Down
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ apisix:
enable_admin_cors: true # Admin API support CORS response headers.
enable_dev_mode: false # Sets nginx worker_processes to 1 if set to true
enable_reuseport: true # Enable nginx SO_REUSEPORT switch if set to true.
show_upstream_status_in_response_header: false # when true all upstream status write to `X-APISIX-Upstream-Status` otherwise only 5xx code
enable_ipv6: true
config_center: etcd # etcd: use etcd to store the config value
# yaml: fetch the config value from local yaml file `/your_path/conf/apisix.yaml`
Expand Down
2 changes: 1 addition & 1 deletion docs/en/latest/debug-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ title: Debug Function

## `5xx` response status code

Similar `5xx` status codes such as 500, 502, 503, etc., are the status codes in response to a server error. When a request has a `5xx` status code; it may come from `APISIX` or `Upstream`. How to identify the source of these response status codes is a very meaningful thing. It can quickly help us determine the problem.
Similar `5xx` status codes such as 500, 502, 503, etc., are the status codes in response to a server error. When a request has a `5xx` status code; it may come from `APISIX` or `Upstream`. How to identify the source of these response status codes is a very meaningful thing. It can quickly help us determine the problem. (When modifying the configuration `show_upstream_status_in_response_header` in `conf/config.yaml` to `true`, all upstream status codes will be returned, not only `5xx` status.)

## How to identify the source of the `5xx` response status code

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/latest/debug-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ title: 调试功能

## `5xx` 响应状态码

500、502、503 等类似的 `5xx` 状态码,是由于服务器错误而响应的状态码,当一个请求出现 `5xx` 状态码时;它可能来源于 `APISIX` 或 `Upstream` 。如何识别这些响应状态码的来源,是一件很有意义的事,它能够快速的帮助我们确定问题的所在。
500、502、503 等类似的 `5xx` 状态码,是由于服务器错误而响应的状态码,当一个请求出现 `5xx` 状态码时;它可能来源于 `APISIX` 或 `Upstream` 。如何识别这些响应状态码的来源,是一件很有意义的事,它能够快速的帮助我们确定问题的所在。(当修改 `conf/config.yaml` 的配置 `show_upstream_status_in_response_header` 为 `true` 时,会返回所有上游状态码,不仅仅是 `5xx` 状态。)

## 如何识别 `5xx` 响应状态码的来源

Expand Down
8 changes: 4 additions & 4 deletions t/node/upstream-status-5xx.t
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ GET /mysleep?seconds=1
--- error_code: 504
--- response_body eval
qr/504 Gateway Time-out/
--- error_log
--- response_headers
X-APISIX-Upstream-Status: 504


Expand Down Expand Up @@ -163,7 +163,7 @@ GET /hello
--- error_code: 502
--- response_body eval
qr/502 Bad Gateway/
--- error_log
--- response_headers
X-APISIX-Upstream-Status: 502


Expand Down Expand Up @@ -208,7 +208,7 @@ GET /server_error
--- error_code: 500
--- response_body_like
.*apisix.apache.org.*
--- error_log
--- response_headers
X-APISIX-Upstream-Status: 500


Expand Down Expand Up @@ -322,7 +322,7 @@ passed
--- request
GET /hello
--- error_code: 502
--- error_log eval
--- response_headers_raw_like eval
qr/X-APISIX-Upstream-Status: 502, 502, 502/


Expand Down
Loading