本文來自Prometheus官網手冊 和 Prometheus簡介node
HTTP API
在Prometheus服務器上的/api/v1
下能夠訪問當前穩定的HTTP API。 將在該端點下添加任何非中斷添加項。web
1、格式概述
API返回是JSON格式,每一個請求成功的返回值都是以2xx
開頭的編碼。若是API處理的是無效請求,返回一個JSON錯誤對象,並返回下面的錯誤碼:數據庫
400 Bad Request
。當參數錯誤或者丟失時。422 Unprocessable Entity
。當一個表達式不能被執行時。503 Service Unavailable
。當查詢超時或者中斷時。
對於在API端點以前發生的錯誤,能夠返回其餘非2xx
代碼。若是存在請求執行的錯誤,則可能會返回一系列警告,並收集全部的數據將在數據字段中返回。express
JSON響應格式以下:api
{ "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時間戳,以秒爲單位。服務器
能夠以[]
結尾的查詢參數的名稱。curl
<series_selector>
佔位符:指的是Prometheus時間序列選擇器,如http_requests_total
或http_requests_total{method =〜"(GET|POST)"}
,須要進行URL編碼。<duration>
佔位符:指的是[0-9]+[smhdwy]
形式的Prometheus持續時間字符串。 例如,5m
指的是5分鐘的持續時間。<bool>
佔位符:指的是引用布爾值(字符串true
和false
)。
2、表達式查詢
能夠在單個時刻或在一段時間內評估查詢語言表達。 如下部分描述了每種表達式查詢的API端點。post
2.1 Instant queries(即時查詢)
如下端點在單個時間點評估即時查詢:編碼
GET /api/v1/query
URL查詢參數:url
query=<string>
: Prometheus表達式查詢字符串。time=<rfc3339 | uninx_timestamp>
: 執行時間戳,可選項。timeout=<duration>
: 執行超時時間設置,可選項,默認由-query.timeout
標誌設置
若是time
缺省,則用當前服務器時間表示執行時刻。這個查詢結果的data
部分有下面格式:
{ "resultType": "matrix" | "vector" | "scalar" | "string", "result": <value> }
<value>
是一個查詢結果數據,依賴於這個resultType
格式,見表達式查詢結果格式。
下面例子執行了在時刻是2015-07-01T20:10:51.781Z
的up
表達式:
$ 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 範圍查詢
如下端點在一段時間內評估表達式查詢:
GET /api/v1/query_range
URL查詢參數
query=<string>
: Prometheus表達式查詢字符串。start=<rfc3339 | unix_timestamp>
: 開始時間戳。end=<rfc3339 | unix_timestamp>
: 結束時間戳。step=<duration>
: 以持續時間格式查詢分辨率步長或浮點秒數。timeout=<duration>
:評估超時。 可選的。 默認爲-query.timeout
標誌的值並受其限制。
查詢結果的數據部分具備如下格式:
{ "resultType": "matrix", "result": <value> }
對於<value>
佔位符的格式,詳見範圍向量結果格式。如下示例在30秒範圍內評估表達式,查詢分辨率爲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" ] ] } ] } }
3、查詢元數據
3.1 經過標籤匹配器找到度量指標列表
如下端點返回與特定標籤集匹配的時間系列列表:
GET /api/v1/series
URL查詢參數:
match[]=<series_selector>
: 選擇器是series_selector,這個參數個數必須大於等於1.start=<rfc3339 | unix_timestamp>
: 開始時間戳。end=<rfc3339 | unix_timestamp>
: 結束時間戳。
查詢結果的data
部分包含一個對象列表,這些對象包含標識每一個系列的標籤名稱/值對。
下面例子返回時間序列數據, 選擇器是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 查詢標籤值
如下端點返回標籤名稱列表:
GET /api/v1/label/<label_name>/values
JSON響應的data
部分是字符串標籤名稱的列表:
$ curl http://localhost:9090/api/v1/label/job/values { "status" : "success", "data" : [ "node", "prometheus" ] }
3.3 查詢標籤值
如下端點返回提供的標籤名稱的標籤值列表:
GET /api/v1/label/<label_name>/values
JSON響應的data
部分是字符串標籤值的列表。此示例查詢做業標籤的全部標籤值:
$ curl http://localhost:9090/api/v1/label/job/values { "status" : "success", "data" : [ "node", "prometheus" ] }
4、表達式查詢結果格式
表達式查詢可能會在data
部分的result
屬性中返回如下響應值。 <sample_value>
佔位符是數字樣本值。 JSON不支持特殊的浮點值,例如NaN
,Inf
和-Inf
,所以樣本值將做爲帶引號的JSON字符串而不是原始數字傳輸。
4.1 範圍向量
範圍向量返回的result類型是一個matrix
矩陣。下面返回的結果是result
部分的數據格式:
[ { "metric": { "<label_name>": "<label_value>", ... }, "values": [ [ <unix_time>, "<sample_value>" ], ... ] }, ... ]
4.2 瞬時向量
瞬時向量的result
類型是vector
。下面是result
部分的數據格式:
[ { "metric": { "<label_name>": "<label_value>", ... }, "value": [ <unix_time>, "<sample_value>" ] }, ... ]
4.3 Scalars標量
標量查詢返回result
類型是scalar
。下面是result
部分的數據格式:
[ <unix_time>, "<scalar_value>" ]
4.4 字符串
字符串的result
類型是string
。下面是result
部分的數據格式:
[ <unix_time>, "<string_value>" ]
5、Targets目標
如下端點返回Prometheus目標發現的當前狀態概述:
GET /api/v1/targets
活動目標和刪除目標都是響應的一部分。 labels
表示從新標記發生後的標籤集,discoveredLabels
表示在發生從新標記以前在服務發現期間檢索到的未修改標籤。
$ 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" }, } ] } }
6、Rules規則
/rules
API端點返回當前加載的警報和記錄規則列表。 此外,它還返回由每一個警報規則的Prometheus實例觸發的當前活動警報。因爲/rules
端點至關新,它沒有與整體API v1相同的穩定性保證。
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" }
7、Alerts報警
/alerts
端點返回全部活動警報的列表。因爲/alerts
端點至關新,它沒有與整體API v1相同的穩定性保證。
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" }
8、查詢目標元數據
如下端點返回有關目標正在刮取的度量標準的元數據。 這是實驗性的,未來可能會發生變化。
GET /api/v1/targets/metadata
URL查詢參數:
match_target=<label_selectors>
:經過標籤集匹配目標的標籤選擇器。 若是留空則選擇全部目標。metric=<string>
:用於檢索元數據的度量標準名稱。 若是留空,則檢索全部度量標準元數據。limit=<number>
:要匹配的最大目標數。
查詢結果的data
部分包含一個包含度量元數據和目標標籤集的對象列表。如下示例從前兩個目標返回go_goroutines
指標的全部元數據條目,標籤爲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": "" } ] }
如下示例返回標籤instance="127.0.0.1:9090"
的全部目標的全部度量標準的元數據:
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": "" }, // ... ] }
9、Altermanagers警報管理器
如下端點返回Prometheus alertmanager發現的當前狀態概述:
GET /api/v1/alertmanagers
活動和丟棄的Alertmanagers都是響應的一部分:
$ 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" } ] } }
10、Status狀態
如下狀態端點顯示當前的Prometheus配置。
10.1 Config配置
如下端點返回當前加載的配置文件:
GET /api/v1/status/config
配置做爲轉儲的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標誌
如下端點返回Prometheus配置的標誌值:
GET /api/v1/status/flags
全部值都以「字符串」的形式出現。
$ 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", ... } }
11、TSDB Admin APIs,TSDB管理API
這些是爲高級用戶公開數據庫功能的API。 除非設置了--web.enable-admin-api
,不然不會啓用這些API。咱們還公開了一個gRPC API,其定義能夠在這裏找到。 這是實驗性的,未來可能會發生變化。
11.1 快照
快照會將全部當前數據的快照建立到TSDB數據目錄下的snapshots/<datetime>-<rand>
中,並將該目錄做爲響應返回。 它能夠選擇跳過僅存在於頭塊中但還沒有壓縮到磁盤的快照數據。
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
11.2 刪除序列
DeleteSeries刪除時間範圍內所選系列的數據。 實際數據仍然存在於磁盤上,並在未來的壓縮中清除,或者能夠經過點擊Clean Tombstones端點來明確清理。
若是成功,則返回204
。
POST /api/v1/admin/tsdb/delete_series
URL查詢參數:
match[]=<series_selector>
:選擇要刪除的系列的重複標籤匹配器參數。 必須至少提供一個match[]
參數。start= <rfc3339 | unix_timestamp>
:開始時間戳。 可選,默認爲最短可能時間。end= <rfc3339 | unix_timestamp>
:結束時間戳。 可選,默認爲最長可能時間。
不說起開始和結束時間將清除數據庫中匹配系列的全部數據。例:
$ curl -X POST \ -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=up&match[]=process_start_time_seconds{job="prometheus"}'
11.3 CleanTombstones
CleanTombstones從磁盤中刪除已刪除的數據並清理現有的邏輯刪除。 這能夠在刪除系列後使用以釋放空間。若是成功,則返回204
。
POST /api/v1/admin/tsdb/clean_tombstones
這不須要參數或正文:
$ curl -XPOST http://localhost:9090/api/v1/admin/tsdb/clean_tombstones