配置文件

官方文档说明:https://prometheus.io/docs/prometheus/latest/configuration/configuration/
官方二进制下载包中默认的配置文件

  1. # my global config
  2. global:
  3. scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  4. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  5. # scrape_timeout is set to the global default (10s).
  6. # Alertmanager configuration
  7. alerting:
  8. alertmanagers:
  9. - static_configs:
  10. - targets:
  11. # - alertmanager:9093
  12. # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
  13. rule_files:
  14. # - "first_rules.yml"
  15. # - "second_rules.yml"
  16. # A scrape configuration containing exactly one endpoint to scrape:
  17. # Here it's Prometheus itself.
  18. scrape_configs:
  19. # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  20. - job_name: 'prometheus'
  21. # metrics_path defaults to '/metrics'
  22. # scheme defaults to 'http'.
  23. static_configs:
  24. - targets: ['localhost:9090']

示例配置文件中有三个配置块:global,rule_files和scrape_configs。
全局块控制Prometheus服务器的全局配置。 1.scrape_interval,它控制Prometheus抓取目标的频率,全局设置是每15s抓取一次。 evaluation_interval选项控制Prometheus评估规则的频率。 Prometheus使用规则创建新的时间序列并生成警报。
rule_files块指定我们希望Prometheus服务器加载的任何规则的位置。
scrape_configs控制Prometheus监视的资源。 在默认配置中,有一个名为prometheus的作业,它会抓取Prometheus服务器公开的时间序列数据。 该作业包含一个静态配置的目标,即端口9090上的localhost。

配置语法

  1. global:
  2. # 默认情况下抓取目标的频率.
  3. [ scrape_interval: <duration> | default = 1m ]
  4. # 抓取超时时间.
  5. [ scrape_timeout: <duration> | default = 10s ]
  6. # 评估规则的频率.
  7. [ evaluation_interval: <duration> | default = 1m ]
  8. # 与外部系统通信时添加到任何时间序列或警报的标签
  9. #(联合,远程存储,Alertma# nager).
  10. external_labels:
  11. [ <labelname>: <labelvalue> ... ]
  12. # 规则文件指定了一个globs列表.
  13. # 从所有匹配的文件中读取规则和警报.
  14. rule_files:
  15. [ - <filepath_glob> ... ]
  16. # 抓取配置列表.
  17. scrape_configs:
  18. [ - <scrape_config> ... ]
  19. # 警报指定与Alertmanager相关的设置.
  20. alerting:
  21. alert_relabel_configs:
  22. [ - <relabel_config> ... ]
  23. alertmanagers:
  24. [ - <alertmanager_config> ... ]
  25. # 与远程写入功能相关的设置.
  26. remote_write:
  27. [ - <remote_write> ... ]
  28. # 与远程读取功能相关的设置.
  29. remote_read:
  30. [ - <remote_read> ... ]

参数值说明

参数 含义
布尔值,true 或 false
持续时间,格式符合正则表达式 [0-9]+(ms|[smhdwy])
标签名,格式符合正则表达式 [a-zA-Z][a-za-z0-9]*
标签值,可以包含任意 unicode 字符
文件名,任意有效的文件路径
主机,可以是主机名或 IP,后面可跟端口号
URL 路径
协议,http 或 https
字符串
密钥,比如密码
模板字符串,里面包含需要展开的变量

global

全局配置节点下的配置对所有其它节点都有效,同时也是其它节点的默认值。

scrape_interval 拉取 targets 的默认时间间隔,默认是1秒
scrape_timeout 拉取一个 target 的超时时间,默认是10秒
evaluation_interval 执行 rules 的时间间隔
external_labels 额外的属性,会添加到拉取的数据并存到数据库中

规则配置rule_files

记录和警报规则存在于规则组中。 组内的规则以固定间隔顺序运行,规则语法是:

  1. groups:
  2. [ - <rule_group> ]

一个简单的示例规则文件将是:

  1. groups:
  2. - name: example
  3. rules:
  4. - record: job:http_inprogress_requests:sum
  5. expr: sum(http_inprogress_requests) by (job)

3.1 <rule_group>
  1. # 组的名称。 在文件中必须是唯一的。
  2. name: <string>
  3. # 评估组中的规则的频率。
  4. [ interval: <duration> | default = global.evaluation_interval ]
  5. rules:
  6. [ - <rule> ... ]

3.2 <rule>

记录规则的语法是:

  1. # 要输出的时间序列的名称。 必须是有效的度量标准名称。
  2. record: <string>
  3. # 要评估的PromQL表达式。 每个评估周期都会在当前时间进行评估,并将结果记录为一组新的时间序列,其中度量标准名称由“记录”给出。
  4. expr: <string>
  5. # 在存储结果之前添加或覆盖的标签。
  6. labels:
  7. [ <labelname>: <labelvalue> ]

警报规则的语法是:

  1. # 警报的名称。 必须是有效的度量标准名称。
  2. alert: <string>
  3. # 要评估的PromQL表达式。 每个评估周期都会在当前时间进行评估,并且所有结果时间序列都会成为待处理/触发警报。
  4. expr: <string>
  5. # 警报一旦被退回这段时间就会被视为开启。
  6. # 尚未解雇的警报被认为是未决的。
  7. [ for: <duration> | default = 0s ]
  8. # 为每个警报添加或覆盖的标签。
  9. labels:
  10. [ <labelname>: <tmpl_string> ]
  11. # 要添加到每个警报的注释。
  12. annotations:
  13. [ <labelname>: <tmpl_string> ]

抓取配置scrape_config

<scrape_config>部分指定一组描述如何获取它们的目标和参数。 在一般情况下,一个scrape配置指定单个作业。 在高级配置中,这可能会改变。
目标可以通过<static_configs>参数静态配置,也可以使用其中一种支持的服务发现机制动态发现。
此外,<relabel_configs>允许在抓取之前对任何目标及其标签进行高级修改。
其中<job_name>在所有scrape配置中必须是唯一的。

  1. # 默认分配给已抓取指标的job名称。
  2. job_name: <job_name>
  3. # 从job中抓取目标的频率.
  4. [ scrape_interval: <duration> | default = <global_config.scrape_interval> ]
  5. # 抓取此job时,每次抓取超时时间.
  6. [ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ]
  7. # 从目标获取指标的HTTP资源路径.
  8. [ metrics_path: <path> | default = /metrics ]
  9. # honor_labels控制Prometheus如何处理已经存在于已抓取数据中的标签与Prometheus将附加服务器端的标签之间的冲突("job"和"instance"标签,手动配置的目标标签以及服务发现实现生成的标签)。
  10. #
  11. # 如果honor_labels设置为"true",则通过保留已抓取数据的标签值并忽略冲突的服务器端标签来解决标签冲突。
  12. #
  13. # 如果honor_labels设置为"false",则通过将已抓取数据中的冲突标签重命名为"exported_ <original-label>"(例如"exported_instance","exported_job")然后附加服务器端标签来解决标签冲突。 这对于联合等用例很有用,其中应保留目标中指定的所有标签。
  14. #
  15. # 请注意,任何全局配置的"external_labels"都不受此设置的影响。 在与外部系统通信时,它们始终仅在时间序列尚未具有给定标签时应用,否则将被忽略。
  16. #
  17. [ honor_labels: <boolean> | default = false ]
  18. # 配置用于请求的协议方案.
  19. [ scheme: <scheme> | default = http ]
  20. # 可选的HTTP URL参数.
  21. params:
  22. [ <string>: [<string>, ...] ]
  23. # 使用配置的用户名和密码在每个scrape请求上设置`Authorization`标头。 password和password_file是互斥的。
  24. basic_auth:
  25. [ username: <string> ]
  26. [ password: <secret> ]
  27. [ password_file: <string> ]
  28. # 使用配置的承载令牌在每个scrape请求上设置`Authorization`标头。 它`bearer_token_file`和是互斥的。
  29. [ bearer_token: <secret> ]
  30. # 使用配置的承载令牌在每个scrape请求上设置`Authorization`标头。 它`bearer_token`和是互斥的。
  31. [ bearer_token_file: /path/to/bearer/token/file ]
  32. # 配置scrape请求的TLS设置.
  33. tls_config:
  34. [ <tls_config> ]
  35. # 可选的代理URL.
  36. [ proxy_url: <string> ]
  37. # Azure服务发现配置列表.
  38. azure_sd_configs:
  39. [ - <azure_sd_config> ... ]
  40. # Consul服务发现配置列表.
  41. consul_sd_configs:
  42. [ - <consul_sd_config> ... ]
  43. # DNS服务发现配置列表。
  44. dns_sd_configs:
  45. [ - <dns_sd_config> ... ]
  46. # EC2服务发现配置列表。
  47. ec2_sd_configs:
  48. [ - <ec2_sd_config> ... ]
  49. # OpenStack服务发现配置列表。
  50. openstack_sd_configs:
  51. [ - <openstack_sd_config> ... ]
  52. # 文件服务发现配置列表。
  53. file_sd_configs:
  54. [ - <file_sd_config> ... ]
  55. # GCE服务发现配置列表。
  56. gce_sd_configs:
  57. [ - <gce_sd_config> ... ]
  58. # Kubernetes服务发现配置列表。
  59. kubernetes_sd_configs:
  60. [ - <kubernetes_sd_config> ... ]
  61. # Marathon服务发现配置列表。
  62. marathon_sd_configs:
  63. [ - <marathon_sd_config> ... ]
  64. # AirBnB的神经服务发现配置列表。
  65. nerve_sd_configs:
  66. [ - <nerve_sd_config> ... ]
  67. # Zookeeper Serverset服务发现配置列表。
  68. serverset_sd_configs:
  69. [ - <serverset_sd_config> ... ]
  70. # Triton服务发现配置列表。
  71. triton_sd_configs:
  72. [ - <triton_sd_config> ... ]
  73. # 此job的标记静态配置目标列表。
  74. static_configs:
  75. [ - <static_config> ... ]
  76. # 目标重新标记配置列表。
  77. relabel_configs:
  78. [ - <relabel_config> ... ]
  79. # 度量标准重新配置列表。
  80. metric_relabel_configs:
  81. [ - <relabel_config> ... ]
  82. # 对每个将被接受的样本数量的每次抓取限制。
  83. # 如果在度量重新标记后存在超过此数量的样本,则整个抓取将被视为失败。 0表示没有限制。
  84. [ sample_limit: <int> | default = 0 ]

配置检查

Prometheus自带的的promtool命令行实用工具可以实现快速检查规则文件在语法上是否正确
Jietu20200328-104521.jpg

帮助文档如下

  1. $ ./promtool -h
  2. usage: promtool [<flags>] <command> [<args> ...]
  3. Tooling for the Prometheus monitoring system.
  4. Flags:
  5. -h, --help Show context-sensitive help (also try --help-long and --help-man).
  6. --version Show application version.
  7. Commands:
  8. help [<command>...]
  9. Show help.
  10. check config <config-files>...
  11. Check if the config files are valid or not.
  12. check rules <rule-files>...
  13. Check if the rule files are valid or not.
  14. check metrics
  15. Pass Prometheus metrics over stdin to lint them for consistency and correctness.
  16. examples:
  17. $ cat metrics.prom | promtool check metrics
  18. $ curl -s http://localhost:9090/metrics | promtool check metrics
  19. query instant <server> <expr>
  20. Run instant query.
  21. query range [<flags>] <server> <expr>
  22. Run range query.
  23. query series --match=MATCH [<flags>] <server>
  24. Run series query.
  25. query labels <server> <name>
  26. Run labels query.
  27. debug pprof <server>
  28. Fetch profiling debug information.
  29. debug metrics <server>
  30. Fetch metrics debug information.
  31. debug all <server>
  32. Fetch all debug information.
  33. test rules <test-rule-file>...
  34. Unit tests for rules.
  1. $ ./promtool check config prometheus.yml
  2. Checking prometheus.yml
  3. SUCCESS: 0 rule files found
  4. $ ./promtool check rules prometheus.yml
  5. Checking prometheus.yml
  6. SUCCESS: 0 rules found

热加载

Prometheus配置信息的热加载有两种方式:
第一种热加载方式:查看Prometheus的进程id,发送SIGHUP信号:

  1. kill -HUP <pid>

第二种热加载方式:发送一个POST请求到/-/reload,但是需要在启动时给定--web.enable-lifecycle选项:

  1. curl -X POST http://localhost:9090/-/reload

如果配置热加载成功,Prometheus会打印出下面的log:

  1. ... msg="Loading configuration file" filename=prometheus.yml ...

我们使用的是第一种热加载方式,systemd unit文件如下:

  1. [Unit]
  2. Description=prometheus
  3. After=network.target
  4. [Service]
  5. Type=simple
  6. User=prometheus
  7. ExecStart=/usr/local/prometheus/prometheus \
  8. --config.file==/usr/local/prometheus/prometheus.yml \
  9. --storage.tsdb.path=/home/prometheus/data \
  10. --storage.tsdb.retention=365d \
  11. --web.listen-address=:9090 \
  12. --web.external-url=https://prometheus.frognew.com
  13. ExecReload=/bin/kill -HUP $MAINPID
  14. Restart=on-failure
  15. [Install]
  16. WantedBy=multi-user.target

在仅需要重新加载配置,而不需重启进程时,只需要运行systemctl reload prometheus即可。

参考

https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/