json_exporter icon indicating copy to clipboard operation
json_exporter copied to clipboard

jsonpath for length of an array

Open endeepak opened this issue 7 years ago • 4 comments

Is there a way to get length of an array?

Example:

- name: active_values_count
  type: object
  path: $.values[*]?(@.state == "ACTIVE").length()

endeepak avatar Aug 22 '17 05:08 endeepak

Hi, I think the path defines element selection for the filter. The length or the array does not help you with element selection. You have it in inappropriate place. Maybe you can use in label or value.

zmlpjuran avatar Oct 03 '17 08:10 zmlpjuran

Is this still an issue? It seems solved.

SuperQ avatar Apr 17 '20 10:04 SuperQ

It is still an issue and it won't be resolved unless the collector is re-written to use a jsonpath module that supports the .length() method, or if kubernetes implements it (since we use their jsonpath module): https://github.com/prometheus-community/json_exporter/blob/1614ee8d771f89a8c6093aee9985609c69b37532/exporter/collector.go#L23

BapRx avatar Mar 07 '22 18:03 BapRx

This is also an issue for accessing specific elements. I can't do $[(@.length - 2)] for instance

Radvendii avatar Nov 08 '22 12:11 Radvendii