Skip to content

Commit

Permalink
fix: fix several problems after updating to the newest db
Browse files Browse the repository at this point in the history
#24 introduced issues after porting to new DAO.
This change fixes the following:

- kong.log.err/warn instead of ERR/WARN
- usage of responses.lua instead of kong.response.exit
- unnecessary use of the consumer's schema in api.lua

From #26
  • Loading branch information
kikito authored and hbagdi committed Oct 17, 2018
1 parent ad1d784 commit 89aef2f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
13 changes: 11 additions & 2 deletions kong/plugins/prometheus/api.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
local Schema = require("kong.db.schema")
local prometheus = require "kong.plugins.prometheus.exporter"

local consumers_schema = kong.db.consumers.schema
-- schemas are used to parse parameters, for example: all params in a
-- form-url-encoded field arrive to the server as strings. But if the provided
-- schema has a field called `timeout` of type `number` then it will be transformed
-- into a number before being passed down to the functions below.
--
-- On this particular case the Prometheus lib uses no schemas and the /metrics
-- path accepts no parameters, but we still need to supply a schema in order to use the
-- "new-db-style" admin API. So we generate an empty one on the fly.
local empty_schema = Schema.new({ fields = {} })

return {

["/metrics"] = {
schema = consumers_schema, -- not used, could be any schema
schema = empty_schema,
methods = {
GET = function()
prometheus.collect()
Expand Down
13 changes: 6 additions & 7 deletions kong/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local responses = require "kong.tools.responses"
local find = string.find
local select = select

Expand All @@ -12,7 +11,7 @@ local prometheus
local function init()
local shm = "prometheus_metrics"
if not ngx.shared.prometheus_metrics then
kong.log.ERR("prometheus: ngx shared dict 'prometheus_metrics' not found")
kong.log.err("prometheus: ngx shared dict 'prometheus_metrics' not found")
return
end

Expand Down Expand Up @@ -41,7 +40,7 @@ end

local function log(message)
if not metrics then
kong.log.ERR("prometheus: can not log metrics because of an initialization "
kong.log.err("prometheus: can not log metrics because of an initialization "
.. "error, please make sure that you've declared "
.. "'prometheus_metrics' shared dict in your nginx template")
return
Expand Down Expand Up @@ -82,15 +81,15 @@ end

local function collect()
if not prometheus or not metrics then
kong.log.ERR("prometheus: plugin is not initialized, please make sure ",
kong.log.err("prometheus: plugin is not initialized, please make sure ",
" 'prometheus_metrics' shared dict is present in nginx template")
return responses.send_HTTP_INTERNAL_SERVER_ERROR()
return kong.response.exit(500, { message = "An unexpected error occurred" })
end

local r = ngx.location.capture "/nginx_status"

if r.status ~= 200 then
kong.log.WARN("prometheus: failed to retrieve /nginx_status ",
kong.log.warn("prometheus: failed to retrieve /nginx_status ",
"while processing /metrics endpoint")

else
Expand All @@ -113,7 +112,7 @@ local function collect()

else
metrics.db_reachable:set(0)
kong.log.ERR("prometheus: failed to reach database while processing",
kong.log.err("prometheus: failed to reach database while processing",
"/metrics endpoint: ", err)
end

Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/prometheus/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function PrometheusHandler:log(conf) -- luacheck: ignore 212
local message = basic_serializer.serialize(ngx)
local ok, err = ngx.timer.at(0, log, message)
if not ok then
kong.log.ERR("failed to create timer: ", err)
kong.log.err("failed to create timer: ", err)
end
end

Expand Down

0 comments on commit 89aef2f

Please sign in to comment.