从零开始学习Prometheus监控报警系统

2022-03-10  本文已影响0人  万猫学社

Prometheus简介

Prometheus是一个开源的监控报警系统,它最初由SoundCloud开发。

2016年,Prometheus被纳入了由谷歌发起的Linux基金会旗下的云原生基金会( Cloud Native Computing Foundation),并成为仅次于Kubernetes的第二大开源项目。自此,它成为了一个独立的开源项目,独立于任何公司进行维护。

Prometheus拥有非常活跃的开发人员和用户社区,目前在GitHub上已拥有三万多的Star。

Prometheus特点

Prometheus架构

Prometheus生态圈由多个组件构成,其中许多组件是可选的:

下面这张图展示了Prometheus的架构和各个组件是如何交互和协作的:

Prometheus架构图

其大概的工作流程是:

  1. Prometheus Server直接从HTTP接口或者Push Gateway拉取指标(Metric)数据。
  2. Prometheus Server在本地存储所有采集的指标(Metric)数据,并在这些数据上运行规则,从现有数据中聚合和记录新的时间序列,或者生成告警。
  3. Alertmanager根据配置文件,对接收到的告警进行处理,发出报警。
  4. 在Grafana或其他API客户端中,可视化收集的数据。

Prometheus数据模型

Prometheus会将所有采集到的监控数据以时间序列的方式保存在内存数据库中,并且定时保存到硬盘上。每一条数据由以下三部分组成:

其中,指标(Metric)通过如下格式标识:

<指标名称>{<标签名称>=<标签值>, ...}

指标名称(Metric Name)可以反映被监控数据的含义。指标名称只能由ASCII字符、数字、下划线以及冒号组成并必须符合正则表达式[a-zA-Z_:][a-zA-Z0-9_:]*
标签(Label)反映了当前数据的特征维度,通过这些维度Prometheus可以对数据进行过滤,聚合等操作。标签的名称只能由ASCII字符、数字以及下划线组成并满足正则表达式[a-zA-Z_][a-zA-Z0-9_]*

比如:

prometheus_http_requests_total{code="200",handler="/metrics"}

指标类型

Prometheus定义了4种不同的指标类型(Metric Type):

Counter(计数器)

Counter类型和计数器一样,只增不减(除非系统发生重置),一般在定义Counter类型指标的名称时推荐使用_total作为后缀。

比如,Prometheus Server中prometheus_http_requests_total, 表示Prometheus处理的HTTP请求总数:

# HELP prometheus_http_requests_total Counter of HTTP requests.
# TYPE prometheus_http_requests_total counter
prometheus_http_requests_total{code="200",handler="/api/v1/label/:name/values"} 3
prometheus_http_requests_total{code="200",handler="/api/v1/query"} 5
prometheus_http_requests_total{code="200",handler="/api/v1/query_range"} 15
prometheus_http_requests_total{code="200",handler="/graph"} 3
prometheus_http_requests_total{code="200",handler="/metrics"} 23
prometheus_http_requests_total{code="200",handler="/static/*filepath"} 18
prometheus_http_requests_total{code="302",handler="/"} 1

Gauge(仪表盘)

Gauge类型侧重于反应系统的某一个瞬时的值,这类指标的数据可增可减。

比如,Prometheus Server中go_threads, 表示Prometheus当前go线程的数量:

# HELP go_threads Number of OS threads created.
# TYPE go_threads gauge
go_threads 13

Histogram(直方图)

Histogram类型由<basename>_bucket{le="<upper inclusive bound>"},<basename>_bucket{le="+Inf"}, <basename>_sum,<basename>_count 组成,主要用于表示一段时间范围内对数据进行采样,并能够对其指定区间以及总数进行统计,通常它采集的数据展示为直方图。

比如,Prometheus Server中prometheus_http_response_size_bytes:

# HELP prometheus_http_response_size_bytes Histogram of response size for HTTP requests.
# TYPE prometheus_http_response_size_bytes histogram
prometheus_http_response_size_bytes_bucket{handler="/",le="100"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1000"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="10000"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="100000"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+06"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+07"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+08"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+09"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="+Inf"} 1
prometheus_http_response_size_bytes_sum{handler="/"} 29
prometheus_http_response_size_bytes_count{handler="/"} 1

Summary(摘要)

Summary类型由 <basename>{quantile="<φ>"},<basename>_sum,<basename>_count 组成,主要用于表示一段时间内数据采样结果,它直接存储了分位数据,而不是根据统计区间计算出来的。

比如,Prometheus Server中prometheus_target_interval_length_seconds:

# HELP prometheus_target_interval_length_seconds Actual intervals between scrapes.
# TYPE prometheus_target_interval_length_seconds summary
prometheus_target_interval_length_seconds{interval="15s",quantile="0.01"} 14.9986249
prometheus_target_interval_length_seconds{interval="15s",quantile="0.05"} 14.998999
prometheus_target_interval_length_seconds{interval="15s",quantile="0.5"} 15.0000428
prometheus_target_interval_length_seconds{interval="15s",quantile="0.9"} 15.0012009
prometheus_target_interval_length_seconds{interval="15s",quantile="0.99"} 15.0016468
prometheus_target_interval_length_seconds_sum{interval="15s"} 315.0013755
prometheus_target_interval_length_seconds_count{interval="15s"} 21

安装Prometheus Server

从官方网站(https://prometheus.io/download/)上找到最新版本的Prometheus Sevrer软件包,如下图:

根据自己的系统下载对应的压缩包,这里以Windows为例,下载prometheus-2.19.0.windows-amd64.tar.gz。

解压后当前目录会包含默认的Prometheus配置文件promethes.yml:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']

暂且不做修改,双击prometheus.exe即可启动,如下图:

访问http://localhost:9090/graph,就可以看到Prometheus自身的监控数据:

尾声

Prometheus的大致介绍已经告一段落了,但是只是万里长征的第一步,Prometheus的更多强大功能和使用方法还等待我们去挖掘。


竟然已经看到这里了,你我定是有缘人,留下你的点赞关注,他日必成大器。

上一篇 下一篇

猜你喜欢

热点阅读