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

feat: remove leading and trailing zeros #119

Merged
merged 7 commits into from
Feb 2, 2021
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
49 changes: 41 additions & 8 deletions prometheus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
-- used when labels were declared). "le" label for histogram metrics always
-- goes last;
-- * bucket boundaries (which are exposed as values of the "le" label) are
-- presented as floating point numbers with leading and trailing zeroes.
-- Number of of zeroes is determined for each bucketer automatically based on
-- bucket boundaries;
-- stored as floating point numbers with leading and trailing zeroes,
-- and those zeros would be removed just before we expose the metrics;
-- * internally "+Inf" bucket is stored as "Inf" (to make it appear after
-- all numeric buckets), and gets replaced by "+Inf" just before we
-- expose the metrics.
Expand All @@ -40,7 +39,14 @@
-- m1_count{site="site1"}
-- m1_sum{site="site1"}
--
-- "Inf" will be replaced by "+Inf" while publishing metrics.
-- And when expose, it would change to:
--
-- m1_bucket{site="site1",le="0.00005"}
-- m1_bucket{site="site1",le="10"}
-- m1_bucket{site="site1",le="1000"}
-- m1_bucket{site="site1",le="+Inf"}
-- m1_count{site="site1"}
-- m1_sum{site="site1"}
--
-- You can find the latest version and documentation at
-- https://github.com/knyar/nginx-lua-prometheus
Expand Down Expand Up @@ -261,6 +267,36 @@ local function construct_bucket_format(buckets)
return "%0" .. (max_order + max_precision + 1) .. "." .. max_precision .. "f"
end

-- Format bucket format when expose metrics.
--
-- This receives a key, remove leading and trailing zeros and return the cleaner
-- number, in order to make the output more clear, without effect on the increasing
-- order.
--
-- Args:
-- key: the metric key
--
-- Returns:
-- (string) the formatted key
local function format_bucket_when_expose(key)
local part1, bucket, part2 = key:match('(.*[,{]le=")(.*)(".*)')
if part1 == nil then
return key
Yiyiyimu marked this conversation as resolved.
Show resolved Hide resolved
end

if bucket == "Inf" then
return table.concat({part1, "+Inf", part2})
else
bucket = tostring(tonumber(bucket))
-- In lua5.3 when decimal part is zero, tonumber would not turn float to int like <5.3,
-- rather it would leave '.0' at the end. So trim it here.
if (bucket:sub(-2, -1) == ".0") then
bucket = bucket:sub(1, -3)
end
return table.concat({part1, bucket, part2})
end
end

-- Return a full metric name for a given metric+label combination.
--
-- This function calculates a full metric name (or, in case of a histogram
Expand Down Expand Up @@ -806,10 +842,7 @@ function Prometheus:metric_data()
end
seen_metrics[short_name] = true
end
-- Replace "Inf" with "+Inf" in each metric's last bucket 'le' label.
if key:find('le="Inf"', 1, true) then
key = key:gsub('le="Inf"', 'le="+Inf"')
end
key = format_bucket_when_expose(key)
table.insert(output, string.format("%s%s %s\n", self.prefix, key, value))
else
if type(err) == "string" then
Expand Down
27 changes: 18 additions & 9 deletions prometheus_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,8 @@ function TestPrometheus:testCustomBucketer2()
luaunit.assertEquals(ngx.logs, nil)
end
function TestPrometheus:testCollect()
local hist3 = self.p:histogram("b1", "Bytes", {"var"}, {100, 2000})
local hist3 = self.p:histogram("b1", "Bytes", {"var", "stale"}, {0.1, 100, 2000})
local hist4 = self.p:histogram("b2", "Labels", {}, {100, 2000})
self.counter1:inc(5)
self.counter2:inc(2, {"v2", "v1"})
self.counter2:inc(2, {"v2", "v1"})
Expand All @@ -586,10 +587,15 @@ function TestPrometheus:testCollect()
self.hist2:observe(3, {"ok", "site2"})
self.hist2:observe(7, {"ok", "site2"})
self.hist2:observe(70000, {"ok","site2"})
hist3:observe(50, {"ok"})
hist3:observe(50, {"ok"})
hist3:observe(150, {"ok"})
hist3:observe(5000, {"ok"})
hist3:observe(0.01, {"ok", "true"})
hist3:observe(50, {"ok", "true"})
hist3:observe(50, {"ok", "true"})
hist3:observe(150, {"ok", "true"})
hist3:observe(5000, {"ok", "true"})
hist4:observe(50, {})
hist4:observe(50, {})
hist4:observe(150, {})
hist4:observe(5000, {})
self.p:collect()

assert(find_idx(ngx.printed, "# HELP metric1 Metric 1") ~= nil)
Expand All @@ -607,10 +613,13 @@ function TestPrometheus:testCollect()

assert(find_idx(ngx.printed, "# TYPE b1 histogram") ~= nil)
assert(find_idx(ngx.printed, "# HELP b1 Bytes") ~= nil)
assert(find_idx(ngx.printed, 'b1_bucket{var="ok",le="0100.0"} 2') ~= nil)
assert(find_idx(ngx.printed, 'b1_sum{var="ok"} 5250') ~= nil)
assert(find_idx(ngx.printed, 'b1_bucket{var="ok",stale="true",le="0.1"} 1') ~= nil)
assert(find_idx(ngx.printed, 'b1_bucket{var="ok",stale="true",le="100"} 3') ~= nil)
assert(find_idx(ngx.printed, 'b1_sum{var="ok",stale="true"} 5250.01') ~= nil)
assert(find_idx(ngx.printed, 'b2_bucket{le="100"} 2') ~= nil)
assert(find_idx(ngx.printed, 'b2_sum{} 5250') ~= nil)

assert(find_idx(ngx.printed, 'l2_bucket{var="ok",site="site2",le="04.000"} 2') ~= nil)
assert(find_idx(ngx.printed, 'l2_bucket{var="ok",site="site2",le="4"} 2') ~= nil)
assert(find_idx(ngx.printed, 'l2_bucket{var="ok",site="site2",le="+Inf"} 4') ~= nil)

-- check that type comment exists and is before any samples for the metric.
Expand Down Expand Up @@ -647,7 +656,7 @@ function TestPrometheus:testCollectWithPrefix()

assert(find_idx(ngx.printed, "# TYPE test_pref_b1 histogram") ~= nil)
assert(find_idx(ngx.printed, "# HELP test_pref_b1 Bytes") ~= nil)
assert(find_idx(ngx.printed, 'test_pref_b1_bucket{var="ok",le="0100.0"} 2') ~= nil)
assert(find_idx(ngx.printed, 'test_pref_b1_bucket{var="ok",le="100"} 2') ~= nil)
assert(find_idx(ngx.printed, 'test_pref_b1_sum{var="ok"} 5250') ~= nil)
end

Expand Down