美文网首页监控
Prometheus 简介

Prometheus 简介

作者: 猴子精h | 来源:发表于2017-05-26 12:12 被阅读704次

    架构

    Prometheus 是一个开源的监控框架,它通过不同的组件完成数据的采集,数据的存储,告警,其中Prometheus server只提供了数据存储(time series data),数据的处理(提供了丰富的查询语法[查询,统计,聚合等等]),数据则通过众多的插件(prometheus称之为exporters)来暴露一个http服务的接口给Prometheus定时抓取, 告警则通过Altermanger。

    prometheus.png

    组件介绍:

    • Prometheus server 包含数据采集scrapes job, stores time series data;
    • push gateway : Prometheus server的一个代理节点, 当一些节点没有提供HTTP endpoint时,可将数据push到代理节点,Prometheus会去代理节点获取数据;
    • exporters: 数据采集插件, 暴露一个http服务的接口给Prometheus server定时抓取;
    • alertmanager: 报警插件;

    部署

    Note that Prometheus by default uses around 3GB in memory. If you have a smaller machine, you can tune Prometheus to use less memory. For details, see the memory usage documentation.

    docker run -p 9090:9090 -d \
        -v ~/prometheus.yml:/etc/prometheus/prometheus.yml \
        -v ~/data:/prometheus \
        prom/prometheus
    
    # 容器中默认的启动命令
    /bin/prometheus -config.file=/etc/prometheus/prometheus.yml \
        -storage.local.path=/prometheus \
        -web.console.libraries=/usr/share/prometheus/console_libraries \
        -web.console.templates=/usr/share/prometheus/consoles 
    

    Prometheus 数据模型

    Prometheus所有的存储都是按时间序列去实现的,相同的metricslabel组成一条时间序列,不同的label表示不同的时间序列。为了支持一些查询,有时还会临时产生一些时间序列存储。

    <metric name>{<label name>=<label value>, ...} float64
    
    example:
    api_http_requests_total{method="POST", handler="/messages"} 10
    

    metrics 和 labels 命名最佳实践

    Prometheus Metrics 的类型

    counter

    • 用于累计,如:请求次数,任务完成数,错误发生次数;
    • 只增加,不减少
    • 重启进程后,会被重置

    Gauge

    • 常规数值,例如:温度变化,内存使用变化;
    • 可增加,可减少
    • 重启进程后,会被重置

    Histogram

    柱状图,常用于跟踪事件发生的规模,例如:请求耗时,相应大小。它会对记录的内容进行分组, 提供 count 和 sum 全部值的功能。如果定于一个 metrics 类型为 Histogram, 则 Prometheus 系统会自动生成三个对应的指标:

    • [metric_name]_bucket{le="上边界"},这个值为小于等于上边界的所有采样点数量。
    • [metric_name]_sum
    • [metric_name]_count

    下面来看一个 prometheus metrics 中的一个 histogram 类型的数据:

    # HELP prometheus_tsdb_compaction_chunk_range Final time range of chunks on their first compaction
    # TYPE prometheus_tsdb_compaction_chunk_range histogram
    prometheus_tsdb_compaction_chunk_range_bucket{le="100"} 0  # prometheus_tsdb_compaction_chunk_range 小于或等于100的次数
    prometheus_tsdb_compaction_chunk_range_bucket{le="400"} 0
    prometheus_tsdb_compaction_chunk_range_bucket{le="1600"} 0
    prometheus_tsdb_compaction_chunk_range_bucket{le="6400"} 0
    prometheus_tsdb_compaction_chunk_range_bucket{le="25600"} 80
    prometheus_tsdb_compaction_chunk_range_bucket{le="102400"} 974
    prometheus_tsdb_compaction_chunk_range_bucket{le="409600"} 1386
    prometheus_tsdb_compaction_chunk_range_bucket{le="1.6384e+06"} 112157
    prometheus_tsdb_compaction_chunk_range_bucket{le="6.5536e+06"} 821535
    prometheus_tsdb_compaction_chunk_range_bucket{le="2.62144e+07"} 821545
    prometheus_tsdb_compaction_chunk_range_bucket{le="+Inf"} 821545
    prometheus_tsdb_compaction_chunk_range_sum 1.334532601458e+12    
    prometheus_tsdb_compaction_chunk_range_count 821545
    

    这个直方图的 X 轴为 le 的值,Y 轴为次数;如果需要得到百分位数,可以使用 histogram_quantile() 函数:

    # 查询 prometheus_tsdb_compaction_chunk_range 95 百分位
    
    histogram_quantile(0.95, prometheus_tsdb_compaction_chunk_range_bucket)
    

    Summary

    • 跟Histogram类似,常用于跟踪事件发生的规模;
    • 它提供一个quantiles的功能,可以按%比划分跟踪的结果。例如:quantile取值0.95,表示取采样值里面的95%数据。

    还是来看一个 prometheus metrics 中的一个 Summary 类型的数据, 它与 histogram 的不同点是直接返回了 百分位的值:

    # HELP prometheus_tsdb_head_gc_duration_seconds Runtime of garbage collection in the head block.
    # TYPE prometheus_tsdb_head_gc_duration_seconds summary
    prometheus_tsdb_head_gc_duration_seconds{quantile="0.5"} 0.03043428
    prometheus_tsdb_head_gc_duration_seconds{quantile="0.9"} 0.03043428
    prometheus_tsdb_head_gc_duration_seconds{quantile="0.99"} 0.03043428
    prometheus_tsdb_head_gc_duration_seconds_sum 0.70902013
    prometheus_tsdb_head_gc_duration_seconds_count 11
    

    Prometheus alert rules

    ALERT <alert name>
      IF <expression>                # PromQL 查询的值
      [ FOR <duration> ]             # 触发告警的持续时间
      [ LABELS <label set> ]
      [ ANNOTATIONS <label set> ]
    
    example:
    # Alert for any instance that is unreachable for >5 minutes.
    ALERT InstanceDown
      IF up == 0
      FOR 5m
      LABELS { severity = "page" }
      ANNOTATIONS {
        summary = "Instance {{ $labels.instance }} down",
        description = "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes.",
      }
    
    # Alert for any instance that have a median request latency >1s.
    ALERT APIHighRequestLatency
      IF api_http_request_latencies_second{quantile="0.5"} > 1
      FOR 1m
      ANNOTATIONS {
        summary = "High request latency on {{ $labels.instance }}",
        description = "{{ $labels.instance }} has a median request latency above 1s (current value: {{ $value }}s)",
      }
    

    参考

    相关文章

      网友评论

        本文标题:Prometheus 简介

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