Created
June 24, 2019 15:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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