本教程是相似"hello,world"的教程,展现怎样在一个简单地例子中安装、配置和使用Prometheus。你将下载和本地化运行Prometheus服务,并写一个配置文件,监控Prometheus服务自己和一个简单的应用,而后配合使用query、rules和graphs展现收集的时间序列数据。git
下载Prometheus最新的发布版本,而后提取和运行它:github
tar zxvf prometheus-*.tar.gz
cd prometheus-*
复制代码
在开始启动Prometheus以前,咱们要配置它golang
Prometheus从监控的目标上经过http方式拉取指标数据,它也能够拉取自身服务数据并监控自身的健康情况。shell
固然Prometheus服务拉取自身服务数据,并无多大的用处,可是它是一个好的开始例子。保存下面的基本Prometheus配置,并命名为:prometheus.yml
:express
global:
scrape_interval: 15s # 默认状况下,每15s拉取一次目标采样点数据。
# 咱们能够附加一些指定标签到采样点度量标签列表中, 用于和第三方系统进行通讯, 包括:federation, remote storage, Alertmanager
external_labels:
monitor: 'codelab-monitor'
# 下面就是拉取自身服务数据配置
scrape_configs:
# job名称会增长到拉取到的全部采样点上,同时还有一个instance目标服务的host:port标签也会增长到采样点上
- job_name: 'prometheus'
# 覆盖global的采样点,拉取时间间隔5s
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
复制代码
对于一个完整的配置选项,请见配置文档浏览器
要使用新建立的配置文件启动Prometheus,请切换到包含Prometheus二进制文件的目录并运行:bash
./prometheus --config.file=prometheus.yml
复制代码
Prometheus服务应该启动了。你能够在浏览器上输入:http://localhost:9090
, 给它几秒钟从本身的HTTP指标端点收集有关自身的数据。dom
您还能够经过导航到其指标端点来验证Prometheus是否正在提供有关自身的指标:http://localhost:9090/metrics
ide
让咱们试着看一下Prometheus收集的关于本身的一些数据。 使用Prometheus的内置表达式浏览器,导航到http://localhost:9090/graph
,并选择带有"Graph"的"Console".测试
在http://localhost:9090/gmetrics
中收集中,有一个metric叫prometheus_target_interval_length_seconds
(从目标收集数据的实际时间量),在表达式的console中输入:
prometheus_target_interval_length_seconds
复制代码
这个应该会返回不少不一样的时间序列数据(以及每一个记录的最新值),这些度量名称都是prometheus_target_interval_length_seconds
,可是带有不一样的标签列表值,这些标签列表值指定了不一样的延迟百分比和目标组间隔。
若是咱们仅仅对99%的延迟感兴趣,则咱们可使用下面的查询去清洗信息:
prometheus_target_interval_length_seconds{quantile="0.99"}
复制代码
为了统计返回时间序列数据个数,你能够写:
count(prometheus_target_interval_length_seconds)
复制代码
有关更多的表达式语言,请见表达式语言文档
见图表表达式,导航到http://localhost:9090/graph
, 而后使用"Graph" tab
例如,输入如下表达式来绘制在自我抓取的Prometheus中建立的每秒块速率:
rate(prometheus_tsdb_head_chunks_created_total[1m])
复制代码
试验graph范围参数和其余设置。
让咱们让这个更有趣,并开始一些示例目标,让Prometheus抓取。
Go客户端库包含一个示例,该示例为具备不一样延迟分布的三个服务导出虚构的RPC延迟。
确保已安装Go编译器并设置了正常工做的Go构建环境(具备正确的GOPATH)。
下载Prometheus的Go客户端,运行三个服务:
git clone https://github.com/prometheus/client_golang.git
cd client_golang/examples/random
go get -d
go build
## 启动三个服务
./random -listen-address=:8080
./random -listen-address=:8081
./random -listen-address=:8082
复制代码
如今你在浏览器输入:http://localhost:8080/metrics
, http://localhost:8081/metrics
, http://localhost:8082/metrics
, 能看到全部采集到的采样点数据。
如今咱们将会配置Prometheus,拉取三个目标服务的采样点。咱们把这三个目标服务组成一个job, 叫example-radom
。 然而,想象成,前两个服务是生产环境服务,后者是测试环境服务。咱们能够经过group标签分组,要在Prometheus中对此进行建模,咱们能够将多组端点添加到单个做业中,为每组目标添加额外的标签。在此示例中,咱们将group ="production"
标签添加到第一组目标,同时将group ="canary"
添加到第二组。
要实现此目的,请将如下做业定义添加到prometheus.yml中的scrape_configs部分,而后从新启动Prometheus实例:
scrape_configs:
- job_name: 'example-random'
scrape_interval: 5s
static_configs:
- targets: ['localhost:8080', 'localhost:8081']
labels:
group: 'production'
- targets: ['localhost:8082']
labels:
group: 'test'
复制代码
转到表达式浏览器并验证Prometheus如今是否有关于这些示例端点公开的时间序列的信息,例如rpc_durations_seconds
指标。
虽然在咱们的示例中不是问题,可是在计算ad-hoc时,聚合了数千个时间序列的查询会变慢。 为了提升效率,Prometheus容许您经过配置的录制规则将表达式预先记录到全新的持久时间序列中。 假设咱们感兴趣的是记录在5分钟窗口内测量的全部实例(但保留做业和服务维度)的平均示例RPC(rpc_durations_seconds_count
)的每秒速率。 咱们能够这样写:
avg(rate(rpc_durations_seconds_count[5m])) by (job, service)
复制代码
要将此表达式生成的时间序列记录到名为job_service:rpc_durations_seconds_count:avg_rate5m
的新度量标准中,请使用如下记录规则建立一个文件并将其另存为prometheus.rules.yml
:
groups:
- name: example
rules:
- record: job_service:rpc_durations_seconds_count:avg_rate5m
expr: avg(rate(rpc_durations_seconds_count[5m])) by (job, service)
复制代码
要使Prometheus选择此新规则,请在prometheus.yml
中添加rule_files
语句。 配置如今应该以下所示:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # Evaluate rules every 15 seconds.
# Attach these extra labels to all timeseries collected by this Prometheus instance.
external_labels:
monitor: 'codelab-monitor'
rule_files:
- 'prometheus.rules.yml'
scrape_configs:
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
- job_name: 'example-random'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:8080', 'localhost:8081']
labels:
group: 'production'
- targets: ['localhost:8082']
labels:
group: 'canary'
复制代码
使用新配置从新启动Prometheus,并经过表达式浏览器查询或绘制图表,验证带有度量标准名称job_service:rpc_durations_seconds_count:avg_rate5m
的新时间序列如今可用。
Prometheus官网地址:prometheus.io/
个人Github:github.com/Alrights/pr…