Merge pull request #373 from zalando-incubator/json-path-array
Handle more complex array in json path
This commit is contained in:
@ -72,10 +72,25 @@ func (g *JSONPathMetricsGetter) GetMetric(metricsURL url.URL) (float64, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(nodes) != 1 {
|
if len(nodes) == 0 {
|
||||||
return 0, fmt.Errorf("unexpected json: expected single numeric or array value")
|
return 0, fmt.Errorf("unexpected json: expected single numeric or array value")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(nodes) > 1 {
|
||||||
|
if g.aggregator == nil {
|
||||||
|
return 0, fmt.Errorf("no aggregator function has been specified")
|
||||||
|
}
|
||||||
|
values := make([]float64, 0, len(nodes))
|
||||||
|
for _, node := range nodes {
|
||||||
|
v, err := node.GetNumeric()
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("unexpected json: did not find numeric or array value '%s': %w", nodes, err)
|
||||||
|
}
|
||||||
|
values = append(values, v)
|
||||||
|
}
|
||||||
|
return g.aggregator(values...), nil
|
||||||
|
}
|
||||||
|
|
||||||
node := nodes[0]
|
node := nodes[0]
|
||||||
if node.IsArray() {
|
if node.IsArray() {
|
||||||
if g.aggregator == nil {
|
if g.aggregator == nil {
|
||||||
|
@ -51,6 +51,13 @@ func TestJSONPathMetricsGetter(t *testing.T) {
|
|||||||
result: 5,
|
result: 5,
|
||||||
aggregator: Average,
|
aggregator: Average,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "glob array query",
|
||||||
|
jsonResponse: []byte(`{"worker_status":[{"last_status":{"backlog":3}},{"last_status":{"backlog":7}}]}`),
|
||||||
|
jsonPath: "$.worker_status.[*].last_status.backlog",
|
||||||
|
result: 5,
|
||||||
|
aggregator: Average,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "json path not resulting in array or number should lead to error",
|
name: "json path not resulting in array or number should lead to error",
|
||||||
jsonResponse: []byte(`{"metric.value":5}`),
|
jsonResponse: []byte(`{"metric.value":5}`),
|
||||||
|
Reference in New Issue
Block a user