Skip to content

Commit 63a4578

Browse files
committed
metrics: add internal copy of kube-state-metrics config types
1 parent a2e73a0 commit 63a4578

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed

pkg/metrics/internal/config/config.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Copyright 2021 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 config
18+
19+
import (
20+
"fmt"
21+
)
22+
23+
// Metrics is the top level configuration object.
24+
type Metrics struct {
25+
Spec MetricsSpec `yaml:"spec" json:"spec"`
26+
}
27+
28+
// MetricsSpec is the configuration describing the custom resource state metrics to generate.
29+
type MetricsSpec struct {
30+
// Resources is the list of custom resources to be monitored. A resource with the same GroupVersionKind may appear
31+
// multiple times (e.g., to customize the namespace or subsystem,) but will incur additional overhead.
32+
Resources []Resource `yaml:"resources" json:"resources"`
33+
}
34+
35+
// Resource configures a custom resource for metric generation.
36+
type Resource struct {
37+
// MetricNamePrefix defines a prefix for all metrics of the resource.
38+
// If set to "", no prefix will be added.
39+
// Example: If set to "foo", MetricNamePrefix will be "foo_<metric>".
40+
MetricNamePrefix *string `yaml:"metricNamePrefix" json:"metricNamePrefix"`
41+
42+
// GroupVersionKind of the custom resource to be monitored.
43+
GroupVersionKind GroupVersionKind `yaml:"groupVersionKind" json:"groupVersionKind"`
44+
45+
// Labels are added to all metrics. If the same key is used in a metric, the value from the metric will overwrite the value here.
46+
Labels `yaml:",inline" json:",inline"`
47+
48+
// Metrics are the custom resource fields to be collected.
49+
Metrics []Generator `yaml:"metrics" json:"metrics"`
50+
// ErrorLogV defines the verbosity threshold for errors logged for this resource.
51+
ErrorLogV int32 `yaml:"errorLogV" json:"errorLogV"`
52+
53+
// ResourcePlural sets the plural name of the resource. Defaults to the plural version of the Kind according to flect.Pluralize.
54+
ResourcePlural string `yaml:"resourcePlural" json:"resourcePlural"`
55+
}
56+
57+
// GroupVersionKind is the Kubernetes group, version, and kind of a resource.
58+
type GroupVersionKind struct {
59+
Group string `yaml:"group" json:"group"`
60+
Version string `yaml:"version" json:"version"`
61+
Kind string `yaml:"kind" json:"kind"`
62+
}
63+
64+
func (gvk GroupVersionKind) String() string {
65+
return fmt.Sprintf("%s_%s_%s", gvk.Group, gvk.Version, gvk.Kind)
66+
}
67+
68+
// Labels is common configuration of labels to add to metrics.
69+
type Labels struct {
70+
// CommonLabels are added to all metrics.
71+
CommonLabels map[string]string `yaml:"commonLabels,omitempty" json:"commonLabels,omitempty"`
72+
// LabelsFromPath adds additional labels where the value is taken from a field in the resource.
73+
LabelsFromPath map[string][]string `yaml:"labelsFromPath,omitempty" json:"labelsFromPath,omitempty"`
74+
}
75+
76+
// Generator describes a unique metric name.
77+
type Generator struct {
78+
// Name of the metric. Subject to prefixing based on the configuration of the Resource.
79+
Name string `yaml:"name" json:"name"`
80+
// Help text for the metric.
81+
Help string `yaml:"help" json:"help"`
82+
// Each targets a value or values from the resource.
83+
Each Metric `yaml:"each" json:"each"`
84+
85+
// Labels are added to all metrics. Labels from Each will overwrite these if using the same key.
86+
Labels `yaml:",inline" json:",inline"` // json will inline because it is already tagged
87+
// ErrorLogV defines the verbosity threshold for errors logged for this metric. Must be non-zero to override the resource setting.
88+
ErrorLogV int32 `yaml:"errorLogV,omitempty" json:"errorLogV,omitempty"`
89+
}
90+
91+
// Metric defines a metric to expose.
92+
// +union
93+
type Metric struct {
94+
// Type defines the type of the metric.
95+
// +unionDiscriminator
96+
Type MetricType `yaml:"type" json:"type"`
97+
98+
// Gauge defines a gauge metric.
99+
// +optional
100+
Gauge *MetricGauge `yaml:"gauge,omitempty" json:"gauge,omitempty"`
101+
// StateSet defines a state set metric.
102+
// +optional
103+
StateSet *MetricStateSet `yaml:"stateSet,omitempty" json:"stateSet,omitempty"`
104+
// Info defines an info metric.
105+
// +optional
106+
Info *MetricInfo `yaml:"info,omitempty" json:"info,omitempty"`
107+
}
108+
109+
// ConfigDecoder is for use with FromConfig.
110+
type ConfigDecoder interface {
111+
Decode(v interface{}) (err error)
112+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2021 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 config
18+
19+
// MetricMeta are variables which may used for any metric type.
20+
type MetricMeta struct {
21+
// LabelsFromPath adds additional labels where the value of the label is taken from a field under Path.
22+
LabelsFromPath map[string][]string `yaml:"labelsFromPath,omitempty" json:"labelsFromPath,omitempty"`
23+
// Path is the path to to generate metric(s) for.
24+
Path []string `yaml:"path" json:"path"`
25+
}
26+
27+
// MetricGauge targets a Path that may be a single value, array, or object. Arrays and objects will generate a metric per element.
28+
// Ref: https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#gauge
29+
type MetricGauge struct {
30+
MetricMeta `yaml:",inline" json:",inline"`
31+
32+
// ValueFrom is the path to a numeric field under Path that will be the metric value.
33+
ValueFrom []string `yaml:"valueFrom" json:"valueFrom"`
34+
// LabelFromKey adds a label with the given name if Path is an object. The label value will be the object key.
35+
LabelFromKey string `yaml:"labelFromKey,omitempty" json:"labelFromKey,omitempty"`
36+
// NilIsZero indicates that if a value is nil it will be treated as zero value.
37+
NilIsZero bool `yaml:"nilIsZero" json:"nilIsZero"`
38+
}
39+
40+
// MetricInfo is a metric which is used to expose textual information.
41+
// Ref: https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#info
42+
type MetricInfo struct {
43+
MetricMeta `yaml:",inline" json:",inline"`
44+
// LabelFromKey adds a label with the given name if Path is an object. The label value will be the object key.
45+
LabelFromKey string `yaml:"labelFromKey,omitempty" json:"labelFromKey,omitempty"`
46+
}
47+
48+
// MetricStateSet is a metric which represent a series of related boolean values, also called a bitset.
49+
// Ref: https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#stateset
50+
type MetricStateSet struct {
51+
MetricMeta `yaml:",inline" json:",inline"`
52+
53+
// List is the list of values to expose a value for.
54+
List []string `yaml:"list" json:"list"`
55+
// LabelName is the key of the label which is used for each entry in List to expose the value.
56+
LabelName string `yaml:"labelName" json:"labelName"`
57+
// ValueFrom is the subpath to compare the list to.
58+
ValueFrom []string `yaml:"valueFrom" json:"valueFrom"`
59+
}

pkg/metrics/internal/config/doc.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2021 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+
// config contains a copy of the types from k8s.io/kube-state-metrics/pkg/customresourcestate.
18+
// The following modifications got applied:
19+
// For `config.go`:
20+
// * Rename the package to `config`.
21+
// * Drop `const customResourceState`.
22+
// * Drop all functions, only preserve structs.
23+
// * Use `int32` instead of `klog.Level`.
24+
// * Use `MetricType` instead of `metric.Type`
25+
// * Add `omitempty` to:
26+
// - `Labels.CommonLabels`
27+
// - `Labels.LabelsFromPath`
28+
// - `Generator.ErrorLogV`
29+
// - `Metric.Gauge`
30+
// - `Metric.StateSet`
31+
// - `Metric.Info`
32+
//
33+
// For `config_metrics_types.go`:
34+
// * Rename the package to `config`.
35+
// * Add `omitempty` to:
36+
// - `MetricMeta.LabelsFromPath
37+
// - `MetricGauge.LabelFromkey`
38+
// - `MetricInfo.LabelFromkey`
39+
package config
40+
41+
// KubeStateMetricsVersion defines which version of kube-state-metrics these types
42+
// are based on and the output file should be compatible to.
43+
const KubeStateMetricsVersion = "v2.13.0"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2024 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 config
18+
19+
// MetricType is the type of a metric.
20+
type MetricType string
21+
22+
// Supported metric types.
23+
const (
24+
MetricTypeGauge MetricType = "Gauge"
25+
MetricTypeStateSet MetricType = "StateSet"
26+
MetricTypeInfo MetricType = "Info"
27+
)

0 commit comments

Comments
 (0)