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

Commit

Permalink
feat(handler) expose dataplane status on control plane
Browse files Browse the repository at this point in the history
  • Loading branch information
fffonion committed Sep 15, 2020
1 parent 5ed9738 commit ec2ff51
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions kong/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local lower = string.lower
local concat = table.concat
local select = select
local balancer = require("kong.runloop.balancer")
local cjson = require("cjson")

local DEFAULT_BUCKETS = { 1, 2, 5, 7, 10, 15, 20, 25, 30, 40, 50, 60, 70,
80, 90, 100, 200, 300, 400, 500, 1000,
Expand All @@ -13,6 +14,8 @@ local metrics = {}
-- prometheus.lua instance
local prometheus

local cp_metrics

-- use the same counter library shipped with Kong
package.loaded['prometheus_resty_counter'] = require("resty.counter")

Expand Down Expand Up @@ -71,6 +74,18 @@ local function init()
"Total bandwidth in bytes " ..
"consumed per service/route in Kong",
{"service", "route", "type"})


-- Hybrid mode status
if kong.version_num >= 2000000 then -- 200.00.00 -> 2.0.0 then
if kong.configuration.role == "control_plane" then
cp_metrics = true

metrics.dataplane_last_seen = prometheus:gauge("dataplane_last_seen",
"Last time data plane contacted control plane",
{"node_id", "hostname", "ip", "hash"})
end
end
end

local function init_worker()
Expand Down Expand Up @@ -236,6 +251,19 @@ local function collect()
{res.workers_lua_vms[i].pid})
end

-- Hybrid mode status
if cp_metrics then
-- Cleanup old metrics
metrics.dataplane_last_seen:reset()

local shdict = ngx.shared.kong_clustering
for _, node_id in ipairs(shdict:get_keys()) do
local status = cjson.decode(shdict:get(node_id))

metrics.dataplane_last_seen:set(status.last_seen, { node_id, status.hostname, status.ip, status.config_hash })
end
end

prometheus:collect()
end

Expand Down

0 comments on commit ec2ff51

Please sign in to comment.