Skip to content

Commit

Permalink
Merge pull request #9 from yciabaud/fixes
Browse files Browse the repository at this point in the history
Remove init and admin errors
  • Loading branch information
yciabaud authored May 16, 2018
2 parents 5c8a546 + e32d4a1 commit 908e3c9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
3 changes: 1 addition & 2 deletions api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ return {
logger:logAdmin(plugin.config)
end

logger:collect()
helpers.responses.send_HTTP_OK()
return logger:collect()
end
}
}
29 changes: 23 additions & 6 deletions logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ end

local function update_metric(metric_name, stat_type, stat_value, label_values)
ngx_log(NGX_DEBUG, string.format("Prometheus: log metric %s (%s)", metric_name, stat_type))

if metrics == nil then
ngx_log(NGX_ERR, string.format("Prometheus: metrics dictionary not found"))
return
end
local metric = metrics[metric_name]
if metric == nil then
ngx_log(NGX_ERR, string.format("Prometheus: metrics %s not found", metric_name))
Expand Down Expand Up @@ -68,6 +71,11 @@ function PrometheusLogger:init(config)
end

function PrometheusLogger:log(message, config)
if prometheus == nil then
ngx_log(NGX_DEBUG, string.format("Prometheus: plugin not initialized"))
PrometheusLogger:init(config)
return
end
ngx_log(NGX_DEBUG, "Prometheus: logging metrics...")

local api_name
Expand All @@ -77,11 +85,11 @@ function PrometheusLogger:log(message, config)
api_name = string_gsub(message.api.name, "%.", "_")
end
local stat_value = {
http_request_size_bytes = tonumber(message.request.size),
http_response_size_bytes = tonumber(message.response.size),
http_request_duration_ms = message.latencies.request,
http_upstream_duration_ms = message.latencies.proxy,
http_kong_duration_ms = message.latencies.kong,
http_request_size_bytes = tonumber(message.request.size),
http_response_size_bytes = tonumber(message.response.size),
http_request_duration_ms = tonumber(message.latencies.request),
http_upstream_duration_ms = tonumber(message.latencies.proxy),
http_kong_duration_ms = tonumber(message.latencies.kong),
http_requests_total = 1,
}

Expand All @@ -108,6 +116,11 @@ function PrometheusLogger:log(message, config)
end

function PrometheusLogger:logAdmin(config)
if prometheus == nil then
ngx_log(NGX_DEBUG, string.format("Prometheus: plugin not initialized"))
PrometheusLogger:init(config)
return
end
ngx_log(NGX_DEBUG, "Prometheus: logging metrics admin...")

local stat_value
Expand Down Expand Up @@ -153,6 +166,10 @@ function PrometheusLogger:logAdmin(config)
end

function PrometheusLogger:collect()
if prometheus == nil then
ngx_log(NGX_ERR, string.format("Prometheus: plugin not initialized properly"))
return
end
prometheus:collect()
end

Expand Down
8 changes: 8 additions & 0 deletions prometheus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
-- https://github.com/knyar/nginx-lua-prometheus
-- Released under MIT license.

local ngx_log = ngx.log
local NGX_ERR = ngx.ERR

-- Default set of latency buckets, 5ms to 10s:
local DEFAULT_BUCKETS = {0.005, 0.01, 0.02, 0.03, 0.05, 0.075, 0.1, 0.2, 0.3,
Expand Down Expand Up @@ -268,6 +270,10 @@ end
function Prometheus.init(dict_name, prefix)
local self = setmetatable({}, Prometheus)
self.dict = ngx.shared[dict_name or "kong_cache"]
if self.dict == nil then
ngx_log(NGX_ERR, string.format("Prometheus: dictionary %s not available", dict_name or "kong_cache"))
return
end
self.help = {}
if prefix then
self.prefix = prefix
Expand Down Expand Up @@ -494,6 +500,7 @@ end
-- It will get the metrics from the dictionary, sort them, and expose them
-- aling with TYPE and HELP comments.
function Prometheus:collect()
ngx.status = 200
ngx.header.content_type = "text/plain"
if not self.initialized then
ngx.log(ngx.ERR, "Prometheus module has not been initialized")
Expand Down Expand Up @@ -528,6 +535,7 @@ function Prometheus:collect()
end
end
end
return ngx.exit(200)
end

return Prometheus

0 comments on commit 908e3c9

Please sign in to comment.