我有一个将由 Prometheus 监控的应用程序, 但应用程序需要自定义标头键,例如:
x-auth-token: <customrandomtoken>
我应该如何处理 prometheus.yml?
Prometheus 本身没有办法定义自定义标头以到达导出器。添加该功能的想法已在此 GitHub 问题 中讨论过。 Tl;dr:如果您需要自定义标头,请使用转发代理注入它(我在另一个answer中发布了一个示例)。
prometheus-blackbox-exporter
标签表明问题与制作探针的导出器有关,这是一个单独的东西,并且它确实有一种设置标头的方法。只是,它不会抓取指标,而是会生成指标。
Blackbox 导出器有自己的配置文件,由 modules 组成。模块是一组参数,定义如何进行探测以及期望的结果。以下是查找 200-299 响应代码并使用
X-Auth-Token
标头的模块示例:
modules:
http_2xx_with_header:
prober: http
http:
headers:
X-Auth-Token: skdjfh98732hjf22exampletoken
当您让blackbox导出器加载新配置时,您还需要调整Prometheus配置:
scrape_configs:
- job_name: 'blackbox'
metrics_path: /probe
params:
module: [http_2xx_with_header] # <- Here goes the name of the new module
static_configs:
- targets:
- http://prometheus.io
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115
Prometheus 不支持指定自定义 HTTP 标头,该标头必须与每个抓取请求一起发送到抓取目标:( 但是,它支持通过
scrape_config部分的
Authorization
和 authorization
选项指定 basic_auth
标头。
例如,以下配置指示 Prometheus 将每个抓取请求中的
Authorization: My-Auth my-super-secret
标头发送到 http://localhost:8428/metrics
:
scrape_configs:
- job_name: foo
authorization:
type: "My-Auth"
credentials: "my-super-secret"
static_configs:
- targets: ["localhost:8428"]
附注如果您仍然需要在每个请求中发送自定义 http 标头到远程目标,那么请查看 vmagent - 监控代理,它了解 Prometheus 抓取配置并可以抓取 Prometheus 目标。这是我正在从事的一个项目。它在 Prometheus 标准
scrape_configs
之上提供了附加功能,包括 headers
选项 - 请参阅 这些文档。 headers
选项允许指定任意数量的附加http标头,这些标头需要发送到抓取目标。例如,以下配置指示将每个请求发送到 x-auth-token: <customrandomtoken>
http 标头到 http://foobar:1234
:
scrape_configs:
- job_name: foo
headers:
- "x-auth-token: <customrandomtoken>"
static_configs:
- targets: ["foobar:1234"]
从 Prometheus 2.55 开始,由于 prometheus.yml 配置文件中 scrape 配置项中的 http_headers 关键字,可以添加自定义请求标头。
这已记录在Prometheus 官方文档中。
在上述情况下,您可以在 prometheus.yml 配置文件中添加以下行:
scrape_configs:
- job_name: "my_monitored_app"
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ["foobar:1234"]
http_headers:
x-auth-token:
values: ['my_public_token']
或者,如果您想在 Prometheus 管理 ui 中向用户隐藏您的令牌:
scrape_configs:
- job_name: "my_monitored_app"
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ["foobar:1234"]
http_headers:
x-auth-token:
secrets: ['my_public_token']