diff --git a/kong/plugins/prometheus/exporter.lua b/kong/plugins/prometheus/exporter.lua index 07f5b91..27ce2c5 100644 --- a/kong/plugins/prometheus/exporter.lua +++ b/kong/plugins/prometheus/exporter.lua @@ -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, @@ -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") @@ -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() @@ -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