prometheus-community / json_exporter

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

check if string value is 'UP' #151

Open PrettySolution opened 2 years ago

PrettySolution commented 2 years ago

hello, I have a response from my server like that"

{
    "status": "UP|DOWN"
}

Is there a way to build a `service_status out of it?

metrics:
  - name: service_status
    path: "{ ?(@.status=='UP') }"

This did not work. Appreciate your help. Thanks!

weigo commented 2 years ago
    metrics:
      - name: health
        type: object
        path: '@.status == "UP"'
        help: Health Check Status
        values:
          active: 1

works for me^(TM)

rustycl0ck commented 1 year ago

This might work for you in case you have more enumerations (not just UP and DOWN) - https://github.com/prometheus-community/json_exporter/issues/186#issuecomment-1328156005

ScroogeZhang commented 1 year ago
  metrics:
  - name: service
    path: '{ .status }'
    type: object
    help: Health Check Status
    labels:
      status: '{}'
    values:
      health: 1

@PrettySolution This might work for you.....XD @rustycl0ck

qq1012827513 commented 1 year ago
  metrics:
  - name: service
    path: '{ .status }'
    type: object
    help: Health Check Status
    labels:
      status: '{}'
    values:
      health: 1

@PrettySolution This might work for you.....XD @rustycl0ck

it doesn't work for me.I got an Error when status goes from UP to DOWN

llamafilm commented 9 months ago

I have a similar issue. The suggestion above works, but it only shows metrics for items in the UP state. Is there a way to also return metrics for DOWN items with a value of 0? Example JSON:

{
    "response": [
        {
            "id": 0,
            "Name": "DM Out 1",
            "VideoStatus": "No Sync"
        },
        {
            "id": 2,
            "Name": "SDI 8K quad",
            "VideoStatus": "Mismatch Format"
        },
        {
            "id": 3,
            "Name": "Kumo Out 4 single",
            "VideoStatus": "Valid"
        }
    ]
}

My config:

metrics:
  - name: e2_input
    type: object
    path: '{.result.response[?(@.VideoStatus == "Valid")]}'
    labels:
      name: '{.Name}'
      id: '{.id}'
    values:
      valid: 1

The result is just one metric

e2_input_valid{id="3",name="Kumo Out 4 single"} 1

I would like to have 3 metrics with the ID number appended to the name like this:

e2_input0_valid{name="DM Out 1"} 0
e2_input2_valid{name="DI 8K quad"} 0
e2_input3_valid{name="Kumo Out 4 single"} 1