Merge pull request #1850 from rexagod/1847

Represent GVK information as labels
This commit is contained in:
Kubernetes Prow Robot
2022-10-20 09:47:01 -07:00
committed by GitHub
4 changed files with 30 additions and 23 deletions

View File

@@ -49,6 +49,8 @@ spec:
- --resources=certificatesigningrequests,configmaps,cronjobs,daemonsets,deployments,endpoints,foos,horizontalpodautoscalers,ingresses,jobs,limitranges,mutatingwebhookconfigurations,namespaces,networkpolicies,nodes,persistentvolumeclaims,persistentvolumes,poddisruptionbudgets,pods,replicasets,replicationcontrollers,resourcequotas,secrets,services,statefulsets,storageclasses,validatingwebhookconfigurations,volumeattachments,verticalpodautoscalers - --resources=certificatesigningrequests,configmaps,cronjobs,daemonsets,deployments,endpoints,foos,horizontalpodautoscalers,ingresses,jobs,limitranges,mutatingwebhookconfigurations,namespaces,networkpolicies,nodes,persistentvolumeclaims,persistentvolumes,poddisruptionbudgets,pods,replicasets,replicationcontrollers,resourcequotas,secrets,services,statefulsets,storageclasses,validatingwebhookconfigurations,volumeattachments,verticalpodautoscalers
``` ```
NOTE: The `group`, `version`, and `kind` common labels are reserved, and will be overwritten by the values from the `groupVersionKind` field.
### Examples ### Examples
The examples in this section will use the following custom resource: The examples in this section will use the following custom resource:
@@ -115,7 +117,7 @@ spec:
Produces the metric: Produces the metric:
```prometheus ```prometheus
kube_myteam_io_v1_Foo_uptime 43.21 kube_crd_uptime{group="myteam.io", kind="Foo", version="v1"} 43.21
``` ```
#### Multiple Metrics/Kitchen Sink #### Multiple Metrics/Kitchen Sink
@@ -166,8 +168,8 @@ spec:
Produces the following metrics: Produces the following metrics:
```prometheus ```prometheus
kube_myteam_io_v1_Foo_active_count{active="1",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-a"} 1 kube_crd_active_count{group="myteam.io", kind="Foo", version="v1", active="1",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-a"} 1
kube_myteam_io_v1_Foo_active_count{active="3",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-b"} 3 kube_crd_active_count{group="myteam.io", kind="Foo", version="v1", active="3",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-b"} 3
``` ```
### Metric types ### Metric types
@@ -202,7 +204,7 @@ spec:
Produces the metric: Produces the metric:
```prometheus ```prometheus
kube_myteam_io_v1_Foo_uptime 43.21 kube_crd_uptime{group="myteam.io", kind="Foo", version="v1"} 43.21
``` ```
#### StateSet #### StateSet
@@ -228,15 +230,15 @@ spec:
list: [Pending, Bar, Baz] list: [Pending, Bar, Baz]
``` ```
Metrics of type `SateSet` will generate a metric for each value defined in `list` for each resource. Metrics of type `StateSet` will generate a metric for each value defined in `list` for each resource.
The value will be 1, if the value matches the one in list. The value will be 1, if the value matches the one in list.
Produces the metric: Produces the metric:
```prometheus ```prometheus
kube_myteam_io_v1_Foo_status_phase{phase="Pending"} 1 kube_crd_status_phase{group="myteam.io", kind="Foo", version="v1", phase="Pending"} 1
kube_myteam_io_v1_Foo_status_phase{phase="Bar"} 0 kube_crd_status_phase{group="myteam.io", kind="Foo", version="v1", phase="Bar"} 0
kube_myteam_io_v1_Foo_status_phase{phase="Baz"} 0 kube_crd_status_phase{group="myteam.io", kind="Foo", version="v1", phase="Baz"} 0
``` ```
#### Info #### Info
@@ -266,7 +268,7 @@ spec:
Produces the metric: Produces the metric:
```prometheus ```prometheus
kube_myteam_io_v1_Foo_version{version="v1.2.3"} 1 kube_crd_version{group="myteam.io", kind="Foo", version="v1", version="v1.2.3"} 1
``` ```
### Naming ### Naming
@@ -288,7 +290,7 @@ spec:
Produces: Produces:
```prometheus ```prometheus
myteam_foos_uptime 43.21 myteam_foos_uptime{group="myteam.io", kind="Foo", version="v1"} 43.21
``` ```
To omit namespace and/or subsystem altogether, set them to the empty string: To omit namespace and/or subsystem altogether, set them to the empty string:
@@ -304,6 +306,11 @@ spec:
... ...
``` ```
Produces:
```prometheus
uptime{group="myteam.io", kind="Foo", version="v1"} 43.21
```
### Logging ### Logging
If a metric path is registered but not found on a custom resource, an error will be logged. For some resources, If a metric path is registered but not found on a custom resource, an error will be logged. For some resources,

View File

@@ -41,9 +41,8 @@ type MetricsSpec struct {
// Resource configures a custom resource for metric generation. // Resource configures a custom resource for metric generation.
type Resource struct { type Resource struct {
// MetricNamePrefix defines a prefix for all metrics of the resource. // MetricNamePrefix defines a prefix for all metrics of the resource.
// Falls back to the GroupVersionKind string prefixed with "kube_", with invalid characters replaced by _ if nil.
// If set to "", no prefix will be added. // If set to "", no prefix will be added.
// Example: If GroupVersionKind is "my-team.io/v1/MyResource", MetricNamePrefix will be "kube_my_team_io_v1_MyResource". // Example: If set to "foo", MetricNamePrefix will be "foo_<metric>".
MetricNamePrefix *string `yaml:"metricNamePrefix" json:"metricNamePrefix"` MetricNamePrefix *string `yaml:"metricNamePrefix" json:"metricNamePrefix"`
// GroupVersionKind of the custom resource to be monitored. // GroupVersionKind of the custom resource to be monitored.
@@ -63,17 +62,11 @@ type Resource struct {
// GetMetricNamePrefix returns the prefix to use for metrics. // GetMetricNamePrefix returns the prefix to use for metrics.
func (r Resource) GetMetricNamePrefix() string { func (r Resource) GetMetricNamePrefix() string {
if r.MetricNamePrefix == nil { p := r.MetricNamePrefix
return strings.NewReplacer( if p == nil {
"/", "_", return "kube_crd"
".", "_",
"-", "_",
).Replace(fmt.Sprintf("kube_%s_%s_%s", r.GroupVersionKind.Group, r.GroupVersionKind.Version, r.GroupVersionKind.Kind))
} }
if *r.MetricNamePrefix == "" { return *p
return ""
}
return *r.MetricNamePrefix
} }
// GetResourceName returns the lowercase, plural form of the resource Kind. This is ResourcePlural if it is set. // GetResourceName returns the lowercase, plural form of the resource Kind. This is ResourcePlural if it is set.

View File

@@ -34,6 +34,13 @@ import (
func compile(resource Resource) ([]compiledFamily, error) { func compile(resource Resource) ([]compiledFamily, error) {
var families []compiledFamily var families []compiledFamily
// Explicitly add GVK labels to all CR metrics.
if resource.CommonLabels == nil {
resource.CommonLabels = map[string]string{}
}
resource.CommonLabels["group"] = resource.GroupVersionKind.Group
resource.CommonLabels["version"] = resource.GroupVersionKind.Version
resource.CommonLabels["kind"] = resource.GroupVersionKind.Kind
for _, f := range resource.Metrics { for _, f := range resource.Metrics {
family, err := compileFamily(f, resource) family, err := compileFamily(f, resource)
if err != nil { if err != nil {

View File

@@ -324,7 +324,7 @@ func Test_fullName(t *testing.T) {
resource: r(nil), resource: r(nil),
f: count, f: count,
}, },
want: "kube_apps_v1_Deployment_count", want: "kube_crd_count",
}, },
{ {
name: "no prefix", name: "no prefix",