prometheus-community / json_exporter

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

Problem with execute jsonpath #242

Open Sk1v opened 1 year ago

Sk1v commented 1 year ago

Hi all!

How to fix the error "Failed to execute jsonpath"

The goal is this: go to the url with a whip and take information from it, then transfer this information to the alert. So that when a letter arrived that something was broken, it was immediately written what exactly (and what exactly broke was written in the check). Check looks very simple:

{
  "status": "OK",
  "description": "All good",
  "sql": null
}

I need to get the description. As a result, I made such a config, focusing on examples:

modules:
  default:
    headers:
      MyHeader: MyHeaderValue
    metrics:
      - name: jsonhc
        type: object
        help: healthcheks description
        path: '{.description}'
        labels:
          env: prod
        values:
          status: 1
          description: '{.description}'

The answer I get is this:

json_exporter  | ts=2023-06-22T11:12:38.760Z caller=collector.go:136 level=error msg="Failed to execute jsonpath" err="description is not found" path={.description} data="\"All good\""
json_exporter  | ts=2023-06-22T11:12:38.761Z caller=collector.go:88 level=error msg="Failed to extract value for metric" path={.description} err="description is not found" metric="Desc{fqName: \"jsonhc_description\", help: \"healthcheks description\", constLabels: {}, variableLabels: [env]}"

The data I get is correct, but I can't take it for an alert. When I check the curl for targets, it's not there.

#curl http://localhost:7979/probe
Target parameter is missing

I've tried different variants of the "path" variable (such as '$.*' and '{ [*] }') but it doesn't work. Please help me figure it out is this a bug or am I doing something wrong?

boturkin commented 12 months ago

Try this, it should work:

modules:
  default:
    headers:
      MyHeader: MyHeaderValue
    metrics:
      - name: jsonhc
        type: object
        help: healthcheks description
        path: '{.description}'
        labels:
          env: prod
          description: '{@}'
        values:
          status: 1
Sk1v commented 11 months ago

So the problem was with Healthcheck itself. This is what it was supposed to be.

{
  "healthcheck": [
    {
      "status": "OK",
      "description": "All good",
      "sql": null
    }
  ]
}

After that the module worked as I wanted it to. Also, to do what I had in mind, I needed a module like this.

path: '{.healthcheck[?(@.status != "OK")]}'

And then in case there was not "OK", I set up an alert so that I would be sent an email when the status == 1

alexunderboots commented 2 months ago

same problem with non-slice JSON ((((

{"sip_status":"OFFLINE"}

not work path: '{.sip_status == "OFFLINE"}' values: status: 0