歡迎加入go語言學習交流羣 636728449git
Prometheus筆記(二)監控go項目實時給grafana展現
Prometheus筆記(一)metric typegithub
先寫好配置文件,保存爲prometheus.yml
,golang
global: scrape_interval: 15s # By default, scrape targets every 15 seconds. # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor' # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' //服務的名稱,後續要監控咱們本身的服務時只須要按照這個格式再添加上 # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ['localhost:9090'] //這個爲服務的ip和port
更多配置文件寫法請參考:https://prometheus.io/docs/operating/configuration/web
官方給出供參考的配置文件:https://github.com/prometheus/prometheus/blob/release-2.3/config/testdata/conf.good.ymldocker
而後利用docker啓動。shell
docker run -p 9090:9090 --network=host -v /root/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
json
啓動以後,能夠先測試一下是否能夠用。api
[root@localhost ~]# curl http://localhost:9090/api/v1/label/job/values { "status":"success", "data":["prometheus"] }
若是是用上面我給出來的默認配置,返回值應該是和這裏給出來的同樣。說明promethues服務端啓動好了。瀏覽器
這篇文章只是演示基本用法,因此用到的grafana的配置都是默認的。直接使用下面的命令啓動就能夠了。curl
$ docker run -d -p 3000:3000 grafana/grafana
若是須要更多功能則須要更復雜的配置了,更多配置方法請參考:http://docs.grafana.org/installation/docker/
docker鏡像起來後,用瀏覽器登入 127.0.0.0:3000 ,會彈出來登入界面,用戶名和密碼爲admin/admin,第一次會提示修改密碼,按照提示操做便可。這樣就完成了安裝。
這一步我主要寫一個簡單的go項目,用來獲取內存的實時使用率數據,而後在grafana展現。
代碼下載地址:https://github.com/Zhanben/goproject/tree/master/gomemory
package main import ( "net/http" "log" "time" "os" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus" "github.com/shirou/gopsutil/mem" ) func main (){ //初始化日誌服務 logger := log.New(os.Stdout, "[Memory]", log.Lshortfile | log.Ldate | log.Ltime) //初始一個http handler http.Handle("/metrics", promhttp.Handler()) //初始化一個容器 diskPercent := prometheus.NewGaugeVec(prometheus.GaugeOpts{ Name: "memeory_percent", Help: "memeory use percent", }, []string {"percent"}, ) prometheus.MustRegister(diskPercent) // 啓動web服務,監聽1010端口 go func() { logger.Println("ListenAndServe at:localhost:1010") err := http.ListenAndServe("localhost:1010", nil) if err != nil { logger.Fatal("ListenAndServe: ", err) } }() //收集內存使用的百分比 for { logger.Println("start collect memory used percent!") v, err := mem.VirtualMemory() if err != nil { logger.Println("get memeory use percent error:%s", err) } usedPercent := v.UsedPercent logger.Println("get memeory use percent:", usedPercent) diskPercent.WithLabelValues("usedMemory").Set(usedPercent) time.Sleep(time.Second*2) } }
程序跑起來的輸出以下:
[root@localhost demoproject]# go run memory.go [Memory]2018/07/14 11:43:12 memory.go:42: start collect memory used percent! [Memory]2018/07/14 11:43:12 memory.go:48: get memeory use percent: 41.22097449562238 [Memory]2018/07/14 11:43:12 memory.go:33: ListenAndServe at:locahost:1010 [Memory]2018/07/14 11:43:14 memory.go:42: start collect memory used percent! [Memory]2018/07/14 11:43:14 memory.go:48: get memeory use percent: 41.219733205342514 [Memory]2018/07/14 11:43:16 memory.go:42: start collect memory used percent! [Memory]2018/07/14 11:43:16 memory.go:48: get memeory use percent: 41.219733205342514 ^Csignal: interrupt
此時能夠查詢的到promethues監控到的數據。
[root@localhost ~]# curl http://localhost:1010/metrics ... # HELP go_memstats_sys_bytes Number of bytes obtained by system. Sum of all system allocations. # TYPE go_memstats_sys_bytes gauge go_memstats_sys_bytes 3.346432e+06 //這個爲代碼添加的字段,其他爲promethues默認監控字段 # HELP memeory_percent memeory use percent # TYPE memeory_percent gauge memeory_percent{percent="usedMemory"} 41.16718525016137 # HELP process_cpu_seconds_total Total user and system CPU time spent in seconds. # TYPE process_cpu_seconds_total counter process_cpu_seconds_total 0.01 ....
先將監控服務註冊到promethues服務端,修改配置文件:promethues.yml
... //和前面的配置文件同樣 scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'memory' //給你的服務取的名字 # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ['localhost:1010'] //改爲你本身代碼裏面使用的端口號
暫停掉以前啓動的promethues和grafana
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 650cb5891e56 grafana/grafana "/run.sh" 19 hours ago Up 19 hours 0.0.0.0:3000->3000/tcp brave_ride 850a44d18dfe prom/prometheus "/bin/prometheus -..." 21 hours ago Up 21 hours 0.0.0.0:9090->9090/tcp zen_keller [root@localhost ~]# docker stop 850a44d18dfe 850a44d18dfe [root@localhost ~]# docker stop 650cb5891e56 650cb5891e56
修改好配置文件以後從新啓動promethues和grafana
[root@localhost ~]# docker run -d -p 9090:9090 --network=host -v /root/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus [root@localhost ~]# docker run -d -p 3000:3000 grafana/grafana
檢驗promethues服務端是否註冊到了咱們本身的服務。
[root@localhost demoproject]# curl http://localhost:9090/api/v1/targets { "status": "success", "data": { "activeTargets": [{ "discoveredLabels": { "__address__": "localhost:1010", "__metrics_path__": "/metrics", "__scheme__": "http", "job": "memory" }, "labels": { "instance": "localhost:1010", "job": "memory" //這個memory即咱們在promethues的配置文件填寫的名字 }, "scrapeUrl": "http://localhost:1010/metrics", "lastError": "", "lastScrape": "2018-07-14T07:39:26.127284982Z", "health": "up" //注意上面這個字段要爲up,要否則後續grafana查詢不到數據 } ], "droppedTargets": [] } }
第一步如圖所示,按照圖中的三步操做。
操做完成以後會進入下圖所示的界面,而後再次按照圖中提示操做便可。
完成以後會彈出來一個panle,單擊下拉框,點擊Edit。
點擊Edit以後會pane下方會獲得下圖展現的界面:
在查詢字段的地方填入,代碼縮寫的字段prometheus.NewGaugeVec建立時填寫的name字段,本示例代碼爲memory_percent。填好以後點擊下option旁邊的query inspector,就能夠在上面的表中查看到數據了。
最後查詢到的數據以下圖所示:
歡迎加入go語言學習交流羣 636728449
一、 https://godoc.org/github.com/prometheus/client_golang/prometheus
二、 https://prometheus.io/docs/introduction/overview/