使用grafana和prometheus模拟统计golang中的http错误率

在网络应用程序中,监控往往是很重要的,有了监控,才能更好的分析和排查应用程序中的问题,提高效率,实现“事前预警,事后跟踪”

在当代开源系统中,可以使用grafana和prometheus相互配合,从而进行监控。
1、prometheus是一个监控和报警工具,可以实现对数据的捕捉和采集,所采集的数据会以指标的形式保存在时序数据库中。
2、grafana是一个开源的可视化工具,可以很好的对接prometheus,十分强大和方便
下面就介绍如何使用这两种工具进行配合使用,实现对golang的指标进行监控

环境依赖

1、安装grafana
2、安装prometheus
3、安装gin框架
4、mac环境
5、golang环境

安装grafana

可以访问grafana的官方网站https://grafana.com/grafana/download,下载对应二进制文件

curl -O [https://dl.grafana.com/enterprise/release/grafana-enterprise-11.1.3.darwin-amd64.tar.gz](https://dl.grafana.com/enterprise/release/grafana-enterprise-11.1.3.darwin-amd64.tar.gz)
tar -zxvf grafana-enterprise-11.1.3.darwin-amd64.tar.gz

启动grafana,启动bin目录下的grafana文件,如下

./bin/grafana server

浏览器访问http://localhost:3000 ,如下启动成功

image.png
安装prometheus

访问官方网站https://prometheus.io/download/进行下载对应的二进制

image.png

启动prometheus

./prometheus

浏览器访问http://localhost:9090/,如下启动成功

image.png

代码实例
package main

import (
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "math/rand"
    "net/http"
)

var (
    WebRequestTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
        Name: "web_reqeust_total",
        Help: "Number of hello requests in total",
    }, []string{"method", "path", "code"})
)

func init() {
    prometheus.MustRegister(WebRequestTotal)
}
func main() {
    r := gin.Default() //创建gin
    r.GET("/", index) //绑定路由
    r.GET("/metrics", gin.WrapH(promhttp.Handler()))
    // 定义一个健康检查的路由
    r.GET("/health", func(c *gin.Context) {
        b := rand.Intn(100)
        label := make(prometheus.Labels, 0)
        if b <= 50 {

            label["code"] = "12"
            label["method"] = c.Request.Method
            label["path"] = c.Request.URL.Path
            WebRequestTotal.With(label).Inc()
        } else {
            label["code"] = "0"
            label["method"] = c.Request.Method
            label["path"] = c.Request.URL.Path
            WebRequestTotal.With(label).Inc()
        }
        //WebRequestTotal.WithLabelValues(c.Request.Method, c.Request.URL.Path).Inc()
        c.String(http.StatusOK, "OK")
    })
    r.POST("/update", func(c *gin.Context) {
        c.String(http.StatusOK, "OK")
    })
    r.Run(":8001") //运行绑定端口
}

func index(c *gin.Context) {
    fmt.Println("1111")
    c.JSON(200, gin.H{
        "message": "go!go!gono!yesyes",
    })
}

1、这边以测试http://localhost:8001/health这个接口为例,统计code不为0的概率是多少
2、需要开放/metrics指标接口,prometheus通过这个接口采集数据到prometheus中时序数据库中,如下显示

image.png

3、需要把项目的域名写入到prometheus.yml中的static_configs->targets中,如下,prometheus就会自动采集这个域名中的指标数据


image.png

然后重启prometheus

配置grafana
配置数据源

点击 设置->data sources


image.png

填写prometheus的域名,如下


image.png
配置监控面板

点击右上角的add panel-> add a new panel


image.png

如下,编写对应的promQl查询语句

sum(increase(web_reqeust_total{path="/health", code!="0"}[5m])) /
sum(increase(web_reqeust_total{path="/health"}[5m])) > 0 or on() vector(0)

并且开启Visibility和设置对应的Values为Mean、Min和Max


image.png

这样就能统计出每个时间段对应的请求的http错误率了,这边的错误率是通过code来判断,code非0的请求数/总共的请求数

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容