Skip to content

Commit

Permalink
refactor(plugin/prometheus): some small optimizations (#11144)
Browse files Browse the repository at this point in the history
* string.find

* ngx.get_phase

* string.len

* ngx.print

* suffix_idx + 1

* labels_start

* target_info.addresses
  • Loading branch information
chronolaw authored Jul 3, 2023
1 parent 49e5ca9 commit 3a491c8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
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

1 comment on commit 3a491c8

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:3a491c81b460aa6d5376a28cd8b953c166598d06
Artifacts available https://github.com/Kong/kong/actions/runs/5440009089

Please sign in to comment.