From 633cbf49d0ecf607c716860b21e03c4afd596b8c Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Fri, 7 Oct 2022 23:48:21 +0530 Subject: [PATCH] Represent GVK information as labels 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. --- docs/customresourcestate-metrics.md | 22 ++++++++++--------- pkg/customresourcestate/config.go | 15 ++++--------- pkg/customresourcestate/registry_factory.go | 7 ++++++ .../registry_factory_test.go | 2 +- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/docs/customresourcestate-metrics.md b/docs/customresourcestate-metrics.md index 963e5fc55b..2a339658dc 100644 --- a/docs/customresourcestate-metrics.md +++ b/docs/customresourcestate-metrics.md @@ -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: @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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: diff --git a/pkg/customresourcestate/config.go b/pkg/customresourcestate/config.go index 76e7786763..00659e8b45 100644 --- a/pkg/customresourcestate/config.go +++ b/pkg/customresourcestate/config.go @@ -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_". MetricNamePrefix *string `yaml:"metricNamePrefix" json:"metricNamePrefix"` // GroupVersionKind of the custom resource to be monitored. @@ -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. diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index 8cfe716c76..5c78294bd9 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -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 { diff --git a/pkg/customresourcestate/registry_factory_test.go b/pkg/customresourcestate/registry_factory_test.go index b553fa4be5..db9dd723d4 100644 --- a/pkg/customresourcestate/registry_factory_test.go +++ b/pkg/customresourcestate/registry_factory_test.go @@ -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",