prometheus-community / json_exporter

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

jsonpath filter for root level key-value #102

Open kunkka234 opened 3 years ago

kunkka234 commented 3 years ago

Hi, i got some problem when trying to use jsonpath filter in root level key-value.

what i want: for the example json:

{
    "status": "running"
}

if status == "running", set static metric = 1 if status == "stopped", set static metric = 0

my config.yml:

metrics:
- name: test
  type: object
  path: '{$.[?($.status=="running")]}'
  help: test
  labels:
    label: test
  values:
    active: 1

- name: test
  type: object
  path: '{$.[?($.status=="stopped")]}'
  help: test
  labels:
    label: test
  values:
    active: 0

what did i see http://localhost:7979/probe?target=http://host.docker.internal:8000/status.json returns me nothing but white screen, and get following error in console

level=error ts=2021-05-27T11:28:23.049Z caller=collector.go:128 msg="Failed to execute jsonpath" err=" is not found" path="{$.[?($.status==\"running\")]}" data="{\n   \"status\":\"running\"\n}"
level=error ts=2021-05-27T11:28:23.050Z caller=collector.go:69 msg="Failed to extract json objects for metric" err=" is not found" metric="Desc{fqName: \"test_active\", help: \"test\", constLabels: {}, variableLabels: [label]}"
level=error ts=2021-05-27T11:28:23.050Z caller=collector.go:128 msg="Failed to execute jsonpath" err=" is not found" path="{$.[?($.status==\"stopped\")]}" data="{\n   \"status\":\"running\"\n}"
level=error ts=2021-05-27T11:28:23.050Z caller=collector.go:69 msg="Failed to extract json objects for metric" err=" is not found" metric="Desc{fqName: \"test_active\", help: \"test\", constLabels: {}, variableLabels: [label]}"

other info the path $.[?($.status=="running")] was tested successfully in https://jsonpath.herokuapp.com/, but failed in https://jsonpath.com/. and i also tried other path like .[?($.status=="running")], .[?(@.status=="running")], and $.[?(@.status=="running")] but none of them working but got the same error message. looking for your help and thanks in advance

redbaron-gt commented 3 years ago

Try this:

'{$[?(@.status=="running")]}'

kunkka234 commented 3 years ago

Try this:

'{$[?(@.status=="running")]}'

thanks for your reply, but it's not working. and the error message doesn't change as well

level=error ts=2021-06-08T03:28:16.722Z caller=collector.go:128 msg="Failed to execute jsonpath" err="map[status:running] is not array or slice and cannot be filtered" path="{$[?(@.status==\"running\")]}" data="{\n   \"status\": \"running\"\n}\n"
level=error ts=2021-06-08T03:28:16.723Z caller=collector.go:69 msg="Failed to extract json objects for metric" err="map[status:running] is not array or slice and cannot be filtered" metric="Desc{fqName: \"test_active\", help: \"test\", constLabels: {}, variableLabels: [label]}"
level=error ts=2021-06-08T03:28:16.723Z caller=collector.go:128 msg="Failed to execute jsonpath" err="map[status:running] is not array or slice and cannot be filtered" path="{$[?(@.status==\"stopped\")]}" data="{\n   \"status\": \"running\"\n}\n"
level=error ts=2021-06-08T03:28:16.723Z caller=collector.go:69 msg="Failed to extract json objects for metric" err="map[status:running] is not array or slice and cannot be filtered" metric="Desc{fqName: \"test_active\", help: \"test\", constLabels: {}, variableLabels: [label]}"
surco1971 commented 2 years ago

@kunkka234

I had the same issue today, no solution after 1 hour of struggling. Did you find a way to solve this ?

crockk commented 2 years ago

+1 Also having this issue

VadimKMaxitech commented 2 years ago

+1 The same issue.

Yifengzis commented 1 year ago

+1 The same issue.

Bagmetalex commented 1 year ago

+1 The same issue.

huazhicai commented 1 year ago

+1 The same issue.

corpocott commented 1 year ago

anyone find the answer to this? Wasn't able to figure it out :(

Flou21 commented 1 year ago

+1 The same issue.

postwarbreakout commented 11 months ago

Anyone has been able to figure this out yet?

Eyeless77 commented 7 months ago

Any updates here?

Eyeless77 commented 7 months ago

Given the same example.json

{
    "status": "running"
}

I was able to extract status and apply it as label as suggested here

My config.yml looks like this:

---
modules:
  health:
    metrics:
    - name: service_status
      type: object
      help: Service Status
      path: '{$}'
      labels:
        status: '{ .status }'
      values:
        availability: 1

To the root path in JSONPath is '{$}'

lew-cmd commented 6 months ago

+1 The same issue here. Solution shown above is looking at list structure, not nested objects, which seems to be the main issue for the json-exporter

petshopjke1337 commented 6 months ago

+1 The same issue.

eugenepaniot commented 6 months ago

+1

fzadow commented 4 months ago

+1

f70ppy commented 4 months ago

+1 same here