How to implement test to check that actuator metrics are exist ? 404 for /actuator/prometheus in tests

huangapple go评论86阅读模式
英文:

How to implement test to check that actuator metrics are exist ? 404 for /actuator/prometheus in tests

问题

I have a spring boot application (version 3.0.4):

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.4</version>
    <relativePath/>
</parent>

In application.yaml of the application (src/main/resources), I have the following configuration:

...
management:
  server:
    port: 8081
  endpoints:
    web:
      exposure:
        include: health, prometheus, info
  endpoint:
    info:
      enabled: true
    health:
      enabled: true
      probes:
        enabled: true
      show-details: always
    prometheus:
      enabled: true
...

When I run the application and access the URL localhost:8081/actuator/prometheus in my browser, I see a long list of metrics, which is expected.

Now I want to implement a test to ensure that Prometheus metrics are available:

@Slf4j
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class ActuatorTests {
    @Autowired
    protected TestRestTemplate testRestTemplate;

    @LocalManagementPort
    private int managementPort;

    @Test
    public void test1() {
        ResponseEntity<String> forEntity = testRestTemplate.getForEntity("/actuator/prometheus", String.class);
        assertEquals(HttpStatus.OK, forEntity.getStatusCode());
    }

    @Test
    public void test2() {
        ResponseEntity<String> actuatorResponse = testRestTemplate.getForEntity("http://localhost:" + managementPort + "/actuator/prometheus", String.class);
        assertEquals(HttpStatus.OK, actuatorResponse.getStatusCode());
    }
}

Both tests fail with the same error:

org.opentest4j.AssertionFailedError:
Expected :200 OK
Actual   :404 NOT_FOUND

How can I fix it?

P.S.

I also tried to copy this test as is: Stack Overflow Answer

But the result is:

org.springframework.web.client.HttpClientErrorException$NotFound: 404 : {"timestamp":"2023-05-31T22:09:49.535+00:00","status":404,"error":"Not Found","path":"/actuator/metrics"}

如何修复此问题?

英文:

I have a spring boot appication(version is 3.0.4):

&lt;parent&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
	&lt;version&gt;3.0.4&lt;/version&gt;
	&lt;relativePath/&gt;
&lt;/parent&gt;

In application.yaml of application(src/main/resources) I have following config:

   ....
    management:
      server:
        port: 8081
      endpoints:
        web:
          exposure:
            include: health,prometheus,info
      endpoint:
        info:
          enabled: true
        health:
          enabled: true
          probes:
            enabled: true
          show-details: always
        prometheus:
          enabled: true
      ...

When I run application and access url localhost:8081/actuator/prometheus in my browser I see a long list of metrics and it is expected

Now I want to implement test to make sure that prometheus metrics are available:

@Slf4j
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class ActuatorTests {
    @Autowired
    protected TestRestTemplate testRestTemplate;

    @LocalManagementPort
    private int managementPort;

    @Test
    public void test1() {
        ResponseEntity&lt;String&gt; forEntity = testRestTemplate.getForEntity(&quot;/actuator/prometheus&quot;, String.class);
        assertEquals(HttpStatus.OK, forEntity.getStatusCode());

    }

    @Test
    public void test2() {
        ResponseEntity&lt;String&gt; actuatorResponse = testRestTemplate.getForEntity(&quot;http://localhost:&quot; + managementPort + &quot;/actuator/prometheus&quot;, String.class);
        assertEquals(HttpStatus.OK, actuatorResponse.getStatusCode());
    }
}

Both tests are failed with the same error:

org.opentest4j.AssertionFailedError: 
Expected :200 OK
Actual   :404 NOT_FOUND

How can I fix it ?

P.S.

ALso I tried to copy this test as is: https://stackoverflow.com/a/75334796/2674303

but result is:

org.springframework.web.client.HttpClientErrorException$NotFound: 404 : &quot;{&quot;timestamp&quot;:&quot;2023-05-31T22:09:49.535+00:00&quot;,&quot;status&quot;:404,&quot;error&quot;:&quot;Not Found&quot;,&quot;path&quot;:&quot;/actuator/metrics&quot;}&quot;

答案1

得分: 0

以下是已翻译的内容:

这个测试配置中的配置解决了这个问题:

management:
prometheus:
metrics.export.enabled: true

这个选项在应用程序中不需要(因为默认值是true)。

package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;
...
@ConfigurationProperties(prefix = "management.prometheus.metrics.export")
public class PrometheusProperties {

/**
 * 是否启用将指标导出到此后端。
 */
private boolean enabled = true;     
...

我找不到一个地方,但似乎一些Spring Boot测试启动器覆盖了这个默认值。不确定当我需要更改配置以使其工作时,这个测试是否有价值。

英文:

This config in test configuration fixes the issue:

management:
  prometheus:
    metrics.export.enabled: true

The tricky thing that this option is not needed in applicaton(because default value is true).

package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;
...
@ConfigurationProperties(prefix = &quot;management.prometheus.metrics.export&quot;)
public class PrometheusProperties {

	/**
	 * Whether exporting of metrics to this backend is enabled.
	 */
	private boolean enabled = true;     
    ...

I wasn't able to find a place but looks like some spring boot test starter overwrite this default value. Not sure if this test has a value in case when I need to change configuration to make it working.

huangapple
  • 本文由 发表于 2023年6月1日 05:50:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定