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

Commit

Permalink
export current config hash
Browse files Browse the repository at this point in the history
  • Loading branch information
fffonion committed May 17, 2021
1 parent 82fabac commit 246ecb1
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions kong/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ local metrics = {}
-- prometheus.lua instance
local prometheus

-- the clustering module, lazy imported
local clustering
-- the clustering and declarative module, lazily imported
local clustering, declarative
local cp_metrics

-- use the same counter library shipped with Kong
Expand Down Expand Up @@ -104,8 +104,18 @@ local function init()
enterprise.init(prometheus)
end


-- For all generic dbless node
if kong.configuration.database == "off" then
declarative = require("kong.db.declarative")

metrics.config_current_hash = prometheus:gauge("config_current_hash",
"Current config hash numeric value for this node")
end

local role = kong.configuration.role
-- Hybrid mode status
if kong.configuration.role == "control_plane" then
if role == "control_plane" then
cp_metrics = true

clustering = require("kong.clustering")
Expand All @@ -123,6 +133,12 @@ local function init_worker()
prometheus:init_worker()
end

-- Convert the MD5 hex string to its numeric representation
-- Note the following will be represented as a float instead of int64 since luajit
-- don't like int64. Good news is prometheus uses float instead of int64 as well
local function config_hash_to_number(hash_str)
return tonumber("0x" .. hash_str)
end

-- Since in the prometheus library we create a new table for each diverged label
-- so putting the "more dynamic" label at the end will save us some memory
Expand Down Expand Up @@ -354,6 +370,14 @@ local function metric_data()
enterprise.metric_data()
end

if metrics.config_current_hash then
local current_hash = declarative.get_current_hash()
-- current_hash is true if it's load from file
if current_hash and current_hash ~= true then
metrics.config_current_hash:set(config_hash_to_number(current_hash))
end
end

-- Hybrid mode status
if cp_metrics then
-- Cleanup old metrics
Expand All @@ -364,9 +388,7 @@ local function metric_data()
for node_id, status in pairs(data_planes) do
local labels = { node_id, status.hostname, status.ip }
metrics.dataplane_last_seen:set(status.last_seen, labels)
-- Note the following will be represented as a float instead of int64 since luajit
-- don't like int64. Good news is prometheus uses float instead of int64 as well
metrics.dataplane_config_hash:set(tonumber("0x" .. status.config_hash), labels)
metrics.dataplane_config_hash:set(config_hash_to_number(status.config_hash), labels)
end
end

Expand Down

0 comments on commit 246ecb1

Please sign in to comment.