[TOC]
protometheus
Prometheus 是由 SoundCloud 開源監控告警解決方案。
prometheus
prometheus存儲的是時序數據
,即按相同時序(相同名稱和標簽),以時間維度存儲連續的數據的集合。
時序(time series
)是由名字(Metric
)以及一組key/value標簽定義的,具有相同的名字以及標簽屬于相同時序。
metric名字:表示metric的功能,如
http_request_total
。時序的名字由 ASCII 字符,數字,下劃線,以及冒號組成,它必須滿足正則表達式 [a-zA-Z_:][a-zA-Z0-9_:]*, 其名字應該具有語義化,一般表示一個可以度量的指標,例如 http_requests_total, 可以表示 http 請求的總數。標簽:
-
樣本:按照某個時序以時間維度采集的數據,稱之為樣本。實際的時間序列,每個序列包括一個float64的值和一個毫秒級的時間戳
一個 float64 值
一個毫秒級的 unix 時間戳
格式:Prometheus時序格式與
OpenTSDB
相似:
<metric name>{<label name>=<label value>, ...}
Metric類型:
Counter
: 一種累加的metric
,如請求的個數,結束的任務數,出現的錯誤數等Gauge
: 常規的metric,如溫度,可任意加減。其為瞬時的,與時間沒有關系的,可以任意變化的數據。Histogram
: 柱狀圖,用于觀察結果采樣,分組及統計,如:請求持續時間,響應大小。其主要用于表示一段時間內對數據的采樣,并能夠對其指定區間及總數進行統計。根據統計區間計算Summary
: 類似Histogram
,用于表示一段時間內數據采樣結果,其直接存儲quantile數據,而不是根據統計區間計算出來的。不需要計算,直接存儲結果
PromQL
PromQL (Prometheus Query Language) 是 Prometheus 自己開發的數據查詢 DSL 語言。
查詢結果類型:
瞬時數據 (Instant vector): 包含一組時序,每個時序只有一個點,例如:
http_requests_total
區間數據 (Range vector): 包含一組時序,每個時序有多個點,例如:
http_requests_total[5m]
純量數據 (Scalar): 純量只有一個數字,沒有時序,例如:
count(http_requests_total)
查詢條件:通過名稱及標簽進行查詢,如http_requests_total
等價于{name="http_requests_total"}
查詢level="info"
的event: logback_events_total{level="info"}
查詢條件支持正則匹配:
http_requests_total{code!="200"} // 表示查詢 code 不為 "200" 的數據
http_requests_total{code=~"2.."} // 表示查詢 code 為 "2xx" 的數據
http_requests_total{code!~"2.."} // 表示查詢 code 不為 "2xx" 的數據
內置函數:
- 如將浮點數轉換為整數:
floor(avg(http_requests_total{code="200"}))
ceil(avg(http_requests_total{code="200"}))
- 查看每秒數據 :
rate(http_requests_total[5m])
基本查詢:
1.查詢當前所有數據
logback_events_total
2.模糊查詢: level="inxx"
logback_events_total{level=~"in.."}
logback_events_total{level=~"in.*"}
3.比較查詢: value>0
logback_events_total > 0
4.范圍查詢: 過去5分鐘數據
logback_events_total[5m]
時間范圍單位有以下:
s
: 秒m
: 分鐘h
: 小時d
: 天w
: 周y
: 年
在瞬時向量表達式或者區間向量表達式中,都是以當前時間為基準。
如果想查詢5分釧前的瞬時樣本數據,則需要使用位移操作,關鍵字:offset
, 其要緊跟在選擇器{}
后面。如:
sum(http_requests_total{method="GET"} offset 5m)
rate(http_requests_total[5m] offset 1w)
聚合、統計高級查詢:
1. count
查詢: count(logback_events_total)
2. sum
查詢: sum(logback_events_total)
3. svg
查詢:
4. topk
: 如查詢2的值:topk(2, logback_events_total)
5. irate
: 如查詢過去5分鐘的平均值: irate( logback_events_total[5m])
配置
啟動時,可以加載運行參數-config.file
指定配置文件, 默認為prometheus.yml
:
在該配置文件中可以指定各種屬性,其結構體定義如下:
type Config struct {
GlobalConfig GlobalConfig `yaml:"global"`
AlertingConfig AlertingConfig `yaml:"alerting,omitempty"`
RuleFiles []string `yaml:"rule_files,omitempty"`
ScrapeConfigs []*ScrapeConfig `yaml:"scrape_configs,omitempty"`
RemoteWriteConfigs []*RemoteWriteConfig `yaml:"remote_write,omitempty"`
RemoteReadConfigs []*RemoteReadConfig `yaml:"remote_read,omitempty"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
// original is the input from which the config was parsed.
original string
}
全局配置
global
: 主要有四個屬性
scrape_interval
: 拉取 targets 的默認時間間隔。scrape_timeout
: 拉取一個 target 的超時時間。evaluation_interval
: 執行 rules 的時間間隔。external_labels
: 額外的屬性,會添加到拉取的數據并存到數據庫中。
Exporter
負責數據匯報的程序統一叫Exporter
,不同的Exporter
負責不同的業務。其統一命名格式:xx_exporter
clientlib
pull模式
prometheus.yml
內容如下:
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
# - "first.rules"
# - "second.rules"
scrape_configs:
- job_name: 'spring'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['自己本機ip:8080']
啟動prometheus docker:
docker run --name prom --hostname prom -p 9090:9090 -v /Users/liukun/config/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
啟動以后,我們運行http://localhost:9090
可以訪問Prometheus。
Pushgateway
使用Pushgateway原因:
Prometheus采用pull模式,可能由于不在一個子網或防火墻導致無法直接拉取各target數據
需要將不同數據匯總后,再由Prometheus統一收集
其缺點:
pushgateway宕機影響范圍會更大。
prometheus拉取狀態up只針對pushgateway,無法做到對每個節點有效。
pushgateway可以持久化推送給它的所有監控數據
docker run -d \
--name=pg \
-p 9091:9091 \
prom/pushgateway
在其啟動后,通過訪問:http://localhost:9091
就可以查看到其界面
pushgateway默認是不持久化數據的,如果需要,則可以通過啟動時加入參數 :
docker run -d -p9091:9091 prom/pushgateway "-persistence.file=push_file"
向pushgateway推送數據:
1. 使用Client SDK
2. 直接使用API
使用API向Pushgateway推數據
如下為直接使用API進行數據推送:
echo "some_metric 3.14" | curl --data-binary @- http://localhost:9091/metrics/job/some_job
發送更復雜的數據,可以還上instance,表示來源位置:
cat <<EOF | curl --data-binary @- http://localhost:9091/metrics/job/some_job/instance/some_instance
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
刪除數據:如果某個監控數據不再需要,則只有手動刪除才生效,否則仍然采集的為舊值
// 刪除某個組下某個實例的所有數據
curl -X DELETE http://localhost:9091/metrics/job/some_job/instance/some_instance
// 刪除某個組下的所有數據
curl -X DELETE http://localhost:9091/metrics/job/some_job
使用Client SDK向Pushgateway推數據
通過Client SDK推送metric信息到Pushgateway:
1.添加pom依賴:
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>0.6.0</version>
</dependency>
2.添加配置:在Prometheus的配置文件中配置,讓其從Pushgateway上進行數據采集,這里0.51.14.23:9091
為我Pushgateway的地址端口。配置完后需要重啟使其配置生效
scrape_configs:
- job_name: 'pushgateway'
static_configs:
- targets: ['10.51.14.23:9091']
labels:
instance: "pushgateway"
3.代碼:
@Test
public void pushToGateWay() throws Exception {
CollectorRegistry registry = new CollectorRegistry();
Gauge duration = Gauge.build().name("my_batch_job_duration_seconds")
.help("Duration of my batch job in second").register(registry);
Gauge.Timer durationTimer = duration.startTimer();
try {
Gauge lastSuccess = Gauge.build().name("my_batch_job_last_success")
.help("Last time my batch job successed, in unixtime")
.register(registry);
lastSuccess.setToCurrentTime();
} finally {
durationTimer.setDuration();
PushGateway pg = new PushGateway("localhost:9091");
pg.pushAdd(registry, "my_batch_job");
}
}
instance
: 收集數據的目標端點,一般對應一個進程,即指定其來源,如某個機器
job
: 實現同一功能或目標的一組instance。 如一組機器的集合。