Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonpath filter for root level key-value #102

Open
kunkka234 opened this issue May 27, 2021 · 19 comments
Open

jsonpath filter for root level key-value #102

kunkka234 opened this issue May 27, 2021 · 19 comments

Comments

@kunkka234
Copy link

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
Copy link

Try this:

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

@kunkka234
Copy link
Author

kunkka234 commented Jun 8, 2021

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
Copy link

@kunkka234

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

@crockk
Copy link

crockk commented Mar 17, 2022

+1 Also having this issue

@VadimKMaxitech
Copy link

+1 The same issue.

3 similar comments
@Yifengzis
Copy link

+1 The same issue.

@Bagmetalex
Copy link

+1 The same issue.

@huazhicai
Copy link

+1 The same issue.

@corpocott
Copy link

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

@Flou21
Copy link

Flou21 commented Jul 23, 2023

+1 The same issue.

@basskitty
Copy link

Anyone has been able to figure this out yet?

@Eyeless77
Copy link

Any updates here?

@Eyeless77
Copy link

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
Copy link

lew-cmd commented Feb 13, 2024

+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
Copy link

+1 The same issue.

@eugenepaniot
Copy link

+1

1 similar comment
@fzadow
Copy link

fzadow commented Apr 22, 2024

+1

@f70ppy
Copy link

f70ppy commented Apr 22, 2024

+1 same here

@konstantin-kornienko
Copy link

Looks like to filter in JSONPath, you need a list.
But single dict like in your example cannot be converted to a list of dicts, i.e.:

{"status": "running"} => [{ "status": "running"}]

so it's impossible to do it with plain JSONPath

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests