Skip to content

Commit

Permalink
metrics: add metrics for the upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed Oct 2, 2018
1 parent 849a489 commit 72a1734
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
37 changes: 37 additions & 0 deletions gateway/src/apicast/metrics/upstream.lua
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions gateway/src/apicast/policy/nginx_metrics/nginx_metrics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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

0 comments on commit 72a1734

Please sign in to comment.