Skip to content

Commit

Permalink
feat: Allow prefixing * paths
Browse files Browse the repository at this point in the history
Allow prefixing * paths, for instance, foo_*: [foo, bar, baz], will
result in foo_baz being the key for all object resolutions under baz.

Signed-off-by: Pranshu Srivastava <rexagod@gmail.com>
  • Loading branch information
rexagod committed Aug 29, 2023
1 parent dbd4568 commit 13d6a05
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
10 changes: 8 additions & 2 deletions docs/customresourcestate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ spec:
# whole objects may be copied into labels by prefixing with "*"
# *anything will be copied into labels, with the highest sorted * strings first
"*": [metadata, labels]
# a prefix before the asterisk will be used as a label prefix
"lorem_*": [metadata, annotations]
"**": [metadata, annotations]

# or specific fields may be copied. these fields will always override values from *s
Expand All @@ -209,8 +211,12 @@ spec:
Produces the following metrics:

```prometheus
kube_customresource_ready_count{customresource_group="myteam.io", customresource_kind="Foo", customresource_version="v1", active="1",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-a"} 2
kube_customresource_ready_count{customresource_group="myteam.io", customresource_kind="Foo", customresource_version="v1", active="3",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-b"} 4
kube_customresource_ready_count{customresource_group="myteam.io", customresource_kind="Foo",
customresource_version="v1", active="1",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-a",
lorem_bar="baz",lorem_qux="quxx",} 2
kube_customresource_ready_count{customresource_group="myteam.io", customresource_kind="Foo",
customresource_version="v1", active="3",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-b",
lorem_bar="baz",lorem_qux="quxx",} 4
```

#### Non-map Arrays
Expand Down
11 changes: 7 additions & 4 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,21 +517,24 @@ func addPathLabels(obj interface{}, labels map[string]valuePath, result map[stri
// always do that first so other labels can override
var stars []string
for k := range labels {
if strings.HasPrefix(k, "*") {
if strings.HasPrefix(k, "*") || strings.HasSuffix(k, "*") {
stars = append(stars, k)
}
}
sort.Strings(stars)
for _, k := range stars {
m := labels[k].Get(obj)
for _, star := range stars {
m := labels[star].Get(obj)
if kv, ok := m.(map[string]interface{}); ok {
for k, v := range kv {
if strings.HasSuffix(star, "*") {
k = star[:len(star)-1] + k
}
result[store.SanitizeLabelName(k)] = fmt.Sprintf("%v", v)
}
}
}
for k, v := range labels {
if strings.HasPrefix(k, "*") {
if strings.HasPrefix(k, "*") || strings.HasSuffix(k, "*") {
continue
}
value := v.Get(obj)
Expand Down
11 changes: 7 additions & 4 deletions pkg/customresourcestate/registry_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,15 @@ func Test_addPathLabels(t *testing.T) {
{name: "*", args: args{
obj: cr,
labels: map[string]valuePath{
"*1": mustCompilePath(t, "metadata", "annotations"),
"bar": mustCompilePath(t, "metadata", "labels", "foo"),
"*1": mustCompilePath(t, "metadata", "annotations"),
"bar": mustCompilePath(t, "metadata", "labels", "foo"),
"label_object_*": mustCompilePath(t, "metadata", "annotations"),
},
want: map[string]string{
"qux": "quxx",
"bar": "bar",
"qux": "quxx",
"bar": "bar",
"label_object_qux": "quxx",
"label_object_bar": "baz",
},
}},
}
Expand Down

0 comments on commit 13d6a05

Please sign in to comment.