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

config: fix detailed metric keys missing in leading keys #528

Conversation

birdayz
Copy link
Contributor

@birdayz birdayz commented Feb 29, 2024

This patch fixes a bug in detailed metrics.

only the last descriptor uses the provided descriptor value in case of detailed metrics. when traversing the list of descriptors, the code "loses" the previous keys. this leads to metrics like:

descriptors = 
- key: first-key
  value: first-value
- key: second-key
  value: second-value
 
=> metric:

"test-domain.first-key_.second-key_second-value"

Value of the first descriptor is missing.
The last descriptor properly uses the detailed metric descriptor value, but all other descriptors (the first one here) are missing the value.

We would expect:

"test-domain.first-key_first_value.second-key_second-value"

this patch introduces a new string builder, that builds the detailed metric as the iteration of the input descriptor is happening. a unit test is attached to show the behavior. it fails without the new code, and successfully preserves all descriptor keys with the patched code.

added a few tests, i'm confident the change works as expected.

formatting changes were done by pre-commit. i'm a bit surprised, are these expected?

@birdayz birdayz force-pushed the jb/fix-detailed-metric-descriptor-values-missing branch from e20cb04 to 0ae8634 Compare February 29, 2024 22:03
@birdayz birdayz force-pushed the jb/fix-detailed-metric-descriptor-values-missing branch 5 times, most recently from 87f87b6 to 220d628 Compare March 1, 2024 09:01
@birdayz
Copy link
Contributor Author

birdayz commented Mar 1, 2024

@envoyproxy/ratelimit-maintainers it would be great if you can have a look. thank you!

@birdayz birdayz force-pushed the jb/fix-detailed-metric-descriptor-values-missing branch 4 times, most recently from ea9e09a to 9c812d9 Compare March 1, 2024 10:06
@birdayz
Copy link
Contributor Author

birdayz commented Mar 6, 2024

@mattklein123 @renuka-fernando excuse me for tagging directly. I am not sure if the suggested tag @envoyproxy/ratelimit-maintainers actually works, does not look like it.

Could you have a look? Thank you!

@birdayz
Copy link
Contributor Author

birdayz commented Apr 2, 2024

@mattklein123 @renuka-fernando kind ping, if you could have a look.
thank you!

@mattklein123 mattklein123 self-assigned this Apr 2, 2024
Copy link
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

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

I do actually prefer your formatting changes, but it makes it hard to see the actual diff. Can you revert all of the formatting changes and just do the actual change? If you want to do a different PR with formatting changes only that is fine, thanks.

only the last descriptor uses the provided descriptor value in case of
detailed metrics. when traversing the list of descriptors, the code
"loses" the previous keys. this leads to metrics like:
"test-domain.first-key_.second-key_second-value", where the last
descriptor properly uses the detailed metric descriptor value, but all
other descriptors (the first one here) are missing the value.

this patch introduces a new string builder, that builds the detailed
metric as the iteration of the input descriptor is happening.
a unit test is attached to show the behavior. it fails without the new
code, and successfully preserves all descriptor keys with the patched
code.

Signed-off-by: Johannes Brüderl <johannes.bruederl@gmail.com>
@birdayz birdayz force-pushed the jb/fix-detailed-metric-descriptor-values-missing branch from 9c812d9 to 1272392 Compare April 3, 2024 10:56
@birdayz
Copy link
Contributor Author

birdayz commented Apr 3, 2024

@mattklein123 thanks for the feedback! i removed the formatting changes - could you review again?
thanks!

Comment on lines +310 to +312
// Build detailed metric as we traverse the list of descriptors
var detailedMetricFullKey strings.Builder
detailedMetricFullKey.WriteString(domain)
Copy link
Member

Choose a reason for hiding this comment

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

perf nit: can we only do this if detailed metrics are enabled?

Copy link
Contributor Author

@birdayz birdayz Apr 3, 2024

Choose a reason for hiding this comment

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

i fear it's not easily possible. only once we traversed the full "tree", we know if the leaf is a detailed metric (it can be hierarchical, and the leaf is the deciding factor).

so we build this string, even if we discard it later (line 364).

the previous solution loses this information because of exactly this reason (keys of previous descriptors in the "tree" are lost). so, basically, we need to memorize the path we have taken (including the keys AND values of the descriptors) we have seen, even if we drop it later.

i can think of two alternatives:

  • we could traverse the tree again, after we have traversed it once, and see that the leaf has detailed metrics on (line 364). I haven't benchmarked, but i don't feel that this is faster than doing a bit of string concatenation (which cost most likely way less than the entire walk through the hierarchy)
  • add a settings option to ratelimit-service, that steers detailed metrics in general. i.e. you can completely turn it off if you wish. however, even here we would spend a little more time doing this string concat, if somebody wants to have detailed metrics only for a small subset of their metrics.

I'm not sure if any of these is simpler than "just" doing the string concat and dropping later.

i tried to avoid refactoring this function more, because i am not very familiar with the codebase - and want to avoid introducing bugs i did not think of.

I personally think it should be fine to add this additional string concat (we do a few already, e.g. line 317). But of course, i'll ultimately defer to your judgment, how do you think about this?

Copy link
Member

Choose a reason for hiding this comment

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

Sure fair enough, thanks for the detailed explanation. SGTM.

@mattklein123 mattklein123 merged commit 247089f into envoyproxy:main Apr 4, 2024
6 checks passed
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.

3 participants