Prometheus 简介

架构

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

image

组件介绍:

  • 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/prometheus.yml:/etc/prometheus/prometheus.yml -v ~/prometheus/data:/prometheus prom/prometheus

# 容器中默认的启动命令
CMD        [ "--config.file=/etc/prometheus/prometheus.yml", \
             "--storage.tsdb.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 命名最佳实践

jobs and instances

在 Prometheus 的术语中,一个可以 scrape 的节点成为一个 instance, 一个 job 中有多个 instance,例如:

job: api-server
  instance1: 1.2.3.4:5670
  instance2: 1.2.3.4:5671

job, instance 会自动地以 label 的形式添加到时间序列的数据中:

up{job="<job-name>", instance="<instance-id>"}: 1

同时 prometheus 的 target 实例都会包含一些默认的标签:

一般来说,Target以 __ 作为前置的标签是在系统内部使用的,因此这些标签不会被写入到样本数据中

  • __address__: prometheus scrape target 的地址 <host>:<port>
  • __scheme__: scrape 的协议
  • __metrics_path__: scrape 的路径
  • __param_<name>:scrape 的请求参数

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 Recoding rules

Prometheus 支持两种不同类型的 rule, 一种是 Recoding rules, 另一种是 Alert rules

Recoding rules 用来优化性能,通常在使用联邦集群时,中心的 Prometheus 接收各个 Prometheus 聚合后的数据,详情见:https://www.robustperception.io/federation-what-is-it-good-for

但某些 PromQL 比较复杂且计算量较大时,直接使用 PromQL 可能会导致 Prometheus 响应超时的情况,这是就使用 Recoding rules 在后台将计算聚合过的数据存入数据库中,查询时直接使用这些聚合后的数据。

groups:
 - name: example
   rules:
   - record: job:http_inprogress_requests:sum
     expr: sum(http_inprogess_requests) by (job)

计算的频率在 global.evaluation_interval 中定义:

global:
  [ evaluation_interval: <duration> | default = 1m ]

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)",
  }

reload

允许通过 Web 的方式:--web.enable-lifecycle

POST /-/reload

参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容