prometheus-community / json_exporter

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

object loop #250

Open yosefy opened 1 year ago

yosefy commented 1 year ago

hi we have this from nginx unit state


    "connections": {
        "accepted": 245214,
        "active": 0,
        "idle": 0,
        "closed": 245214
    },
    "requests": {
        "total": 245178
    },
    "applications": {
        "site2.com": {
            "processes": {
                "running": 1,
                "starting": 0,
                "idle": 1
            },
            "requests": {
                "active": 0
            }
        },
        "site1.com": {
            "processes": {
                    "running": 1,
                    "starting": 0,
                    "idle": 1
            },
            "requests": {
                "active": 0
            }
        }
    }
}```

so we need add metrics adding "application name" as label
meorkamalmeorsulaiman commented 9 months ago

I came across similar situation:

---
modules:
  device_stats:
    metrics:
    - name: status
      help: Connected Switches
      type: object
      path: '{ @ }'
      labels:
        status: '{ .status }'
      values:
        return: 1

    - name: list_of_interfaces
      help: list interfaces
      type: object
      path: '{ @.if_stat.* }'
      labels:
        ifname: '{ .port_id }'
      values:
        return: 1

JSON:

{
   "status": "connected",
    "hostname": "X",
    "if_stat": {
        "em0.0": {
            "tx_pkts": 238084479,
            "rx_pkts": 187104366,
            "rx_bytes": 28492395648,
            "port_id": "em0",
            "up": true,
            "tx_bytes": 144260009035
        },
        "irb.80": {
            "tx_pkts": 133010,
            "vlan": 80,
            "rx_pkts": 41813989,
            "rx_bytes": 14102487191,
            "port_id": "irb",
            "up": true,
            "tx_bytes": 7384805
        }
    }
}

I tried to iterate the interface however received the errors:

19 error(s) occurred:
* collected metric "list_of_interfaces_return" { label:{name:"ifname" value:"irb"} untyped:{value:1}} was collected before with the same name and label values
* collected metric "list_of_interfaces_return" { label:{name:"ifname" value:"irb"} untyped:{value:1}} was collected before with the same name and label values
* <snipped>

Would it be possible or is there a way that if we can iterate the nested object so that the metric comes like below?

list_of_interfaces_return{ifname="em0"} 1
list_of_interfaces_return{ifname="irb.80"} 1
ivanov17 commented 2 months ago

Has anyone found any solution for this problem? It seems the simplest solution would be to get the key of each object during iteration. But it looks like this is not possible with the current jsonpath library.

@rustycl0ck, @SuperQ could you help with this?