|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/prometheus/client_golang/prometheus" |
| 21 | + dto "github.com/prometheus/client_model/go" |
| 22 | + |
| 23 | + "k8s.io/kube-state-metrics/pkg/options" |
| 24 | +) |
| 25 | + |
| 26 | +type gathererFunc func() ([]*dto.MetricFamily, error) |
| 27 | + |
| 28 | +func (f gathererFunc) Gather() ([]*dto.MetricFamily, error) { |
| 29 | + return f() |
| 30 | +} |
| 31 | + |
| 32 | +// FilteredGatherer wraps a prometheus.Gatherer to filter metrics based on a |
| 33 | +// white or blacklist. Whitelist and blacklist are mutually exclusive. |
| 34 | +func FilteredGatherer(r prometheus.Gatherer, whitelist options.MetricSet, blacklist options.MetricSet) prometheus.Gatherer { |
| 35 | + whitelistEnabled := !whitelist.IsEmpty() |
| 36 | + blacklistEnabled := !blacklist.IsEmpty() |
| 37 | + |
| 38 | + if whitelistEnabled { |
| 39 | + return gathererFunc(func() ([]*dto.MetricFamily, error) { |
| 40 | + metricFamilies, err := r.Gather() |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + |
| 45 | + newMetricFamilies := []*dto.MetricFamily{} |
| 46 | + for _, metricFamily := range metricFamilies { |
| 47 | + // deferencing this string may be a performance bottleneck |
| 48 | + name := *metricFamily.Name |
| 49 | + _, onWhitelist := whitelist[name] |
| 50 | + if onWhitelist { |
| 51 | + newMetricFamilies = append(newMetricFamilies, metricFamily) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return newMetricFamilies, nil |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + if blacklistEnabled { |
| 60 | + return gathererFunc(func() ([]*dto.MetricFamily, error) { |
| 61 | + metricFamilies, err := r.Gather() |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + newMetricFamilies := []*dto.MetricFamily{} |
| 67 | + for _, metricFamily := range metricFamilies { |
| 68 | + name := *metricFamily.Name |
| 69 | + _, onBlacklist := blacklist[name] |
| 70 | + if onBlacklist { |
| 71 | + continue |
| 72 | + } |
| 73 | + newMetricFamilies = append(newMetricFamilies, metricFamily) |
| 74 | + } |
| 75 | + |
| 76 | + return newMetricFamilies, nil |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + return r |
| 81 | +} |
0 commit comments