Skip to content

Instantly share code, notes, and snippets.

@gdvalle
Created June 24, 2019 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdvalle/95f1abd55dcbe8e63876efa9f0395699 to your computer and use it in GitHub Desktop.
Save gdvalle/95f1abd55dcbe8e63876efa9f0395699 to your computer and use it in GitHub Desktop.
Record Prometheus metrics from nginx stub_status module using https://github.com/knyar/nginx-lua-prometheus
-- A module to export Prometheus metrics from nginx stub_status module.
local ngx = ngx
local ngx_var = ngx.var
local prometheus = prometheus
local select = select
local tonumber = tonumber
local find = string.find
local capture = ngx.location.capture
local log = ngx.log
local ERR = ngx.ERR
local _M = {_VERSION = "0.0.1"}
local mt = {__index = _M}
function _M.new(_, opts)
if not opts then
return nil, "no options table specified"
end
return setmetatable({
-- Location block with "stub_status;".
location=opts.location or "/nginx_status",
}, mt)
end
--- Connect, read, and parse metrics.
function _M.set_metrics(self)
local r = capture(self.location)
if r.status ~= 200 then
prometheus:inc("nginx_stub_status_failure_total")
local err_msg = "failure querying stub_status location, " .. tostring(self.location)
log(ERR, err_msg)
return err_msg
end
local accepted, handled, total = select(3, find(r.body, "accepts handled requests\n (%d*) (%d*) (%d*)"))
prometheus:set_key("nginx_connections_accepted_total", tonumber(accepted))
prometheus:set_key("nginx_connections_handled_total", tonumber(handled))
prometheus:set_key("nginx_connections_total", tonumber(total))
prometheus:set_key("nginx_connections_active", tonumber(ngx_var.connections_active))
prometheus:set_key("nginx_connections_reading", tonumber(ngx_var.connections_reading))
prometheus:set_key("nginx_connections_writing", tonumber(ngx_var.connections_writing))
prometheus:set_key("nginx_connections_waiting", tonumber(ngx_var.connections_waiting))
end
return _M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment