diff --git a/gateway/src/apicast/metrics/upstream.lua b/gateway/src/apicast/metrics/upstream.lua new file mode 100644 index 000000000..8a7fa573f --- /dev/null +++ b/gateway/src/apicast/metrics/upstream.lua @@ -0,0 +1,37 @@ +local tonumber = tonumber + +local prometheus = require('apicast.prometheus') + +local _M = {} + +local upstream_status_codes = prometheus( + 'counter', + 'upstream_status', + 'HTTP status from upstream servers', + { 'status' } +) + +local upstream_resp_times = prometheus( + 'histogram', + 'upstream_resp_times', + 'Response times from upstream servers' +) + +local function inc_status_codes_counter(status) + if status and upstream_status_codes then + upstream_status_codes:inc(1, { status }) + end +end + +local function add_resp_time(response_time) + if response_time and upstream_resp_times then + upstream_resp_times:observe(tonumber(response_time)) + end +end + +function _M.report(status, response_time) + inc_status_codes_counter(status) + add_resp_time(response_time) +end + +return _M diff --git a/gateway/src/apicast/policy/nginx_metrics/nginx_metrics.lua b/gateway/src/apicast/policy/nginx_metrics/nginx_metrics.lua index 158b726ea..1d9a99622 100644 --- a/gateway/src/apicast/policy/nginx_metrics/nginx_metrics.lua +++ b/gateway/src/apicast/policy/nginx_metrics/nginx_metrics.lua @@ -9,6 +9,8 @@ local select = select local find = string.find local pairs = pairs +local upstream_metrics = require('apicast.metrics.upstream') + local new = _M.new local log_levels_list = { @@ -110,4 +112,8 @@ function _M:metrics() end end +function _M.log() + upstream_metrics.report(ngx.var.upstream_status, ngx.var.upstream_response_time) +end + return _M