I have JSON strings upload to AWS Cloudwatch logs and am now trying to write a filter that generates metrics from those logs.
Suppose I have JSON like the following:
{"test_results": [{"result": "success", "name": "foo", "nr_failed": 0}, {"result": "failure", "name": "bar", "nr_failed": 3}]}
The AWS Cloudwatch log metrics filter allows me to specify a pattern like the following:
{ $.test_results[0].result = "failure" }
The above means, "match the entire line if the zeroth entry in the value of 'test_results' has a value of 'failure' for its 'result' field". In this particular case, the zeroth entry had a result of 'success', so it wouldn't match. But this next line would match:
{ $.test_results[1].result = "failure" }
Now suppose I want to match the whole line if any of the entries had a value of 'failure'. Basically, I want to do this:
{ $.test_results[*].result = "failure" }
But the pattern syntax doesn't seem to allow wildcards in the array index. How can I match lines where any of the result values are "failure" ?
Alternatively, how could I pick out the numeric values for all the "nr_failed" fields in the "test_results" array and create metrics from those?