MockMvc 调用 Prometheus 端点时收到 404 错误

7

我想使用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'
2个回答

6
解决方案:我们需要向测试注解或application-test.yml文件中添加属性。
@ExtendWith(SpringExtension.class)
@SpringBootTest(
  webEnvironment = RANDOM_PORT,
  properties = "management.metrics.export.prometheus.enabled")
@ActiveProfiles({"test"})
@AutoConfigureMockMvc

0

另外一个添加的解决方案:

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

由于org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration需要启用才能导出


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接