Skip to content

Allow white- and blacklisting metrics to be exposed #488

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

Merged
merged 1 commit into from
Jul 24, 2018

Conversation

brancz
Copy link
Member

@brancz brancz commented Jul 6, 2018

What this PR does / why we need it:

In large Kubernetes clusters single metrics can have a significant effect on the total number of metrics exposed by a single scrape. This PR allows white or blacklisting metrics to be exposed.

@andyxning

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 6, 2018
@k8s-ci-robot k8s-ci-robot requested a review from andyxning July 6, 2018 13:15
CHANGELOG.md Outdated
@@ -1,6 +1,7 @@
## Unreleased

* [CHANGE] `kube_job_status_start_time` and `kube_job_status_completion_time` metric types changed from counter to gauge.
* [FEATURE] Allow white- and blacklisting metrics to be exposed.
Copy link
Member

Choose a reason for hiding this comment

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

s/white- and blacklisting/white- and black-listing

@@ -25,6 +25,43 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type MetricSet map[string]struct{}

func (c *MetricSet) String() string {
Copy link
Member

Choose a reason for hiding this comment

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

s/c /ms. ms stands for MetricSet

type MetricSet map[string]struct{}

func (c *MetricSet) String() string {
s := *c
Copy link
Member

Choose a reason for hiding this comment

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

ditto

return strings.Join(ss, ",")
}

func (c *MetricSet) Set(value string) error {
Copy link
Member

Choose a reason for hiding this comment

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

ditto

}

func (c *MetricSet) Set(value string) error {
s := *c
Copy link
Member

Choose a reason for hiding this comment

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

ditto

return nil
}

func (s MetricSet) asSlice() []string {
Copy link
Member

Choose a reason for hiding this comment

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

s/s /ms

Actually s is fine to me, but i think we should be consistent. :)

return metrics
}

func (s MetricSet) IsEmpty() bool {
Copy link
Member

Choose a reason for hiding this comment

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

ditto

return len(s.asSlice()) == 0
}

func (s *MetricSet) Type() string {
Copy link
Member

Choose a reason for hiding this comment

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

ditto

main.go Outdated
blacklistEnabled := !blacklist.IsEmpty()
filterEnabled := whitelistEnabled || blacklistEnabled

return GathererFunc(func() ([]*dto.MetricFamily, error) {
Copy link
Member

@andyxning andyxning Jul 8, 2018

Choose a reason for hiding this comment

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

I suggest we make this function a package-level function thus we can add some unit tests for it. We need to determine the relationship between whitelist and blacklist:

  • Does we permit users specify both whitelist and blacklist
  • Does kube-state-metrics should only prefer whitelist and ignore blacklist even blacklist is not empty.

@brancz

Currently, the implementation is kube-state-metrics prefer whitelist metrics even blacklist is not empty which i think is not the right logic. :)

The right way is to filter the collected metrics on whitelist and blacklist and then merge the filtered metrics.

@brancz brancz force-pushed the limit branch 2 times, most recently from 64bad71 to 6d04729 Compare July 9, 2018 08:58
blacklistEnabled := !blacklist.IsEmpty()
filterEnabled := whitelistEnabled || blacklistEnabled

return GathererFunc(func() ([]*dto.MetricFamily, error) {
Copy link
Member

Choose a reason for hiding this comment

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

I suggest we split the GathererFunc into a inter-package level function, just like FilteredGatherer And add some ut for this.

Copy link
Member

Choose a reason for hiding this comment

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

@brancz Once you have time, could you please update this PR? :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you explain what you meant me to do with GathererFunc? It's just so a function satisfies the prometheus.Gatherer interface.

continue
}

_, onBlacklist := blacklist[name]
Copy link

Choose a reason for hiding this comment

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

Why do you check whether whitelistEnabled is true, but you don't check blacklistEnabled here.

Copy link
Member Author

Choose a reason for hiding this comment

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

We have to opt for one strategy here. Both the whitelist and blacklist are explicit, so which one is the right one, the one where we explicitly said we definitely want this, or the one where we said we definitely don't want this. As whitelists are usually pretty hand picked I figured that would be the stronger of the two.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to make sure to document he precedence behavior.

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed blacklist and whitelist to be mutually exclusive. It really doesn't make any sense to have both here.

@brancz
Copy link
Member Author

brancz commented Jul 23, 2018

This is ready for a re-review @andyxning @dohnto.

Copy link
Contributor

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks for the refactor.

@@ -69,6 +73,8 @@ func (o *Options) AddFlags() {
o.flags.StringVar(&o.TelemetryHost, "telemetry-host", "0.0.0.0", `Host to expose kube-state-metrics self metrics on.`)
o.flags.Var(&o.Collectors, "collectors", fmt.Sprintf("Comma-separated list of collectors to be enabled. Defaults to %q", &DefaultCollectors))
o.flags.Var(&o.Namespaces, "namespace", fmt.Sprintf("Comma-separated list of namespaces to be enabled. Defaults to %q", &DefaultNamespaces))
o.flags.Var(&o.MetricWhitelist, "metric-whitelist", "Comma-separated list of metrics to be exposed. The whitelist and blacklist are mutually exclusive.")
Copy link
Member

Choose a reason for hiding this comment

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

We should state this clearly. !

@andyxning
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 24, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: andyxning, brancz

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

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 6c07045 into kubernetes:master Jul 24, 2018
@brancz brancz deleted the limit branch July 24, 2018 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants