快速开始

译者:詹叶

本文类似 “Hello World” 的向导,教你怎么安装,配置,并且用一个简单的例子演示如何使用 Prometheus。你可以在本地下载并运行 Prometheus,配置以采集自身和示例应用的运行数据,然后使用查询语句,规则和图形来使用收集到的时间序列数据。

下载和运行 Prometheus

为你的平台下载最新版本的 Prometheus,执行以下命令解压:

  1. tar xvfz prometheus-*.tar.gz
  2. cd prometheus-*

在启动 Prometheus 之前,我们先做一些配置。

配置 Prometheus 来监控自己

Prometheus 通过在目标节点的 HTTP 端口上采集 metrics(遥测专用词,度量指标)来监控目标节点(以下会称为“采样目标”)。因为 Prometheus 也以相同的方式暴露自己的数据,所以他也可以采集和检查自己的健康状况。

虽然在生产实践中 Prometheus 服务器只收集自己的数据没多大作用,但是这是个不错的入门示例。保存以下基础配置到文件 prometheus.yml 中:

  1. global:
  2. scrape_interval: 15s # By default, scrape targets every 15 seconds.
  3. # Attach these labels to any time series or alerts when communicating with
  4. # external systems (federation, remote storage, Alertmanager).
  5. external_labels:
  6. monitor: 'codelab-monitor'
  7. # A scrape configuration containing exactly one endpoint to scrape:
  8. # Here it's Prometheus itself.
  9. scrape_configs:
  10. # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  11. - job_name: 'prometheus'
  12. # Override the global default and scrape targets from this job every 5 seconds.
  13. scrape_interval: 5s
  14. static_configs:
  15. - targets: ['localhost:9090']

完整配置选项说明,请查看配置文档

启动 Prometheus

使用上一步创建的配置文件启动 Prometheus,修改以下命令为你的平台中 Prometheus 二进制文件所在路径,执行命令启动:

  1. # Start Prometheus.
  2. # By default, Prometheus stores its database in ./data (flag --storage.tsdb.path).
  3. ./prometheus --config.file=prometheus.yml

此时 Prometheus 应该启动了。你应该也可以通过浏览器打开状态页面 localhost:9090。稍等几秒钟 Prometheus 就可以从自己的 HTTP metrics 端点收集自己的数据。

你也可以访问 metrics 端点 localhost:9090/metrics 验证 Prometheus 是否正在提供 metrics 服务。

使用表达式浏览器

让我们来看看 Prometheus 已经收集到的自己的 metrics 数据。为了使用 Prometheus 内置的表达式浏览器,访问 http://localhost:9090/graph 选择”Graph” 标签页中的 “Console” 视图

正如你可以从 localhost:9090/metrics 收集 metrics,Prometheus 暴露的一个度量指标称为 prometheus_target_interval_length_seconds(目标擦除之间的实际时间量)。继续在表达式控制台输入:

  1. prometheus_target_interval_length_seconds

此时应该返回许多不同的时间序列(以及每条记录的最新值),所有时间序列都有 metric 名称 prometheus_target_interval_length_seconds ,但具有不同的标签。这些标签指定不同延迟百分比和目标组间隔。

如果我们只关心99%延迟,可以使用以下查询语句来检索信息:

  1. prometheus_target_interval_length_seconds{quantile="0.99"}

要计算返回的时间序列数量,可以输入:

  1. count(prometheus_target_interval_length_seconds)

更多表达式语言,请看 expression language documentation

使用绘图接口

访问 http://localhost:9090/graph 并选择”Graph” 标签页,打开图形绘制界面

例如,输入以下表达式来绘制 Prometheus 自我采集每秒创建块的速率:

  1. rate(prometheus_tsdb_head_chunks_created_total[1m])

实验采用图形范围参数和其他设置。

启动一些示例应用

让我们玩点更有意思的东西,启动一些样例目标让 Prometheus 采集。

Go 客户端库包含一个示例,该示例为具有不同延迟分布的三个服务暴露 RPC 延迟。

确保你已经安装了 Go 语言编译器并且指定 Go 工作目录 (在环境变量中指定正确的 GOPATH)

下载 Prometheus 的 Go 客户端库并运行这三个示例:

  1. # Fetch the client library code and compile example.
  2. git clone https://github.com/prometheus/client_golang.git
  3. cd client_golang/examples/random
  4. go get -d
  5. go build
  6. # Start 3 example targets in separate terminals:
  7. ./random -listen-address=:8080
  8. ./random -listen-address=:8081
  9. ./random -listen-address=:8082

此时监听目标启动 http://localhost:8080/metrics, http://localhost:8081/metricshttp://localhost:8082/metrics

配置 Prometheus 来监控示例目标

现在我们配置 Prometheus 来采集这些新的目标。让我们将这三个目标分组到一个名为 example-random的作业。但是,假设前两个端点(即: http://localhost:8080/metrics, http://localhost:8081/metrics )都是生产级目标应用,第三个端点(即: http://localhost:8082/metrics )为金丝雀实例。要在 Prometheus 中对此进行建模,我们可以将多组端点添加到单个作业中,为每组目标添加额外的标签。 在此示例中,我们将 group =“production” 标签添加到第一组目标,同时将 group =“canary” 添加到第二组。

要实现此目的,请将以下作业定义添加到 prometheus.yml 中的 scrape_configs 部分,然后重新启动 Prometheus 实例:

  1. scrape_configs:
  2. - job_name: 'example-random'
  3. # Override the global default and scrape targets from this job every 5 seconds.
  4. scrape_interval: 5s
  5. static_configs:
  6. - targets: ['localhost:8080', 'localhost:8081']
  7. labels:
  8. group: 'production'
  9. - targets: ['localhost:8082']
  10. labels:
  11. group: 'canary'

转到表达式浏览器并验证 Prometheus 现在是否有关于这些示例端点公开的时间序列的信息,例如 rpc_durations_seconds 的 metric 指标。

配置规则聚合抓取的数据到新的时间序列

在计算ad-hoc时,聚合了上千个时间序列会使查询会变慢,虽然在我们的示例中不会有这样的问题。 为了提高效率,Prometheus允许您通过配置的录制规则将表达式预先记录到全新的持久时间序列中。假设我们关心的是记录在5分钟窗口内测量的所有实例(但保留作业和服务维度)的 RPC 的平均每秒速率(rpc_durations_seconds_count)。 我们可以这样写:

  1. avg(rate(rpc_durations_seconds_count[5m])) by (job, service)

尝试绘制此表达式。

要将此表达式生成的时间序列记录到名为 job_service:rpc_durations_seconds_count:avg_rate5m 的新的 metric 指标中,请使用以下记录规则创建一个文件并将其另存为prometheus.rules.yml

  1. groups:
  2. - name: example
  3. rules:
  4. - record: job_service:rpc_durations_seconds_count:avg_rate5m
  5. expr: avg(rate(rpc_durations_seconds_count[5m])) by (job, service)

要使 Prometheus 使用此新规则,需要在 prometheus.yml 中添加 rule_files 语句。 配置现在应该如下所示:

  1. global:
  2. scrape_interval: 15s # By default, scrape targets every 15 seconds.
  3. evaluation_interval: 15s # Evaluate rules every 15 seconds.
  4. # Attach these extra labels to all timeseries collected by this Prometheus instance.
  5. external_labels:
  6. monitor: 'codelab-monitor'
  7. rule_files:
  8. - 'prometheus.rules.yml'
  9. scrape_configs:
  10. - job_name: 'prometheus'
  11. # Override the global default and scrape targets from this job every 5 seconds.
  12. scrape_interval: 5s
  13. static_configs:
  14. - targets: ['localhost:9090']
  15. - job_name: 'example-random'
  16. # Override the global default and scrape targets from this job every 5 seconds.
  17. scrape_interval: 5s
  18. static_configs:
  19. - targets: ['localhost:8080', 'localhost:8081']
  20. labels:
  21. group: 'production'
  22. - targets: ['localhost:8082']
  23. labels:
  24. group: 'canary'

重启 Prometheus 是新配置生效,并通过表达式浏览器查询或绘制图表来验证带有新 metric 指标名称 job_service:rpc_durations_seconds_count:avg_rate5m 的新时间序列现在可用。