Postman測試Prometheus-v2.17.1 API

0.概述

  • Prometheus 穩定的 HTTP API 都在 /api/v1 端點下;
  • HTTP API 響應格式爲JSON;
響應狀態碼 描述
2xx 成功
400 參數丟失或不正確
422 無法執行表達式
503 查詢超時或中止時
  • JSON 響應格式:
{
  "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時間戳(以秒爲單位)格式,可選的小數位用於亞秒級精度。輸出時間戳始終以秒爲單位表示爲Unix時間戳;
  • 重複的查詢參數名稱以[]結尾;
  • <series_selector> 佔位符指的是Prometheus 時間序列選擇器;

例如http_requests_total或http_requests_total{method=~"(GET|POST)"}並且需要進行URL編碼。

  • <duration> 佔位符是指形式爲Prometheus的持續時間字符串 [0-9]+[smhdwy];

例如,5m指持續時間爲5分鐘。

  • <bool> 佔位符指的是布爾值(字符串true和false);

1.表達式查詢

查詢語言表達式可以在單個瞬間一段時間內求值;

1.1 即時查詢

GET /api/v1/query
POST /api/v1/query

參數 描述
query=<string> Prometheus表達式查詢字符串
*time=<rfc3339 | unix_timestamp> 時間戳,可選,默認當前服務器時間
*timeout=<duration> 超時,可選,默認-query.timeout標誌的值

time時間戳生成:

# -*- coding:utf-8 -*-

import time
import datetime

# RFC-3339 時間戳會存在某次請求,數據爲空的情況,建議使用 Unix 時間戳
rfctime = datetime.datetime.now().isoformat("T") + "Z"

print("RFC-3339 時間戳:{}".format(rfctime))
# RFC-3339 時間戳:2020-04-20T14:23:10.302701Z


# Unix時間戳
unixtime = time.time()

print("Unix 時間戳:{}".format(unixtime))
# Unix 時間戳:1587363790.302701

data數據格式:

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

http://IP:9090/api/v1/query?query=up
在這裏插入圖片描述

http://IP:9090/api/v1/query?query=up&time=2020-04-19T10:23:10.302Z
在這裏插入圖片描述

http://IP:9090/api/v1/query?query=up&time=1587363790.302701
在這裏插入圖片描述

1.2 範圍查詢

GET /api/v1/query_range
POST /api/v1/query_range

參數 描述
query=<string> Prometheus表達式查詢字符串
start=<rfc3339 | unix_timestamp> 開始時間戳
end=<rfc3339 | unix_timestamp> 結束時間戳
step=<duration | float> 以duration格式或浮點秒數查詢分辨率步長
timeout=<duration> 超時,可選,默認-query.timeout標誌的值

data數據格式:

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

http://IP:9090/api/v1/query_range?query=up&start=2020-04-19T10:00:00.201Z&end=2020-04-19T10:30:00.201Z&step=60s
在這裏插入圖片描述

2.查詢元數據

2.1 通過標籤匹配器查找序列

GET /api/v1/series
POST /api/v1/series

參數 描述
match[]=<series_selector> 重複的序列選擇器參數,用於選擇要返回的序列;
match[]必須至少提供一個參數;
start=<rfc3339 | unix_timestamp> 開始時間戳
end=<rfc3339 | unix_timestamp> 結束時間戳

http://IP:9090/api/v1/series?&match[]=up&match[]=process_start_time_seconds{job=“prometheus”}
在這裏插入圖片描述

2.2 獲取標籤名稱

GET /api/v1/labels
POST /api/v1/labels

http://IP:9090/api/v1/labels
在這裏插入圖片描述

2.3 查詢標籤值

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

http://IP:9090/api/v1/label/job/values
在這裏插入圖片描述

3.表達式查詢結果格式

表達式查詢可能在result 該data節的屬性中返回以下響應值。<sample_value>佔位符是數字樣本值。JSON不支持的特殊浮點值,例如NaN,Inf和-Inf,因此採樣值轉移所報JSON字符串,而不是原始數據。

3.1 範圍向量

範圍向量作爲結果類型返回 matrix。相應的 result 屬性具有以下格式:

[
  {
    "metric": { "<label_name>": "<label_value>", ... },
    "values": [ [ <unix_time>, "<sample_value>" ], ... ]
  },
  ...
]

3.2 即時向量

即時向量將作爲結果類型返回 vector。相應的 result 屬性具有以下格式:

[
  {
    "vector": { "<label_name>": "<label_value>", ... },
    "value": [ <unix_time>, "<sample_value>" ]
  },
  ...
]

3.3 標量

標量結果作爲結果類型返回 scalar。相應的 result 屬性具有以下格式:

[ <unix_time>, "<scalar_value>" ]

3.4 絃樂

字符串結果作爲結果類型返回 string。相應的 result 屬性具有以下格式:

[ <unix_time>, "<string_value>" ]

4.目標

以下端點返回Prometheus目標發現的當前狀態的概述:

GET /api/v1/targets

默認情況下,活動目標和已刪除目標都是響應的一部分。
labels 表示重新貼標籤後的標籤集。
discoveredLabels 表示重新標記發生之前在服務發現期間檢索到的未修改標籤。

參數 描述
*state=active|dropped|any 允許過濾目標,(例如,state=active,state=dropped,state=any)。請注意,對於已濾除的目標,仍然返回空數組。其他值將被忽略。

http://IP:9090/api/v1/targets?state=active
在這裏插入圖片描述

5.規則

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

參數 描述
*type=alert|record 返回警報規則(例如type=alert)或記錄規則(例如type=record)。如果該參數不存在或爲空,則不執行任何過濾。

http://IP:9090/api/v1/rules
在這裏插入圖片描述

6.報警

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

GET /api/v1/alerts

http://IP:9090/api/v1/alerts
在這裏插入圖片描述

7.查詢目標元數據

GET /api/v1/targets/metadata

參數 描述
*match_target=<label_selectors> 通過標籤集匹配目標的標籤選擇器。默認選擇所有目標。
*metric=<string> 爲檢索元數據指標名稱。默認檢索所有度量標準元數據。
*limit=<number> 匹配的最大目標數,默認所有目標

http://IP:9090/api/v1/targets/metadata?metric=go_goroutines&match_target={job=“prometheus”}&limit=2
在這裏插入圖片描述

8.查詢指標元數據

GET /api/v1/metadata

參數 描述
limit=<number> 返回的最大指標數量
*metric=<string> 指標名稱,用於過濾元數據,默認檢索所有度量標準元數據

http://IP:9090/api/v1/metadata?limit=3
在這裏插入圖片描述

9.Alertmanager

GET /api/v1/alertmanagers
激活和已刪除的Alertmanagers都是響應的一部分

http://IP:9090/api/v1/alertmanagers
在這裏插入圖片描述

10.狀態

查詢Prometheus配置

10.1 設定檔

GET /api/v1/status/config

http://IP:9090/api/v1/status/config
在這裏插入圖片描述

10.2 標誌

GET /api/v1/status/flags

http://IP:9090/api/v1/status/flags
在這裏插入圖片描述

10.3 運行時信息

GET /api/v1/status/runtimeinfo

http://IP:9090/api/v1/status/runtimeinfo
在這裏插入圖片描述

10.4 建造信息

GET /api/v1/status/buildinfo

http://IP:9090/api/v1/status/buildinfo
在這裏插入圖片描述

10.5 TSDB統計

GET /api/v1/status/tsdb

參數 描述
seriesCountByMetricName 提供指標名稱及其系列計數的列表
labelValueCountByLabelName 提供標籤名稱及其值計數的列表
memoryInBytesByLabelName 提供標籤名稱和以字節爲單位使用的內存的列表。內存使用量是通過將給定標籤名稱的所有值的長度相加得出的
seriesCountByLabelPair 提供標籤值對及其系列計數的列表

http://IP:9090/api/v1/status/tsdb
在這裏插入圖片描述

參考:
https://prometheus.io/docs/prometheus/latest/querying/api/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章