From 641a1a0318a6de88ad5e4643258f7165783141e7 Mon Sep 17 00:00:00 2001 From: roger yuan Date: Wed, 19 May 2021 09:42:33 -0600 Subject: [AAI] Export relevant key metrics for monitoring in Prometheus Make the key metrics available to the monitoring system by instrumenting the code. The Key metrics are available via /actuator/prometheus /actuator/info /actuator/health Issue-ID: AAI-3343 Signed-off-by: Roger Yuan Change-Id: I69f7eafb5105a04369526c70902ac7b676038c36 --- .../java/org/onap/aai/rest/ConfigurationTest.java | 35 ++++++++++++++++++++++ .../src/test/resources/application-test.properties | 8 ++++- 2 files changed, 42 insertions(+), 1 deletion(-) (limited to 'aai-resources/src/test') diff --git a/aai-resources/src/test/java/org/onap/aai/rest/ConfigurationTest.java b/aai-resources/src/test/java/org/onap/aai/rest/ConfigurationTest.java index ee33bfc..39f71fd 100644 --- a/aai-resources/src/test/java/org/onap/aai/rest/ConfigurationTest.java +++ b/aai-resources/src/test/java/org/onap/aai/rest/ConfigurationTest.java @@ -27,6 +27,7 @@ import org.onap.aai.ResourcesTestConfiguration; import org.onap.aai.restclient.PropertyPasswordConfiguration; import org.onap.aai.config.SpringContextAware; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Import; @@ -42,6 +43,7 @@ import java.util.Collections; import java.util.UUID; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * Test REST requests against configuration resource @@ -58,10 +60,14 @@ public class ConfigurationTest extends AbstractSpringRestTest { @LocalServerPort int randomPort; + @Value("${local.management.port}") + private int mgtPort; + private HttpEntity httpEntity; private HttpEntity httpEntityPut; private HttpEntity httpEntityPatch; private String baseUrl; + private String actuatorurl; private HttpHeaders headers; @Before public void setup() throws UnsupportedEncodingException { @@ -79,6 +85,7 @@ public class ConfigurationTest extends AbstractSpringRestTest { httpEntity = new HttpEntity(headers); baseUrl = "http://localhost:" + randomPort; + actuatorurl = "http://localhost:" + mgtPort; } @Test @@ -157,4 +164,32 @@ public class ConfigurationTest extends AbstractSpringRestTest { } + + @Test + public void TestManagementEndpointConfiguration() { + ResponseEntity responseEntity = null; + String responseBody = null; + + //set Accept as text/plain in order to get access of endpoint "/actuator/prometheus" + headers.set("Accept", "text/plain"); + httpEntity = new HttpEntity(headers); + responseEntity = restTemplate.exchange(actuatorurl + "/actuator/prometheus", HttpMethod.GET, httpEntity, String.class); + responseBody = (String) responseEntity.getBody(); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertTrue(responseBody.contains("app_id")); + assertTrue(responseBody.contains("group_id")); + + //Set Accept as MediaType.APPLICATION_JSON in order to get access of endpoint "/actuator/info" and "/actuator/health" + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + httpEntity = new HttpEntity(headers); + responseEntity = restTemplate.exchange(actuatorurl + "/actuator/info", HttpMethod.GET, httpEntity, String.class); + responseBody = (String) responseEntity.getBody(); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertTrue(responseBody.contains("aai-resources")); + + responseEntity = restTemplate.exchange(actuatorurl + "/actuator/health", HttpMethod.GET, httpEntity, String.class); + responseBody = (String) responseEntity.getBody(); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertTrue(responseBody.contains("UP")); + } } diff --git a/aai-resources/src/test/resources/application-test.properties b/aai-resources/src/test/resources/application-test.properties index b96b9ce..8aee5e1 100644 --- a/aai-resources/src/test/resources/application-test.properties +++ b/aai-resources/src/test/resources/application-test.properties @@ -15,7 +15,8 @@ spring.autoconfigure.exclude=\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.keycloak.adapters.springboot.KeycloakAutoConfiguration,\ - org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration + org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\ + org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration spring.profiles.active=production #The max number of active threads in this pool @@ -72,3 +73,8 @@ schema.version.edge.label.start=v12 schema.version.api.default=v15 schema.translator.list=config +#To expose the Prometheus scraping endpoint in unit test +management.server.port=0 +management.endpoints.enabled-by-default=true +management.endpoints.web.exposure.include=info, health, prometheus +management.metrics.web.server.auto-time-requests=false -- cgit 1.2.3-korg