prometheus-community / json_exporter

A prometheus exporter which scrapes remote JSON by JSONPath
Apache License 2.0
638 stars 197 forks source link

jsonpath syntax / AND condition #300

Open nmamn opened 7 months ago

nmamn commented 7 months ago

Hi, I am trying to parse a simple json and to add a "AND" condition in my filter, but it seems to give me a syntax error.

let's say my json is

  "healthCheck": [
    {
      "service": "APP1",
      "url": "http://app1",
      "status": "OK"
    },
    {
      "service": "APP2",
      "url": "http://app2",
      "status": "OK"
    }
  ]
}

It seems I cannot put something like this in the config : path: '{.healthCheck[?(@.service == "APP1" && @.status == "OK")]}' I get an error in the logs : caller=collector.go:76 level=error msg="Failed to extract json objects for metric" err="unrecognized character in action: U+0026 '&'"

Am I missing something ? this syntax seems perfectly valid in jsonpath, if I test it in https://jsonpath.com/ it just perfectly works. Is it supported by this exporter ?

Many thanks,

Nicolas

arokiaputhotta commented 6 months ago

I have the same problem, any help on this?

maksimse commented 1 month ago

I think this is due to k8s.io/client-go/util/jsonpath doesn't allow regex in jsonpath.

We have solved this using Prometheus metric_relabel_configs . @nmamn , it should work like this:

path: "{ .healthCheck[*] }"

and prometheus.yml:

                    metric_relabel_configs:
                      - source_labels: [service]
                        action: drop
                        regex: "^(APP1|APPn|.*APPz.*)$"