• GPE(Grafana+Prometheus+Exporter)项目实战之Golang篇(上)


    项目目标-为什么要搭建GPE(Grafana+Prometheus+Exporter)

    关注我的朋友知道,前几天开源了boot4go-gateway的项目,boot4go-gateway的http服务没有使用go net/http的原生http服务,使用的fasthttp改造后的http服务,在单一请求访问的情况下,fasthttp的性能秒杀go net/http,当然虽然go net/http性能已经比springboot系列的tomcat优秀不少,但是和fasthttp还是相形见绌了, 希望通过压力性能测试来对boot4go-gateway的性能以及并发负载能力进行测试,工欲善其事,必先利其器,压力测试里必不少的就是系统的监控体系,jmeter里也有机器采集的性能metrics,但是毕竟薄弱,metrics当然要选择最专业的GPE组合了。

    先来看看效果图

    这个仪表盘展示了有关golang程序的相关metrics,包括负载量,并非数,总请求数,失败请求数,内存变化,文件句柄变化,协程数,线程数,GC数量,alloc数量,这些能够反映一个golang程序的当前状况的关键性指标。

    如何把自己的golang集成到Prometheus里

    首先要了解Prometheus的运行方式, prometheus可以看作是一个TimeSerialDB时序数据库,prometheus产品系列里的TSDB,也是一个不错的好产品, prometheus的服务端通过pull待采集端的exporter的程序,将需要采集的数据,抓取到promethues服务端来, prometheus保存到自己的TSDB里, prometheus提供自己的ql语法,进行时序数据的查询分析和统计。

    所以要将golang集成到prometheus里,就是要找到合适的exporter即可。go-prometheus的客户端包,已经包含了一些有关prometheus的client接口, 要把自己的golang集成到prometheus里,

    下图就是prometheus里制定要进行抓取的exporter Job

    动手开干, 支持fasthttp

    go-prometheus支持的是标准的net/http库,golang里最有名的以快著称的是fasthttp。而prometheus没有支持fasthttp。 例如咱们的boot4go-gateway就是用fasthttp进行http服务实现的,要让fasthttp支持,就必须了解一下go-prometheus的exporter格式,自行来写了, 不过这里还好, boot4go-prometheus这个子项目,在go-prometheus的基础上,封装了fasthttp的支持, 由于是直接在go-prometheus提供的包进行封装改造的,不需要担心以后exporter协议升级的兼容问题,

    如果有朋友也是用fasthttp进行开发的,可以直接使用boot4go-prometheus这个包

    1. import (
    2. prometheusfasthttp "github.com/gohutool/boot4go-prometheus/fasthttp"
    3. )

    添加自己的指标metrics

    虽然go-prometheus包含了一些metrics,但是毕竟自己的golang有时必不可少的满足不到已有的metrics,就需要加入自己的指标

    网关转发数量

    1. TotalCounterVec = prometheus.NewCounterVec(
    2. prometheus.CounterOpts{
    3. Subsystem: "go",
    4. Name: "forward_total",
    5. Help: "Total number of host forward",
    6. },
    7. []string{"host"},

    进行网关转发中的并发请求数量

    1. ConcurrentRequestsGauge = prometheus.NewGauge(
    2. prometheus.GaugeOpts{
    3. Subsystem: "go",
    4. Name: "request_handle_concurrent",
    5. Help: "Number of incoming HTTP Requests handling concurrently now.",
    6. },
    7. )

    网关转发的延迟响应时间

    1. LatencyRequestsHistogram = prometheus.NewHistogramVec(
    2. prometheus.HistogramOpts{
    3. Subsystem: "go",
    4. Name: "request_handle_latency",
    5. Help: "Histogram statistics of http(s) requests latency second",
    6. Buckets: []float64{0.01, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30},
    7. },
    8. []string{"code"},
    9. )

    通过boot4go-prometheus集成fasthttp

    fasthttp目前不支持prometheus的集成,所以在做boot4go-gateway的过程中,把这块的东西单独拿出来,作为一个golang的独立包项目,该项目地址在

    https://github.com/gohutool/boot4go-prometheus

    注册采集器

    1. prometheus.MustRegister(ConcurrentRequestsGauge)
    2. prometheus.MustRegister(LatencyRequestsHistogram)
    3. prometheus.MustRegister(TotalCounterVec)

    加入RequestHandler

    1. return prometheusfasthttp.RequestCounterHandler(func(ctx *fasthttp.RequestCtx) {
    2. host := string(ctx.Host())
    3. if string(ctx.Path()) == "/metrics" {
    4. prometheusfasthttp.PrometheusHandler(prometheusfasthttp.HandlerOpts{})(ctx)
    5. return
    6. } else {
    7. if next != nil {
    8. TotalCounterVec.WithLabelValues(host).Inc()
    9. startTime := time.Now()
    10. ConcurrentRequestsGauge.Inc()
    11. defer ConcurrentRequestsGauge.Dec()
    12. next(ctx)
    13. finishTime := time.Now()
    14. LatencyRequestsHistogram.WithLabelValues(
    15. strconv.Itoa(ctx.Response.StatusCode())).Observe(float64(finishTime.Sub(startTime) / time.Second))
    16. }
    17. }
    18. })

    程序方面就差不多了,下面就到重头戏,Grafana+Prometheus了

    作为开发人员的我,其实对这些运维工具是特别有兴趣的, 这些工具的集成和搭配以及选型,才是多元化的,才正在的有味道。

    下一期,将接着实战后面的grafana和prometheus的安装配置

  • 相关阅读:
    Cairo介绍及源码构建安装(2)
    如何低成本运营独立站
    隧道未来如何发展?路网全息感知,颠覆公路交通安全史
    【编译原理】类型检查
    向量组的秩、矩阵的秩
    【JavaScript高级】手写apply()、call()、bind()
    Kafka系列之:深入理解Kafka Connect REST API
    Linux内存管理(二十一):LRU 第二次机会法
    计算机毕业设计ssm+vue基本微信小程序的好物推荐分享系统
    DPDK Ring
  • 原文地址:https://blog.csdn.net/inthirties/article/details/124923659