From 89aef2fad43466202ed8eda7b1c285aaa385f92f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Wed, 17 Oct 2018 20:32:02 +0200 Subject: [PATCH] fix: fix several problems after updating to the newest db #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 --- kong/plugins/prometheus/api.lua | 13 +++++++++++-- kong/plugins/prometheus/exporter.lua | 13 ++++++------- kong/plugins/prometheus/handler.lua | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/kong/plugins/prometheus/api.lua b/kong/plugins/prometheus/api.lua index d9fbf4acaea..c25d5593e9a 100644 --- a/kong/plugins/prometheus/api.lua +++ b/kong/plugins/prometheus/api.lua @@ -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() diff --git a/kong/plugins/prometheus/exporter.lua b/kong/plugins/prometheus/exporter.lua index ea41945d073..c1c6e4b5796 100644 --- a/kong/plugins/prometheus/exporter.lua +++ b/kong/plugins/prometheus/exporter.lua @@ -1,4 +1,3 @@ -local responses = require "kong.tools.responses" local find = string.find local select = select @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/kong/plugins/prometheus/handler.lua b/kong/plugins/prometheus/handler.lua index d7ff220c9d0..8fb54785673 100644 --- a/kong/plugins/prometheus/handler.lua +++ b/kong/plugins/prometheus/handler.lua @@ -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