Skip to content

Commit d4fdcda

Browse files
authored
Merge pull request #1850 from rexagod/1847
Represent GVK information as labels
2 parents 339b134 + 762f98b commit d4fdcda

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

docs/customresourcestate-metrics.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ spec:
4949
- --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
5050
```
5151
52+
NOTE: The `group`, `version`, and `kind` common labels are reserved, and will be overwritten by the values from the `groupVersionKind` field.
53+
5254
### Examples
5355

5456
The examples in this section will use the following custom resource:
@@ -115,7 +117,7 @@ spec:
115117
Produces the metric:
116118

117119
```prometheus
118-
kube_myteam_io_v1_Foo_uptime 43.21
120+
kube_crd_uptime{group="myteam.io", kind="Foo", version="v1"} 43.21
119121
```
120122

121123
#### Multiple Metrics/Kitchen Sink
@@ -166,8 +168,8 @@ spec:
166168
Produces the following metrics:
167169

168170
```prometheus
169-
kube_myteam_io_v1_Foo_active_count{active="1",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-a"} 1
170-
kube_myteam_io_v1_Foo_active_count{active="3",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-b"} 3
171+
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
172+
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
171173
```
172174

173175
### Metric types
@@ -202,7 +204,7 @@ spec:
202204
Produces the metric:
203205

204206
```prometheus
205-
kube_myteam_io_v1_Foo_uptime 43.21
207+
kube_crd_uptime{group="myteam.io", kind="Foo", version="v1"} 43.21
206208
```
207209

208210
#### StateSet
@@ -228,15 +230,15 @@ spec:
228230
list: [Pending, Bar, Baz]
229231
```
230232

231-
Metrics of type `SateSet` will generate a metric for each value defined in `list` for each resource.
233+
Metrics of type `StateSet` will generate a metric for each value defined in `list` for each resource.
232234
The value will be 1, if the value matches the one in list.
233235

234236
Produces the metric:
235237

236238
```prometheus
237-
kube_myteam_io_v1_Foo_status_phase{phase="Pending"} 1
238-
kube_myteam_io_v1_Foo_status_phase{phase="Bar"} 0
239-
kube_myteam_io_v1_Foo_status_phase{phase="Baz"} 0
239+
kube_crd_status_phase{group="myteam.io", kind="Foo", version="v1", phase="Pending"} 1
240+
kube_crd_status_phase{group="myteam.io", kind="Foo", version="v1", phase="Bar"} 0
241+
kube_crd_status_phase{group="myteam.io", kind="Foo", version="v1", phase="Baz"} 0
240242
```
241243

242244
#### Info
@@ -266,7 +268,7 @@ spec:
266268
Produces the metric:
267269

268270
```prometheus
269-
kube_myteam_io_v1_Foo_version{version="v1.2.3"} 1
271+
kube_crd_version{group="myteam.io", kind="Foo", version="v1", version="v1.2.3"} 1
270272
```
271273

272274
### Naming
@@ -288,7 +290,7 @@ spec:
288290

289291
Produces:
290292
```prometheus
291-
myteam_foos_uptime 43.21
293+
myteam_foos_uptime{group="myteam.io", kind="Foo", version="v1"} 43.21
292294
```
293295

294296
To omit namespace and/or subsystem altogether, set them to the empty string:
@@ -304,6 +306,11 @@ spec:
304306
...
305307
```
306308

309+
Produces:
310+
```prometheus
311+
uptime{group="myteam.io", kind="Foo", version="v1"} 43.21
312+
```
313+
307314
### Logging
308315

309316
If a metric path is registered but not found on a custom resource, an error will be logged. For some resources,

pkg/customresourcestate/config.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ type MetricsSpec struct {
4141
// Resource configures a custom resource for metric generation.
4242
type Resource struct {
4343
// MetricNamePrefix defines a prefix for all metrics of the resource.
44-
// Falls back to the GroupVersionKind string prefixed with "kube_", with invalid characters replaced by _ if nil.
4544
// If set to "", no prefix will be added.
46-
// Example: If GroupVersionKind is "my-team.io/v1/MyResource", MetricNamePrefix will be "kube_my_team_io_v1_MyResource".
45+
// Example: If set to "foo", MetricNamePrefix will be "foo_<metric>".
4746
MetricNamePrefix *string `yaml:"metricNamePrefix" json:"metricNamePrefix"`
4847

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

6463
// GetMetricNamePrefix returns the prefix to use for metrics.
6564
func (r Resource) GetMetricNamePrefix() string {
66-
if r.MetricNamePrefix == nil {
67-
return strings.NewReplacer(
68-
"/", "_",
69-
".", "_",
70-
"-", "_",
71-
).Replace(fmt.Sprintf("kube_%s_%s_%s", r.GroupVersionKind.Group, r.GroupVersionKind.Version, r.GroupVersionKind.Kind))
65+
p := r.MetricNamePrefix
66+
if p == nil {
67+
return "kube_crd"
7268
}
73-
if *r.MetricNamePrefix == "" {
74-
return ""
75-
}
76-
return *r.MetricNamePrefix
69+
return *p
7770
}
7871

7972
// GetResourceName returns the lowercase, plural form of the resource Kind. This is ResourcePlural if it is set.

pkg/customresourcestate/registry_factory.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ import (
3434

3535
func compile(resource Resource) ([]compiledFamily, error) {
3636
var families []compiledFamily
37+
// Explicitly add GVK labels to all CR metrics.
38+
if resource.CommonLabels == nil {
39+
resource.CommonLabels = map[string]string{}
40+
}
41+
resource.CommonLabels["group"] = resource.GroupVersionKind.Group
42+
resource.CommonLabels["version"] = resource.GroupVersionKind.Version
43+
resource.CommonLabels["kind"] = resource.GroupVersionKind.Kind
3744
for _, f := range resource.Metrics {
3845
family, err := compileFamily(f, resource)
3946
if err != nil {

pkg/customresourcestate/registry_factory_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func Test_fullName(t *testing.T) {
324324
resource: r(nil),
325325
f: count,
326326
},
327-
want: "kube_apps_v1_Deployment_count",
327+
want: "kube_crd_count",
328328
},
329329
{
330330
name: "no prefix",

0 commit comments

Comments
 (0)