我的应用程序的每个实例都有不同的URL。如何配置prometheus.yml以便它获取目标的路径以及主机名?
scrape_configs:
- job_name: 'example-random'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:8090','localhost:8080']
labels:
group: 'dummy'
您目前无法在作业中为每个目标配置metrics_path
,但您可以为每个目标创建单独的作业,以便您可以为每个目标定义metrics_path
。
你的配置文件看起来像这样:
scrape_configs:
- job_name: 'example-target-1'
scrape_interval: 5s
metrics_path: /target-1-path-to-metrics
static_configs:
- targets: ['localhost:8090']
labels:
group: 'dummy'
- job_name: 'example-target-2'
scrape_interval: 5s
metrics_path: /totally-different-path-for-target-2
static_configs:
- targets: ['localhost:8080']
labels:
group: 'dummy-2'
我相信您需要对__metrics_path__
标签集进行一些重新标记,以包含应用程序的不同路径。
普罗米修斯configuration docs将在这里证明对你有用,this article应该帮助你更好地理解重新标记。
我通过使用file_sd_config选项实现了这一点。所有目标都在单独的文件中描述,这些文件可以是YML或JSON格式。
prometheus.yml:
scrape_configs:
- job_name: 'dummy' # This will be overridden in targets.yml
file_sd_configs:
- files:
- targets.yml
targets.yml:
- targets: ['host1:9999']
labels:
job: my_job
__metrics_path__: /path1
- targets: ['host2:9999']
labels:
job: my_job # can belong to the same job
__metrics_path__: /path2
这是我用来启动和运行prometheus的配置。
普罗米修斯终点:http://localhost:8080/appcontext/v1/actuator/prometheus
配置:在/etc/prometheus/prometheus.yml
下添加以下配置
- job_name: 'appdev'
scrape_interval: 5s
metrics_path: /appcontext/v1/actuator/prometheus
static_configs:
- targets: ['localhost:8082']
labels:
group: 'appdev'