java 当prometheus端点正在调用时,MockMvc接收404

ldioqlga  于 2023-05-27  发布在  Java
关注(0)|答案(2)|浏览(185)

我想使用MockMvc类测试Prometheus指标端点。
一切正常,但昨天我将我的项目迁移到Java 15SpringBoot 2.4.3SpringCloud 2020.0.1。现在,只有prometheus测试不起作用,我收到404而不是预期的200。我对build.gradle有所有必要的依赖:runtime("io.micrometer:micrometer-registry-prometheus")。在application-test.yml上,我有一个配置,用于禁用安全性、合约测试、合约代理端点等。
我的测试:

@ExtendWith({SpringExtension.class, PostgresSpringDataSourceExtension.class})
@ActiveProfiles({"test"})
@SpringBootTest
@AutoConfigureMockMvc
public class PrometheusEndpointTest {

 @Autowired private MockMvc mockMvc;

 @Test
 public void metricsThroughPrometheusEndpoint() throws Exception {
  MvcResult result = 
  this.mockMvc.perform(get("/metrics")).andExpect(status().isOk()).andReturn();
 }
}

application.yaml配置的一部分:

management:
  endpoint:
    prometheus:
      enabled: true
  endpoints:
    enabled-by-default: false
    web:
      exposure:
        include: 'prometheus'
      base-path: '/'
      path-mapping:
        prometheus: 'metrics'
2nc8po8w

2nc8po8w1#

解决方案:我们需要将属性添加到测试注解或application-test.yml

@ExtendWith(SpringExtension.class)
@SpringBootTest(
  webEnvironment = RANDOM_PORT,
  properties = "management.metrics.export.prometheus.enabled")
@ActiveProfiles({"test"})
@AutoConfigureMockMvc
oprakyz7

oprakyz72#

另一个解决方案添加:

@TestPropertySource(properties = ["management.metrics.export.prometheus.enabled=true"])

as org.springframework. Boot .actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration需要启用它才能导出

相关问题