访问 Spring Boot 指标端点的问题
Posted
技术标签:
【中文标题】访问 Spring Boot 指标端点的问题【英文标题】:Problem accessing spring boot metric endpoint 【发布时间】:2019-09-21 01:37:24 【问题描述】:当我添加以下类时,我无法访问 /metrics
端点 -
ExporterRegister.class
public class ExporterRegister
private List<Collector> collectors;
public ExporterRegister(List<Collector> collectors)
for (Collector collector : collectors)
collector.register();
this.collectors = collectors;
public List<Collector> getCollectors()
return collectors;
CollectorRegistry.class
@Configuration
@ConditionalOnClass(CollectorRegistry.class)
public class MetricController
private static final CollectorRegistry metricRegistry = CollectorRegistry.defaultRegistry;
@Bean
ServletRegistrationBean registerPrometheusExporterServlet()
return new ServletRegistrationBean(new MetricsServlet(metricRegistry), new String[]"/metrics");
@Bean
ExporterRegister exporterRegister()
List<Collector> collectors = new ArrayList<>();
collectors.add(new StandardExports());
collectors.add(new MemoryPoolsExports());
collectors.add(new GarbageCollectorExports());
collectors.add(new ThreadExports());
ExporterRegister register = new ExporterRegister(collectors);
return register;
我使用的是 spring boot 2.1.4 版本,以下是我在 pom.xml 中使用的 prometheus 依赖项
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>0.6.0</version>
</dependency>
我的应用程序属性 -
server.servlet.context-path=/something
server.error.whitelabel.enabled = true
#overriding old spring beans with newer version
spring.main.allow-bean-definition-overriding=true
#enable console logs
logging.level.org.apache.http.wire=DEBUG
#Metrics related config
management.endpoint.metrics.enabled=true
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
management.endpoint.beans.enabled=false
management.endpoint.info.enabled=true
management.endpoints.enabled-by-default=true
management.server.port=8081
当我尝试访问此网址时 - http://localhost:8080/something/metrics
我收到 404 未找到
【问题讨论】:
【参考方案1】:默认情况下,所有执行器端点都映射在/actuator
下。使用/something
的上下文路径,这意味着指标端点将在/something/actuator/metrics
可用。
【讨论】:
所以我出于某种原因在此处添加了此属性,当我访问时找不到 404 - localhost:8080/something/actuator/metrics 但是当我添加 management.server.port=8081 并尝试访问 localhost:8081/actuator/metrics 然后我能够看到指标。我在上面添加了 management.properties以上是关于访问 Spring Boot 指标端点的问题的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 执行器端点 /prometheus 未列出与 tomcat 相关的指标,例如请求计数
无法访问 Spring Boot Actuator“/actuator”端点