Prometheus學(xué)習(xí)系列(二十三)之PromQL HTTP API

在Prometheus服務(wù)器上的/api/v1下可以訪問當(dāng)前穩(wěn)定的HTTP API。 將在該端點下添加任何非中斷添加項。

一、格式概述

這個API返回是JSON格式。每個請求成功的返回值都是以2xx開頭的編碼。

到達API處理的無效請求,返回一個JSON錯誤對象,并返回下面的錯誤碼:

  • 400 Bad Request。當(dāng)參數(shù)錯誤或者丟失時。
  • 422 Unprocessable Entity。當(dāng)一個表達式不能被執(zhí)行時。
  • 503 Service Unavailable。當(dāng)查詢超時或者中斷時。

對于在到達API端點之前發(fā)生的錯誤,可以返回其他非2xx代碼。

如果存在不阻止請求執(zhí)行的錯誤,則可以返回警告數(shù)組。 成功收集的所有數(shù)據(jù)都將在數(shù)據(jù)字段中返回。

JSON響應(yīng)信封格式如下:

{
  "status": "success" | "error",
  "data": <data>,

  // Only set if status is "error". The data field may still hold
  // additional data.
  "errorType": "<string>",
  "error": "<string>",

  // Only if there were warnings while executing the request.
  // There will still be data in the data field.
  "warnings": ["<string>"]
}

輸入時間戳可以以RFC3339格式提供,也可以以秒為單位提供給Unix時間戳,可選的小數(shù)位數(shù)用于亞秒級精度。 輸出時間戳始終表示為Unix時間戳,以秒為單位。

可以以[]結(jié)尾的查詢參數(shù)的名稱。

<series_selector>占位符指的是Prometheus時間序列選擇器,如http_requests_totalhttp_requests_total{method =?"(GET|POST)"},需要進行URL編碼。

<duration>占位符指的是[0-9]+[smhdwy]形式的Prometheus持續(xù)時間字符串。 例如,5m指的是5分鐘的持續(xù)時間。

<bool>占位符引用布爾值(字符串truefalse)。

二、表達式查詢

可以在單個時刻或在一段時間內(nèi)評估查詢語言表達。 以下部分描述了每種表達式查詢的API端點。

2.1 Instant queries(即時查詢)

以下端點在單個時間點評估即時查詢:

GET /api/v1/query

URL查詢參數(shù):

  • query=<string>: Prometheus表達式查詢字符串。
  • time=<rfc3339 | uninx_timestamp>: 執(zhí)行時間戳,可選項。
  • timeout=<duration>: 執(zhí)行超時時間設(shè)置,可選項,默認(rèn)由-query.timeout標(biāo)志設(shè)置

如果time缺省,則用當(dāng)前服務(wù)器時間表示執(zhí)行時刻。

這個查詢結(jié)果的data部分有下面格式:

{
 "resultType": "matrix" | "vector" | "scalar" | "string",
 "result": <value>
}

<value>是一個查詢結(jié)果數(shù)據(jù),依賴于這個resultType格式,見表達式查詢結(jié)果格式>
。

下面例子執(zhí)行了在時刻是2015-07-01T20:10:51.781Zup表達式:

$ curl 'http://localhost:9090/api/v1/query?query=up&time=2015-07-01T20:10:51.781Z'
{
 "status": "success",
 "data":{
    "resultType": "vector",
    "result" : [
         {
            "metric" : {
               "__name__" : "up",
               "job" : "prometheus",
               "instance" : "localhost:9090"
            },
            "value": [ 1435781451.781, "1" ]
         },
         {
            "metric" : {
               "__name__" : "up",
               "job" : "node",
               "instance" : "localhost:9100"
            },
            "value" : [ 1435781451.781, "0" ]
         }
    ]
 }
}
2.2 范圍查詢

以下端點在一段時間內(nèi)評估表達式查詢:

GET /api/v1/query_range

URL查詢參數(shù)

  • query=<string>: Prometheus表達式查詢字符串。
  • start=<rfc3339 | unix_timestamp>: 開始時間戳。
  • end=<rfc3339 | unix_timestamp>: 結(jié)束時間戳。
  • step=<duration>: 以持續(xù)時間格式查詢分辨率步長或浮點秒數(shù)。
  • timeout=<duration>:評估超時。 可選的。 默認(rèn)為-query.timeout標(biāo)志的值并受其限制。

查詢結(jié)果的數(shù)據(jù)部分具有以下格式:

{
    "resultType": "matrix",
    "result": <value>
}

對于<value>占位符的格式,詳見范圍向量結(jié)果格式。

以下示例在30秒范圍內(nèi)評估表達式,查詢分辨率為15秒。

$ curl 'http://localhost:9090/api/v1/query_range?query=up&start=2015-07-01T20:10:30.781Z&end=2015-07-01T20:11:00.781Z&step=15s'
{
   "status" : "success",
   "data" : {
      "resultType" : "matrix",
      "result" : [
         {
            "metric" : {
               "__name__" : "up",
               "job" : "prometheus",
               "instance" : "localhost:9090"
            },
            "values" : [
               [ 1435781430.781, "1" ],
               [ 1435781445.781, "1" ],
               [ 1435781460.781, "1" ]
            ]
         },
         {
            "metric" : {
               "__name__" : "up",
               "job" : "node",
               "instance" : "localhost:9091"
            },
            "values" : [
           [ 1435781430.781, "0" ],
               [ 1435781445.781, "0" ],
               [ 1435781460.781, "1" ]
            ]
         }
      ]
   }
}
三、查詢元數(shù)據(jù)
3.1 通過標(biāo)簽匹配器找到度量指標(biāo)列表

以下端點返回與特定標(biāo)簽集匹配的時間系列列表。

GET /api/v1/series

URL查詢參數(shù):

  • match[]=<series_selector>: 選擇器是series_selector。這個參數(shù)個數(shù)必須大于等于1.
  • start=<rfc3339 | unix_timestamp>: 開始時間戳。
  • end=<rfc3339 | unix_timestamp>: 結(jié)束時間戳。

查詢結(jié)果的data部分包含一個對象列表,這些對象包含標(biāo)識每個系列的標(biāo)簽名稱/值對。

下面這個例子返回時間序列數(shù)據(jù), 選擇器是up或者process_start_time_seconds{job="prometheus"}

$ curl -g 'http://localhost:9090/api/v1/series?match[]=up&match[]=process_start_time_seconds{job="prometheus"}'
{
   "status" : "success",
   "data" : [
      {
         "__name__" : "up",
         "job" : "prometheus",
         "instance" : "localhost:9090"
      },
      {
         "__name__" : "up",
         "job" : "node",
         "instance" : "localhost:9091"
      },
      {
         "__name__" : "process_start_time_seconds",
         "job" : "prometheus",
         "instance" : "localhost:9090"
      }
   ]
}
3.2 查詢標(biāo)簽值

以下端點返回標(biāo)簽名稱列表:

GET /api/v1/label/<label_name>/values

JSON響應(yīng)的data部分是字符串標(biāo)簽名稱的列表。

這是一個例子。

$ curl http://localhost:9090/api/v1/label/job/values
{
   "status" : "success",
   "data" : [
      "node",
      "prometheus"
   ]
}
3.3 查詢標(biāo)簽值

以下端點返回提供的標(biāo)簽名稱的標(biāo)簽值列表:

GET /api/v1/label/<label_name>/values

JSON響應(yīng)的data部分是字符串標(biāo)簽值的列表。

此示例查詢作業(yè)標(biāo)簽的所有標(biāo)簽值:

$ curl http://localhost:9090/api/v1/label/job/values
{
   "status" : "success",
   "data" : [
      "node",
      "prometheus"
   ]
}
四、表達式查詢結(jié)果格式

表達式查詢可能會在data部分的result屬性中返回以下響應(yīng)值。 <sample_value>占位符是數(shù)字樣本值。 JSON不支持特殊的浮點值,例如NaNInf-Inf,因此樣本值將作為帶引號的JSON字符串而不是原始數(shù)字傳輸。

4.1 范圍向量

范圍向量返回的result類型是一個matrix矩陣。下面返回的結(jié)果是result部分的數(shù)據(jù)格式:

[
  {
    "metric": { "<label_name>": "<label_value>", ... },
    "values": [ [ <unix_time>, "<sample_value>" ], ... ]
  },
  ...
]
4.2 瞬時向量

瞬時向量的result類型是vector。下面是result部分的數(shù)據(jù)格式

[
  {
    "metric": { "<label_name>": "<label_value>", ... },
    "value": [ <unix_time>, "<sample_value>" ]
  },
  ...
]
4.3 Scalars標(biāo)量

標(biāo)量查詢返回result類型是scalar。下面是result部分的數(shù)據(jù)格式:

[ <unix_time>, "<scalar_value>" ]

4.4 字符串

字符串的result類型是string。下面是result部分的數(shù)據(jù)格式:

[ <unix_time>, "<string_value>" ]

五、Targets目標(biāo)

以下端點返回Prometheus目標(biāo)發(fā)現(xiàn)的當(dāng)前狀態(tài)概述:

GET /api/v1/targets

活動目標(biāo)和刪除目標(biāo)都是響應(yīng)的一部分。 labels表示重新標(biāo)記發(fā)生后的標(biāo)簽集。 discoveredLabels表示在發(fā)生重新標(biāo)記之前在服務(wù)發(fā)現(xiàn)期間檢索到的未修改標(biāo)簽。

$ curl http://localhost:9090/api/v1/targets
{
  "status": "success",
  "data": {
    "activeTargets": [
      {
        "discoveredLabels": {
          "__address__": "127.0.0.1:9090",
          "__metrics_path__": "/metrics",
          "__scheme__": "http",
          "job": "prometheus"
        },
        "labels": {
          "instance": "127.0.0.1:9090",
          "job": "prometheus"
        },
        "scrapeUrl": "http://127.0.0.1:9090/metrics",
        "lastError": "",
        "lastScrape": "2017-01-17T15:07:44.723715405+01:00",
        "health": "up"
      }
    ],
    "droppedTargets": [
      {
        "discoveredLabels": {
          "__address__": "127.0.0.1:9100",
          "__metrics_path__": "/metrics",
          "__scheme__": "http",
          "job": "node"
        },
      }
    ]
  }
}
六、Rules規(guī)則

/rules API端點返回當(dāng)前加載的警報和記錄規(guī)則列表。 此外,它還返回由每個警報規(guī)則的Prometheus實例觸發(fā)的當(dāng)前活動警報。

由于/rules端點相當(dāng)新,它沒有與總體API v1相同的穩(wěn)定性保證。

GET /api/v1/rules

$ curl http://localhost:9090/api/v1/rules

{
    "data": {
        "groups": [
            {
                "rules": [
                    {
                        "alerts": [
                            {
                                "activeAt": "2018-07-04T20:27:12.60602144+02:00",
                                "annotations": {
                                    "summary": "High request latency"
                                },
                                "labels": {
                                    "alertname": "HighRequestLatency",
                                    "severity": "page"
                                },
                                "state": "firing",
                                "value": 1
                            }
                        ],
                        "annotations": {
                            "summary": "High request latency"
                        },
                        "duration": 600,
                        "health": "ok",
                        "labels": {
                            "severity": "page"
                        },
                        "name": "HighRequestLatency",
                        "query": "job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5",
                        "type": "alerting"
                    },
                    {
                        "health": "ok",
                        "name": "job:http_inprogress_requests:sum",
                        "query": "sum(http_inprogress_requests) by (job)",
                        "type": "recording"
                    }
                ],
                "file": "/rules.yaml",
                "interval": 60,
                "name": "example"
            }
        ]
    },
    "status": "success"
}
七、Alerts報警

/alerts端點返回所有活動警報的列表。

由于/alerts端點相當(dāng)新,它沒有與總體API v1相同的穩(wěn)定性保證。

GET /api/v1/alerts

$ curl http://localhost:9090/api/v1/alerts

{
    "data": {
        "alerts": [
            {
                "activeAt": "2018-07-04T20:27:12.60602144+02:00",
                "annotations": {},
                "labels": {
                    "alertname": "my-alert"
                },
                "state": "firing",
                "value": 1
            }
        ]
    },
    "status": "success"
}
八、查詢目標(biāo)元數(shù)據(jù)

以下端點返回有關(guān)目標(biāo)正在刮取的度量標(biāo)準(zhǔn)的元數(shù)據(jù)。 這是實驗性的,將來可能會發(fā)生變化。

GET /api/v1/targets/metadata

URL查詢參數(shù):

  • match_target=<label_selectors>:通過標(biāo)簽集匹配目標(biāo)的標(biāo)簽選擇器。 如果留空則選擇所有目標(biāo)。
  • metric=<string>:用于檢索元數(shù)據(jù)的度量標(biāo)準(zhǔn)名稱。 如果留空,則檢索所有度量標(biāo)準(zhǔn)元數(shù)據(jù)。
  • limit=<number>:要匹配的最大目標(biāo)數(shù)。

查詢結(jié)果的data部分包含一個包含度量元數(shù)據(jù)和目標(biāo)標(biāo)簽集的對象列表。

以下示例從前兩個目標(biāo)返回go_goroutines指標(biāo)的所有元數(shù)據(jù)條目,標(biāo)簽為job ="prometheus"。

curl -G http://localhost:9091/api/v1/targets/metadata \
    --data-urlencode 'metric=go_goroutines' \
    --data-urlencode 'match_target={job="prometheus"}' \
    --data-urlencode 'limit=2'
{
  "status": "success",
  "data": [
    {
      "target": {
        "instance": "127.0.0.1:9090",
        "job": "prometheus"
      },
      "type": "gauge",
      "help": "Number of goroutines that currently exist.",
      "unit": ""
    },
    {
      "target": {
        "instance": "127.0.0.1:9091",
        "job": "prometheus"
      },
      "type": "gauge",
      "help": "Number of goroutines that currently exist.",
      "unit": ""
    }
  ]
}

以下示例返回標(biāo)簽instance="127.0.0.1:9090"的所有目標(biāo)的所有度量標(biāo)準(zhǔn)的元數(shù)據(jù)。

curl -G http://localhost:9091/api/v1/targets/metadata \
    --data-urlencode 'match_target={instance="127.0.0.1:9090"}'
{
  "status": "success",
  "data": [
    // ...
    {
      "target": {
        "instance": "127.0.0.1:9090",
        "job": "prometheus"
      },
      "metric": "prometheus_treecache_zookeeper_failures_total",
      "type": "counter",
      "help": "The total number of ZooKeeper failures.",
      "unit": ""
    },
    {
      "target": {
        "instance": "127.0.0.1:9090",
        "job": "prometheus"
      },
      "metric": "prometheus_tsdb_reloads_total",
      "type": "counter",
      "help": "Number of times the database reloaded block data from disk.",
      "unit": ""
    },
    // ...
  ]
}
九、Altermanagers警報管理器

以下端點返回Prometheus alertmanager發(fā)現(xiàn)的當(dāng)前狀態(tài)概述:

GET /api/v1/alertmanagers

活動和丟棄的Alertmanagers都是響應(yīng)的一部分。

$ curl http://localhost:9090/api/v1/alertmanagers
{
  "status": "success",
  "data": {
    "activeAlertmanagers": [
      {
        "url": "http://127.0.0.1:9090/api/v1/alerts"
      }
    ],
    "droppedAlertmanagers": [
      {
        "url": "http://127.0.0.1:9093/api/v1/alerts"
      }
    ]
  }
}
十、Status狀態(tài)

以下狀態(tài)端點顯示當(dāng)前的Prometheus配置。

10.1 Config配置

以下端點返回當(dāng)前加載的配置文件:

GET /api/v1/status/config

配置作為轉(zhuǎn)儲的YAML文件返回。 由于YAML庫的限制,不包括YAML注釋。

$ curl http://localhost:9090/api/v1/status/config
{
  "status": "success",
  "data": {
    "yaml": "<content of the loaded config file in YAML>",
  }
}
10.2 Flags標(biāo)志

以下端點返回Prometheus配置的標(biāo)志值:

GET /api/v1/status/flags

所有值都以“字符串”的形式出現(xiàn)。

$ curl http://localhost:9090/api/v1/status/flags
{
  "status": "success",
  "data": {
    "alertmanager.notification-queue-capacity": "10000",
    "alertmanager.timeout": "10s",
    "log.level": "info",
    "query.lookback-delta": "5m",
    "query.max-concurrency": "20",
    ...
  }
}

v2.2中的新內(nèi)容。

十一、TSDB Admin APIs,TSDB管理API

這些是為高級用戶公開數(shù)據(jù)庫功能的API。 除非設(shè)置了--web.enable-admin-api,否則不會啟用這些API。

我們還公開了一個gRPC API,其定義可以在這里找到。 這是實驗性的,將來可能會發(fā)生變化。

11.1 快照

快照會將所有當(dāng)前數(shù)據(jù)的快照創(chuàng)建到TSDB數(shù)據(jù)目錄下的snapshots/<datetime>-<rand>中,并將該目錄作為響應(yīng)返回。 它可以選擇跳過僅存在于頭塊中但尚未壓縮到磁盤的快照數(shù)據(jù)。

POST /api/v1/admin/tsdb/snapshot?skip_head=<bool>

$ curl -XPOST http://localhost:9090/api/v1/admin/tsdb/snapshot
{
  "status": "success",
  "data": {
    "name": "20171210T211224Z-2be650b6d019eb54"
  }
}

快照已存在<data-dir>/snapshots/20171210T211224Z-2be650b6d019eb54
v2.1新內(nèi)容。

11.2 刪除序列

DeleteSeries刪除時間范圍內(nèi)所選系列的數(shù)據(jù)。 實際數(shù)據(jù)仍然存在于磁盤上,并在將來的壓縮中清除,或者可以通過點擊Clean Tombstones端點來明確清理。

如果成功,則返回204。

POST /api/v1/admin/tsdb/delete_series

URL查詢參數(shù):

  • match[]=<series_selector>:選擇要刪除的系列的重復(fù)標(biāo)簽匹配器參數(shù)。 必須至少提供一個match[]參數(shù)。
  • start= <rfc3339 | unix_timestamp>:開始時間戳。 可選,默認(rèn)為最短可能時間。
  • end= <rfc3339 | unix_timestamp>:結(jié)束時間戳。 可選,默認(rèn)為最長可能時間。

不提及開始和結(jié)束時間將清除數(shù)據(jù)庫中匹配系列的所有數(shù)據(jù)。

例:

$ curl -X POST \
  -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=up&match[]=process_start_time_seconds{job="prometheus"}'

v2.1新內(nèi)容

11.3 CleanTombstones

CleanTombstones從磁盤中刪除已刪除的數(shù)據(jù)并清理現(xiàn)有的邏輯刪除。 這可以在刪除系列后使用以釋放空間。

如果成功,則返回204。

POST /api/v1/admin/tsdb/clean_tombstones

這不需要參數(shù)或正文。

$ curl -XPOST http://localhost:9090/api/v1/admin/tsdb/clean_tombstones

v2.1新內(nèi)容。

十二、鏈接

Prometheus官網(wǎng)地址:https://prometheus.io/
我的Github:https://github.com/Alrights/prometheus

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容