Skip to content

Commit

Permalink
crd allow info and stateSet metrics to expose values from arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Jul 4, 2022
1 parent 88d189c commit e6eda69
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pkg/customresourcestate/config_metrics_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ type MetricStateSet struct {
List []string `yaml:"list" json:"list"`
// LabelName is the key of the label which is used for each entry in List to expose the value.
LabelName string `yaml:"labelName" json:"labelName"`
// ValueFrom is the subpath to compare the list to.
ValueFrom []string `yaml:"valueFrom" json:"valueFrom"`
}
47 changes: 43 additions & 4 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,15 @@ func newCompiledMetric(m Metric) (compiledMetric, error) {
if err != nil {
return nil, fmt.Errorf("each.stateSet: %w", err)
}
valueFromPath, err := compilePath(m.StateSet.ValueFrom)
if err != nil {
return nil, fmt.Errorf("each.gauge.valueFrom: %w", err)
}
return &compiledStateSet{
compiledCommon: *cc,
List: m.StateSet.List,
LabelName: m.StateSet.LabelName,
ValueFrom: valueFromPath,
}, nil
default:
return nil, fmt.Errorf("unknown metric type %s", m.Type)
Expand Down Expand Up @@ -248,7 +253,23 @@ type compiledInfo struct {
compiledCommon
}

func (c *compiledInfo) Values(v interface{}) (result []eachValue, err []error) {
func (c *compiledInfo) Values(v interface{}) (result []eachValue, errs []error) {
if vs, isArray := v.([]interface{}); isArray {
for _, obj := range vs {
ev, err := c.values(obj)
if len(err) > 0 {
errs = append(errs, err...)
continue
}
result = append(result, ev...)
}
return
}

return c.values(v)
}

func (c *compiledInfo) values(v interface{}) (result []eachValue, err []error) {
value := eachValue{Value: 1, Labels: map[string]string{}}
addPathLabels(v, c.labelFromPath, value.Labels)
result = append(result, value)
Expand All @@ -257,14 +278,32 @@ func (c *compiledInfo) Values(v interface{}) (result []eachValue, err []error) {

type compiledStateSet struct {
compiledCommon
ValueFrom valuePath
List []string
LabelName string
}

func (c *compiledStateSet) Values(v interface{}) (result []eachValue, err []error) {
value, ok := v.(string)
func (c *compiledStateSet) Values(v interface{}) (result []eachValue, errs []error) {
if vs, isArray := v.([]interface{}); isArray {
for _, obj := range vs {
ev, err := c.values(obj)
if len(err) > 0 {
errs = append(errs, err...)
continue
}
result = append(result, ev...)
}
return
}

return c.values(v)
}

func (c *compiledStateSet) values(v interface{}) (result []eachValue, errs []error) {
comparable := c.ValueFrom.Get(v)
value, ok := comparable.(string)
if !ok {
return []eachValue{}, []error{fmt.Errorf("%s: expected value for path to be string, got %T", c.path, v)}
return []eachValue{}, []error{fmt.Errorf("%s: expected value for path to be string, got %T", c.path, comparable)}
}

for _, entry := range c.List {
Expand Down

0 comments on commit e6eda69

Please sign in to comment.