小战prometheus

2023-05-15 ⏳1.8分钟(0.7千字)

最近因为业务上的某些延迟问题,需要去监控envoy代理的状态。发现无论是做业务开发还是基础组件,都绕不开Observability.官方的定义是:通过系统外部的输出来衡量其内部的状态的一种方法。尤其是envoy这种黑盒。如果没有Observability,开发人员根本难以定位其中的问题。常规理解来说,Observability就是关于日志(logs), 指标(metrics)和跟踪(traces)的互相组合,让系统更可见。

本次就简单聊一聊 prometheus1 . 基于时间序列的聚合观测工具。

指标类型

counter 只增不减的计数器

type Counter interface {
 Metric
 Collector
 // Inc increments the counter by 1. Use Add to  increment it by arbitrary
 // non-negative values.
 Inc()
 // Add adds the given value to the counter. It  panics if the value is <
 // 0.
 Add(float64)
}

常用的指标比如:

http_requests_total 接口请求数.

连接池的hit/miss 计数器。

常用的函数比如:

rate() 获取增长率

topk() 获取前10

gauge 可增可减的仪表盘

type Gauge interface {
        Metric
        Collector

        // Set sets the Gauge to an arbitrary value.
        Set(float64)
        // Inc increments the Gauge by 1. Use Add to increment it by arbitrary
        // values.
        Inc()
        // Dec decrements the Gauge by 1. Use Sub to decrement it by arbitrary
        // values.
        Dec()
        // Add adds the given value to the Gauge. (The value can be negative,
        // resulting in a decrease of the Gauge.)
        Add(float64)
        // Sub subtracts the given value from the Gauge. (The value can be
        // negative, resulting in an increase of the Gauge.)
        Sub(float64)

        // SetToCurrentTime sets the Gauge to the current Unix time in seconds.
        SetToCurrentTime()
}

常用的指标比如:

go_goroutines 协程数.

go_memstats_heap_alloc_bytes 当前堆内存字节数。

常用的函数比如:

delta() 获取变化率

histogram(直方图) & summary(摘要) 分析数据分布情况

type Histogram interface {
        Metric
        Collector

        // Observe adds a single observation to the histogram.
        Observe(float64)
}
type Summary interface {
        Metric
        Collector

        // Observe adds a single observation to the summary.
        Observe(float64)
}

histogram & summary 都是用来统计某些离散指标的分布情况比如 响应时间/使用频率。需要设置bucket,可以分析出是平均问题或者是长尾问题。两者都会反应当前指标的记录的总数(以_count作为后缀)以及其值的总量(以_sum作为后缀)。不同的是histogram会将每个区间的数量纪录下来,而summary是直接展示分位信息。histogram是可以聚合的,summary是无法聚合的。

常用的指标比如: duration_seconds 耗时.

举例:summary

// summary
duration_seconds{quantile="0"} 0.000200695
duration_seconds{quantile="0.25"} 0.000397052
duration_seconds{quantile="0.5"} 0.000785441
duration_seconds{quantile="0.75"} 0.000931322
duration_seconds{quantile="1"} 0.006186776
duration_seconds_sum 2.445380113
duration_seconds_count 3001

// histogram
durations_seconds_bucket{le="0.005"} 3.156806e+06
durations_seconds_bucket{le="0.01"} 3.204874e+06
durations_seconds_bucket{le="0.025"} 3.249096e+06
durations_seconds_bucket{le="0.05"} 3.255064e+06
durations_seconds_bucket{le="0.1"} 3.255715e+06
durations_seconds_bucket{le="0.25"} 3.255736e+06
durations_seconds_bucket{le="0.5"} 3.255738e+06
durations_seconds_bucket{le="1"} 3.255738e+06
durations_seconds_bucket{le="+Inf"} 3.255738e+06
durations_seconds_sum{} 2426.07721706609
durations_seconds_count{} 3.255738e+06

4个黄金指标2

Google针对大量分布式监控的经验总结,提出了四个黄金指标,可以在服务级别帮助衡量终端用户体验、服务中断、业务影响等层面的问题。原文可以直接查看The Four Golden Signals

业务实践

各path的QPS值:

sum(rate(durations_seconds_count{}[2m])) by (path)

各path的耗时:

avg(increase(durations_seconds_sum{}[2m])/ increase(durations_seconds_count{}[2m])>0 ) by (path)

各实例的GoRoutine数量:

avg(go_goroutines{}) by (instance)

GC频率:

sum(rate(go_gc_duration_seconds_count{}[2m]))

GC耗时:

avg(go_gc_duration_seconds{quantile="0.75"})

99分位耗时:

histogram_quantile(0.99, sum(rate(durations_seconds_bucket[2m])) by (le))

连接数:

avg(redis_connections_total{}) by (instance)

小结

Prometheus还有多样的生态圈。我们在业务中实践的也许只是冰山一角。比如之前在做海外项目时,因为某些耗时问题,想要去尝试的Prometheus Push Gateway的方式让客户端主动推送指标等等等。关于Prometheus,我们还需要了解的更多。