Skip to content

Commit

Permalink
customresource as metric appendix and label prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
bavarianbidi committed Jan 5, 2023
1 parent 6f76c22 commit ed85920
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ 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 `cr_group`, `cr_version`, and `cr_kind` common labels are reserved, and will be overwritten by the values from the `groupVersionKind` field.
NOTE: The `customresource_group`, `customresource_version`, and `customresource_kind` common labels are reserved, and will be overwritten by the values from the `groupVersionKind` field.

### Examples

Expand Down
6 changes: 5 additions & 1 deletion pkg/customresourcestate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
"k8s.io/kube-state-metrics/v2/pkg/customresource"
)

// customResourceState is used to prefix the auto-generated GVK labels as well as an appendix for the metric itself
// if no custom metric name is defined
const customResourceState string = "customresource"

// Metrics is the top level configuration object.
type Metrics struct {
Spec MetricsSpec `yaml:"spec" json:"spec"`
Expand Down Expand Up @@ -64,7 +68,7 @@ type Resource struct {
func (r Resource) GetMetricNamePrefix() string {
p := r.MetricNamePrefix
if p == nil {
return "kube_crd"
return "kube_" + customResourceState
}
return *p
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@ import (
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
)

const gvkLabelPrefix string = "cr_"

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[gvkLabelPrefix+"group"] = resource.GroupVersionKind.Group
resource.CommonLabels[gvkLabelPrefix+"version"] = resource.GroupVersionKind.Version
resource.CommonLabels[gvkLabelPrefix+"kind"] = resource.GroupVersionKind.Kind
resource.CommonLabels[customResourceState+"_group"] = resource.GroupVersionKind.Group
resource.CommonLabels[customResourceState+"_version"] = resource.GroupVersionKind.Version
resource.CommonLabels[customResourceState+"_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 @@ -333,7 +333,7 @@ func Test_fullName(t *testing.T) {
resource: r(nil),
f: count,
},
want: "kube_crd_count",
want: "kube_customresource_count",
},
{
name: "no prefix",
Expand Down

0 comments on commit ed85920

Please sign in to comment.