[AAI] Export relevant key metrics for monitoring in Prometheus 24/121424/3
authorroger yuan <roger.yuan@yoppworks.com>
Wed, 19 May 2021 16:02:03 +0000 (10:02 -0600)
committerroger yuan <roger.yuan@yoppworks.com>
Wed, 19 May 2021 18:22:31 +0000 (12:22 -0600)
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 <roger.yuan@yoppworks.com>
Change-Id: If72858484231d95a39c938bacfc1773430806e5a

aai-traversal/pom.xml
aai-traversal/src/main/java/org/onap/aai/rest/DslConsumer.java
aai-traversal/src/main/java/org/onap/aai/rest/QueryConsumer.java
aai-traversal/src/main/java/org/onap/aai/rest/RecentAPIConsumer.java
aai-traversal/src/main/java/org/onap/aai/rest/search/SearchProvider.java
aai-traversal/src/main/java/org/onap/aai/web/MicrometerConfiguration.java [new file with mode: 0644]
aai-traversal/src/main/resources/application.properties
aai-traversal/src/test/java/org/onap/aai/rest/ConfigurationTest.java [new file with mode: 0644]
aai-traversal/src/test/resources/application-test.properties

index 5a78953..d45af14 100644 (file)
                <schema.version.list>v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23</schema.version.list>
                <schema.uri.base.path>/aai</schema.uri.base.path>
                <keycloak.version>11.0.2</keycloak.version>
+               <micrometer-spring-legacy.version>1.3.19</micrometer-spring-legacy.version>
+               <micrometer-core.version>1.6.6</micrometer-core.version>
+               <micrometer-registry-prometheus.version>1.6.6</micrometer-registry-prometheus.version>
+               <micrometer-jersey2>1.6.6</micrometer-jersey2>
                <!-- End of Default ONAP Schema Properties -->
        </properties>
        <profiles>
                <artifactId>spring-boot-starter-parent</artifactId> <type>pom</type> <scope>import</scope> 
                </dependency> </dependencies> </dependencyManagement> -->
        <dependencies>
+               <dependency>
+                       <groupId>org.springframework.boot</groupId>
+                       <artifactId>spring-boot-starter-actuator</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.springframework.boot</groupId>
+                       <artifactId>spring-boot-starter-hateoas</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>io.micrometer</groupId>
+                       <artifactId>micrometer-spring-legacy</artifactId>
+                       <version>${micrometer-spring-legacy.version}</version>
+               </dependency>
+               <dependency>
+                       <groupId>io.micrometer</groupId>
+                       <artifactId>micrometer-core</artifactId>
+                       <version>${micrometer-core.version}</version>
+               </dependency>
+               <dependency>
+                       <groupId>io.micrometer</groupId>
+                       <artifactId>micrometer-registry-prometheus</artifactId>
+                       <version>${micrometer-registry-prometheus.version}</version>
+               </dependency>
+               <dependency>
+                       <groupId>io.micrometer</groupId>
+                       <artifactId>micrometer-jersey2</artifactId>
+                       <version>${micrometer-jersey2}</version>
+               </dependency>
                <dependency>
                    <groupId>javax.jms</groupId>
                    <artifactId>javax.jms-api</artifactId>
index 41a3a3c..fefd2dc 100644 (file)
@@ -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;
index affee3a..e015ec3 100644 (file)
@@ -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;
index f2b88f3..7ec20bc 100644 (file)
@@ -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";
index 241cffd..1db2699 100644 (file)
@@ -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 (file)
index 0000000..9b48dae
--- /dev/null
@@ -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<Tag> httpLongRequestTags(RequestEvent event) {
+                return Tags.of(JerseyTags.method(event.getContainerRequest()), JerseyTags.uri(event));
+            }
+        };
+    }
+}
index a22f708..4ef501b 100644 (file)
@@ -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 (file)
index 0000000..891956d
--- /dev/null
@@ -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<String> 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<String>(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<String>(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"));
+    }
+}
index 8994c2e..b763c59 100644 (file)
@@ -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