Prometheus2.0 從入門(mén)到放棄

Prometheus 是什么?

Prometheus是一套開(kāi)源的監(jiān)控&報(bào)警&時(shí)間序列數(shù)據(jù)庫(kù)的組合,起始是由SoundCloud公司開(kāi)發(fā)的。隨著發(fā)展,越來(lái)越多公司和組織接受采用Prometheus,社區(qū)也十分活躍,他們便將它獨(dú)立成開(kāi)源項(xiàng)目,并且有公司來(lái)運(yùn)作。google SRE的書(shū)內(nèi)也曾提到跟他們BorgMon監(jiān)控系統(tǒng)相似的實(shí)現(xiàn)是Prometheus。現(xiàn)在最常見(jiàn)的Kubernetes容器管理系統(tǒng)中,通常會(huì)搭配Prometheus進(jìn)行監(jiān)控。

Prometheus 的優(yōu)點(diǎn)

  • 非常少的外部依賴(lài),安裝使用超簡(jiǎn)單
  • 已經(jīng)有非常多的系統(tǒng)集成 例如:docker HAProxy Nginx JMX等等
  • 服務(wù)自動(dòng)化發(fā)現(xiàn)
  • 直接集成到代碼
  • 設(shè)計(jì)思想是按照分布式、微服務(wù)架構(gòu)來(lái)實(shí)現(xiàn)的

Prometheus 的特性

  • 自定義多維度的數(shù)據(jù)模型
  • 非常高效的存儲(chǔ) 平均一個(gè)采樣數(shù)據(jù)占 ~3.5 bytes左右,320萬(wàn)的時(shí)間序列,每30秒采樣,保持60天,消耗磁盤(pán)大概228G。
  • 強(qiáng)大的查詢(xún)語(yǔ)句
  • 輕松實(shí)現(xiàn)數(shù)據(jù)可視化

架構(gòu)圖

prometheus 架構(gòu)圖

組件介紹

Prometheus生態(tài)系統(tǒng)由多個(gè)組件組成。其中許多組件都是可選的

Promethus server
  • 必須安裝,
  • 本質(zhì)是一個(gè)時(shí)序數(shù)據(jù)庫(kù)
  • 主要負(fù)責(zé)數(shù)據(jù)pull、存儲(chǔ)、分析
Push Gateway
  • 非必選項(xiàng)
  • 支持臨時(shí)性Job主動(dòng)推送指標(biāo)的中間網(wǎng)關(guān)
exporters
  • 部署在客戶(hù)端的agent,如 node_exporte, mysql_exporter等
alertmanager
  • 用來(lái)進(jìn)行報(bào)警,Promethus server 經(jīng)過(guò)分析, 把出發(fā)的警報(bào)發(fā)送給 alertmanager 組件,alertmanager 組件通過(guò)自身的規(guī)則,來(lái)發(fā)送通知,(郵件,或者webhook)

接下來(lái)就是實(shí)戰(zhàn)啦

本章內(nèi)容
mkdir /opt/monitor/ -p
cd /opt/monitor/
wget https://github.com/x82423990/prometheus/archive/v2.0.0.tar.gz
tar xf v2.0.0.tar.gz
cd prometheus-2.0.linux-amd64
# 運(yùn)行
# ./prometheus &

通過(guò)啟動(dòng)日志,可以看到 Prometheus Server 默認(rèn)端口是 9090。

當(dāng) Prometheus 啟動(dòng)后,你可以通過(guò)瀏覽器來(lái)訪問(wèn)

http://IP:9090,將看到如下頁(yè)面

在默認(rèn)配置中,我們已經(jīng)添加了 Prometheus Server 的監(jiān)控,所以我們現(xiàn)在可以使用PromQL(Prometheus Query Language)來(lái)查看,比如

Prometheus Query Language

so , 既然他是一個(gè)數(shù)據(jù)庫(kù), 我們來(lái)簡(jiǎn)單的了解他的數(shù)據(jù)結(jié)構(gòu)及他的數(shù)據(jù)模型

Prometheus 存儲(chǔ)的是時(shí)序數(shù)據(jù), 即按照相同時(shí)序(相同的名字和標(biāo)簽),以時(shí)間維度存儲(chǔ)連續(xù)的數(shù)據(jù)的集合。

時(shí)序索引

時(shí)序(time series) 是由名字(Metric),以及一組 key/value 標(biāo)簽定義的,具有相同的名字以及標(biāo)簽屬于相同時(shí)序。

時(shí)序的名字由 ASCII 字符,數(shù)字,下劃線,以及冒號(hào)組成,它必須滿足正則表達(dá)式 [a-zA-Z_:][a-zA-Z0-9_:]*, 其名字應(yīng)該具有語(yǔ)義化,一般表示一個(gè)可以度量的指標(biāo),例如 http_requests_total, 可以表示 http 請(qǐng)求的總數(shù)。

時(shí)序的標(biāo)簽可以使 Prometheus 的數(shù)據(jù)更加豐富,能夠區(qū)分具體不同的實(shí)例,例如 http_requests_total{method="POST"} 可以表示所有 http 中的 POST 請(qǐng)求。

標(biāo)簽名稱(chēng)由 ASCII 字符,數(shù)字,以及下劃線組成, 其中 __ 開(kāi)頭屬于 Prometheus 保留,標(biāo)簽的值可以是任何 Unicode 字符,支持中文。

時(shí)序樣本

按照某個(gè)時(shí)序以時(shí)間維度采集的數(shù)據(jù),稱(chēng)之為樣本,其值包含:

  • 一個(gè) float64 值
  • 一個(gè)毫秒級(jí)的 unix 時(shí)間戳

格式

Prometheus 時(shí)序格式與 OpenTSDB 相似:

<metric name>{<label name>=<label value>, ...}

其中包含時(shí)序名字以及時(shí)序的標(biāo)簽。

作業(yè)和實(shí)例

prometheus 中,將任意一個(gè)獨(dú)立的數(shù)據(jù)源(target)稱(chēng)之為實(shí)例(instance)。包含相同類(lèi)型的實(shí)例的集合稱(chēng)之為作業(yè)(job)。
如下是一個(gè)含有四個(gè)重復(fù)實(shí)例的作業(yè):

- job: api-server
    - instance 1: 1.2.3.4:5670
    - instance 2: 1.2.3.4:5671
    - instance 3: 5.6.7.8:5670
    - instance 4: 5.6.7.8:5671

自生成標(biāo)簽和時(shí)序

prometheus 在采集數(shù)據(jù)的同時(shí),會(huì)自動(dòng)在時(shí)序的基礎(chǔ)上添加標(biāo)簽,作為數(shù)據(jù)源(target)的標(biāo)識(shí),以便區(qū)分:

job: The configured job name that the target belongs to.
instance: The <host>:<port> part of the target's URL that was scraped.

如果其中任一標(biāo)簽已經(jīng)在此前采集的數(shù)據(jù)中存在,那么將會(huì)根據(jù) honor_labels 設(shè)置選項(xiàng)來(lái)決定新標(biāo)簽。詳見(jiàn)官網(wǎng)解釋?zhuān)?scrape configuration documentation

對(duì)每一個(gè)實(shí)例而言,prometheus 按照以下時(shí)序來(lái)存儲(chǔ)所采集的數(shù)據(jù)樣本:

up{job="<job-name>", instance="<instance-id>"}: 1 表示該實(shí)例正常工作
up{job="<job-name>", instance="<instance-id>"}: 0 表示該實(shí)例故障

scrape_duration_seconds{job="<job-name>", instance="<instance-id>"} 表示拉取數(shù)據(jù)的時(shí)間間隔

scrape_samples_post_metric_relabeling{job="<job-name>", instance="<instance-id>"} 表示采用重定義標(biāo)簽(relabeling)操作后仍然剩余的樣本數(shù)

scrape_samples_scraped{job="<job-name>", instance="<instance-id>"}  表示從該數(shù)據(jù)源獲取的樣本數(shù)

其中 up 時(shí)序可以有效應(yīng)用于監(jiān)控該實(shí)例是否正常工作。


時(shí)序 4 種類(lèi)型

Prometheus 時(shí)序數(shù)據(jù)分為 Counter, Gauge, Histogram, Summary 四種類(lèi)型。

Counter

Counter 表示收集的數(shù)據(jù)是按照某個(gè)趨勢(shì)(增加/減少)一直變化的,我們往往用它記錄服務(wù)請(qǐng)求總量,錯(cuò)誤總數(shù)等。

例如 Prometheus server 中 http_requests_total, 表示 Prometheus 處理的 http 請(qǐng)求總數(shù),可以使用 delta, 很容易得到任意區(qū)間數(shù)據(jù)的增量。

Gauge

Gauge 表示搜集的數(shù)據(jù)是一個(gè)瞬時(shí)的,與時(shí)間沒(méi)有關(guān)系,可以任意變高變低,往往可以用來(lái)記錄內(nèi)存使用率、磁盤(pán)使用率等。

例如 Prometheus server 中 go_goroutines, 表示 Prometheus 當(dāng)前 goroutines 的數(shù)量。

Histogram

Histogram 由 <basename>_bucket{le="<upper inclusive bound>"}<basename>_bucket{le="+Inf"}, <basename>_sum<basename>_count 組成,主要用于表示一段時(shí)間范圍內(nèi)對(duì)數(shù)據(jù)進(jìn)行采樣,(通常是請(qǐng)求持續(xù)時(shí)間或響應(yīng)大?。?,并能夠?qū)ζ渲付▍^(qū)間以及總數(shù)進(jìn)行統(tǒng)計(jì),通常我們用它計(jì)算分位數(shù)的直方圖。

例如 Prometheus server 中 prometheus_local_storage_series_chunks_persisted, 表示 Prometheus 中每個(gè)時(shí)序需要存儲(chǔ)的 chunks 數(shù)量,我們可以用它計(jì)算待持久化的數(shù)據(jù)的分位數(shù)。

Summary

Summary 和 Histogram 類(lèi)似,由 <basename>{quantile="<φ>"},<basename>_sum,<basename>_count 組成,主要用于表示一段時(shí)間內(nèi)數(shù)據(jù)采樣結(jié)果,(通常是請(qǐng)求持續(xù)時(shí)間或響應(yīng)大?。?,它直接存儲(chǔ)了 quantile 數(shù)據(jù),而不是根據(jù)統(tǒng)計(jì)區(qū)間計(jì)算出來(lái)的。

例如 Prometheus server 中 prometheus_target_interval_length_seconds

Histogram vs Summary

  • 都包含 <basename>_sum,<basename>_count
  • Histogram 需要通過(guò) <basename>_bucket 計(jì)算 quantile, 而 Summary 直接存儲(chǔ)了 quantile 的值。

PromQL 基本使用

PromQL (Prometheus Query Language) 是 Prometheus 自己開(kāi)發(fā)的數(shù)據(jù)查詢(xún) DSL 語(yǔ)言,語(yǔ)言表現(xiàn)力非常豐富,內(nèi)置函數(shù)很多,在日常數(shù)據(jù)可視化,rule 告警中都會(huì)使用到它。

我們可以在頁(yè)面 http://localhost:9090/graph 中,輸入下面的查詢(xún)語(yǔ)句,查看結(jié)果,例如:

http_requests_total{code="200"}

字符串和數(shù)字

字符串: 在查詢(xún)語(yǔ)句中,字符串往往作為查詢(xún)條件 labels 的值,和 Golang 字符串語(yǔ)法一致,可以使用 "", '', 或者 ``, 格式如:

"this is a string"
'these are unescaped: \n \\ \t'
`these are not unescaped: \n ' " \t`

正數(shù),浮點(diǎn)數(shù): 表達(dá)式中可以使用正數(shù)或浮點(diǎn)數(shù),例如:

3
-2.4

查詢(xún)結(jié)果類(lèi)型

PromQL 查詢(xún)結(jié)果主要有 3 種類(lèi)型:

  • 瞬時(shí)數(shù)據(jù) (Instant vector): 包含一組時(shí)序,每個(gè)時(shí)序只有一個(gè)點(diǎn),例如:http_requests_total
  • 區(qū)間數(shù)據(jù) (Range vector): 包含一組時(shí)序,每個(gè)時(shí)序有多個(gè)點(diǎn),例如:http_requests_total[5m]
  • 純量數(shù)據(jù) (Scalar): 純量只有一個(gè)數(shù)字,沒(méi)有時(shí)序,例如:count(http_requests_total)

查詢(xún)條件

Prometheus 存儲(chǔ)的是時(shí)序數(shù)據(jù),而它的時(shí)序是由名字和一組標(biāo)簽構(gòu)成的,其實(shí)名字也可以寫(xiě)出標(biāo)簽的形式,例如 http_requests_total 等價(jià)于 {name="http_requests_total"}。

一個(gè)簡(jiǎn)單的查詢(xún)相當(dāng)于是對(duì)各種標(biāo)簽的篩選,例如:

http_requests_total{code="200"} // 表示查詢(xún)名字為 http_requests_total,code 為 "200" 的數(shù)據(jù)

查詢(xún)條件支持正則匹配,例如:

http_requests_total{code!="200"}  // 表示查詢(xún) code 不為 "200" 的數(shù)據(jù)
http_requests_total{code=~"2.."} // 表示查詢(xún) code 為 "2xx" 的數(shù)據(jù)
http_requests_total{code!~"2.."} // 表示查詢(xún) code 不為 "2xx" 的數(shù)據(jù)

操作符

Prometheus 查詢(xún)語(yǔ)句中,支持常見(jiàn)的各種表達(dá)式操作符,例如

算術(shù)運(yùn)算符:

支持的算術(shù)運(yùn)算符有 +,-,*,/,%,^, 例如 http_requests_total * 2 表示將 http_requests_total 所有數(shù)據(jù) double 一倍。

比較運(yùn)算符:

支持的比較運(yùn)算符有 ==,!=,>,<,>=,<=, 例如 http_requests_total > 100 表示 http_requests_total 結(jié)果中大于 100 的數(shù)據(jù)。

邏輯運(yùn)算符:

支持的邏輯運(yùn)算符有 and,or,unless, 例如 http_requests_total == 5 or http_requests_total == 2 表示 http_requests_total 結(jié)果中等于 5 或者 2 的數(shù)據(jù)。

聚合運(yùn)算符:

支持的聚合運(yùn)算符有 sum,min,max,avg,stddev,stdvar,count,count_values,bottomk,topk,quantile,, 例如 max(http_requests_total) 表示 http_requests_total 結(jié)果中最大的數(shù)據(jù)。

注意,和四則運(yùn)算類(lèi)型,Prometheus 的運(yùn)算符也有優(yōu)先級(jí),它們遵從(^)> (*, /, %) > (+, -) > (==, !=, <=, <, >=, >) > (and, unless) > (or) 的原則。

內(nèi)置函數(shù)

Prometheus 內(nèi)置不少函數(shù),方便查詢(xún)以及數(shù)據(jù)格式化,例如將結(jié)果由浮點(diǎn)數(shù)轉(zhuǎn)為整數(shù)的 floor 和 ceil,

floor(avg(http_requests_total{code="200"}))
ceil(avg(http_requests_total{code="200"}))

查看 http_requests_total 5分鐘內(nèi),平均每秒數(shù)據(jù)

rate(http_requests_total[5m])

更多請(qǐng)參見(jiàn)詳情。


與 SQL 對(duì)比

下面我將以 Prometheus server 收集的 http_requests_total 時(shí)序數(shù)據(jù)為例子展開(kāi)對(duì)比。

MySQL 數(shù)據(jù)準(zhǔn)備

mysql>
# 創(chuàng)建數(shù)據(jù)庫(kù)
create database prometheus_practice;
use prometheus_practice;

# 創(chuàng)建 http_requests_total 表
CREATE TABLE http_requests_total (
    code VARCHAR(256),
    handler VARCHAR(256),
    instance VARCHAR(256),
    job VARCHAR(256),
    method VARCHAR(256),
    created_at DOUBLE NOT NULL,
    value DOUBLE NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE http_requests_total ADD INDEX created_at_index (created_at);

# 初始化數(shù)據(jù)
# time at 2017/5/22 14:45:27
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "query_range", "localhost:9090", "prometheus", "get", 1495435527, 3);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("400", "query_range", "localhost:9090", "prometheus", "get", 1495435527, 5);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "prometheus", "localhost:9090", "prometheus", "get", 1495435527, 6418);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "static", "localhost:9090", "prometheus", "get", 1495435527, 9);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("304", "static", "localhost:9090", "prometheus", "get", 1495435527, 19);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "query", "localhost:9090", "prometheus", "get", 1495435527, 87);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("400", "query", "localhost:9090", "prometheus", "get", 1495435527, 26);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "graph", "localhost:9090", "prometheus", "get", 1495435527, 7);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "label_values", "localhost:9090", "prometheus", "get", 1495435527, 7);

# time at 2017/5/22 14:48:27
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "query_range", "localhost:9090", "prometheus", "get", 1495435707, 3);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("400", "query_range", "localhost:9090", "prometheus", "get", 1495435707, 5);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "prometheus", "localhost:9090", "prometheus", "get", 1495435707, 6418);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "static", "localhost:9090", "prometheus", "get", 1495435707, 9);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("304", "static", "localhost:9090", "prometheus", "get", 1495435707, 19);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "query", "localhost:9090", "prometheus", "get", 1495435707, 87);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("400", "query", "localhost:9090", "prometheus", "get", 1495435707, 26);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "graph", "localhost:9090", "prometheus", "get", 1495435707, 7);
INSERT INTO http_requests_total (code, handler, instance, job, method, created_at, value) values ("200", "label_values", "localhost:9090", "prometheus", "get", 1495435707, 7);

數(shù)據(jù)初始完成后,通過(guò)查詢(xún)可以看到如下數(shù)據(jù):

mysql>
mysql> select * from http_requests_total;
+------+--------------+----------------+------------+--------+------------+-------+
| code | handler      | instance       | job        | method | created_at | value |
+------+--------------+----------------+------------+--------+------------+-------+
| 200  | query_range  | localhost:9090 | prometheus | get    | 1495435527 |     3 |
| 400  | query_range  | localhost:9090 | prometheus | get    | 1495435527 |     5 |
| 200  | prometheus   | localhost:9090 | prometheus | get    | 1495435527 |  6418 |
| 200  | static       | localhost:9090 | prometheus | get    | 1495435527 |     9 |
| 304  | static       | localhost:9090 | prometheus | get    | 1495435527 |    19 |
| 200  | query        | localhost:9090 | prometheus | get    | 1495435527 |    87 |
| 400  | query        | localhost:9090 | prometheus | get    | 1495435527 |    26 |
| 200  | graph        | localhost:9090 | prometheus | get    | 1495435527 |     7 |
| 200  | label_values | localhost:9090 | prometheus | get    | 1495435527 |     7 |
| 200  | query_range  | localhost:9090 | prometheus | get    | 1495435707 |     3 |
| 400  | query_range  | localhost:9090 | prometheus | get    | 1495435707 |     5 |
| 200  | prometheus   | localhost:9090 | prometheus | get    | 1495435707 |  6418 |
| 200  | static       | localhost:9090 | prometheus | get    | 1495435707 |     9 |
| 304  | static       | localhost:9090 | prometheus | get    | 1495435707 |    19 |
| 200  | query        | localhost:9090 | prometheus | get    | 1495435707 |    87 |
| 400  | query        | localhost:9090 | prometheus | get    | 1495435707 |    26 |
| 200  | graph        | localhost:9090 | prometheus | get    | 1495435707 |     7 |
| 200  | label_values | localhost:9090 | prometheus | get    | 1495435707 |     7 |
+------+--------------+----------------+------------+--------+------------+-------+
18 rows in set (0.00 sec)

基本查詢(xún)對(duì)比

假設(shè)當(dāng)前時(shí)間為 2017/5/22 14:48:30

  • 查詢(xún)當(dāng)前所有數(shù)據(jù)
// PromQL
http_requests_total

// MySQL
SELECT * from http_requests_total WHERE created_at BETWEEN 1495435700 AND 1495435710;

我們查詢(xún) MySQL 數(shù)據(jù)的時(shí)候,需要將當(dāng)前時(shí)間向前推一定間隔,比如這里的 10s (Prometheus 數(shù)據(jù)抓取間隔),這樣才能確保查詢(xún)到數(shù)據(jù),而 PromQL 自動(dòng)幫我們實(shí)現(xiàn)了這個(gè)邏輯。

  • 條件查詢(xún)
// PromQL
http_requests_total{code="200", handler="query"}

// MySQL
SELECT * from http_requests_total WHERE code="200" AND handler="query" AND created_at BETWEEN 1495435700 AND 1495435710;
  • 模糊查詢(xún): code 為 2xx 的數(shù)據(jù)
// PromQL
http_requests_total{code~="2xx"}

// MySQL
SELECT * from http_requests_total WHERE code LIKE "%2%" AND created_at BETWEEN 1495435700 AND 1495435710;
  • 比較查詢(xún): value 大于 100 的數(shù)據(jù)
// PromQL
http_requests_total > 100

// MySQL
SELECT * from http_requests_total WHERE value > 100 AND created_at BETWEEN 1495435700 AND 1495435710;
  • 范圍區(qū)間查詢(xún): 過(guò)去 5 分鐘數(shù)據(jù)
// PromQL
http_requests_total[5m]

// MySQL
SELECT * from http_requests_total WHERE created_at BETWEEN 1495435410 AND 1495435710;

聚合, 統(tǒng)計(jì)高級(jí)查詢(xún)

  • count 查詢(xún): 統(tǒng)計(jì)當(dāng)前記錄總數(shù)
// PromQL
count(http_requests_total)

// MySQL
SELECT COUNT(*) from http_requests_total WHERE created_at BETWEEN 1495435700 AND 1495435710;
  • sum 查詢(xún): 統(tǒng)計(jì)當(dāng)前數(shù)據(jù)總值
// PromQL
sum(http_requests_total)

// MySQL
SELECT SUM(value) from http_requests_total WHERE created_at BETWEEN 1495435700 AND 1495435710;
  • avg 查詢(xún): 統(tǒng)計(jì)當(dāng)前數(shù)據(jù)平均值
// PromQL
avg(http_requests_total)

// MySQL
SELECT AVG(value) from http_requests_total WHERE created_at BETWEEN 1495435700 AND 1495435710;
  • top 查詢(xún): 查詢(xún)最靠前的 3 個(gè)值
// PromQL
topk(3, http_requests_total)

// MySQL
SELECT * from http_requests_total WHERE created_at BETWEEN 1495435700 AND 1495435710 ORDER BY value DESC LIMIT 3;
  • irate 查詢(xún),過(guò)去 5 分鐘平均每秒數(shù)值
// PromQL
irate(http_requests_total[5m])

// MySQL
SELECT code, handler, instance, job, method, SUM(value)/300 AS value from http_requests_total WHERE created_at BETWEEN 1495435700 AND 1495435710  GROUP BY code, handler, instance, job, method;

總結(jié)

通過(guò)以上一些示例可以看出,在常用查詢(xún)和統(tǒng)計(jì)方面,PromQL 比 MySQL 簡(jiǎn)單和豐富很多,而且查詢(xún)性能也高不少。


Prometheus Web

Prometheus 自帶了 Web Console, 安裝成功后可以訪問(wèn) http://localhost:9090/graph 頁(yè)面,用它可以進(jìn)行任何 PromQL 查詢(xún)和調(diào)試工作,非常方便,例如:

prometheus web console
prometheus web graph

通過(guò)上圖你不難發(fā)現(xiàn),Prometheus 自帶的 Web 界面比較簡(jiǎn)單,因?yàn)樗哪康氖菫榱思皶r(shí)查詢(xún)數(shù)據(jù),方便 PromeQL 調(diào)試。

它并不是像常見(jiàn)的 Admin Dashboard,在一個(gè)頁(yè)面盡可能展示多的數(shù)據(jù),如果你有這方面的需求,不妨試試 Grafana。


Grafana 使用

Grafana 是一套開(kāi)源的分析監(jiān)視平臺(tái),支持 Graphite, InfluxDB, OpenTSDB, Prometheus, Elasticsearch, CloudWatch 等數(shù)據(jù)源,其 UI 非常漂亮且高度定制化。

這是 Prometheus web console 不具備的,在上一節(jié)中我已經(jīng)說(shuō)明了選擇它的原因。

版本說(shuō)明

  • Mac version 4.3.2

安裝和運(yùn)行程序

這里我使用 brew 安裝,命令為

brew update
brew install grafana

當(dāng)安裝成功后,你可以使用默認(rèn)配置啟動(dòng)程序

grafana-server -homepath /usr/local/Cellar/grafana/4.3.2/share/grafana/

如果順利,你將看到如下日志

INFO[06-11|15:20:14] Starting Grafana                         logger=main version=4.3.2 commit=unknown-dev compiled=2017-06-01T05:47:48+0800
INFO[06-11|15:20:14] Config loaded from                       logger=settings file=/usr/local/Cellar/grafana/4.3.2/share/grafana/conf/defaults.ini
INFO[06-11|15:20:14] Path Home                                logger=settings path=/usr/local/Cellar/grafana/4.3.2/share/grafana/
INFO[06-11|15:20:14] Path Data                                logger=settings path=/usr/local/Cellar/grafana/4.3.2/share/grafana/data
INFO[06-11|15:20:14] Path Logs                                logger=settings path=/usr/local/Cellar/grafana/4.3.2/share/grafana/data/log
INFO[06-11|15:20:14] Path Plugins                             logger=settings path=/usr/local/Cellar/grafana/4.3.2/share/grafana/data/plugins
INFO[06-11|15:20:14] Initializing DB                          logger=sqlstore dbtype=sqlite3
INFO[06-11|15:20:14] Starting DB migration                    logger=migrator
INFO[06-11|15:20:14] Executing migration                      logger=migrator id="copy data account to org"
INFO[06-11|15:20:14] Skipping migration condition not fulfilled logger=migrator id="copy data account to org"
INFO[06-11|15:20:14] Executing migration                      logger=migrator id="copy data account_user to org_user"
INFO[06-11|15:20:14] Skipping migration condition not fulfilled logger=migrator id="copy data account_user to org_user"
INFO[06-11|15:20:14] Starting plugin search                   logger=plugins
INFO[06-11|15:20:14] Initializing Alerting                    logger=alerting.engine
INFO[06-11|15:20:14] Initializing CleanUpService              logger=cleanup
INFO[06-11|15:20:14] Initializing Stream Manager
INFO[06-11|15:20:14] Initializing HTTP Server                 logger=http.server address=0.0.0.0:3000 protocol=http subUrl= socket=

此時(shí),你可以打開(kāi)頁(yè)面 http://localhost:3000, 訪問(wèn) Grafana 的 web 界面。

其他平臺(tái)安裝方案,請(qǐng)參考更多安裝

登錄并設(shè)置 Prometheus 數(shù)據(jù)源

Grafana 本身支持 Prometheus 數(shù)據(jù)源,故不需要安裝其他插件。

使用默認(rèn)賬號(hào) admin/admin 登錄 grafana

grafana-login

在 Dashboard 首頁(yè),點(diǎn)擊添加數(shù)據(jù)源

grafana-datasource

配置 Prometheus 數(shù)據(jù)源

grafana-prometheus-data-source

目前為止,Grafana 已經(jīng)和 Prometheus 連上了,你將看到這樣的 Dashboard

grafana-default-dashbord

自定義監(jiān)視畫(huà)板

由頂部 Manage dashboard -> Settings 進(jìn)入管理頁(yè)面

rafana-into-manage-dashboard

在管理頁(yè)面中取消 Hide Controls

grafana-hide-controls

點(diǎn)擊頁(yè)面底部 + ADD ROW 按鈕, 并選擇 Graph 類(lèi)型

grafana-add-graph

點(diǎn)擊 Panel Title -> Edit 進(jìn)入 Panel 編輯頁(yè)面,并在 Metrics
Metric lookup 選擇 go_goroutines

grafana-edit-panel

你也可以直接在管理界面中填寫(xiě) Prometheus 的查詢(xún)語(yǔ)句,以及修改查詢(xún)的 step 數(shù)值。

當(dāng)你修改了 Dashboard 后,記得點(diǎn)擊頂部的 Save dashboard 按鈕,或直接 CTRL+S 保存。

至此,我們自定義的 Panel 已添加完成

grafana-added-panel

我們可以通過(guò)拖拽,拉升調(diào)節(jié) panel 的位置和尺寸,我們調(diào)節(jié)的目的是盡量在一個(gè)屏幕顯示更多信息。

總結(jié)

Grafana 是一款非常漂亮,強(qiáng)大的監(jiān)視分析平臺(tái),本身支持了 Prometheus 數(shù)據(jù)源,所以在做數(shù)據(jù)和監(jiān)視可視化的時(shí)候,Grafana + Prometheus 是個(gè)不錯(cuò)的選擇。


全局配置

global 屬于全局的默認(rèn)配置,它主要包含 4 個(gè)屬性,

  • scrape_interval: 拉取 targets 的默認(rèn)時(shí)間間隔。
  • scrape_timeout: 拉取一個(gè) target 的超時(shí)時(shí)間。
  • evaluation_interval: 執(zhí)行 rules 的時(shí)間間隔。
  • external_labels: 額外的屬性,會(huì)添加到拉取的數(shù)據(jù)并存到數(shù)據(jù)庫(kù)中。

配置文件結(jié)構(gòu)大概為:

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.
  evaluation_interval: 15s # By default, scrape targets every 15 seconds.
  scrape_timeout: 10s # 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: 'codelab-monitor'
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 基于Grafana和Prometheus的監(jiān)視系統(tǒng) 1. Prometheus 1.1 Prometheus 介紹...
    醉里挑燈A閱讀 42,260評(píng)論 0 22
  • Prometheus Prometheus是一套開(kāi)源的監(jiān)控&報(bào)警&時(shí)間序列數(shù)據(jù)庫(kù)的組合,起始是由SoundClou...
    YichenWong閱讀 19,563評(píng)論 0 6
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評(píng)論 19 139
  • Prometheus Querying 查詢(xún) prometheus提供了功能性表達(dá)式語(yǔ)言,可讓用戶(hù)對(duì)于時(shí)間序列的數(shù)...
    YichenWong閱讀 44,431評(píng)論 6 7
  • 西風(fēng)古道,流水小橋 行夜半忽夢(mèng),紅顏奴嬌 路迢迢,水迢迢,功名盡在遠(yuǎn)征道 英雄年少 金甲,寒光依照,紅顏!憔悴了!
    珞木閱讀 353評(píng)論 0 5

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