Skip to content

Commit

Permalink
fix(admin-gui): add basic kong manager statistics to phone home
Browse files Browse the repository at this point in the history
Add `km_visits` to phone home report.

The counter will be increased when kong manager is visited,
but will not respond to the static assets request.

This fix KAG-2127
  • Loading branch information
nekolab committed Jul 21, 2023
1 parent 3e52664 commit e29272e
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
8 changes: 8 additions & 0 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ local constants = require "kong.constants"
local get_ctx_table = require("resty.core.ctx").get_ctx_table
local admin_gui = require "kong.admin_gui"
local wasm = require "kong.runloop.wasm"
local reports = require "kong.reports"


local kong = kong
Expand Down Expand Up @@ -221,6 +222,7 @@ do
"events:requests:ws",
"events:requests:wss",
"events:requests:go_plugins",
"events:km:visit",
"events:streams",
"events:streams:tcp",
"events:streams:tls",
Expand Down Expand Up @@ -1695,6 +1697,12 @@ function Kong.admin_gui_kconfig_content()
end
end

function Kong.admin_gui_log()
if kong.configuration.anonymous_reports then
reports.admin_gui_log(ngx.ctx)
end
end

function Kong.status_content()
return serve_content("kong.status")
end
Expand Down
14 changes: 14 additions & 0 deletions kong/reports.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ local TLS_STREAM_COUNT_KEY = "events:streams:tls"
local UDP_STREAM_COUNT_KEY = "events:streams:udp"


local KM_VISIT_COUNT_KEY = "events:km:visit"


local GO_PLUGINS_REQUEST_COUNT_KEY = "events:requests:go_plugins"


Expand Down Expand Up @@ -336,6 +339,7 @@ local function send_ping(host, port)
_ping_infos.grpcs_reqs = get_counter(GRPCS_REQUEST_COUNT_KEY)
_ping_infos.ws_reqs = get_counter(WS_REQUEST_COUNT_KEY)
_ping_infos.wss_reqs = get_counter(WSS_REQUEST_COUNT_KEY)
_ping_infos.km_visits = get_counter(KM_VISIT_COUNT_KEY)
_ping_infos.go_plugin_reqs = get_counter(GO_PLUGINS_REQUEST_COUNT_KEY)

_ping_infos.request_route_cache_hit_pos = get_counter(REQUEST_ROUTE_CACHE_HITS_KEY_POS)
Expand All @@ -352,6 +356,7 @@ local function send_ping(host, port)
reset_counter(GRPCS_REQUEST_COUNT_KEY, _ping_infos.grpcs_reqs)
reset_counter(WS_REQUEST_COUNT_KEY, _ping_infos.ws_reqs)
reset_counter(WSS_REQUEST_COUNT_KEY, _ping_infos.wss_reqs)
reset_counter(KM_VISIT_COUNT_KEY, _ping_infos.km_visits)
reset_counter(GO_PLUGINS_REQUEST_COUNT_KEY, _ping_infos.go_plugin_reqs)
reset_counter(REQUEST_ROUTE_CACHE_HITS_KEY_POS, _ping_infos.request_route_cache_hit_pos)
reset_counter(REQUEST_ROUTE_CACHE_HITS_KEY_NEG, _ping_infos.request_route_cache_hit_neg)
Expand Down Expand Up @@ -485,6 +490,15 @@ return {
incr_counter(count_key .. ":" .. ROUTE_CACHE_HITS_KEY .. ":" .. route_match_cached)
end
end,
admin_gui_log = function(ctx)
if not _enabled then
return
end

if ctx.km_visit_mark then
incr_counter(KM_VISIT_COUNT_KEY)
end
end,

-- custom methods
toggle = function(enable)
Expand Down
5 changes: 5 additions & 0 deletions kong/templates/nginx_kong_gui_include.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,10 @@ location ~* ^$(admin_gui_path_prefix)(?<path>/.*)?$ {
> end
sub_filter_once off;
sub_filter_types *;
log_by_lua_block {
ngx.ctx.km_visit_mark = true
Kong.admin_gui_log()
}
}
]]
88 changes: 88 additions & 0 deletions spec/02-integration/17-admin_gui/03-reports_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
local cjson = require "cjson"

local helpers = require "spec.helpers"
local constants = require "kong.constants"

describe("anonymous reports for kong manager visit", function()

local dns_hostsfile

local reports_send_ping = function()
ngx.sleep(0.2) -- hand over the CPU so other threads can do work (processing the sent data)
local admin_client = helpers.admin_client()
local res = admin_client:post("/reports/send-ping?port=" .. constants.REPORTS.STATS_TLS_PORT)
assert.response(res).has_status(200)
admin_client:close()
end

local assert_km_visits = function (value)
local reports_server = helpers.tcp_server(constants.REPORTS.STATS_TLS_PORT, {tls=true})
reports_send_ping()
local _, reports_data = assert(reports_server:join())
reports_data = cjson.encode(reports_data)

assert.match("km_visits=" .. value, reports_data)
end

lazy_setup(function()
dns_hostsfile = assert(os.tmpname() .. ".hosts")
local fd = assert(io.open(dns_hostsfile, "w"))
assert(fd:write("127.0.0.1 " .. constants.REPORTS.ADDRESS))
assert(fd:close())

local bp = assert(helpers.get_db_utils(nil, {}, { "reports-api" }))

bp.plugins:insert({
name = "reports-api",
config = {}
})

assert(helpers.start_kong({
admin_gui_listen = "127.0.0.1:9012",
anonymous_reports = true,
plugins = "bundled,reports-api",
dns_hostsfile = dns_hostsfile,
}))
end)

lazy_teardown(function()
os.remove(dns_hostsfile)

helpers.stop_kong()
end)

it("should have value 0 when no kong mananger visit occurs", function ()
assert_km_visits(0)
end)

it("should increase counter by 1 when kong mananger visit occurs", function ()
local admin_gui_client = helpers.admin_gui_client(nil, 9012)
assert(admin_gui_client:send({ method = "GET", path = "/" }))
admin_gui_client:close()

assert_km_visits(1)
end)

it("should reset the counter after report", function ()
assert_km_visits(0)

local admin_gui_client = helpers.admin_gui_client(nil, 9012)
assert(admin_gui_client:send({ method = "GET", path = "/" }))
admin_gui_client:close()

assert_km_visits(1)
end)

it("should not increase the counter for GUI assets", function ()
local admin_gui_client = helpers.admin_gui_client(nil, 9012)
admin_gui_client:send({ method = "GET", path = "/kconfig.js" })
admin_gui_client:send({ method = "GET", path = "/robots.txt" })
admin_gui_client:send({ method = "GET", path = "/favicon.ico" })
admin_gui_client:send({ method = "GET", path = "/test.js" })
admin_gui_client:send({ method = "GET", path = "/test.css" })
admin_gui_client:send({ method = "GET", path = "/test.png" })
admin_gui_client:close()

assert_km_visits(0)
end)
end)
5 changes: 5 additions & 0 deletions spec/fixtures/custom_nginx.template
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ server {
> end
sub_filter_once off;
sub_filter_types *;

log_by_lua_block {
ngx.ctx.km_visit_mark = true
Kong.admin_gui_log()
}
}
}
> end -- of (role == "control_plane" or role == "traditional") and #admin_listen > 0 and #admin_gui_listeners > 0
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/default_nginx.template
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ server {
> end
sub_filter_once off;
sub_filter_types *;

log_by_lua_block {
ngx.ctx.km_visit_mark = true
Kong.admin_gui_log()
}
}
}
> end -- of (role == "control_plane" or role == "traditional") and #admin_listen > 0 and #admin_gui_listeners > 0
Expand Down

0 comments on commit e29272e

Please sign in to comment.