fix(prometheus): avoid negative latency caused by inconsistent Nginx metrics (#5150)

Co-authored-by: 罗泽轩 <spacewanderlzx@gmail.com>
This commit is contained in:
leslie 2021-09-29 08:56:32 +08:00 committed by GitHub
parent d413d77a9e
commit 58d60ca2a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -153,6 +153,14 @@ function _M.log(conf, ctx)
metrics.latency:observe(upstream_latency,
gen_arr("upstream", route_id, service_id, consumer_name, balancer_ip))
apisix_latency = apisix_latency - upstream_latency
-- The latency might be negative, as Nginx uses different time measurements in
-- different metrics.
-- See https://github.com/apache/apisix/issues/5146#issuecomment-928919399
if apisix_latency < 0 then
apisix_latency = 0
end
end
metrics.latency:observe(apisix_latency,
gen_arr("apisix", route_id, service_id, consumer_name, balancer_ip))
@ -165,9 +173,10 @@ function _M.log(conf, ctx)
end
local ngx_status_items = {"active", "accepted", "handled", "total",
"reading", "writing", "waiting"}
local label_values = {}
local ngx_status_items = {"active", "accepted", "handled", "total",
"reading", "writing", "waiting"}
local label_values = {}
local function nginx_status()
local res = ngx_capture("/apisix/nginx_status")
if not res or res.status ~= 200 then