promethues golang
學習promethues
理論
pull vs push
- 官方建議在長生命週期時讓promethues自己pull而非服務端主動push
本地使用promethues
啟動promethues
prometheus --config.file=./prometheus.yaml
是否promethues 有成功metrics
curl http://192.168.0.101:9092/metrics
測試是否有成功pull metrics
curl http://localhost:9090/api/v1/targets
連接Grpc使用
配置config
- 這邊監聽得port是promethues中metrics的而非grpc的port
global:
scrape_timeout: 10s
evaluation_interval: 30s
external_labels:
monitor: 'grpc-template'
scrape_configs:
- job_name: 'grpc_echo_server'
scrape_interval: 1s
static_configs:
- targets: ['192.168.0.101:9092']
註冊promethue的metrics
httpServer := &http.Server{Addr: ":9092", Handler: promhttp.HandlerFor(reg, promhttp.HandlerOpts{})}
go func() {
if err := httpServer.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()