go 搭配 prometheus ,grafana 实现 监控 功能


配置 prometheus

先去这个网站下载 对应版本 的 prometheus prometheus 下载
然后

tar xvfz prometheus-*.tar.gz
cd prometheus-*

启动之前 先配置 vim prometheus.yml

prometheus.yml

# my global config
global:
  scrape_interval: 15s # 将抓取间隔设置为每 15 秒。默认为每 1 分钟。
  evaluation_interval: #15s 每 15 秒评估一次规则。默认值为每 1 分钟。
  # 抓取超时 设置为全局默认值(10 秒)。

# 警报管理器配置
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# 加载规则一次并根据全局“evaluation_interval”定期评估它们。
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"

# 只包含一个要抓取的端点的抓取配置:
# 这里是普罗米修斯本身。
scrape_configs:
  # 作业名称作为标签 `job=` 添加到从此配置中抓取的任何时间序列中。
  - job_name: "prometheus"

    # 指标路径 默认 为 '/metrics'
    # scheme defaults to 'http'.  方案默认为“http”。
    static_configs:
      - targets: [ "localhost:9090" ]  # 这个监听的就是 prometheus 服务本身的指标 输入 http:://localhost:9090/metrics 就能看到 


  - job_name: 'node'

    static_configs:
      # 这个监听的就是 localhost 本身的 硬件指标(cpu 内存 硬盘 等信息  ,需要在 本机上面 安装 node_exporter 软件(具体百度)
      - targets: [ 'localhost:9554' ]    
        labels:
          group: 'x99-exporter'

      - targets: [ 'localhost:8554' ]  # 这个是 要自己写个go 程序 定义  port=8554 的服务 ,下面 会讲到
        labels:
          group: 'x99-metrics-client'

具体参数 可以 看官网 的文档 配置文档

然后执行 ./prometheus --config.file prometheus.yml 启动 prometheus 服务

这里最好 开 tmux 或者干脆 用 sh 脚本 做成个服务 ,不然占 你一个 命令行 窗口 ,不 ctrl+c 退出 就 干不了其他的事情了 ……

访问 http:??/localhost:9090 (我这里是 http:??/172.168.10.99:9090 我在172.168.10.99上安装 的prometheus 服务 )

可以 点击 这个 地球 查看 已经收集的指标



然后点击这个graph 就能看到 对应指标的 数据 变化曲线了

http:??/localhost:9090/metrics 就是 prometheus 这个服务自身提供的指标

编写 go client 指标上报客户端

上面 的 http:??/localhost:9090/metrics 是自带的指标,我们要是想自定义 指标上报 ,就 得自己写个 client ,然后给 prometheus server 上报就可以 了

写个 指标上报的 go 程序
main.go

package main

import (
	"fmt"
	"github.com/fatih/color"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"net/http"
	"os/exec"
	"strconv"
	"strings"
	"time"
)
 
func main() {
	 
	// metrics 有四种类型 [metrics 类型](https://prometheus.io/docs/concepts/metric_types/)
	// Counter Gauge Histogram Summary 我这里用的是 gauge 类型
	var gaugeVec = promauto.NewGaugeVec( // 定义一个自动注册的服务(这样不用手动写  prometheus.Register())
		prometheus.GaugeOpts{
			Namespace: "命名空间(英语)",
			Name:      "metrics1 ",
			Help:      "随便写写帮助",
		}, []string{ // 定义 label
			"ip",
			"n_start",
			"type",
		},
	) // 这样定义出来的格式就是 命名空间(英语)_metrics1{ip="xxx",n_start="xxx",type="xxx"} 0(float64类型) 这样的

	//每2秒,就 给 prometheus 传 指标数据
	go func() {
		for {
			// WithLabelValues 确定 label的值 ,set 确定这条数据 的值(这里写固定了,实际业务是要把 value 填进去的)
			gaugeVec.WithLabelValues("localhost", "02", "duduType").Set(233)
			time.Sleep(2 * time.Second)
		}
	}()

	http.Handle("/metrics", promhttp.Handler())  // 访问 /metrics 触发 
	sprint := fmt.Sprintf("程序启动,正在坚挺 8554 端口 ")
	color.Blue(sprint)
	err := http.ListenAndServe(":8554", nil)
	if err != nil {
		sprint1 := fmt.Sprintf("程序启动失败, %+v", err)
		color.Blue(sprint1)
	}

}

 

执行 go run main.go 就把 指标客户 端 跑起来了
然后 在 prometheus 后台 搜索 就会有对应的数据

配置 grafana

grafana 是 用来 显示图表的 这里为 prometheus 数据 显示图标
下载安装 grafana

使用

默认情况下,Grafana 将监听 http://localhost:3000。默认登录名是“admin”/“admin”。

创建 Prometheus 数据源

点击 dashboard -> home -> 创建 dashboard


可以看到 对应指标的 图表 创建成功了

配置报警 (有空在写)

安装 下载 alertManager

在github 上面 下载对应版本的 alertManager 下载地址

解压
$ tar -zxvf alertmanager-0.23.0.linux-amd64.tar.gz alertmanager-0.23.0.linux-amd64/ alertmanager-0.23.0.linux-amd64/alertmanager.yml alertmanager-0.23.0.linux-amd64/LICENSE alertmanager-0.23.0.linux-amd64/NOTICE alertmanager-0.23.0.linux-amd64/alertmanager alertmanager-0.23.0.linux-amd64/amtool