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

Conversation

Yiyiyimu
Copy link
Contributor

@Yiyiyimu Yiyiyimu commented Feb 1, 2021

Signed-off-by: yiyiyimu wosoyoung@gmail.com

fix #118

Some transformation examples:

00.750 -> .75
01.000 -> 1
01.010 -> 1.01

One question: do we need a switch for this transformation? Then maybe we need to pass come config when init lua-prometheus. Considering prometheus library in other language (golang, python) does not add leading and trailing zeros, maybe we could just directly remove them.

Signed-off-by: yiyiyimu <wosoyoung@gmail.com>
Signed-off-by: yiyiyimu <wosoyoung@gmail.com>
@coveralls
Copy link

coveralls commented Feb 1, 2021

Coverage Status

Coverage decreased (-0.1%) to 94.758% when pulling f3aa27c on Yiyiyimu:remove-zeros into 9308961 on knyar:master.

prometheus.lua Outdated

--remove leading zeros
local _
_, bucket = string.match(bucket, '(0*)(.*)')
Copy link
Contributor

Choose a reason for hiding this comment

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

This will remove all leading zeros (e.g. 00.01 -> .01) you'd probably want to leave one before decimal point (0.01).

Actually, I think the simplest way to do the number formatting here would be bucket = tostring(tonumber(bucket)). This will take care of both leading and trailing zeros. I'm not 100% sure about speed, but I think it won't be bad at all, because both functions are probably implemented in C and will be faster than using patterns and iterating strings in Lua.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's right! That is faster (regex in pure lua is indeed pretty slow) and way more elegant. Thanks a lot!

prometheus.lua Outdated
-- Returns:
-- (string) the formatted key
local function format_bucket_when_expose(key)
local all_le_string = string.match(key, '(le=".*")')
Copy link
Contributor

Choose a reason for hiding this comment

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

This will probably work correctly only if le is the last label. Otherwise, it'll match too much. E.g.: for key='my_metric{le="0.10",z="x"}' it will return 'le="0.10",z="x"'.

Also, it might match incorrectly if there were a label ending with 'le', e.g.: my_metric{stale="0",le="0.01"}.

I think this function deserves more thorough unit tests...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually le would always be the last label in the current implementation, see

local bucket_pref
if self.label_count > 0 then
-- strip last }
bucket_pref = self.name .. "_bucket" .. string.sub(labels, 1, #labels-1) .. ","
else
bucket_pref = self.name .. "_bucket{"
end
for i, buc in ipairs(self.buckets) do
full_name[i+2] = string.format("%sle=\"%s\"}", bucket_pref, self.bucket_format:format(buc))
end
so I think we don't need to worry about that.

But the later one is what I missed before. Thanks for remind!

I tried to get the last le=, but when talking about sum/count, there would be no label le but the others say stale, so get the last one is not appropriate. Right now I fix this problem by filter both ,le=" and {le=". Do you have some other suggestions?

And new unit tests added for 1. label "stale" 2. no labels.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I didn't check the implementation details. So it is probably ok to do it this way, but this assumption should be checked in unit tests (so it doesn't break in future, if someone changes the ordering of labels).

Regarding the checking: You can match both variants at once:

local part1, bucket, part2 = key:match('([,{]le=")(.*)(")')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I've added unit tests for these scenarios, thanks for remind!

And thanks for the regex suggestions🆒

…ould be count in while it should not

Signed-off-by: yiyiyimu <wosoyoung@gmail.com>
Signed-off-by: yiyiyimu <wosoyoung@gmail.com>
Copy link
Owner

@knyar knyar left a comment

Choose a reason for hiding this comment

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

Thank you for preparing this PR! Note that tests seem to be failing on lua 5.3 for some reason.

prometheus.lua Show resolved Hide resolved
prometheus.lua Outdated Show resolved Hide resolved
prometheus.lua Outdated Show resolved Hide resolved
Signed-off-by: yiyiyimu <wosoyoung@gmail.com>
…process

Signed-off-by: yiyiyimu <wosoyoung@gmail.com>
Signed-off-by: yiyiyimu <wosoyoung@gmail.com>
@Yiyiyimu
Copy link
Contributor Author

Yiyiyimu commented Feb 2, 2021

Thank you for preparing this PR! Note that tests seem to be failing on lua 5.3 for some reason.

Got it. It's pretty annoying that tonumber would have different output between different lua version (Ref)

> $ lua5.1 -e 'print(tonumber("9.0"))'
> 9
> $ lua5.3 -e 'print(tonumber("9.0"))'
> 9.0

Fixed with another check for the .0 tail

@dolik-rce
Copy link
Contributor

dolik-rce commented Feb 2, 2021

Well, the behaviour of Lua 5.3 is kind of inconsistent ☹️

tostring(tonumber("0100")) --> "100"
tostring(tonumber("0100.0")) --> "100.0"

Since there is only one type for integers and floats and those numbers are equal (tonumber("0100") == tonumber("0100.0") returns true), I'd expect that both would be converted to same string... Almost looks like a bug.

Anyway @Yiyiyimu, your solution (stripping trailing ".0") is OK, I can't think of anything better.

@Yiyiyimu
Copy link
Contributor Author

Yiyiyimu commented Feb 2, 2021

Hi @dolik-rce do I need to worry about the coverage check?

@dolik-rce
Copy link
Contributor

Hi @dolik-rce do I need to worry about the coverage check?

That's actually a question for @knyar, he's the maintainer. I'm just an interested bystander 😁

@Yiyiyimu
Copy link
Contributor Author

Yiyiyimu commented Feb 2, 2021

That's actually a question for @knyar, he's the maintainer. I'm just an interested bystander 😁

My bad I thought you also help to maintain the project🤣
You are so nice!! 🌹 Thanks again for the review!!

@knyar knyar merged commit 47d4db1 into knyar:master Feb 2, 2021
@knyar
Copy link
Owner

knyar commented Feb 2, 2021

Thanks a lot for preparing this PR and for addressing the comments! I'll wait for a few days and will cut a new release that includes this.

@Yiyiyimu
Copy link
Contributor Author

Yiyiyimu commented Feb 3, 2021

@knyar cool! plz ping me when the new release got published~

@knyar
Copy link
Owner

knyar commented Feb 3, 2021

Anyway @Yiyiyimu, your solution (stripping trailing ".0") is OK, I can't think of anything better.

Actually it seems that ngx_lua always uses lua 5.1, so there is no need to support 5.3. I have removed trimming of .0 in 857e1d9 to make the code simpler.

@Yiyiyimu
Copy link
Contributor Author

Yiyiyimu commented Feb 3, 2021

Actually it seems that ngx_lua always uses lua 5.1, so there is no need to support 5.3. I have removed trimming of .0 in 857e1d9 to make the code simpler.

Oh yes you're right! BTW we could also remove Travis CI for lua 5.2

@knyar
Copy link
Owner

knyar commented Feb 6, 2021

@knyar cool! plz ping me when the new release got published~

@Yiyiyimu I have just released 0.20210206. Thanks again for your contribution!

fffonion pushed a commit to Kong/lua-resty-prometheus that referenced this pull request Jul 19, 2021
…#119)

Bucket label values no longer have weird leading and trailing zeroes.
fffonion pushed a commit to Kong/lua-resty-prometheus that referenced this pull request Jul 19, 2021
…#119)

Bucket label values no longer have weird leading and trailing zeroes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

request help: any methods to remove leading and trailing zeros for histrogram buckets
4 participants