Skip to content

Commit

Permalink
Merge pull request kubernetes#1850 from rexagod/1847
Browse files Browse the repository at this point in the history
Represent GVK information as labels
  • Loading branch information
k8s-ci-robot authored Oct 20, 2022
2 parents 339b134 + 762f98b commit d4fdcda
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
27 changes: 17 additions & 10 deletions docs/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
NOTE: The `group`, `version`, and `kind` common labels are reserved, and will be overwritten by the values from the `groupVersionKind` field.

### Examples

The examples in this section will use the following custom resource:
Expand Down Expand Up @@ -115,7 +117,7 @@ spec:
Produces the metric:

```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
Expand Down Expand Up @@ -166,8 +168,8 @@ spec:
Produces the following metrics:

```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_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="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="3",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-b"} 3
```

### Metric types
Expand Down Expand Up @@ -202,7 +204,7 @@ spec:
Produces the metric:

```prometheus
kube_myteam_io_v1_Foo_uptime 43.21
kube_crd_uptime{group="myteam.io", kind="Foo", version="v1"} 43.21
```

#### StateSet
Expand All @@ -228,15 +230,15 @@ spec:
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.

Produces the metric:

```prometheus
kube_myteam_io_v1_Foo_status_phase{phase="Pending"} 1
kube_myteam_io_v1_Foo_status_phase{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="Pending"} 1
kube_crd_status_phase{group="myteam.io", kind="Foo", version="v1", phase="Bar"} 0
kube_crd_status_phase{group="myteam.io", kind="Foo", version="v1", phase="Baz"} 0
```

#### Info
Expand Down Expand Up @@ -266,7 +268,7 @@ spec:
Produces the metric:

```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
Expand All @@ -288,7 +290,7 @@ spec:

Produces:
```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:
Expand All @@ -304,6 +306,11 @@ spec:
...
```

Produces:
```prometheus
uptime{group="myteam.io", kind="Foo", version="v1"} 43.21
```

### Logging

If a metric path is registered but not found on a custom resource, an error will be logged. For some resources,
Expand Down
17 changes: 5 additions & 12 deletions pkg/customresourcestate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ type MetricsSpec struct {
// Resource configures a custom resource for metric generation.
type Resource struct {
// 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.
// 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"`

// GroupVersionKind of the custom resource to be monitored.
Expand All @@ -63,17 +62,11 @@ type Resource struct {

// GetMetricNamePrefix returns the prefix to use for metrics.
func (r Resource) GetMetricNamePrefix() string {
if r.MetricNamePrefix == nil {
return strings.NewReplacer(
"/", "_",
".", "_",
"-", "_",
).Replace(fmt.Sprintf("kube_%s_%s_%s", r.GroupVersionKind.Group, r.GroupVersionKind.Version, r.GroupVersionKind.Kind))
p := r.MetricNamePrefix
if p == nil {
return "kube_crd"
}
if *r.MetricNamePrefix == "" {
return ""
}
return *r.MetricNamePrefix
return *p
}

// GetResourceName returns the lowercase, plural form of the resource Kind. This is ResourcePlural if it is set.
Expand Down
7 changes: 7 additions & 0 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ import (

func compile(resource Resource) ([]compiledFamily, error) {
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 {
family, err := compileFamily(f, resource)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/customresourcestate/registry_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func Test_fullName(t *testing.T) {
resource: r(nil),
f: count,
},
want: "kube_apps_v1_Deployment_count",
want: "kube_crd_count",
},
{
name: "no prefix",
Expand Down

0 comments on commit d4fdcda

Please sign in to comment.