Skip to content

Commit

Permalink
Represent GVK information as labels
Browse files Browse the repository at this point in the history
Represent GVK information as labels in the metrics, instead of appending
them to the metric name itself. This would allow users to aggregate
varying GVKs of a CR under the same metric, making operations much more
easier.
  • Loading branch information
rexagod committed Oct 7, 2022
1 parent 12402a5 commit 633cbf4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
22 changes: 12 additions & 10 deletions docs/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 @@ -114,7 +116,7 @@ spec:
Produces the metric:

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

#### Multiple Metrics/Kitchen Sink
Expand Down Expand Up @@ -165,8 +167,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
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
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 @@ -201,7 +203,7 @@ spec:
Produces the metric:

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

#### StateSet
Expand All @@ -227,15 +229,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
status_phase{group="myteam.io", kind="Foo", version="v1", phase="Pending"} 1
status_phase{group="myteam.io", kind="Foo", version="v1", phase="Bar"} 0
status_phase{group="myteam.io", kind="Foo", version="v1", phase="Baz"} 0
```

#### Info
Expand Down Expand Up @@ -265,7 +267,7 @@ spec:
Produces the metric:

```prometheus
kube_myteam_io_v1_Foo_version{version="v1.2.3"} 1
version{group="myteam.io", kind="Foo", version="v1", version="v1.2.3"} 1
```

### Naming
Expand All @@ -287,7 +289,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 Down
15 changes: 4 additions & 11 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))
}
if *r.MetricNamePrefix == "" {
p := r.MetricNamePrefix
if p == nil {
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: "count",
},
{
name: "no prefix",
Expand Down

0 comments on commit 633cbf4

Please sign in to comment.