Skip to content
This repository has been archived by the owner on Aug 7, 2021. It is now read-only.

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fffonion committed Apr 15, 2021
1 parent 54e87dc commit d4b7436
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 8 deletions.
13 changes: 7 additions & 6 deletions kong/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ if pok then
enterprise = require("kong.plugins.prometheus.enterprise.exporter")
end

local kong_subsystem = ngx.config.subsystem

local function init()
local shm = "prometheus_metrics"
Expand All @@ -35,7 +36,7 @@ local function init()
prometheus = require("kong.plugins.prometheus.prometheus").init(shm, "kong_")

-- global metrics
if ngx.config.subsystem == "http" then
if kong_subsystem == "http" then
metrics.connections = prometheus:gauge("nginx_http_current_connections",
"Number of HTTP connections",
{"state"})
Expand Down Expand Up @@ -66,13 +67,13 @@ local function init()

local res = kong.node.get_memory_stats()
for shm_name, value in pairs(res.lua_shared_dicts) do
memory_stats.shm_capacity:set(value.capacity, { shm_name, ngx.config.subsystem })
memory_stats.shm_capacity:set(value.capacity, { shm_name, kong_subsystem })
end

metrics.memory_stats = memory_stats

-- per service/route
if ngx.config.subsystem == "http" then
if kong_subsystem == "http" then
metrics.status = prometheus:counter("http_status",
"HTTP status codes per service/route in Kong",
{"service", "route", "code"})
Expand Down Expand Up @@ -129,7 +130,7 @@ end

local log

if ngx.config.subsystem == "http" then
if kong_subsystem == "http" then
function log(message, serialized)
if not metrics then
kong.log.err("prometheus: can not log metrics because of an initialization "
Expand Down Expand Up @@ -324,11 +325,11 @@ local function metric_data()
-- memory stats
local res = kong.node.get_memory_stats()
for shm_name, value in pairs(res.lua_shared_dicts) do
metrics.memory_stats.shms:set(value.allocated_slabs, { shm_name, ngx.config.subsystem })
metrics.memory_stats.shms:set(value.allocated_slabs, { shm_name, kong_subsystem })
end
for i = 1, #res.workers_lua_vms do
metrics.memory_stats.worker_vms:set(res.workers_lua_vms[i].http_allocated_gc,
{ res.workers_lua_vms[i].pid, ngx.config.subsystem })
{ res.workers_lua_vms[i].pid, kong_subsystem })
end

if enterprise then
Expand Down
64 changes: 63 additions & 1 deletion spec/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ describe("Plugin: prometheus (access)", function()
path = "/metrics",
})
local body = assert.res_status(200, res)
assert.matches('kong_memory_workers_lua_vms_bytes', body, nil, true)
assert.matches('kong_memory_workers_lua_vms_bytes{pid="%d+",kong_subsystem="http"}', body, nil, true)
assert.not_matches('kong_memory_workers_lua_vms_bytes{pid="%d+",kong_subsystem="stream"}', body, nil, true)

assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)
end)
Expand All @@ -301,6 +302,10 @@ describe("Plugin: prometheus (access)", function()
assert.matches('kong_memory_lua_shared_dict_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="http"}', body, nil, true)

assert.matches('kong_memory_lua_shared_dict_total_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="stream"} 5242880', body, nil, true)
assert.matches('kong_memory_lua_shared_dict_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="stream"}', body, nil, true)
assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)
end)

Expand All @@ -316,6 +321,63 @@ describe("Plugin: prometheus (access)", function()
end)
end)

describe("Plugin: prometheus (access) no stream listeners", function()
local admin_client

setup(function()
local bp = helpers.get_db_utils()

bp.plugins:insert {
protocols = { "http", "https", "grpc", "grpcs", "tcp", "tls" },
name = "prometheus"
}

assert(helpers.start_kong {
nginx_conf = nginx_conf,
plugins = "bundled, prometheus",
})
admin_client = helpers.admin_client()
end)

teardown(function()
if admin_client then
admin_client:close()
end

helpers.stop_kong()
end)

it("exposes Lua worker VM stats only for http subsystem", function()
local res = assert(admin_client:send {
method = "GET",
path = "/metrics",
})
local body = assert.res_status(200, res)
assert.matches('kong_memory_workers_lua_vms_bytes{pid="%d+",kong_subsystem="http"}', body, nil, true)
assert.not_matches('kong_memory_workers_lua_vms_bytes{pid="%d+",kong_subsystem="stream"}', body, nil, true)

assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)
end)

it("exposes lua_shared_dict metrics only for http subsystem", function()
local res = assert(admin_client:send {
method = "GET",
path = "/metrics",
})
local body = assert.res_status(200, res)
assert.matches('kong_memory_lua_shared_dict_total_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="http"} 5242880', body, nil, true)
assert.matches('kong_memory_lua_shared_dict_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="http"}', body, nil, true)

assert.not_matches('kong_memory_lua_shared_dict_total_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="stream"} 5242880', body, nil, true)
assert.not_matches('kong_memory_lua_shared_dict_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="stream"}', body, nil, true)
assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)
end)
end)

describe("Plugin: prometheus (access) per-consumer metrics", function()
local proxy_client
local admin_client
Expand Down
68 changes: 67 additions & 1 deletion spec/04-status_api_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local helpers = require "spec.helpers"
local pl_file = require "pl.file"

local TCP_PROXY_PORT = 9007

-- Note: remove the below hack when https://github.com/Kong/kong/pull/6952 is merged
local stream_available, _ = pcall(require, "kong.tools.stream_api")

Expand Down Expand Up @@ -138,6 +140,7 @@ describe("Plugin: prometheus (access via status API)", function()
nginx_conf = nginx_conf,
plugins = "bundled, prometheus",
status_listen = "0.0.0.0:9500",
stream_listen = "127.0.0.1:" .. TCP_PROXY_PORT,
})
proxy_client = helpers.proxy_client()
status_client = helpers.http_client("127.0.0.1", 9500, 20000)
Expand Down Expand Up @@ -373,7 +376,8 @@ describe("Plugin: prometheus (access via status API)", function()
path = "/metrics",
})
local body = assert.res_status(200, res)
assert.matches('kong_memory_workers_lua_vms_bytes', body, nil, true)
assert.matches('kong_memory_workers_lua_vms_bytes{pid="%d+",kong_subsystem="http"}', body, nil, true)
assert.matches('kong_memory_workers_lua_vms_bytes{pid="%d+",kong_subsystem="stream"}', body, nil, true)
end)

it("exposes lua_shared_dict metrics", function()
Expand All @@ -386,5 +390,67 @@ describe("Plugin: prometheus (access via status API)", function()
'{shared_dict="prometheus_metrics",kong_subsystem="http"} 5242880', body, nil, true)
assert.matches('kong_memory_lua_shared_dict_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="http"}', body, nil, true)
assert.matches('kong_memory_lua_shared_dict_total_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="stream"} 5242880', body, nil, true)
assert.matches('kong_memory_lua_shared_dict_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="stream"}', body, nil, true)
end)
end)

describe("Plugin: prometheus (access via status API), no stream listeners", function()
local status_client

setup(function()
local bp = helpers.get_db_utils()

bp.plugins:insert {
name = "prometheus"
}

status_client = helpers.http_client("127.0.0.1", 9500, 20000)

assert(helpers.start_kong {
nginx_conf = nginx_conf,
plugins = "bundled, prometheus",
status_listen = "0.0.0.0:9500",
})
end)

teardown(function()
if status_client then
status_client:close()
end

helpers.stop_kong()
end)

it("exposes Lua worker VM stats only for http subsystem", function()
local res = assert(status_client:send {
method = "GET",
path = "/metrics",
})
local body = assert.res_status(200, res)
assert.matches('kong_memory_workers_lua_vms_bytes{pid="%d+",kong_subsystem="http"}', body, nil, true)
assert.not_matches('kong_memory_workers_lua_vms_bytes{pid="%d+",kong_subsystem="stream"}', body, nil, true)

assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)
end)

it("exposes lua_shared_dict metrics only for http subsystem", function()
local res = assert(status_client:send {
method = "GET",
path = "/metrics",
})
local body = assert.res_status(200, res)
assert.matches('kong_memory_lua_shared_dict_total_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="http"} 5242880', body, nil, true)
assert.matches('kong_memory_lua_shared_dict_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="http"}', body, nil, true)
assert.not_matches('kong_memory_lua_shared_dict_total_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="stream"} 5242880', body, nil, true)
assert.not_matches('kong_memory_lua_shared_dict_bytes' ..
'{shared_dict="prometheus_metrics",kong_subsystem="stream"}', body, nil, true)

assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)
end)
end)

0 comments on commit d4b7436

Please sign in to comment.