kubernetes-sigs / prometheus-adapter

An implementation of the custom.metrics.k8s.io API using Prometheus
Apache License 2.0
1.9k stars 551 forks source link

pkg/naming: shorten regexp when matching many similar names #670

Open abursavich opened 1 month ago

abursavich commented 1 month ago

Fixes #668.

Pods from a deployment will have long names like:

  1. foobar-59c8d74c75-9gv24
  2. foobar-59c8d74c75-dhfbj
  3. foobar-59c8d74c75-kggvk
  4. foobar-59c8d74c75-nd8jm
  5. foobar-59c8d74c75-nqctr
  6. foobar-59c8d74c75-pxlnd
  7. foobar-59c8d74c75-rnxk7
  8. foobar-59c8d74c75-rpj7q
  9. foobar-59c8d74c75-vl6n6
  10. foobar-59c8d74c75-xhvp4

The naive regexp that matches these pods is foobar-59c8d74c75-9gv24|foobar-59c8d74c75-dhfbj|foobar-59c8d74c75-kggvk|foobar-59c8d74c75-nd8jm|foobar-59c8d74c75-nqctr|foobar-59c8d74c75-pxlnd|foobar-59c8d74c75-rnxk7|foobar-59c8d74c75-rpj7q|foobar-59c8d74c75-vl6n6|foobar-59c8d74c75-xhvp4 (len=239).

A shorter regexp that factors out the shared prefixes and matches the same pods is foobar-59c8d74c75-(9gv24|dhfbj|kggvk|n(d8jm|qctr)|pxlnd|r(nxk7|pj7q)|vl6n6|xhvp4) (len=81).

A similar set of only 5 pods achieves 59% compression. This example with 10 pods achieves 66% compression. At 20 pods it's ~71%, at 40 pods it's ~73%, at 80 pods it's ~74%, and at 200 pods it starts to converge to ~75%. The savings ultimately depend on the length of the deployment name, but even pods with short deployment names (e.g. foobar) still benefit because the replicaset hash (e.g. 59c8d74c75) contributes a lot of redundancy.

Instead of trying to implement this transformation directly, this change lets the regexp/syntax package do all the heavy lifting. In some cases this doesn't result in the shortest possible string (e.g. some short prefix repetitions are more optimal than complete prefix factoring: nd8jm|nqctr vs n(d8jm|qctr)), but writing and maintaining a separate implementation isn't worth it to get at these crumbs.

k8s-ci-robot commented 1 month ago

Welcome @abursavich!

It looks like this is your first PR to kubernetes-sigs/prometheus-adapter 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/prometheus-adapter has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. :smiley:

k8s-ci-robot commented 1 month ago

Hi @abursavich. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.
dgrisonnet commented 1 month ago

/assign /triage accepted

dgrisonnet commented 1 month ago

/ok-to-test

k8s-ci-robot commented 1 month ago

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: abursavich Once this PR has been reviewed and has the lgtm label, please ask for approval from dgrisonnet. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files: - **[OWNERS](https://github.com/kubernetes-sigs/prometheus-adapter/blob/master/OWNERS)** Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
abursavich commented 1 month ago

/retest

abursavich commented 3 weeks ago

Hey, @dgrisonnet! Please take another look. Tests are passing.