Skip to content

Commit d16935b

Browse files
committed
2047: Add LabelsFromPath functionality to Info metrics
1 parent e3d99ba commit d16935b

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

pkg/customresourcestate/registry_factory.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,25 @@ func (c *compiledInfo) Values(v interface{}) (result []eachValue, errs []error)
324324
continue
325325
}
326326
}
327-
// labelFromKey logic
328-
for key := range iter {
327+
328+
// labelFromKey / labelFromPath logic
329+
for key, it := range iter {
330+
labels := make(map[string]string)
331+
329332
if key != "" && c.labelFromKey != "" {
333+
labels[c.labelFromKey] = key
334+
}
335+
336+
addPathLabels(it, c.LabelFromPath(), labels)
337+
338+
if len(labels) > 0 {
330339
result = append(result, eachValue{
331-
Labels: map[string]string{
332-
c.labelFromKey: key,
333-
},
334-
Value: 1,
340+
Labels: labels,
341+
Value: 1,
335342
})
336343
}
337344
}
345+
338346
result = append(result, value...)
339347
default:
340348
result, errs = c.values(v)

pkg/customresourcestate/registry_factory_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,17 @@ func Test_values(t *testing.T) {
292292
newEachValue(t, 1, "type", "type-a"),
293293
newEachValue(t, 1, "type", "type-b"),
294294
}},
295+
{name: "info label from path", each: &compiledInfo{
296+
compiledCommon: compiledCommon{
297+
path: mustCompilePath(t, "status", "sub"),
298+
labelFromPath: map[string]valuePath{
299+
"active": mustCompilePath(t, "active"),
300+
},
301+
},
302+
}, wantResult: []eachValue{
303+
newEachValue(t, 1, "active", "1"),
304+
newEachValue(t, 1, "active", "3"),
305+
}},
295306
{name: "stateset", each: &compiledStateSet{
296307
compiledCommon: compiledCommon{
297308
path: mustCompilePath(t, "status", "phase"),

0 commit comments

Comments
 (0)