美文网首页
prometheus原理和使用分享

prometheus原理和使用分享

作者: robertzhai | 来源:发表于2022-09-24 21:19 被阅读0次

    一、什么是prometheus

    1、prometheus是由SoundCloud开源监控解决方案,目前是CNCF(云原生计算基金会)的成员,是新一代的开源解决方案,很多理念与Google SRE运维之道不谋而合。
    
    2、prometheus(普罗米修斯)是古希腊的一个神明,名字的意思是「先见之明」。从它的名字可以看出,Prometheus 是做「先见之明」的监控告警用途。维基百科简单写了它的作用:Prometheus is a free software application used for event monitoring and alerting(Prometheus 是用来监控、报警的免费软件)
    

    二、为什么要用prometheus

    1、 prometheus 其实就是一个数据监控解决方案,它能帮你简单快速地搭建起一套可视化的监控系统。

    2、对于运维人员来说,他们需要监控机器的 CPU、内存、硬盘的使用情况,以此来保证运行在机器上的应用的稳定性。
    
         对于研发人员来说,他们关注某个异常指标的变化情况,从而来保证业务的稳定运行。
    
         对于产品或运营来说,他们更关心产品层面的事情,例如:某个活动参加人数的增长情况,活动积分的发放情况。
    
     3、除了数据收集、告警功能之外,Prometheus 还有很多强大的功能,例如:强大的 ProQL 查询、许多客户端库等。
    
     4、与 Grafana等 配套使用可以呈现出非常多样化的图表配置。对于中小规模的团队来说,可以极大地减少成本,加快研发速度
    

    三、prometheus的4种时序数据

     1、Counter: 计数器,累计量 如:每秒请求数,错误数
    
     2、Gauge:  度量器 ,瞬时量与时间无关,当前进程的goroutine数
    
     3、Histogram: 直方图,Prometheus 会根据配置的 Bucket 来计算样本的分布情况,后期可以再加工,一般多用于耗时的监控,通过 Histogram 可以计算出 P99/P95/P50等耗时,同时也可以监控处理的个数,如果用上 Histogram 就不需要再用 Counter 统计个数。可以用 Histogram 来监控接口响应时间/数据库访问耗时等。
    
     4、 Summary:摘要,和 Histogram 有一点类似,也是计算样本的分布情况,区别是 Summary 会在客户端计算出分布情况(P99/P95/Sum/Count),因此也会更占客户端资源,后期不可再聚合计算处理,同样可以用 Summary 来监控接口响应时间/数据库访问耗时等。
    

    四、安装

       1、[https://prometheus.io/download/](https://prometheus.io/download/)
       2、mkdir -p /Users/data/promethus
       3、tar zxvf prometheus-2.37.0.darwin-amd64.tar.gz
       4、 cd prometheus-2.37.0.darwin-amd64
           vim prometheus.yml
    
        ![image.png](https://img.haomeiwen.com/i3840925/9d8acb22306fd580.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    
         ./prometheus --config.file=prometheus.yml
    
           ![image.png](https://img.haomeiwen.com/i3840925/bcbdcdcd26871bdb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    
    5、启动go 服务
    
         ![image.png](https://img.haomeiwen.com/i3840925/72f27b193c195c80.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    
     6、[http://localhost:9090/graph](http://localhost:9090/graph)   切换到graph tab 输入 go_goroutines
    
    image.png

    可以看到一条曲线,和下方的 go_goroutines{instance="localhost:6060", job="golang-server"}

    7、Grafana 配置数据源

    (1) Grafana 是一个用来展示各种各样数据的开源软件。

    (2) 我们只需要在 Grafana 上配置一个 Prometheus 的数据源。接着我们就可以配置各种图表,Grafana 就会自动去 Prometheus 拉取数据进行展示。

    (3) 参考:https://www.modb.pro/db/179825

    下载后执行 bin/grafana-server web 启动 grafana, 访问 http://localhost:3000/ 默认用户名为admin,默认密码为admin

    image.png

    五、原理

    1、整体架构

       收集数据=》处理数据=》可视化展示&分析&报警
    
    image.png

    2、pull

    image.png
    image.png image.png

    3、触发告警:

    image.png

    4、PromQL 查询:

    image.png

    4、local storage(TSDB)

    • --storage.tsdb.path: Where Prometheus writes its database. Defaults to data/.
    • --storage.tsdb.retention.time: When to remove old data. Defaults to 15d. Overrides storage.tsdb.retention if this flag is set to anything other than default.
    • --storage.tsdb.retention.size: The maximum number of bytes of storage blocks to retain. The oldest data will be removed first. Defaults to 0 or disabled. Units supported: B, KB, MB, GB, TB, PB, EB. Ex: "512MB". Based on powers-of-2, so 1KB is 1024B. Only the persistent blocks are deleted to honor this retention although WAL and m-mapped chunks are counted in the total size. So the minimum requirement for the disk is the peak space taken by the wal (the WAL and Checkpoint) and chunks_head (m-mapped Head chunks) directory combined (peaks every 2 hours).
    • --storage.tsdb.retention: Deprecated in favor of storage.tsdb.retention.time.
    • --storage.tsdb.wal-compression: Enables compression of the write-ahead log (WAL). Depending on your data, you can expect the WAL size to be halved with little extra cpu load. This flag was introduced in 2.11.0 and enabled by default in 2.20.0. Note that once enabled, downgrading Prometheus to a version below 2.11.0 will require deleting the WAL.

    存储目录结构

    ./data
    ├── 01BKGV7JBM69T2G1BGBGM6KB12
    │   └── meta.json
    ├── 01BKGTZQ1SYQJTR4PB43C8PD98
    │   ├── chunks
    │   │   └── 000001
    │   ├── tombstones
    │   ├── index
    │   └── meta.json
    ├── 01BKGTZQ1HHWHV8FBJXW1Y3W0K
    │   └── meta.json
    ├── 01BKGV7JC0RY8A6MACW02A2PJD
    │   ├── chunks
    │   │   └── 000001
    │   ├── tombstones
    │   ├── index
    │   └── meta.json
    ├── chunks_head
    │   └── 000001
    └── wal
        ├── 000000002
        └── checkpoint.00000001
            └── 00000000
    

    5、remote read /write

    image.png

    六、使用

    1、main 函数启动一个goroutine 来启动一个端口来作为 target 的 metrics http 6060端口, 供promethus 来拉取数据, 引入sdk https://github.com/prometheus/client_golang

    // AddPrometheus将启动promethues监控功能,这样就能收集metrics信息了
    func AddPrometheus() {
       go func() {
          defer func() {
             if err := recover(); err != nil {
                zap_logger.Errorf(nil, logTag, "prometheus客户端出现了问题 %+v", err)
             }
          }()
          server := http.NewServeMux()
          // start an http server using the mux server
          // register a new handler for the /metrics endpoint
          server.Handle("/metrics", promhttp.Handler())
          // start an http server using the mux server
          zap_logger.Errorf(nil, logTag, "prometheus error:%v", http.ListenAndServe(":6060", server))
       }()
    }
    

    2、counter demo

    package main
    
    import (
       "net/http"
       "time"
    
       "github.com/prometheus/client_golang/prometheus"
       "github.com/prometheus/client_golang/prometheus/promauto"
       "github.com/prometheus/client_golang/prometheus/promhttp"
    )
    
    func startCounter() {
       go func() {
          for {
             myCounter1.Inc()
             myCounter2.Inc()
             time.Sleep(2 * time.Second)
          }
       }()
    }
    
    var (
       myCounter1 = promauto.NewCounter(prometheus.CounterOpts{
          Name: "my_counter_1",
          Help: "The total number of counter ",
       })
       myCounter2 = promauto.NewCounter(prometheus.CounterOpts{
          Name: "my_counter_2",
          Help: "The total number of counter ",
       })
    )
    
    func main() {
       startCounter()
       http.Handle("/metrics", promhttp.Handler())
       http.ListenAndServe(":2112", nil)
    }
    

    访问:http://localhost:6060/metrics

    image.png

    参考:

    相关文章

      网友评论

          本文标题:prometheus原理和使用分享

          本文链接:https://www.haomeiwen.com/subject/rcenortx.html