Prometheus Querying
查詢
prometheus提供了功能性表達式語言,可讓用戶對于時間序列的數(shù)據(jù)進行選擇和聚合。通過表達式查詢的結果可以繪制為曲線圖,也可以在prometheus提供的表達式瀏覽器中顯示為表格,也可以通過外部系統(tǒng)以HTTP API來調用使用。
expression language data types
prometheus 表達式語言中,有四種類型:
- 即時向量(instant vector) 包含每個時間序列的單個樣本的一組時間序列,共享相同的時間戳。
- 范圍向量(Range vector) 包含每個時間序列隨時間變化的數(shù)據(jù)點的一組時間序列。
- 標量(Scalar) 一個簡單的數(shù)字浮點值
- 字符串(String) 一個簡單的字符串值(目前未被使用)
根據(jù)使用情況(例如繪圖或者顯示表達式的輸出),這些類型中只有一些是由用戶指定的表達式產(chǎn)生的結果而有效的,例如,即時向量表達式是可以繪圖的唯一類型。
時間序列選擇器
-
即時向量選擇器
即時向量選擇器允許選擇一組時間序列,或者某個給定的時間戳的樣本數(shù)據(jù)。下面這個例子選擇了具有http_requests_total的時間序列:
http_requests_total你可以通過附加一組標簽,并用{}括起來,來進一步篩選這些時間序列。下面這個例子只選擇有http_requests_total名稱的、有prometheus工作標簽的、有canary組標簽的時間序列:
http_requests_total{job="prometheus",group="canary"}另外,也可以也可以將標簽值反向匹配,或者對正則表達式匹配標簽值。下面列舉匹配操作符:
=:選擇正好相等的字符串標簽 !=:選擇不相等的字符串標簽 =~:選擇匹配正則表達式的標簽(或子標簽) !=:選擇不匹配正則表達式的標簽(或子標簽)例如,選擇staging、testing、development環(huán)境下的,GET之外的HTTP方法的http_requests_total的時間序列:
http_requests_total{environment=~"staging|testing|development",method!="GET"} -
范圍向量選擇器
范圍向量表達式正如即時向量表達式一樣運行,前者返回從當前時刻的時間序列回來。語法是,在一個向量表達式之后添加[]來表示時間范圍,持續(xù)時間用數(shù)字表示,后接下面單元之一:
- s:seconds
- m:minutes
- h:hours
- d:days
- w:weeks
- y:years
在下面這個例子中,我們選擇此刻開始5分鐘內的所有記錄,metric名稱為http_requests_total、作業(yè)標簽為prometheus的時間序列的所有值:
http_requests_total{job="prometheus"}[5m] -
偏移修飾符(offset modifier)
偏移修飾符允許更改查詢中單個即時向量和范圍向量的時間偏移量,例如,以下表達式返回相對于當前查詢時間5分鐘前的http_requests_total值:http_requests_total offset 5mNote:請注意,偏移量修飾符始終需要跟隨選擇器,即以下是正確的:
sum(http_requests_total{method="GET"} offset 5m) // GOOD.下面是錯誤的:
sum(http_requests_total{method="GET"}) offset 5m // INVALID.如下是范圍向量的相同樣本。這返回http_requests_total在一周前5分鐘內的速率:
rate(http_requests_total[5m] offset 1w) -
操作符
Prometheus支持多種二元和聚合的操作符請查看這里
函數(shù)
Prometheus支持多種函數(shù),來對數(shù)據(jù)進行操作請查看這里
怎么使用prometheus監(jiān)控容器?
prometheus監(jiān)控不同的目標服務需要實現(xiàn)不同的exporter插件,早期的時候,官方出了container-exporter項目,但是現(xiàn)在項目已經(jīng)停止。推薦使用谷歌的cAdvisor項目作為prometheus的exporter。cAdvisor作為一個監(jiān)控單機容器的項目,數(shù)據(jù)較為全面,但是也有很大的問題,例如io等數(shù)據(jù)沒有等等。結合prometheus后就能在整個集群監(jiān)控查詢容器。舉個例子,你有一個項目有3個容器分布在三臺機器,你怎么監(jiān)控整個項目的流量,內存量,負載量的實時數(shù)據(jù)。這就是prometheus的多維度查詢解決的問題,數(shù)據(jù)從3臺機器的cadvisor得到每個容器的數(shù)據(jù),它的多維度查詢語法就能讓你得到你想要的數(shù)據(jù)。
這里假設你有10臺機器部署了容器需要監(jiān)控,你在10臺機器上分別部署cAdvisor容器
sudo docker run \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:rw \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--publish=8080:8080 \
--detach=true \
--name=cadvisor \
google/cadvisor:latest
找一臺機器部署prometheus服務,這里依然使用容器部署:
docker run \
-p 9090:9090 \
--log-driver none \
-v /hdd1/prometheus/etc/:/etc/prometheus/ \
-v /hdd1/prometheus/data/:/prometheus/ \
-v /etc/localtime:/etc/localtime \
--name prometheus \
prom/prometheus
創(chuàng)建/hdd1/prometheus/etc/prometheus.yml配置文件
my global config
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # By default, scrape targets every 15 seconds.
# scrape_timeout is set to the global default (10s).
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'container-monitor'
# Load and evaluate rules in this file every 'evaluation_interval' seconds.
rule_files:
- "/etc/prometheus/rules/common.rules"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'container'
static_configs:
- targets: ['10.12.1.129:8080','10.12.1.130:8080','10.50.1.92:8080','10.50.1.93:8080','10.50.1.119:8080']
配置文件中 -targets中的端點填寫你的實際cadvisor所在的ip和暴露的端口.正確啟動后訪問ip:9090就能查詢數(shù)據(jù)了哦。