我有一个以下的撰写文件命名为d docker-comment. yaml:
version: '3.7'
networks:
back-tier:
services:
prometheus:
image: prom/prometheus:v2.36.2
volumes:
- ./prometheus/:/prometheus/
ports:
- 9090:9090
networks:
- back-tier
restart: always
文件夹结构如下所示:
[![在此处输入图像描述][1]][1]
prometheus. yml看起来像这样:
# Sample Prometheus config
# This assumes that your Prometheus instance can access this application on localhost:8080
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
scrape_configs:
- job_name: 'spring boot scrape'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:8080']
我已经开始了
docker-compoe up -d
我有Spring启动应用程序启动本地:当我打http://localhost:8080/actuator/prometheus
我看到以下(我的自定义指标):
....
# HELP greeting_time_seconds Time taken to return greeting
# TYPE greeting_time_seconds summary
greeting_time_seconds{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="none",method="greeting",quantile="0.5",} 8.8064E-5
greeting_time_seconds{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="none",method="greeting",quantile="0.9",} 0.022018048
greeting_time_seconds_count{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="none",method="greeting",} 9.0
greeting_time_seconds_sum{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="none",method="greeting",} 0.022103
# HELP greeting_time_seconds_max Time taken to return greeting
# TYPE greeting_time_seconds_max gauge
greeting_time_seconds_max{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="none",method="greeting",} 0.0212423
....
但当我试图进入普罗米修斯
http://localhost:9090/
我没有看到任何greeting_time_seconds
指标
看来普罗米修斯无法收集我的指标。
我检查了http://localhost:9090/config
它的反应是:
global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 15s
alerting:
alertmanagers:
- follow_redirects: true
enable_http2: true
scheme: http
timeout: 10s
api_version: v2
static_configs:
- targets: []
scrape_configs:
- job_name: prometheus
honor_timestamps: true
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
follow_redirects: true
enable_http2: true
static_configs:
- targets:
- localhost:9090
所以看起来它没有从本地文件夹中看到我的配置。
我怎样才能修好它?
我已经更正了docker-comort文件中的卷:…… prometheus:image:prom/prometheus:v2.36.2卷:-./prometheus/:/etc/prometheus/……
现在http://localhost:9090/
使用我提供的配置进行响应:
global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 15s
scrape_configs:
- job_name: spring boot scrape
honor_timestamps: true
scrape_interval: 5s
scrape_timeout: 5s
metrics_path: /actuator/prometheus
scheme: http
follow_redirects: true
enable_http2: true
static_configs:
- targets:
- localhost:8080
根据DazWilkin的建议,我已经更新了撰写文件
version: '3.7'
#networks:
# back-tier:
services:
prometheus:
image: prom/prometheus:v2.36.2
volumes:
- ./prometheus/:/etc/prometheus/
ports:
- 9090:9090
# networks:
# - back-tier
network_mode: "host"
restart: always
组合后,我看到容器是活的,但URLlocalhost:9090现在无法访问
[1]: https://i.stack.imgur.com/gAtOE.jpg
最后我找到了一个解决方案:在prometheus配置中,我必须提供host. docker.内部
而不是localhost
scrape_configs:
- job_name: 'spring boot scrape'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
- targets: ['host.docker.internal:8080']