Skip to content

Commit

Permalink
[prometheus] create prometheus exporter skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed Feb 27, 2018
1 parent 0841451 commit 6df301c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions gateway/Roverfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ luassert 1.7.10-0||testing
luasystem 0.2.1-0||testing
markdown 0.33-1||development
mediator_lua 1.1.2-0||testing
nginx-lua-prometheus 0.20171117-4||production
penlight 1.5.4-1||production,development,testing
router 2.1-0||production
say 1.3-1||testing
1 change: 1 addition & 0 deletions gateway/apicast-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies = {
'liquid',
'argparse',
'penlight',
'nginx-lua-prometheus',
}
build = {
type = "make",
Expand Down
13 changes: 13 additions & 0 deletions gateway/conf/nginx.conf.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ http {
{% include "conf.d/apicast.conf" %}
}

{% if port.metrics %}
lua_shared_dict prometheus_metrics 16M;
server {
access_log off;
listen {{ port.metrics }} default_server;
server_name _;

location /metrics {
content_by_lua_block { require('apicast.executor'):metrics() }
}
}
{% endif %}

{% for file in "sites.d/*.conf" | filesystem %}
{% include file %}
{% endfor %}
Expand Down
3 changes: 2 additions & 1 deletion gateway/config/development.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ return {
lua_code_cache = 'on',
configuration_loader = 'lazy',
configuration_cache = 0,
configuration = data_url('application/json', configuration)
configuration = data_url('application/json', configuration),
port = { metrics = 9100 },
}
8 changes: 8 additions & 0 deletions gateway/src/apicast/executor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require('apicast.loader') -- to load code from deprecated paths
local PolicyChain = require('apicast.policy_chain')
local policy = require('apicast.policy')
local linked_list = require('apicast.linked_list')
local prometheus = require('apicast.prometheus')

local setmetatable = setmetatable

Expand Down Expand Up @@ -58,4 +59,11 @@ function _M:context(phase)
return shared_build_context(self)
end

local metrics = _M.metrics
--- Render metrics from all policies.
function _M:metrics(...)
metrics(self, ...)
return prometheus:collect()
end

return _M.new(PolicyChain.default())
2 changes: 1 addition & 1 deletion gateway/src/apicast/policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local PHASES = {
'rewrite', 'access',
'content', 'balancer',
'header_filter', 'body_filter',
'post_action', 'log'
'post_action', 'log', 'metrics',
}

local setmetatable = setmetatable
Expand Down
20 changes: 20 additions & 0 deletions gateway/src/apicast/prometheus.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local prometheus = require('nginx.prometheus')

local dict = 'prometheus_metrics'

if ngx.shared[dict] then
local init = prometheus.init(dict)

local metrics = { }
local __call = function(_, type, name, ...)
if not metrics[name] then
metrics[name] = init[type](init, name, ...)
end

return metrics[name]
end

return setmetatable({ }, { __call = __call, __index = init })
else
return function() end
end

0 comments on commit 6df301c

Please sign in to comment.