From d16935b3df5c1dfa7d7392c09cd7db19dd0881d1 Mon Sep 17 00:00:00 2001 From: David Murphy Date: Fri, 14 Apr 2023 23:05:00 +0100 Subject: [PATCH] 2047: Add LabelsFromPath functionality to Info metrics --- pkg/customresourcestate/registry_factory.go | 20 +++++++++++++------ .../registry_factory_test.go | 11 ++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index 75c207faac..65129efa12 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -324,17 +324,25 @@ func (c *compiledInfo) Values(v interface{}) (result []eachValue, errs []error) continue } } - // labelFromKey logic - for key := range iter { + + // labelFromKey / labelFromPath logic + for key, it := range iter { + labels := make(map[string]string) + if key != "" && c.labelFromKey != "" { + labels[c.labelFromKey] = key + } + + addPathLabels(it, c.LabelFromPath(), labels) + + if len(labels) > 0 { result = append(result, eachValue{ - Labels: map[string]string{ - c.labelFromKey: key, - }, - Value: 1, + Labels: labels, + Value: 1, }) } } + result = append(result, value...) default: result, errs = c.values(v) diff --git a/pkg/customresourcestate/registry_factory_test.go b/pkg/customresourcestate/registry_factory_test.go index ca17bcbb15..d68f22b617 100644 --- a/pkg/customresourcestate/registry_factory_test.go +++ b/pkg/customresourcestate/registry_factory_test.go @@ -292,6 +292,17 @@ func Test_values(t *testing.T) { newEachValue(t, 1, "type", "type-a"), newEachValue(t, 1, "type", "type-b"), }}, + {name: "info label from path", each: &compiledInfo{ + compiledCommon: compiledCommon{ + path: mustCompilePath(t, "status", "sub"), + labelFromPath: map[string]valuePath{ + "active": mustCompilePath(t, "active"), + }, + }, + }, wantResult: []eachValue{ + newEachValue(t, 1, "active", "1"), + newEachValue(t, 1, "active", "3"), + }}, {name: "stateset", each: &compiledStateSet{ compiledCommon: compiledCommon{ path: mustCompilePath(t, "status", "phase"),