[英]Prometheus scrape /metric with custom header
I have an application that will be monitored by Prometheus, but the application need the custom header key like:我有一个将由 Prometheus 监控的应用程序,但该应用程序需要自定义 header 密钥,例如:
x-auth-token: <customrandomtoken>
What should I do with prometheus.yml?我应该如何处理 prometheus.yml?
Prometheus itself does not have a way to define custom headers in order to reach an exporter. Prometheus 本身无法定义自定义标头以到达导出器。 The idea of adding the feature was discussed in this GitHub issue .在此 GitHub 问题中讨论了添加该功能的想法。 Tl;dr: if you need a custom header, inject it with a forward or a reverse proxy. Tl;博士:如果您需要自定义 header,请使用正向或反向代理注入它。
The prometheus-blackbox-exporter
tag suggest that the question is about the exporter that makes probes, which is a separate thing and it does have a way to set headers. prometheus-blackbox-exporter
标签表明问题是关于制作探针的导出器,这是一个单独的东西,它确实有一种设置标头的方法。 Only, it does not scrape metrics, it makes them .只是,它不会抓取指标,它会生成它们。
Blackbox exporter has it's own configuration file and it consists of modules . Blackbox 导出器有它自己的配置文件,它由模块组成。 A module is a set of parameters, defining how to do the probe and what result to expect.模块是一组参数,定义了如何进行探测以及预期的结果。 Here is an example of a module that looks for 200-299 response code and uses X-Auth-Token
header:下面是一个查找 200-299 响应代码并使用X-Auth-Token
header 的模块示例:
modules:
http_2xx_with_header:
prober: http
http:
headers:
X-Auth-Token: skdjfh98732hjf22exampletoken
More examples can be found here and the list of configuration options - here .可以在此处找到更多示例,并在此处找到配置选项列表。
When you made the blackbox exporter to load the new configuration, you need to adjust Prometheus configuration as well:当您让 blackbox exporter 加载新配置时,您还需要调整 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 doesn't support specifying custom HTTP headers, which must be sent with each scrape request to scrape target:( However, it supports specifying Authorization
header via authorization
and basic_auth
options at scrape_config section. Prometheus 不支持指定自定义 HTTP 标头,该标头必须与每个抓取请求一起发送到抓取目标:(但是,它支持通过authorization
和在scrape_config部分的basic_auth
选项指定Authorization
header。
For example, the following config instructs Prometheus to send Authorization: My-Auth my-super-secret
header with each scrape request to http://localhost:8428/metrics
:例如,以下配置指示 Prometheus 发送Authorization: My-Auth my-super-secret
header 每个抓取请求到http://localhost:8428/metrics
:
scrape_configs:
- job_name: foo
authorization:
type: "My-Auth"
credentials: "my-super-secret"
static_configs:
- targets: ["localhost:8428"]
PS If you still need sending custom http headers with each request to remote target, then take a look at vmagent - monitoring agent, which understands Prometheus scrape configs and can scrape Prometheus targets. PS 如果您仍然需要将自定义 http 标头与每个请求一起发送到远程目标,请查看vmagent - 监控代理,它了解 Prometheus 抓取配置并可以抓取 Prometheus 目标。 This is a project I work on.这是我从事的一个项目。 It provides additional features on top of standard scrape_configs
from Prometheus, including headers
option - see these docs .它在 Prometheus 的标准scrape_configs
之上提供了附加功能,包括headers
选项 - 请参阅这些文档。 The headers
option allows specifying arbitrary number of additional http headers, which need to be sent to scrape target. headers
选项允许指定任意数量的附加 http 标头,这些标头需要发送到抓取目标。 For example, the following config instructs sending x-auth-token: <customrandomtoken>
http header with each request to http://foobar:1234
:例如,以下配置指示发送x-auth-token: <customrandomtoken>
http header 每个请求都发送到http://foobar:1234
:
scrape_configs:
- job_name: foo
headers:
- "x-auth-token: <customrandomtoken>"
static_configs:
- targets: ["foobar:1234"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.