2 Copyright 2019 The Kubernetes Authors All rights reserved.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
22 metricsstore "k8s.io/kube-state-metrics/pkg/metrics_store"
25 // FamilyGenerator provides everything needed to generate a metric family with a
27 type FamilyGenerator struct {
31 GenerateFunc func(obj interface{}) *Family
34 // Generate calls the FamilyGenerator.GenerateFunc and gives the family its
35 // name. The reasoning behind injecting the name at such a late point in time is
36 // deduplication in the code, preventing typos made by developers as
37 // well as saving memory.
38 func (g *FamilyGenerator) Generate(obj interface{}) *Family {
39 family := g.GenerateFunc(obj)
44 func (g *FamilyGenerator) generateHeader() string {
45 header := strings.Builder{}
46 header.WriteString("# HELP ")
47 header.WriteString(g.Name)
49 header.WriteString(g.Help)
50 header.WriteByte('\n')
51 header.WriteString("# TYPE ")
52 header.WriteString(g.Name)
54 header.WriteString(string(g.Type))
56 return header.String()
59 // ExtractMetricFamilyHeaders takes in a slice of FamilyGenerator metrics and
60 // returns the extracted headers.
61 func ExtractMetricFamilyHeaders(families []FamilyGenerator) []string {
62 headers := make([]string, len(families))
64 for i, f := range families {
65 headers[i] = f.generateHeader()
71 // ComposeMetricGenFuncs takes a slice of metric families and returns a function
72 // that composes their metric generation functions into a single one.
73 func ComposeMetricGenFuncs(familyGens []FamilyGenerator) func(obj interface{}) []metricsstore.FamilyByteSlicer {
74 return func(obj interface{}) []metricsstore.FamilyByteSlicer {
75 families := make([]metricsstore.FamilyByteSlicer, len(familyGens))
77 for i, gen := range familyGens {
78 families[i] = gen.Generate(obj)
85 type whiteBlackLister interface {
86 IsIncluded(string) bool
87 IsExcluded(string) bool
90 // FilterMetricFamilies takes a white- and a blacklist and a slice of metric
91 // families and returns a filtered slice.
92 func FilterMetricFamilies(l whiteBlackLister, families []FamilyGenerator) []FamilyGenerator {
93 filtered := []FamilyGenerator{}
95 for _, f := range families {
96 if l.IsIncluded(f.Name) {
97 filtered = append(filtered, f)