Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

/actuator/prometheus missing in @SpringbootTest

I'm using springbooot 2.4.0 and I added the following dependencies for enabling prometheus metrics:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

then in my application.properties I have the following properties

management.endpoints.web.exposure.include=*
management.metrics.enable.all=true

I'm trying to run a simple integration test to see my custom metrics appearing at /actuator/prometheus endpoint. Below the code

package com.example.demo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import static io.restassured.RestAssured.given;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTest {

  @LocalServerPort
  private int port;

  private String baseUrl;

  @BeforeEach
  public void setup() {
      baseUrl = "http://localhost:" + port;
  }

  @Test
  public void metricsEndpoint() throws Exception {

    given().when().get(baseUrl + "/demo/actuator/prometheus")
            .then()
            .statusCode(200);
    }
}

The error I get here is

java.lang.AssertionError: 1 expectation failed.
Expected status code <200> but was <404>.

while if I repeat the same request for any other endpoint provided by springboot actuator I correctly geth the response, for example I tried /actuator/health, /actuator/info, /actuator/metrics etc..

This happens only during integration tests with @Springboot annotation and this is strange because if I run my application and make a request with postman to the address localhost:8080/actuator/prometheus I correctly get a response.

It is like the prometheus registry is not loaded during tests.

Can anyone help?

Thanks in advance.

EDIT: the solution is the one suggested by Johannes Klug. Adding the annotation @AutoConfigureMetrics solved my problem

like image 636
Massimo Basile Avatar asked Dec 18 '20 16:12

Massimo Basile


People also ask

What is Prometheus in actuator?

Introducing Prometheus It is a time-series database, which stores a sequence of data points, across time. Prometheus… carrying the torch. It's generally used to store metrics and performance data from your applications. And this allows you to perform time-series analysis of metrics.

What is micrometer registry Prometheus?

“Micrometer is a dimensional-first metrics collection facade whose aim is to allow you to time, count, and gauge your code with a vendor-neutral API.” Moreover, a micrometer is a vendor-neutral data provider and exposes application metrics to other external monitoring systems like Prometheus, AWS Cloudwatch etc…

What is micrometer in spring boot?

Micrometer provides a simple facade over the instrumentation clients for a number of popular monitoring systems. Currently, it supports the following monitoring systems: Atlas, Datadog, Graphite, Ganglia, Influx, JMX, and Prometheus.

What is Prometheus Spring Boot actuator?

Prometheus is a powerful monitoring servers and provides a lot of features. With Spring boot actuator, it’s easy to integrate and monitor the application health using Prometheus. The source code for this post is available on the GitHub

How do I get Prometheus alerts in Spring Boot?

You can reach Prometheus Alert UI from the following URL: http://localhost:9090/alerts If your Spring Boot application is available there should be no Alerts. Now, try to stop your Spring Boot application: As you can see, an Alert is firing since the application is not available.

How to test a Spring Boot application as a whole?

This way, Spring doesn’t fire up a whole application context each time the test is started. We can test our Spring Boot application as a whole, unit by unit, and also layer by layer. Using Spring Boot’s test slice annotations we can test each layer separately.

How do I enable actuator in Spring Boot?

Enabling Actuator in Spring Boot All the actuators production-grade features provided through the spring-boot-actuator starter. To enable these features in our application, we need to add spring-boot-starter-actuator in the pom.xml. This will add the actuator feature to our Spring Boot application.


Video Answer


1 Answers

I faced the same issue. After some tracing through spring-context ConditionEvaluator, I found that the newly introduced @ConditionalOnEnabledMetricsExport("prometheus") condition on PrometheusMetricsExportAutoConfiguration prevented the endpoint from loading.

This is intended behavior due to https://github.com/spring-projects/spring-boot/pull/21658 and impacts spring-boot 2.4.x

Fix: add @AutoConfigureMetrics to your test

@AutoConfigureMetrics
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTest {
like image 59
Johannes Klug Avatar answered Oct 21 '22 00:10

Johannes Klug