How to use prometheus/thanos api: /api/v1/rules param rule_group

838 Views Asked by At

when i use prometheus api to get specify rule group infomation, the api doc as following: https://prometheus.io/docs/prometheus/latest/querying/api/#rules

URL query parameters:

type=alert|record: return only the alerting rules (e.g. type=alert) or the recording rules (e.g. type=record). When the parameter is absent or empty, no filtering is done.
rule_name[]=<string>: only return rules with the given rule name. If the parameter is repeated, rules with any of the provided names are returned. If we've filtered out all the rules of a group, the group is not returned. When the parameter is absent or empty, no filtering is done.
rule_group[]=<string>: only return rules with the given rule group name. If the parameter is repeated, rules with any of the provided rule group names are returned. When the parameter is absent or empty, no filtering is done.
file[]=<string>: only return rules with the given filepath. If the parameter is repeated, rules with any of the provided filepaths are returned. When the parameter is absent or empty, no filtering is done.

but when i curl the api use following three methods, it all doesn't work!

curl -s http://localhost:9090/api/v1/rules?rule_group=ec.rules

curl -s http://localhost:9090/api/v1/rules -d '{"rule_group": "ec.rules"}' -H "Content-Type: application/json"

curl -XGET -s 'http://localhost:9090/api/v1/rules' --data-urlencode "rule_group=ec.rules" -H "Content-Type: application/x-www-form-urlencoded"

curl thanos query is the same result

curl -s http://localhost:10902/api/v1/rules?rule_group=ec.rules

Anybody know any wrong when i use the curl?Or tell me how to use the prometheus/thanos api? Thanks a lot!

prometheus_version: v2.45.0
thanos_version: v0.31.0
1

There are 1 best solutions below

0
Edward Zhang On

Using latest prometheus version https://github.com/prometheus/prometheus/releases/tag/v2.47.2 can get the correct response. My example:

curl -X GET "http://10.136.134.115:9090/api/v1/rules?type=alert&rule_name[]=dingshi"    


{
  "status": "success",
  "data": {
    "groups": [
      {
        "name": "node-alerts",
        "file": "/etc/prometheus/rules/rules.yaml",
        "rules": [
          {
            "state": "inactive",
            "name": "dingshi",
            "query": "100 - (node_memory_MemFree_bytes{instance=\"10.136.134.240:9100\"} / node_memory_MemTotal_bytes{instance=\"10.136.134.240:9100\"} * 100) > 123",
            "duration": 60,
            "keepFiringFor": 0,
            "labels": {
              "severity": "P1"
            },
            "annotations": {
              "description": "test_two",
              "instance": "10.136.134.240",
              "metric": "内存使用率",
              "operator": "gt",
              "threshold": "123"
            },
            "alerts": [],
            "health": "ok",
            "evaluationTime": 0.000517632,
            "lastEvaluation": "2023-11-06T17:51:09.945355348+08:00",
            "type": "alerting"
          }
        ],
        "interval": 60,
        "limit": 0,
        "evaluationTime": 0.001195302,
        "lastEvaluation": "2023-11-06T17:51:09.945345529+08:00"
      }
    ]
  }
}