Grafana 番外篇1.1、使用Grafana、Prometheus監控K6產生的測試狀況

固定化代碼測試作業,避免被未來的變化所破壞

Ruei-Chi Huang
Kirin Blog | Kirin の IT Geek
10 min readDec 4, 2022

--

在一個中大型開發的團隊裡,因為要不斷的進行新功能開發,日積月累之後,測試的工作會變得很繁雜,每佈署一次版本就需要進行數十次的測試,所以一個正規化的開發團隊,都會制訂一套標準化的功能測試SOP,而執行這個測試的人員大多是軟體開發人員,code不斷的變動累積龐大的架構,最後造成測試工作是一個很重的負擔,在國外有位DevOps大神Nathan Peck提出一套金字塔理論,描述了測試工作的結構性,四種組件形成了一個完整的整體,缺一不可。

Testing Pyramid Credit:https://medium.com/@nathankpeck

在這樣環境下,自動化測試就能夠簡化很多負擔,本篇番外篇就在於介紹,如何使用Grafana+Prometheus整合K6,來達到自動化代碼測試並監控的方式。

Grafana安裝可以參考入門篇1.1 : Grafana Enterprise 安裝 (Ubuntu)

Prometheus安裝可以參考入門篇1.2 : 整合Prometheus監控Proxmox ve

K6 是什麼?

K6 是一套Open-source 開源負載測試工具,有安裝版與雲端版兩種,主要用於解決系統開發時的測試與效能監控作業,以自動化、可視化、縮短測試週期見長,K6已於2021年6月被Grafana lab併購。

環境 :

Proxmox ve : https://192.168.50.60:8006

Grafana : http://192.168.50.61:3000/login

Prometheus : http://192.168.50.62:9090

Docker Host : 192.168.50.62

Portainer : https://192.168.50.62:9443

K8s Master : 192.168.50.61

K8s Node1 : 192.168.50.63

K6 Host : 192.168.50.64

Step1. 安裝K6 hosts

K6安裝在這次架構上選擇安裝在獨立VM上,初期不建議用container ,因為這台VM在標準化運作之前,會有很多的Script 變動,container變動Files不是很方便。

1.1、K6 install (Ubuntu)

gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69

這邊需要留意的地方,command 下去之後可能會有以下這種狀況

gpg: failed to create temporary file '/root/.gnupg/.#lk0x00005644674f6270.k6srv.4523': No such file or directory

出現以上這個錯誤,就可以用以下的command解決,

cd root
mkdir .gnupg
find ~/.gnupg -type d -exec chmod 700 {} \;
echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list
sudo apt-get update
sudo apt-get install k6
k6 安裝完成

Step2. 安裝statsd-exporter

2.1安裝 : 我這邊的做法是先將K6 的資料推送到statsd_exporter,然後再讓Prometheus抓取statsd_exporter的數據,statsd-exporter直接安裝在K6的主機上。

statsd_exporter安裝

sudo snap install icey-prometheus-statsd-exporter
安裝完成後透過9102 port 可以查看到各項metric

2.2 設定 : 讓prometheus 的service discover 可以抓到statsd_exporter

ssh 到 docker host

ssh 192.168.50.62 22

先進到tmp 然後新增一份 prometheus.yml

cd /tmp
nano prometheus.yml

加入以下內容後儲存

global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 15s
alerting:
alertmanagers:
- follow_redirects: true
enable_http2: true
scheme: http
timeout: 10s
api_version: v2
static_configs:
- targets: []
scrape_configs:
- job_name: prometheus
honor_timestamps: true
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
follow_redirects: true
enable_http2: true
static_configs:
- targets:
- localhost:9090

- job_name: 'proxmox'
metrics_path: /pve
static_configs:
- targets: ['192.168.50.60:9221']

- job_name: 'statsd_exporter'
static_configs:
- targets: ['192.168.50.62:9102']
labels: {}
metric_relabel_configs:
- regex: '(job|instance|url)'
action: labeldrop

再來先到portainer關閉prometheus,然後將prometheus.yml寫入prometheus的container,完成後開啟prometheus。

docker cp /tmp/prometheus.yml 154f28014e9d391063ac0321aae82e7e0c6e321b21781e810bc5a6dd429f8582:/etc/prometheus/

2.3 測試 : k6 output to statsd_exporter

新增一個js 的測試範本檔

mkdir /tmp/k6
cd /tmp/k6
nano simple-test.js

simple-test.js內容

import http from 'k6/http';
import { sleep, check } from 'k6';
import { Counter } from 'k6/metrics';

// A simple counter for http requests

export const requests = new Counter('http_reqs');

// you can specify stages of your test (ramp up/down patterns) through the options object
// target is the number of VUs you are aiming for

export const options = {
stages: [
{ target: 20, duration: '1m' },
{ target: 15, duration: '1m' },
{ target: 0, duration: '1m' },
],
thresholds: {
http_reqs: ['count < 100'],
},
};

export default function () {
// our HTTP request, note that we are saving the response to res, which can be accessed later

const res = http.get('http://test.k6.io');

sleep(1);

const checkRes = check(res, {
'status is 200': (r) => r.status === 200,
'response body': (r) => r.body.indexOf('Feel free to browse') !== -1,
});
}

run simple-test.js並加入 output 到 statsd

k6 run -o statsd simple-test.js

Step3. Grafana 匯入 K6 metrics & dashboard

3.1 import k6 dashboard

進入到Grafana 並在Dashboard → import → ID : 13861 匯入K6資料

完成後,就可以看到剛剛K6 run 的資訊

結論 :

Grafana 跟 Prometheus的整合性效果很不錯,SRE & DevOps都有不錯的發揮,不只監控一般infrastructure,系統運作效能、測試的監控都可以整合一體,APM也有不錯的表現。

--

--

Ruei-Chi Huang
Kirin Blog | Kirin の IT Geek

I am Kirin., I was ShellFans AI Technology’s CTO. Our team specializes in IoT, Blockchain, Cloud and Ai. | https://www.shell.fans