Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(plugin/prometheus): some small optimizations #11144

Merged
merged 7 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions kong/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local kong = kong
local ngx = ngx
local get_phase = ngx.get_phase
local lower = string.lower
local concat = table.concat
local ngx_timer_pending_count = ngx.timer.pending_count
local ngx_timer_running_count = ngx.timer.running_count
local balancer = require("kong.runloop.balancer")
Expand Down Expand Up @@ -441,8 +440,9 @@ local function metric_data(write_fn)
if target_info ~= nil and target_info.addresses ~= nil and
#target_info.addresses > 0 then
-- healthchecks_off|healthy|unhealthy
for _, address in ipairs(target_info.addresses) do
local address_label = concat({address.ip, ':', address.port})
for i = 1, #target_info.addresses do
local address = target_info.addresses[i]
local address_label = address.ip .. ":" .. address.port
local status = lower(address.health)
set_healthiness_metrics(upstream_target_addr_health_table, upstream_name, target_name, address_label, status, metrics.upstream_target_health)
end
Expand Down
9 changes: 4 additions & 5 deletions kong/plugins/prometheus/prometheus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ local ngx_log = ngx.log
local ngx_sleep = ngx.sleep
local ngx_re_match = ngx.re.match
local ngx_re_gsub = ngx.re.gsub
local ngx_print = ngx.print
local error = error
local type = type
local pairs = pairs
Expand Down Expand Up @@ -255,7 +254,7 @@ local function short_metric_name(full_name)
-- `_bucket` suffix here, since it alphabetically goes before other
-- histogram suffixes (`_count` and `_sum`).
local suffix_idx, _ = full_name:find("_bucket{", 1, true)
if suffix_idx and full_name:find("le=", 1, true) then
if suffix_idx and full_name:find("le=", labels_start + 1, true) then
-- this is a histogram metric
return full_name:sub(1, suffix_idx - 1)
end
Expand Down Expand Up @@ -321,7 +320,7 @@ local function construct_bucket_format(buckets)

local dot_idx = as_string:find(".", 1, true)
max_order = math.max(max_order, dot_idx - 1)
max_precision = math.max(max_precision, as_string:len() - dot_idx)
max_precision = math.max(max_precision, #as_string - dot_idx)
end

return "%0" .. (max_order + max_precision + 1) .. "." .. max_precision .. "f"
Expand Down Expand Up @@ -757,7 +756,7 @@ function Prometheus.init(dict_name, options_or_prefix)
self:counter(self.error_metric_name, "Number of nginx-lua-prometheus errors")
self.dict:set(self.error_metric_name, 0)

if ngx.get_phase() == 'init_worker' then
if phase == 'init_worker' then
self:init_worker(self.sync_interval)
end
return self
Expand Down Expand Up @@ -919,7 +918,7 @@ function Prometheus:metric_data(write_fn, local_only)
ngx_log(ngx.ERR, "Prometheus module has not been initialized")
return
end
write_fn = write_fn or ngx_print
write_fn = write_fn or ngx.print

-- Force a manual sync of counter local state (mostly to make tests work).
self._counter:sync()
Expand Down
Loading