Prometheus配置

半兽人 发表于: 2019-07-24   最后更新时间: 2020-10-20 22:49:21  
{{totalSubscript}} 订阅, 4,788 游览

配置

Prometheus可以通过命令行参数和配置文件来配置它的服务参数。命令行主要用于配置系统参数(例如:存储的位置,保留在磁盘和内存中的数据量的大小等),配置文件主要用于配置与抓取任务和任务下的实例相关的所有内容, 并且加载指定的抓取规则file

可以通过运行prometheus -h命令, 查看Prometheus所有可用的命令行参数,

Prometheus服务可以reload它的配置。如果这个配置错误,则更改后的配置不生效。配置reolad是通过给Prometheus服务发送信号量SIGHUP或者通过http发送一个post请求到/-/reload。这也会重载所有配置的规则文件(rule files)。

配置文件(Configuration file)

使用-config.file命令行参数来指定Prometheus启动所需要的配置文件。

这个配置文件是YAML格式, 通过下面描述的范式定义, 括号表示参数是可选的。对于非列表参数,这个值被设置了默认值。

通用占位符由下面定义:

  • <boolean>: 一个布尔值,包括true或者false.
  • <duration>: 持续时间,与正则表达式[0-9]+(ms|smhdwy)匹配
  • <labelname>: 一个与正则表达式[a-zA-Z_][a-zA-Z0-9_]*匹配的字符串
  • <labelvalue>: 一个为unicode字符串
  • <filename>: 当前工作目录下的有效路径
  • <host>: 一个包含主机名或者IP地址,并且可以带上一个非必需的端口号的有效字符串
  • <path>: 一个有效的URL路径
  • <scheme>: 一个可以是http或者https的字符串
  • <string>: 一个正则表达式字符串

其他的占位符被分开指定:

一个有效的配置文件示例

全局配置指定的参数,在其他上下文配置中是生效的。这也默认这些全局参数在其他配置区域有效。

global:
  # 抓取目标实例的频率时间值,默认1分钟
  [ scrape_interval: <duration> | default = 1m ]

  # 一次抓取请求超时时间值,默认10s
  [ scrape_timeout: <duration> | default = 10s ]

  # 执行配置文件规则的频率时间值, 默认1m
  [ evaluation_interval: <duration> | default = 1m ]

  # 当和外部系统通信时(federation, remote storage, Alertmanager), 这些标签会增加到度量指标数据中
  external_labels:
    [ <labelname>: <labelvalue> ... ]

# 规则文件指定规则文件路径列表。规则和警报是从所有匹配的文件中读取的
rule_files:
  [ - <filepath_glob> ... ]

# 抓取配置的列表
scrape_configs:
  [ - <scrape_config> ... ]

# Alerting指定与Alertmanager相关的设置。
alerting:
  alert_relabel_configs:
    [ - <relabel_config> ... ]
  alertmanagers:
    [ - <alertmanager_config> ... ]

# 与远程写入功能相关的设置。
remote_write:
  [ - <remote_write> ... ]

# 与远程读取功能相关的设置。
remote_read:
  [ - <remote_read> ... ]

<scrape_config>

scrape_config 区域指定了一组目标和描述如何抓取(scrape)目标的参数。通常,一个抓取配置只能指定了一个job。在高级配置中,这可能会改变。

通过static_configs参数静态指定要监控的目标列表,或者使用一些服务发现机制动态的发现目标。

另外,relabel_configs允许在获取度量指标数据之前,对任何目标和它的标签进行进一步地修改。

# 默认下任务名称赋值给要抓取的度量指标
job_name: <job_name>

# 从这个任务中抓取目标的频率时间值
[ scrape_interval: <duration> | default= <global_config.scrape_interval>]

# 当抓取这个任务的所有目标时,超时时间值
[ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ]

# 从目标列表中抓取度量指标的http资源路径, 默认为/metrics
[ metrics_path: <path> | default = /metrics ]

# honor_labels controls how Prometheus handles conflicts between would labels that are already present in scraped data and labels that Prometheus would attach server-side ("job" and "instance" labels, manually configured target  labels, and labels generated by service discovery implementations).
# If honor_labels is set to "true", label conflicts are resolved by keeping label
# values from the scraped data and ignoring the conflicting server-side labe# ls. If honor_labels is set to "false", label conflicts are resolved by ren# amin conflicting labels in the scraped data to "exported_<original-label>" (for example "exported_instance", "exported_job") and then attaching server-side labels. This is useful for use cases such as federation, where all label#s specified in the target should be preserved. Note that any globally configured "external_labels" are unaffected by this
# setting. In communication with external systems, they are always applied 
# only when a time series does not have a given label yet and are ignored otherwise.
[ honor_labels: <boolean> | default = false ]

# 配置请求的协议范式, 默认为http请求
[ scheme: <scheme> | default = http ]

# 可选的http url参数
params:
    [ <string>:[<string>, ...]]

# 在`Authorization`头部设置每次抓取请求的用户名和密码
basic_auth:
[username: <string>]
[password: <string>]

# Sets the `Authorization` header on every scrape request with
# the configured bearer token. It is mutually exclusive with `bearer_token_file`.
[ bearer_token: <string> ]

# Sets the `Authorization` header on every scrape request with the bearer token read from the configured file. It is mutually exclusive with `bearer_token`.
[ bearer_token_file: /path/to/bearer/token/file ]

# 配置抓取请求的TLS设置
tls_config:
  [ <tls_config> ]

# 可选的代理URL
[ proxy_url: <string> ]

# 微软的Azure服务发现配置列表
azure_sd_configs:
  [ - <azure_sd_config> ... ]

# Consul服务发现配置列表
consul_sd_configs:
  [ - <consul_sd_config> ... ]

# DNS服务发现配置列表
dns_sd_configs:
  [ - <dns_sd_config> ... ]

# 亚马逊EC2服务发现的配置列表
ec2_sd_configs:
  [ - <ec2_sd_config> ... ]

# 文件服务发现配置列表
file_sd_configs:
  [ - <file_sd_config> ... ]

# google GCE服务发现配置列表
gce_sd_configs:
  [ - <gce_sd_config> ... ]

# Kubernetes服务发现配置列表
kubernetes_sd_configs:
  [ - <kubernetes_sd_config> ... ]

# Marathon服务发现配置列表
marathon_sd_configs:
  [ - <marathon_sd_config> ... ]

# AirBnB的Nerve服务发现配置列表
nerve_sd_configs:
  [ - <nerve_sd_config> ... ]

# Zookeeper服务发现配置列表
serverset_sd_configs:
  [ - <serverset_sd_config> ... ]

# Triton服务发现配置列表
triton_sd_configs:
  [ - <triton_sd_config> ... ]

# 静态配置目标列表
static_configs:
  [ - <static_config> ... ]

# 抓取之前的标签重构配置列表
relabel_configs:
  [ - <relabel_config> ... ]

# List of metric relabel configurations.
metric_relabel_configs:
  [ - <relabel_config> ... ]

# Per-scrape limit on number of scraped samples that will be accepted.
# If more than this number of samples are present after metric relabelling
# the entire scrape will be treated as failed. 0 means no limit.
[ sample_limit: <int> | default = 0 ]

# Per-scrape config limit on number of unique targets that will be
# accepted. If more than this number of targets are present after target
# relabeling, Prometheus will mark the targets as failed without scraping them.
# 0 means no limit. This is an experimental feature, this behaviour could
# change in the future.
[ target_limit: <int> | default = 0 ]

记住:在所有获取配置中<job_name>必须是唯一的。

<tls_config>

<tls_config>允许配置TLS连接。

# CA证书
[ ca_file: <filename> ]

# 证书和key文件
[ cert_file: <filename> ]
[ key_file: <filename> ]

# ServerName extension to indicate the name of the server.
# http://tools.ietf.org/html/rfc4366#section-3.1
[ server_name: <string> ]

# Disable validation of the server certificate.
[ insecure_skip_verify: <boolean> ]

sd_config配置

<static_config>

一个static_config允许指定目标列表,以及附带的通用标签。在获取配置中指定静态目标是规范的方法

# The targets specified by the static config.
targets:
  [ - '<host>' ]

# Labels assigned to all metrics scraped from the targets.
labels:
  [ <labelname>: <labelvalue> ... ]

<relabel_config>

Relabeling是一个非常强大的工具,在获取度量指标之前,它可以动态地重写标签集合。 每个获取配置过程中,多个relabeling步骤能够被配置。它们按照出现在配置文件中的顺序,应用到每个目标的标签集中。

最初,除了配置的每个目标标签之外,目标的job标签设置为相应获取配置的job_name值,这个__address__标签设置为目标地址<host>:<port>。在relabeling之后,这个instance标签默认设置为__address__标签值。这个__scheme____metrics_path__标签设置为各自目标的范式和度量指标路径。 __param_<name>标签设置为成为<name>的第一个传入的URL参数。

另外以__meta__为前缀的标签在relabeling阶段是可用的。他们由服务发现机制设置。

在relabeling完成之后,由__开头的标签将会从标签集合从移除。

如果一个relabeling步骤仅仅需要临时地存储标签值(作为后续relabeling步骤的输入),使用以__tmp为前缀的标签名称。这个前缀需要确保Prometheus本身从没有使用。

# 源标签从现有标签中选择值。 它们的内容使用配置的分隔符连接起来,并与配置的正则表达式匹配,以进行替换,保持和`drop`动作。
[ source_labels: '[' <labelname> [, ...] ']' ]

# 在连接的source label值之间放置分隔符。
[ separator: <string> | default = ; ]

# 在替换操作中将结果值写入的标签。
# 替换操作是必需的。 正则表达式捕获组可用。
[ target_label: <labelname> ]

# 与提取的值匹配的正则表达式。
[ regex: <regex> | default = (.*) ]

# Modulus to take of the hash of the source label values.
[ modulus: <int> ]

# 如果正则表达式匹配,则执行正则表达式替换的替换值。 正则表达式捕获组可用。
[ replacement: <string> | default = $1 ]

# 基于正则表达式匹配执行的操作。
[ action: <relabel_action> | default = replace ]

<regex>是任何有效的正则表达式,它提供replace, keep, drop, labelmap, labeldrop, labelkeep动作,正则表达式处于两端。要取消指定正则表达式,请使用 .*<regex>.*

<relabel_action>决定要采取的relabeling动作。

  • replace: 匹配与source_labels相反的regex。然后,设置target_label替换source_labels, 返回结果包括(${1}, ${2}, ...)。 如果正则表达会不匹配,则不进行任何替换。
  • keep: 放弃与source_labels标签不匹配的目标
  • drop: 放弃与source_labels标签匹配的目标
  • hashmod: 将target_label设置为source_labels的散列模数
  • labelmap: 匹配所有的标签名称,然后将匹配到的标签值复制为由匹配组引用(${1}, ${2},...) 替换的标签名称替换为其值
  • labeldrop: 匹配所有的标签名称。然后删除匹配到的标签集合。
  • labelkeep: 匹配所有的标签名称。然后保留匹配到的标签集合。

必须注意labeldroplabelkeep, 以确保除去标签后,度量指标仍然会被唯一标识。

<alert_relabel_configs>

在警告被发送到Alertmanager之前,警告relabeling应用到alerts。它有相同配置格式和目标relabeling动作。警告relabeling被应用到外部标签。

一个用途是确保HA对Prometheus服务与不同的外部标签发送相同的警告。

<alertmanager_config>

Alertmanager实例的动态发现是处于alpha状态。在将来的版本中配置会发生较大地更改。通过-alertmanager.url标志使用静态配置

alertmanager_config区域指定了Prometheus服务发送警告的Alertmanager实例。它也提供参数配置与这些Alertmanagers的通信。

Alertmanagers可以通过static_configs参数静态配置,或者使用服务发现机制动态发现目标。

另外,从发现的实体和使用的API路径,relabel_configs允许从发现的实体列表和提供可使用的API路径中选择路径。这个api path是通过__alerts_path__标签暴露出来的。

# Per-target Alertmanager timeout when pushing alerts.
[ timeout: <duration> | default = 10s ]

# Prefix for the HTTP path alerts are pushed to.
[ path_prefix: <path> | default = / ]

# Configures the protocol scheme used for requests.
[ scheme: <scheme> | default = http ]

# Sets the `Authorization` header on every request with the
# configured username and password.
basic_auth:
  [ username: <string> ]
  [ password: <string> ]

# Sets the `Authorization` header on every request with
# the configured bearer token. It is mutually exclusive with `bearer_token_file`.
[ bearer_token: <string> ]

# Sets the `Authorization` header on every request with the bearer token
# read from the configured file. It is mutually exclusive with `bearer_token`.
[ bearer_token_file: /path/to/bearer/token/file ]

# Configures the scrape request's TLS settings.
tls_config:
  [ <tls_config> ]

# Optional proxy URL.
[ proxy_url: <string> ]

# List of Azure service discovery configurations.
azure_sd_configs:
  [ - <azure_sd_config> ... ]

# List of Consul service discovery configurations.
consul_sd_configs:
  [ - <consul_sd_config> ... ]

# List of DNS service discovery configurations.
dns_sd_configs:
  [ - <dns_sd_config> ... ]

# List of EC2 service discovery configurations.
ec2_sd_configs:
  [ - <ec2_sd_config> ... ]

# List of file service discovery configurations.
file_sd_configs:
  [ - <file_sd_config> ... ]

# List of GCE service discovery configurations.
gce_sd_configs:
  [ - <gce_sd_config> ... ]

# List of Kubernetes service discovery configurations.
kubernetes_sd_configs:
  [ - <kubernetes_sd_config> ... ]

# List of Marathon service discovery configurations.
marathon_sd_configs:
  [ - <marathon_sd_config> ... ]

# List of AirBnB's Nerve service discovery configurations.
nerve_sd_configs:
  [ - <nerve_sd_config> ... ]

# List of Zookeeper Serverset service discovery configurations.
serverset_sd_configs:
  [ - <serverset_sd_config> ... ]

# List of Triton service discovery configurations.
triton_sd_configs:
  [ - <triton_sd_config> ... ]

# List of labeled statically configured Alertmanagers.
static_configs:
  [ - <static_config> ... ]

# List of Alertmanager relabel configurations.
relabel_configs:
  [ - <relabel_config> ... ]

<remote_write>

远程写是实验性的:在将来的版本中配置可能会实质性地变化

url是发送样本的端点URL。remote_timeout指定发送请求到URL的超时时间。目前没有重试机制

basic_auth, tls_configproxy_url和在scrape_config区域里有相同的含义。

write_relabel_configs是relabeling应用到样本数据的。写relabeling是应用到外部标签之后的。这可能有样本发送数量的限制。

这里有一个小Demo,告诉你怎样使用这个功能

更新于 2020-10-20
在线,8小时前登录

飞狭 3年前

global:
scrape_interval: 15s # By default, scrape targets every 15 seconds. 这个是15s吧
https://prometheus.io/docs/prometheus/latest/getting_started/#configuring-prometheus-to-monitor-itself

查看Prometheus更多相关的文章或提一个关于Prometheus的问题,也可以与我们一起分享文章