使用Prometheus和Grafana監控golang服務

環境

centOS 7.0
Prometheus2.14.0
Grafana6.5.2linux

下載安裝Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.14.0/prometheus-2.14.0.linux-386.tar.gz

tar -xavf prometheus-2.14.0.linux-386.tar.gz
啓動

在解壓目錄裏就有缺省的配置文件prometheus.yml。能夠不用修改直接使用啓動。git

./prometheus --config.file=prometheus.yml

在瀏覽器中輸入主機IP:9090訪問就能看到Prometheus界面
屏幕快照 2019-12-17 下午3.06.50.pnggithub

時序類型

<1>Counter:計數器,數據的值持續增長或持續減小。表示的是一個持續變化趨勢值,用來記錄當前的數量。通常用於記錄當前請求數量,錯誤數golang

<2>Gauge:計量器(相似儀表盤)。表示當前數據的一個瞬時值,改值可任意增長或減小。通常用來記錄內存使用量,磁盤使用量,文件打開數量等。api

<3>Histogram:柱狀圖。主要用於在必定範圍內對數據進行採樣,計算在必定範圍內的分佈狀況,一般它採集的數據展現爲直方圖。通常用來記錄請求時長或響應時長瀏覽器

<4>Summary:摘要。主要用於表示一段時間內數據採樣結果。總量,而不是根據統計區間計算出來ide

Grafana

下載測試

wget https://dl.grafana.com/oss/release/grafana-6.5.2-1.x86_64.rpm

安裝ui

sudo yum localinstall grafana-6.5.2-1.x86_64.rpm

啓動spa

systemctl daemon-reload 
systemctl start grafana-server
systemctl status grafana-server

配置文件
配置文件在/etc/sysconfig/grafana-server

GRAFANA_USER=grafana
GRAFANA_GROUP=grafana
GRAFANA_HOME=/usr/share/grafana
LOG_DIR=/var/log/grafana
DATA_DIR=/var/lib/grafana
MAX_OPEN_FILES=10000
CONF_DIR=/etc/grafana
CONF_FILE=/etc/grafana/grafana.ini
RESTART_ON_UPGRADE=true
PLUGINS_DIR=/var/lib/grafana/plugins
PROVISIONING_CFG_DIR=/etc/grafana/provisioning
# Only used on systemd systems
PID_FILE_DIR=/var/run/grafana

訪問
瀏覽器輸入IP:3000,初次登錄賬號和密碼都是admin
進入後會要求生成初次數據源(create your first data source)
屏幕快照 2019-12-17 下午4.58.10.png

屏幕快照 2019-12-17 下午5.00.12.png

生成新的dashboard
屏幕快照 2019-12-17 下午5.01.07.png

屏幕快照 2019-12-17 下午5.02.06.png

屏幕快照 2019-12-17 下午5.03.43.png

屏幕快照 2019-12-17 下午5.06.54.png

實例

接下來作幾個實際的例子看看實際效果
測試代碼請到 例子代碼

Counter

例子監控rpc的數量。counter的計數是不斷累加的

golang代碼,關鍵部分

//Create a new CounterVec
rpcCounter = prometheus.NewCounterVec(
    prometheus.CounterOpts{
        Name: "rpc_counter",
        Help: "RPC counts",
    },
    []string{"api"},
)
    
//registers the provided collector     
prometheus.MustRegister(rpcCounter)

//Add the given value to counter
rpcCounter.WithLabelValues("api_bookcontent").Add(float64(rand.Int31n(50)))
rpcCounter.WithLabelValues("api_chapterlist").Add(float64(rand.Int31n(10)))

在prometheus的配置文件中添加

- job_name: 'req-monitor'
    static_configs:
      - targets: ['localhost:8082']
        labels:
          group: 'newgroup1'

重啓prometheus

ps -aux | grep prometheus
kill -9 xxxx

./prometheus --config.file=prometheus.yml

編譯程序(在linux下運行)

GOOS=linux go build

執行

./prometheus_rpc_http -listen-address=:8082 &

在prometheus下查看
屏幕快照 2019-12-24 下午2.47.18.png
屏幕快照 2019-12-24 下午2.47.52.png

在Grafana下新建dashboard
屏幕快照 2019-12-24 下午2.54.13.png

其中計算公式爲 rate(rpc_counter[1m]) 意思是 對1minute 的rpc_counter值取平均

能夠看到其中有兩條線 api="api_bookcontent", api="api_chapterlist"正是咱們在代碼中經過rpcCounter.WithLabelValues()設置的label

Gauge

golang關鍵部分代碼

rpcReqSize = prometheus.NewGaugeVec(
    prometheus.GaugeOpts{
        Name: "rpc_req_size",
        Help: "RPC request size",
    },
    []string{"api"},
)
    
prometheus.MustRegister(rpcReqSize)

rpcReqSize.WithLabelValues("api_bookcontent").Set(float64(rand.Int31n(8000)))
rpcReqSize.WithLabelValues("api_chapterlist").Set(float64(rand.Int31n(5000)))

在prometheus下查看
屏幕快照 2019-12-24 下午3.02.00.png
屏幕快照 2019-12-24 下午3.02.14.png

在Grafana下新建dashboard
屏幕快照 2019-12-24 下午3.04.10.png

Histogram

golang關鍵部分代碼

httpReqDurationsHistogram = prometheus.NewHistogramVec(
    prometheus.HistogramOpts{
        Name: "http_req_durations_histogram",
        Help: "http req latency distributions.",
        // 4 buckets, starting from 0.1 and adding 0.5 between each bucket
        Buckets: prometheus.LinearBuckets(0.1, 0.5, 4),
    },
    []string{"http_req_histogram"},
)


prometheus.MustRegister(httpReqDurationsHistogram)

v := rand.Float64()
httpReqDurationsHistogram.WithLabelValues("booksvc_req").Observe(1.5 * v)

prometheus下查看
屏幕快照 2019-12-24 下午3.09.26.png

能夠看到咱們在代碼中定義了4個buckets,在圖中就有對應的四個buckets數據(le="0.1",le="0.6",le="1.1",le="1.6")

在Grafana下新建dashboard
屏幕快照 2019-12-24 下午3.17.19.png

計算公式使用rate(http_req_durations_histogram_bucket[30s])
計算30s http_req_durations_histogram_bucket的平均值
根據數值能夠看到0.1秒響應的佔1.3%, 0.6秒內佔17.3%, 1.1秒內響應的佔34.7, 1.6秒內響應的佔60%

Summary

golang關鍵代碼

rpcDurations = prometheus.NewSummaryVec(
        prometheus.SummaryOpts{
            Name:       "rpc_durations_seconds",
            Help:       "RPC latency distributions.",
            Objectives: map[float64]float64{0.5: 0.5, 0.9: 1.5, 0.99: 2.0},
        },
        []string{"service"},
    )
    
prometheus.MustRegister(rpcDurations)

v = rand.Float64()
rpcDurations.WithLabelValues("user_rpc").Observe(v)

v = 0.5 + rand.Float64()            rpcDurations.WithLabelValues("book_rpc").Observe(v)
            
v = 1.0 + rand.Float64()    rpcDurations.WithLabelValues("bookshelf_rpc").Observe(v)

在prometheus下查看
屏幕快照 2019-12-24 下午5.53.01.png

在Grafana下新建dashboard
屏幕快照 2019-12-24 下午5.54.42.png

計算公式爲rate(rpc_durations_seconds_sum[1m])

相關文章
相關標籤/搜索