最強(qiáng)性能監(jiān)控工具之Grafana+Prometheus+Exporters
1 監(jiān)控邏輯
最流行的監(jiān)控邏輯:
有測(cè)試工具、監(jiān)控工具,才能做性能分析和瓶頸定位。
不管數(shù)據(jù)啥形式展示,最要緊還是數(shù)據(jù)來(lái)源和含義,以做正確判斷。
2 JMeter+InfluxDB+Grafana數(shù)據(jù)展示邏輯
JMeter壓測(cè)時(shí),使用JMeter控制臺(tái)查看結(jié)果:
或裝插件看結(jié)果:
或JMeter生成HTML:
壓力工具只關(guān)心三條曲線:TPS(T由測(cè)試目標(biāo)定義)、響應(yīng)時(shí)間、錯(cuò)誤率。錯(cuò)誤率還只是輔助排查問(wèn)題的曲線,沒(méi)問(wèn)題時(shí),只看TPS、響應(yīng)時(shí)間。
2.1 傳統(tǒng)方案的缺陷
- 1. 整理結(jié)果費(fèi)時(shí)
- 2. 在GUI用插件看曲線,高并發(fā)時(shí)不現(xiàn)實(shí)
- 3. 在場(chǎng)景運(yùn)行時(shí)間比較長(zhǎng)時(shí),采用生成HTML,會(huì)出現(xiàn)消耗內(nèi)存過(guò)大的情況。有很多生成的圖也并不關(guān)注
- 4. 生成的結(jié)果保存之后再查看比較麻煩,還一個(gè)個(gè)找
2.2 解決方案
用JMeter的Backend Listener實(shí)時(shí)發(fā)數(shù)據(jù)到InfluxDB或Graphite。Graphite Backend Listener支持在JMeter 2.13版本,InfluxdDB Backend Listener的支持在JMeter 3.3,都是異步發(fā)數(shù)據(jù),以便查看。
有這JMeter發(fā)給InfluxDB的數(shù)據(jù),無(wú)需看上面的那些HTML數(shù)據(jù),也能直觀看到系統(tǒng)的性能趨勢(shì)。以后復(fù)看也方便比對(duì)。
3 JMeter+InfluxDB+Grafana結(jié)構(gòu)
JMeter發(fā)送壓力到服務(wù)器的同時(shí),統(tǒng)計(jì)TPS、響應(yīng)時(shí)間、線程數(shù)、錯(cuò)誤率等信息。
默認(rèn)每30s在控制臺(tái)輸出一次(jmeter.properties參數(shù)#summariser.interval=30可以控制)。
配置Backend Listener后,將統(tǒng)計(jì)結(jié)果異步發(fā)到InfluxDB。最后在Grafana配置:
- ? InfluxDB數(shù)據(jù)源
- ? JMeter顯示模板
就能實(shí)時(shí)查看JMeter測(cè)試結(jié)果,這看到的數(shù)據(jù)和控制臺(tái)數(shù)據(jù)一樣。
4 數(shù)據(jù)的傳輸和展示邏輯
4.1 JMeter中Backend Listener配置
就InfluxDB的Backend Listener做個(gè)說(shuō)明。在腳本中加上即可:
先配好influxdb Url、application等信息,application配置可看成是場(chǎng)景名。
4.2 JMeter如何發(fā)數(shù)據(jù)給InfluxDB
關(guān)鍵源碼:
private void addMetrics(String transaction, SamplerMetric metric) {
// FOR ALL STATUS
addMetric(transaction, metric.getTotal(), metric.getSentBytes(), metric.getReceivedBytes(), TAG_ALL, metric.getAllMean(), metric.getAllMinTime(),
metric.getAllMaxTime(), allPercentiles.values(), metric::getAllPercentile);
// FOR OK STATUS
addMetric(transaction, metric.getSuccesses(), null, null, TAG_OK, metric.getOkMean(), metric.getOkMinTime(),
metric.getOkMaxTime(), okPercentiles.values(), metric::getOkPercentile);
// FOR KO STATUS
addMetric(transaction, metric.getFailures(), null, null, TAG_KO, metric.getKoMean(), metric.getKoMinTime(),
metric.getKoMaxTime(), koPercentiles.values(), metric::getKoPercentile);
metric.getErrors().forEach((error, count) -> addErrorMetric(transaction, error.getResponseCode(),
error.getResponseMessage(), count));
}
站在全局統(tǒng)計(jì)視角,這里把JMeter運(yùn)行的統(tǒng)計(jì)結(jié)果:
? 如事務(wù)的Total請(qǐng)求、發(fā)送接收字節(jié)、平均值、最大值、最小值等,都加到metric
? 同時(shí)也把成功/失敗的事務(wù)信息加到metric
更多的添加metric的步驟看JMeter源碼InfluxdbBackendListenerClient.java。
保存metric后,再用InfluxdbMetricsSender發(fā)到Influxdb:
@Override
public void writeAndSendMetrics() {
........
if (!copyMetrics.isEmpty()) {
try {
if(httpRequest == null) {
httpRequest = createRequest(url);
}
StringBuilder sb = new StringBuilder(copyMetrics.size()*35);
for (MetricTuple metric : copyMetrics) {
// Add TimeStamp in nanosecond from epoch ( default in InfluxDB )
sb.append(metric.measurement)
.append(metric.tag)
.append(" ") //$NON-NLS-1$
.append(metric.field)
.append(" ")
.append(metric.timestamp+"000000")
.append("\n"); //$NON-NLS-1$
}
StringEntity entity = new StringEntity(sb.toString(), StandardCharsets.UTF_8);
httpRequest.setEntity(entity);
lastRequest = httpClient.execute(httpRequest, new FutureCallback<HttpResponse>() {
@Override
public void completed(final HttpResponse response) {
int code = response.getStatusLine().getStatusCode();
/*
* HTTP response summary 2xx: If your write request received
* HTTP 204 No Content, it was a success! 4xx: InfluxDB
* could not understand the request. 5xx: The system is
* overloaded or significantly impaired.
*/
if (MetricUtils.isSuccessCode(code)) {
if(log.isDebugEnabled()) {
log.debug("Success, number of metrics written: {}", copyMetrics.size());
}
} else {
log.error("Error writing metrics to influxDB Url: {}, responseCode: {}, responseBody: {}", url, code, getBody(response));
}
}
@Override
public void failed(final Exception ex) {
log.error("failed to send data to influxDB server : {}", ex.getMessage());
}
@Override
public void cancelled() {
log.warn("Request to influxDB server was cancelled");
}
});
........
}
}
}
通過(guò)writeAndSendMetrics,就將所有保存的metrix都發(fā)給InfluxDB。
5 InfluxDB存儲(chǔ)結(jié)構(gòu)
InfluxDB如何存儲(chǔ):
> show databases
name: databases
name
----
_internal
jmeter
> use jmeter
Using database jmeter
> show MEASUREMENTS
name: measurements
name
----
events
jmeter
> select * from events where applicatinotallow='7ddemo'
name: events
time application text title
---- ----------- ---- -----
1575255462806000000 7ddemo Test Cycle1 started ApacheJMeter
1575256463820000000 7ddemo Test Cycle1 ended ApacheJMeter
..............
> select * from jmeter where applicatinotallow='7ddemo' limit 10
name: jmeter
time application avg count countError endedT hit max maxAT meanAT min minAT pct90.0 pct95.0 pct99.0 rb responseCode responseMessage sb startedT statut transaction
---- ----------- --- ----- ---------- ------ --- --- ----- ------ --- ----- ------- ------- ------- -- ------------ --------------- -- -------- ------ -----------
1575255462821000000 7ddemo 0 0 0 0 0 internal
1575255467818000000 7ddemo 232.82352941176472 17 0 17 849 122 384.9999999999996 849 849 0 0 all all
1575255467824000000 7ddemo 232.82352941176472 17 849 122 384.9999999999996 849 849 0 0 all 0_openIndexPage
1575255467826000000 7ddemo 232.82352941176472 17 849 122 384.9999999999996 849 849 ok 0_openIndexPage
1575255467829000000 7ddemo 0 1 1 1 1 internal
1575255472811000000 7ddemo 205.4418604651163 26 0 26 849 122 252.6 271.4 849 0 0 all all
1575255472812000000 7ddemo 0 1 1 1 1 internal
1575255472812000000 7ddemo 205.4418604651163 26 849 122 252.6 271.4 849 ok 0_openIndexPage
1575255472812000000 7ddemo 205.4418604651163 26 849 122 252.6 271.4 849 0 0 all 0_openIndexPage
1575255477811000000 7ddemo 198.2142857142857 27 0 27 849 117 263.79999999999995 292.3500000000001 849 0 0 all all
InfluxDB中創(chuàng)建兩個(gè)MEASUREMENTS:
? events
? jmeter
這兩個(gè)各自存數(shù)據(jù),在界面中配置的testtile和eventTags放在events這個(gè)MEASUREMENTS中。在模板中這兩個(gè)值暫都不用。
在jmeter這個(gè)MEASUREMENTS中,可看到application和事務(wù)的統(tǒng)計(jì)信息,這些值和控制臺(tái)一致。
在Grafana中顯示時(shí),就是從這個(gè)表中取出數(shù)據(jù),根據(jù)時(shí)序做曲線。
6 Grafana配置
有了JMeter發(fā)送到InfluxDB中的數(shù)據(jù)后,下面得配置Grafana展示。
6.1 配置一個(gè)InfluxDB數(shù)據(jù)源
在這配置URL、Database、User、Password,點(diǎn)擊保存。
6.2 添加一個(gè)JMeter dashboard
常用dashboard是Grafana官方ID為5496的模板。導(dǎo)入進(jìn)來(lái)后,選好對(duì)應(yīng)數(shù)據(jù)源:
就看到界面啦:
這時(shí)還沒(méi)數(shù)據(jù),稍后做個(gè)示例,看JMeter的數(shù)據(jù)怎么對(duì)應(yīng)的該界面數(shù)據(jù)。
7 數(shù)據(jù)對(duì)比
圖中兩個(gè)重要的數(shù)據(jù)查詢(xún)語(yǔ)句。
7.1 TPS曲線
SELECT last("count") / $send_interval
FROM "$measurement_name"
WHERE ("transaction" =~ /^$transaction$/
AND "statut" = 'ok')
AND $timeFilter
GROUP BY time($__interval)
即Total TPS,在這稱(chēng)為throughput。
這里取的數(shù)據(jù)來(lái)自MEASUREMENTS中成功狀態(tài)的所有事務(wù)。
7.2 響應(yīng)時(shí)間曲線
SELECT mean("pct95.0")
FROM "$measurement_name"
WHERE ("application" =~ /^$application$/)
AND $timeFilter
GROUP BY "transaction", time($__interval) fill(null)
這是用95 pct內(nèi)的響應(yīng)時(shí)間畫(huà)出的曲線。
7.3 整體展示效果
7.4 JMeter配置場(chǎng)景
10個(gè)線程,每個(gè)線程迭代10次及兩個(gè)HTTP請(qǐng)求:
會(huì)產(chǎn)生10x10x2=200次請(qǐng)求。JMeter跑下:
請(qǐng)求數(shù)和預(yù)想一樣,看Grafana展示結(jié)果:
針對(duì)每個(gè)事務(wù)的統(tǒng)計(jì):
7.5 意義
JMeter到Grafana的展示過(guò)程完成。以后就:
? 不用再保存JMeter執(zhí)行結(jié)果
? 不用等JMeter輸出HTML
8 node_exporter+Prometheus+Grafana數(shù)據(jù)展示邏輯
性能測(cè)試,在常用的Grafana+Prometheus+Exporter邏輯,第一步就要看os資源。
以node_exporter為例,說(shuō)明os抽取數(shù)據(jù)的邏輯,來(lái)看監(jiān)控?cái)?shù)據(jù)的來(lái)源:
node_exporter可支持很多個(gè)os:
當(dāng)然你也能擴(kuò)展自己的Exporter。
8.1 配置node_exporter
node_exporter目錄:
[root@7dgroup2 node_exporter-0.18.1.linux-amd64]# ll
total 16524
-rw-r--r-- 1 3434 3434 11357 Jun 5 00:50 LICENSE
-rwxr-xr-x 1 3434 3434 16878582 Jun 5 00:41 node_exporter
-rw-r--r-- 1 3434 3434 463 Jun 5 00:50 NOTICE
啟動(dòng):
./node_exporter --web.listen-address=:9200 &
8.2 配置Prometheus
下載:
wget -c https://github.com/prometheus/prometheus/releases/download/v2.14.0/prometheus-2.14.0.linux-amd64.tar.gz
解壓后目錄:
[root@ prometheus-2.11.1.linux-amd64]# ll
total 120288
drwxr-xr-x. 2 3434 3434 4096 Jul 10 23:26 console_libraries
drwxr-xr-x. 2 3434 3434 4096 Jul 10 23:26 consoles
drwxr-xr-x. 3 root root 4096 Nov 30 12:55 data
-rw-r--r--. 1 3434 3434 11357 Jul 10 23:26 LICENSE
-rw-r--r--. 1 root root 35 Aug 7 23:19 node.yml
-rw-r--r--. 1 3434 3434 2770 Jul 10 23:26 NOTICE
-rwxr-xr-x. 1 3434 3434 76328852 Jul 10 21:53 prometheus
-rw-r--r-- 1 3434 3434 1864 Sep 21 09:36 prometheus.yml
-rwxr-xr-x. 1 3434 3434 46672881 Jul 10 21:54 promtool
在prometheus.yml加如下配置,以取數(shù)據(jù):
- job_name: 's1'
static_configs:
- targets: ['172.17.211.143:9200']
啟動(dòng):
./prometheus --config.file=prometheus.yml &
8.3 配置Grafana
① 配置數(shù)據(jù)源
② 配置node_exporter模板
如選擇官方模板(ID:11074)的展示:
8.4 數(shù)據(jù)邏輯
做性能測(cè)試和分析,最重要是知道數(shù)據(jù)來(lái)源和含義。
如上圖CPU使用率,點(diǎn)擊title上的edit,看語(yǔ)句:
avg(irate(node_cpu_seconds_total{instance=~"$node",mode="system"}[30m])) by (instance)
avg(irate(node_cpu_seconds_total{instance=~"$node",mode="user"}[30m])) by (instance)
avg(irate(node_cpu_seconds_total{instance=~"$node",mode="iowait"}[30m])) by (instance)
1 - avg(irate(node_cpu_seconds_total{instance=~"$node",mode="idle"}[30m])) by (instance)
都是從Prometheus取出的數(shù)據(jù),SQL讀Prometheus中node_cpu_seconds_total的不同的模塊數(shù)據(jù)。
看node_exporter暴露的計(jì)數(shù)器:
值和top一樣,都來(lái)自/proc/目錄。如下即是top命令的數(shù)據(jù):
因此,os中監(jiān)控?cái)?shù)據(jù)的取值邏輯:
? 從os本身的計(jì)數(shù)器取值
? 傳給Prometheus
? 再由Grafana中的query語(yǔ)句查出相應(yīng)的數(shù)據(jù)
? 最后由Grafana展示
9 總結(jié)
為何專(zhuān)門(mén)解釋數(shù)據(jù)的邏輯?有人說(shuō)有Prometheus+Grafana+Exportor,就無(wú)需再手工執(zhí)行命令。
但監(jiān)控平臺(tái)取的所有的數(shù)據(jù),必然是被監(jiān)控者可提供的數(shù)據(jù),像node_exporter這樣小巧的監(jiān)控收集器,可獲取的監(jiān)控?cái)?shù)據(jù),并非整個(gè)系統(tǒng)全部的性能數(shù)據(jù),只是取到常見(jiàn)計(jì)數(shù)器。
這些計(jì)數(shù)器不管用命令查看,還是用花里胡哨的工具,它的值本身都不會(huì)變。所以不管在監(jiān)控平臺(tái) or 命令行中看到的數(shù)據(jù),最重要是知道含義及這些值的變化對(duì)性能測(cè)試和分析的下一步的影響。
JMeter如何把數(shù)據(jù)推送到Grafana中?
1.在JMeter中啟用插件:要將JMeter數(shù)據(jù)推送到Grafana中,您需要在JMeter中啟用插件。在JMeter的lib/ext目錄中找到JMeter插件管理器插件文件(JMeterPlugins-Manager.jar),并將其放置在該目錄中。重啟JMeter后,單擊“選項(xiàng)”菜單中的“插件管理器”,然后選擇“可選插件”選項(xiàng)卡。在這里,選擇“grafana-backendlistener”并單擊“應(yīng)用更改”。
2.配置Grafana:打開(kāi)Grafana中的數(shù)據(jù)源列表,選擇一個(gè)數(shù)據(jù)源(例如InfluxDB)并創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)。請(qǐng)確保將數(shù)據(jù)庫(kù)名稱(chēng)、用戶(hù)名和密碼配置為與JMeter對(duì)接的數(shù)據(jù)源的名稱(chēng)、用戶(hù)名和密碼相同。
3.在JMeter中添加Backend Listener:在JMeter測(cè)試計(jì)劃中添加Backend Listener。在Backend Listener的屬性中,選擇“InfluxDBBackendListenerClient”作為Backend Listener實(shí)現(xiàn),并按照屏幕上的說(shuō)明設(shè)置InfluxDB的服務(wù)器和端口。
4.在Grafana中查看測(cè)試結(jié)果:創(chuàng)建一個(gè)Grafana儀表板,并選擇InfluxDB作為數(shù)據(jù)源。在儀表板上選擇一個(gè)面板,并將其設(shè)置為在Grafana中顯示JMeter測(cè)試結(jié)果的數(shù)據(jù)。
都是監(jiān)控os計(jì)數(shù)器,監(jiān)控平臺(tái)的數(shù)據(jù)和監(jiān)控命令中的數(shù)據(jù)啥區(qū)別?
1.監(jiān)控平臺(tái)是一個(gè)基于web或客戶(hù)端的可視化平臺(tái),可以將實(shí)時(shí)的OS監(jiān)控指標(biāo)以圖表、表格等形式展示出來(lái),以便于管理員進(jìn)行查看與分析。
2.監(jiān)控命令是一種命令行方式的工具,提供了豐富的OS監(jiān)控指標(biāo)查詢(xún)和分析功能。它通過(guò)在終端輸入不同的命令參數(shù),實(shí)時(shí)獲取和顯示各種系統(tǒng)統(tǒng)計(jì)和性能指標(biāo)。它主要用于開(kāi)發(fā)和運(yùn)維人員進(jìn)行診斷和分析。
3.監(jiān)控平臺(tái)是一種可配置、可擴(kuò)展的監(jiān)控方案,它可以幫助管理員實(shí)現(xiàn)對(duì)復(fù)雜的分布式應(yīng)用的監(jiān)控。而監(jiān)控命令通常只能監(jiān)控單個(gè)系統(tǒng)的指標(biāo)。
4.監(jiān)控平臺(tái)通常要安裝一個(gè)客戶(hù)端,以便向平臺(tái)發(fā)送數(shù)據(jù)。而在監(jiān)控命令中,可以直接在終端輸入命令,獲取OS的監(jiān)控指標(biāo)。
綜上:
? 監(jiān)控平臺(tái)提供GUI,便于管理員查看和管理指標(biāo)數(shù)據(jù)
? 監(jiān)控命令則更靈活,提供更多細(xì)節(jié)和具體信息