From 6180838e24698c885091d65603c6ce19c54853f4 Mon Sep 17 00:00:00 2001 From: roger yuan Date: Wed, 19 May 2021 10:02:03 -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: If72858484231d95a39c938bacfc1773430806e5a --- .../main/java/org/onap/aai/rest/DslConsumer.java | 2 + .../main/java/org/onap/aai/rest/QueryConsumer.java | 2 + .../java/org/onap/aai/rest/RecentAPIConsumer.java | 2 + .../org/onap/aai/rest/search/SearchProvider.java | 2 + .../org/onap/aai/web/MicrometerConfiguration.java | 60 ++++++++++++ .../src/main/resources/application.properties | 13 ++- .../java/org/onap/aai/rest/ConfigurationTest.java | 105 +++++++++++++++++++++ .../src/test/resources/application-test.properties | 9 +- 8 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 aai-traversal/src/main/java/org/onap/aai/web/MicrometerConfiguration.java create mode 100644 aai-traversal/src/test/java/org/onap/aai/rest/ConfigurationTest.java (limited to 'aai-traversal/src') diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/DslConsumer.java b/aai-traversal/src/main/java/org/onap/aai/rest/DslConsumer.java index 41a3a3c..fefd2dc 100644 --- a/aai-traversal/src/main/java/org/onap/aai/rest/DslConsumer.java +++ b/aai-traversal/src/main/java/org/onap/aai/rest/DslConsumer.java @@ -22,6 +22,7 @@ package org.onap.aai.rest; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import io.micrometer.core.annotation.Timed; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; import org.janusgraph.core.SchemaViolationException; import org.onap.aai.concurrent.AaiCallable; @@ -58,6 +59,7 @@ import java.util.*; import java.util.stream.Collectors; @Path("{version: v[1-9][0-9]*|latest}/dsl") +@Timed public class DslConsumer extends TraversalConsumer { private HttpEntry traversalUriHttpEntry; diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/QueryConsumer.java b/aai-traversal/src/main/java/org/onap/aai/rest/QueryConsumer.java index affee3a..e015ec3 100644 --- a/aai-traversal/src/main/java/org/onap/aai/rest/QueryConsumer.java +++ b/aai-traversal/src/main/java/org/onap/aai/rest/QueryConsumer.java @@ -22,6 +22,7 @@ package org.onap.aai.rest; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import io.micrometer.core.annotation.Timed; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.onap.aai.concurrent.AaiCallable; @@ -62,6 +63,7 @@ import java.util.*; import java.util.stream.Collectors; @Path("{version: v[1-9][0-9]*|latest}/query") +@Timed public class QueryConsumer extends TraversalConsumer { private QueryProcessorType processorType = QueryProcessorType.LOCAL_GROOVY; diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/RecentAPIConsumer.java b/aai-traversal/src/main/java/org/onap/aai/rest/RecentAPIConsumer.java index f2b88f3..7ec20bc 100644 --- a/aai-traversal/src/main/java/org/onap/aai/rest/RecentAPIConsumer.java +++ b/aai-traversal/src/main/java/org/onap/aai/rest/RecentAPIConsumer.java @@ -19,6 +19,7 @@ */ package org.onap.aai.rest; +import io.micrometer.core.annotation.Timed; import org.onap.aai.concurrent.AaiCallable; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.ModelType; @@ -54,6 +55,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; @Path("/recents/{version: v[1-9][0-9]*|latest}") +@Timed public class RecentAPIConsumer extends RESTAPI { private static final String AAI_3021 = "AAI_3021"; diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/search/SearchProvider.java b/aai-traversal/src/main/java/org/onap/aai/rest/search/SearchProvider.java index 241cffd..1db2699 100644 --- a/aai-traversal/src/main/java/org/onap/aai/rest/search/SearchProvider.java +++ b/aai-traversal/src/main/java/org/onap/aai/rest/search/SearchProvider.java @@ -19,6 +19,7 @@ */ package org.onap.aai.rest.search; +import io.micrometer.core.annotation.Timed; import org.onap.aai.aailog.logs.AaiDBTraversalMetricLog; import org.onap.aai.concurrent.AaiCallable; import org.onap.aai.dbgraphmap.SearchGraph; @@ -59,6 +60,7 @@ import java.util.Optional; * X-TransactionId in the header. */ @Path("/{version: v[1-9][0-9]*|latest}/search") +@Timed public class SearchProvider extends RESTAPI { private static final Logger LOGGER = LoggerFactory.getLogger(SearchProvider.class); diff --git a/aai-traversal/src/main/java/org/onap/aai/web/MicrometerConfiguration.java b/aai-traversal/src/main/java/org/onap/aai/web/MicrometerConfiguration.java new file mode 100644 index 0000000..9b48dae --- /dev/null +++ b/aai-traversal/src/main/java/org/onap/aai/web/MicrometerConfiguration.java @@ -0,0 +1,60 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.web; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Tags; +import io.micrometer.jersey2.server.JerseyTags; +import io.micrometer.jersey2.server.JerseyTagsProvider; +import org.glassfish.jersey.server.ContainerResponse; +import org.glassfish.jersey.server.monitoring.RequestEvent; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Configuration Class to add customized tags to http metrics scraped in /actuator/prometheus endpoint + */ +@Configuration +public class MicrometerConfiguration { + private static final String TAG_AAI_URI = "aai_uri"; + private static final String NOT_AVAILABLE = "NOT AVAILABLE"; + + @Bean + public JerseyTagsProvider jerseyTagsProvider() { + return new JerseyTagsProvider() { + @Override + public Iterable httpRequestTags(RequestEvent event) { + ContainerResponse response = event.getContainerResponse(); + return Tags.of(JerseyTags.method(event.getContainerRequest()), + JerseyTags.exception(event), JerseyTags.status(response), JerseyTags.outcome(response), getAaiUriTag(event)); + } + private Tag getAaiUriTag(RequestEvent event) { + String aai_uri = event.getUriInfo().getRequestUri().toString(); + if (aai_uri == null) { + aai_uri = NOT_AVAILABLE; + } + return Tag.of(TAG_AAI_URI, aai_uri); + } + @Override + public Iterable httpLongRequestTags(RequestEvent event) { + return Tags.of(JerseyTags.method(event.getContainerRequest()), JerseyTags.uri(event)); + } + }; + } +} diff --git a/aai-traversal/src/main/resources/application.properties b/aai-traversal/src/main/resources/application.properties index a22f708..4ef501b 100644 --- a/aai-traversal/src/main/resources/application.properties +++ b/aai-traversal/src/main/resources/application.properties @@ -13,7 +13,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.jersey.application-path=${schema.uri.base.path} @@ -112,3 +113,13 @@ aaf.cadi.file=${server.local.startpath}/cadi.properties history.enabled=false; history.truncate.window.days=365 + +#To Expose the Prometheus scraping endpoint +management.server.port=8448 +#To Enable Actuator Endpoint, you can override this to True in OOM charts +management.endpoints.enabled-by-default=false +#To Enable Actuator Endpoint, you can override this in OOM Charts +#management.endpoints.web.exposure.include=info, health, prometheus +management.metrics.web.server.auto-time-requests=false +management.metrics.tags.group_id=aai +management.metrics.tags.app_id=${info.build.artifact} diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/ConfigurationTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/ConfigurationTest.java new file mode 100644 index 0000000..891956d --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/rest/ConfigurationTest.java @@ -0,0 +1,105 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.rest; + +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.TraversalApp; +import org.onap.aai.TraversalTestConfiguration; +import org.onap.aai.config.SpringContextAware; +import org.onap.aai.restclient.PropertyPasswordConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.http.*; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.web.client.RestTemplate; + +import java.io.UnsupportedEncodingException; +import java.util.Base64; +import java.util.Collections; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Test REST requests against configuration resource + */ +@TestPropertySource(locations = "classpath:application-test.properties") +@ContextConfiguration(initializers = PropertyPasswordConfiguration.class, classes = {SpringContextAware.class}) +@Import(TraversalTestConfiguration.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {SpringContextAware.class, TraversalApp.class}) +public class ConfigurationTest extends AbstractSpringRestTest { + @Autowired + RestTemplate restTemplate; + + @Value("${local.management.port}") + private int mgtPort; + + private HttpEntity httpEntity; + private String actuatorurl; + private HttpHeaders headers; + @Before + public void setup() throws UnsupportedEncodingException { + + headers = new HttpHeaders(); + + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + headers.set("Accept", "text/plain"); + headers.add("Real-Time", "true"); + headers.add("X-FromAppId", "JUNIT"); + headers.add("X-TransactionId", "JUNIT"); + + String authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8")); + headers.add("Authorization", "Basic " + authorization); + + httpEntity = new HttpEntity(headers); + baseUrl = "http://localhost:" + randomPort; + actuatorurl = "http://localhost:" + mgtPort; + } + + @Test + public void TestManagementEndpointConfiguration() { + ResponseEntity responseEntity = null; + String responseBody = null; + + //set Accept as text/plain in order to get access of endpoint "/actuator/prometheus" + 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-traversal")); + + 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-traversal/src/test/resources/application-test.properties b/aai-traversal/src/test/resources/application-test.properties index 8994c2e..b763c59 100644 --- a/aai-traversal/src/test/resources/application-test.properties +++ b/aai-traversal/src/test/resources/application-test.properties @@ -12,7 +12,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.jersey.application-path=${schema.uri.base.path} @@ -80,3 +81,9 @@ schema.version.edge.label.start=v12 schema.version.api.default=v23 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 \ No newline at end of file -- cgit 1.2.3-korg