From 13533270bb8498fce76a3d7b685b1a0f6d057d71 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Tue, 24 Aug 2021 12:55:16 +0100 Subject: Export basic prometheus metrics from clamp POLICY-3557: Export basic prometheus metrics from clamp Change-Id: Ica71d089255e2a8881f668ceeb578993996f9a38 Signed-off-by: FrancescoFioraEst --- .../main/resources/etc/ClRuntimeParameters.yaml | 6 ++ pom.xml | 2 +- runtime-controlloop/pom.xml | 10 +++ .../runtime/config/ConverterConfiguration.java | 7 ++ .../src/main/resources/application.yaml | 6 ++ .../runtime/main/rest/ActuatorControllerTest.java | 91 ++++++++++++++++++++++ .../runtime/util/rest/CommonRestController.java | 45 ++++++++++- .../src/test/resources/application_test.properties | 2 + 8 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/rest/ActuatorControllerTest.java diff --git a/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml b/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml index 0aa3fb782..635b98cb8 100644 --- a/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml +++ b/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml @@ -50,3 +50,9 @@ runtime: servers: - ${topicServer:message-router} topicCommInfrastructure: dmaap + +management: + endpoints: + web: + exposure: + include: health, metrics, prometheus diff --git a/pom.xml b/pom.xml index 61af7c48e..9fdfed004 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ org.onap.policy.parent integration - 3.4.0-SNAPSHOT + 3.4.1-SNAPSHOT org.onap.policy.clamp diff --git a/runtime-controlloop/pom.xml b/runtime-controlloop/pom.xml index 508f44a80..54405548f 100644 --- a/runtime-controlloop/pom.xml +++ b/runtime-controlloop/pom.xml @@ -83,6 +83,16 @@ springfox-boot-starter ${version.springfox} + + org.springframework.boot + spring-boot-starter-actuator + ${version.springboot} + + + io.micrometer + micrometer-registry-prometheus + ${version.io.micrometer} + io.springfox springfox-swagger-ui diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/ConverterConfiguration.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/ConverterConfiguration.java index ee0461994..b14c675df 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/ConverterConfiguration.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/ConverterConfiguration.java @@ -20,9 +20,12 @@ package org.onap.policy.clamp.controlloop.runtime.config; +import java.util.Arrays; import java.util.List; import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @@ -32,5 +35,9 @@ public class ConverterConfiguration implements WebMvcConfigurer { public void extendMessageConverters(List> converters) { converters.add(new CoderHttpMesageConverter<>("yaml")); converters.add(new CoderHttpMesageConverter<>("json")); + + StringHttpMessageConverter converter = new StringHttpMessageConverter(); + converter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN)); + converters.add(converter); } } diff --git a/runtime-controlloop/src/main/resources/application.yaml b/runtime-controlloop/src/main/resources/application.yaml index ea98aaa8c..cddb3d0fb 100644 --- a/runtime-controlloop/src/main/resources/application.yaml +++ b/runtime-controlloop/src/main/resources/application.yaml @@ -50,3 +50,9 @@ runtime: servers: - ${topicServer:localhost} topicCommInfrastructure: dmaap + +management: + endpoints: + web: + exposure: + include: health, metrics, prometheus diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/rest/ActuatorControllerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/rest/ActuatorControllerTest.java new file mode 100644 index 000000000..433e914ae --- /dev/null +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/rest/ActuatorControllerTest.java @@ -0,0 +1,91 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.runtime.main.rest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.Response; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; +import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@AutoConfigureMetrics +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@TestPropertySource(locations = {"classpath:application_test.properties"}) +class ActuatorControllerTest extends CommonRestController { + + private static final String HEALTH_ENDPOINT = "health"; + private static final String METRICS_ENDPOINT = "metrics"; + private static final String PROMETHEUS_ENDPOINT = "prometheus"; + + @LocalServerPort + private int randomServerPort; + + @BeforeEach + public void setUpPort() { + super.setHttpPrefix(randomServerPort); + } + + @Test + void testGetHealth_Unauthorized() throws Exception { + assertUnauthorizedActGet(HEALTH_ENDPOINT); + } + + @Test + void testGetMetrics_Unauthorized() throws Exception { + assertUnauthorizedActGet(METRICS_ENDPOINT); + } + + @Test + void testGetPrometheus_Unauthorized() throws Exception { + assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); + } + + @Test + void testGetHealth() throws Exception { + Invocation.Builder invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + + @Test + void testGetMetrics() throws Exception { + Invocation.Builder invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + + @Test + void testGePrometheus() throws Exception { + Invocation.Builder invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } +} diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java index eebaa5281..ccac0c63b 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/rest/CommonRestController.java @@ -42,7 +42,9 @@ import org.onap.policy.common.utils.network.NetworkUtil; public class CommonRestController { public static final String SELF = NetworkUtil.getHostname(); - public static final String ENDPOINT_PREFIX = "onap/controlloop/v2/"; + public static final String CONTEXT_PATH = "onap/controlloop"; + public static final String ENDPOINT_PREFIX = CONTEXT_PATH + "/v2/"; + public static final String ACTUATOR_ENDPOINT = CONTEXT_PATH + "/actuator/"; private static String httpPrefix; @@ -71,7 +73,18 @@ public class CommonRestController { } /** - * Sends a request to an endpoint, without any authorization header. + * Sends a request to an actuator endpoint. + * + * @param endpoint the target endpoint + * @return a request builder + * @throws Exception if an error occurs + */ + protected Invocation.Builder sendActRequest(final String endpoint) throws Exception { + return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, true); + } + + /** + * Sends a request to an Rest Api endpoint, without any authorization header. * * @param endpoint the target endpoint * @return a request builder @@ -81,6 +94,17 @@ public class CommonRestController { return sendFqeRequest(httpPrefix + ENDPOINT_PREFIX + endpoint, false); } + /** + * Sends a request to an actuator endpoint, without any authorization header. + * + * @param endpoint the target endpoint + * @return a request builder + * @throws Exception if an error occurs + */ + protected Invocation.Builder sendNoAuthActRequest(final String endpoint) throws Exception { + return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, false); + } + /** * Sends a request to a fully qualified endpoint. * @@ -102,7 +126,7 @@ public class CommonRestController { final WebTarget webTarget = client.target(fullyQualifiedEndpoint); - return webTarget.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON); + return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN); } /** @@ -140,6 +164,17 @@ public class CommonRestController { assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); } + /** + * Assert that GET call to actuator endpoint is Unauthorized. + * + * @param endPoint the endpoint + * @throws Exception if an error occurs + */ + protected void assertUnauthorizedActGet(final String endPoint) throws Exception { + Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke(); + assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); + } + /** * Assert that DELETE call is Unauthorized. * @@ -159,4 +194,8 @@ public class CommonRestController { protected void setHttpPrefix(int port) { httpPrefix = "http://" + SELF + ":" + port + "/"; } + + protected String getHttpPrefix() { + return httpPrefix; + } } diff --git a/runtime-controlloop/src/test/resources/application_test.properties b/runtime-controlloop/src/test/resources/application_test.properties index ad2a4b12c..0074d9f95 100644 --- a/runtime-controlloop/src/test/resources/application_test.properties +++ b/runtime-controlloop/src/test/resources/application_test.properties @@ -28,3 +28,5 @@ runtime.topicParameterGroup.topicSources[0].fetchTimeout=15000 runtime.topicParameterGroup.topicSinks[0].topic=POLICY-CLRUNTIME-PARTICIPANT runtime.topicParameterGroup.topicSinks[0].servers[0]=localhost runtime.topicParameterGroup.topicSinks[0].topicCommInfrastructure=dmaap + +management.endpoints.web.exposure.include=health,metrics,prometheus -- cgit 1.2.3-korg