Adding deployment metrics to PAP 22/127222/5
authora.sreekumar <ajith.sreekumar@bell.ca>
Mon, 21 Feb 2022 13:13:20 +0000 (13:13 +0000)
committera.sreekumar <ajith.sreekumar@bell.ca>
Wed, 23 Feb 2022 10:29:36 +0000 (10:29 +0000)
Change-Id: I4b6a93045c1ddfd7fff037e7568b029e2e45b0b3
Issue-ID: POLICY-3757
Signed-off-by: a.sreekumar <ajith.sreekumar@bell.ca>
main/src/main/java/org/onap/policy/pap/main/PapConstants.java
main/src/main/java/org/onap/policy/pap/main/notification/DeploymentStatus.java
main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java
main/src/test/java/org/onap/policy/pap/main/notification/DeploymentStatusTest.java
main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java
main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java
main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeleteTest.java
main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeployTest.java
main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java

index df43c69..36d5eeb 100644 (file)
@@ -33,6 +33,7 @@ public final class PapConstants {
     public static final String REG_STATISTICS_MANAGER = "object:manager/statistics";
     public static final String REG_PDP_MODIFY_LOCK = "lock:pdp";
     public static final String REG_PDP_MODIFY_MAP = "object:pdp/modify/map";
+    public static final String REG_METER_REGISTRY = "object:meter/registry";
 
     // topic names
     public static final String TOPIC_POLICY_PDP_PAP = "POLICY-PDP-PAP";
index 20e5933..7a1aa10 100644 (file)
@@ -21,6 +21,7 @@
 
 package org.onap.policy.pap.main.notification;
 
+import io.micrometer.core.instrument.MeterRegistry;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -33,10 +34,13 @@ import java.util.function.BiPredicate;
 import java.util.stream.Collectors;
 import lombok.AccessLevel;
 import lombok.Getter;
+import org.onap.policy.common.utils.resources.PrometheusUtils;
+import org.onap.policy.common.utils.services.Registry;
 import org.onap.policy.models.pap.concepts.PolicyNotification;
 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.pap.main.PapConstants;
 import org.onap.policy.pap.main.notification.StatusAction.Action;
 import org.onap.policy.pap.main.service.PolicyStatusService;
 
@@ -51,6 +55,7 @@ import org.onap.policy.pap.main.service.PolicyStatusService;
  * </ol>
  */
 public class DeploymentStatus {
+
     /**
      * Tracks the groups that have been loaded.
      */
@@ -123,6 +128,7 @@ public class DeploymentStatus {
     public void flush(PolicyNotification notif) {
         // must add notifications BEFORE deleting undeployments
         addNotifications(notif);
+        updateMetrics();
         deleteUndeployments();
         flush();
     }
@@ -171,6 +177,26 @@ public class DeploymentStatus {
         }
     }
 
+    private void updateMetrics() {
+        MeterRegistry meterRegistry = Registry.get(PapConstants.REG_METER_REGISTRY, MeterRegistry.class);
+        String counterName = "pap_" + PrometheusUtils.POLICY_DEPLOYMENTS_METRIC;
+        recordMap.forEach((key, value) -> {
+            if (value.getAction().equals(StatusAction.Action.UPDATED)) {
+                if (value.getStatus().getState().equals(State.SUCCESS)) {
+                    meterRegistry.counter(counterName, PrometheusUtils.OPERATION_METRIC_LABEL,
+                        value.getStatus().isDeploy() ? PrometheusUtils.DEPLOY_OPERATION
+                            : PrometheusUtils.UNDEPLOY_OPERATION,
+                        PrometheusUtils.STATUS_METRIC_LABEL, State.SUCCESS.name()).increment();
+                } else if (value.getStatus().getState().equals(State.FAILURE)) {
+                    meterRegistry.counter(counterName, PrometheusUtils.OPERATION_METRIC_LABEL,
+                        value.getStatus().isDeploy() ? PrometheusUtils.DEPLOY_OPERATION
+                            : PrometheusUtils.UNDEPLOY_OPERATION,
+                        PrometheusUtils.STATUS_METRIC_LABEL, State.FAILURE.name()).increment();
+                }
+            }
+        });
+    }
+
     /**
      * Deletes records for any policies that have been completely undeployed.
      */
index 0020722..f8c4fb3 100644 (file)
@@ -22,6 +22,7 @@
 
 package org.onap.policy.pap.main.startstop;
 
+import io.micrometer.core.instrument.MeterRegistry;
 import java.util.List;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
@@ -92,7 +93,8 @@ public class PapActivator extends ServiceManagerContainer {
      * @param papParameterGroup the parameters for the pap service
      */
     public PapActivator(PapParameterGroup papParameterGroup, PolicyNotifier policyNotifier,
-        PdpHeartbeatListener pdpHeartbeatListener, PdpModifyRequestMap pdpModifyRequestMap) {
+        PdpHeartbeatListener pdpHeartbeatListener, PdpModifyRequestMap pdpModifyRequestMap,
+        MeterRegistry meterRegistry) {
         super("Policy PAP");
         this.papParameterGroup = papParameterGroup;
         TopicEndpointManager.getManager().addTopics(papParameterGroup.getTopicParameterGroup());
@@ -118,6 +120,11 @@ public class PapActivator extends ServiceManagerContainer {
         final AtomicReference<PdpModifyRequestMap> requestMap = new AtomicReference<>();
 
         // @formatter:off
+
+        addAction("Meter Registry",
+            () -> Registry.register(PapConstants.REG_METER_REGISTRY, meterRegistry),
+            () -> Registry.unregister(PapConstants.REG_METER_REGISTRY));
+
         addAction("PAP parameters",
             () -> ParameterService.register(papParameterGroup),
             () -> ParameterService.deregister(papParameterGroup.getName()));
index 3b7f7e1..cc1f74b 100644 (file)
@@ -26,6 +26,7 @@ import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -33,13 +34,16 @@ import java.util.Map;
 import java.util.Set;
 import lombok.NonNull;
 import org.apache.commons.lang3.builder.CompareToBuilder;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Captor;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.common.utils.services.Registry;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.pap.concepts.PolicyNotification;
 import org.onap.policy.models.pap.concepts.PolicyStatus;
@@ -47,6 +51,7 @@ import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.PdpPolicyStatusBuilder;
 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.pap.main.PapConstants;
 import org.onap.policy.pap.main.notification.StatusAction.Action;
 import org.onap.policy.pap.main.service.PolicyStatusService;
 
@@ -80,6 +85,22 @@ public class DeploymentStatusTest {
 
     private DeploymentStatus tracker;
 
+    /**
+     * Set up the meter registry for tests.
+     */
+    @BeforeClass
+    public static void setUpBeforeClass() {
+        Registry.register(PapConstants.REG_METER_REGISTRY, new SimpleMeterRegistry());
+    }
+
+    /**
+     * Tear down the meter registry after tests.
+     */
+    @AfterClass
+    public static void tearDownAfterClass() {
+        Registry.unregister(PapConstants.REG_METER_REGISTRY);
+    }
+
     /**
      * Sets up.
      */
@@ -97,6 +118,7 @@ public class DeploymentStatusTest {
                         .deploy(true)
                         .state(State.SUCCESS);
         // @formatter:on
+
     }
 
     @Test
index 153a2bf..b566714 100644 (file)
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import io.micrometer.core.instrument.MeterRegistry;
 import java.io.File;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -92,6 +93,7 @@ public class ProviderSuper {
     protected PdpModifyRequestMap reqmap;
     protected ToscaPolicy policy1;
     protected PapStatisticsManager statsmanager;
+    protected MeterRegistry meterRegistry;
 
     /**
      * Configures DAO, captors, and various mocks.
@@ -109,6 +111,8 @@ public class ProviderSuper {
         policy1 = loadPolicy("policy.json");
         statsmanager = mock(PapStatisticsManager.class);
 
+        meterRegistry = mock(MeterRegistry.class);
+
         List<PdpGroup> groups = loadGroups("groups.json");
 
         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(groups);
@@ -119,6 +123,8 @@ public class ProviderSuper {
         Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, lockit);
         Registry.register(PapConstants.REG_PDP_MODIFY_MAP, reqmap);
         Registry.register(PapConstants.REG_STATISTICS_MANAGER, statsmanager);
+        Registry.register(PapConstants.REG_METER_REGISTRY, meterRegistry);
+
     }
 
     /**
index 9c9f36a..575e33f 100644 (file)
@@ -22,6 +22,7 @@
 
 package org.onap.policy.pap.main.rest.e2e;
 
+import io.micrometer.core.instrument.MeterRegistry;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -34,12 +35,14 @@ import org.onap.policy.common.parameters.ValidationResult;
 import org.onap.policy.common.utils.coder.Coder;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.resources.PrometheusUtils;
 import org.onap.policy.common.utils.resources.ResourceUtils;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.pdp.concepts.PdpGroup;
 import org.onap.policy.models.pdp.concepts.PdpGroups;
 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
+import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
 import org.onap.policy.models.pdp.concepts.PdpStatistics;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
@@ -81,6 +84,15 @@ public abstract class End2EndBase extends CommonPapRestServer {
     @Autowired
     public ToscaServiceTemplateService toscaService;
 
+    @Autowired
+    public MeterRegistry meterRegistry;
+
+    public String deploymentsCounterName = "pap_" + PrometheusUtils.POLICY_DEPLOYMENTS_METRIC;
+    public String[] deploymentSuccessTag = {PrometheusUtils.OPERATION_METRIC_LABEL, PrometheusUtils.DEPLOY_OPERATION,
+        PrometheusUtils.STATUS_METRIC_LABEL, State.SUCCESS.name()};
+    public String[] unDeploymentSuccessTag = {PrometheusUtils.OPERATION_METRIC_LABEL,
+        PrometheusUtils.UNDEPLOY_OPERATION, PrometheusUtils.STATUS_METRIC_LABEL, State.SUCCESS.name()};
+
     /**
      * Tears down.
      */
@@ -95,7 +107,7 @@ public abstract class End2EndBase extends CommonPapRestServer {
             }
             context = null;
         }
-
+        meterRegistry.clear();
         super.tearDown();
     }
 
index e3ad004..a838788 100644 (file)
@@ -22,6 +22,7 @@
 
 package org.onap.policy.pap.main.rest.e2e;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
@@ -149,6 +150,8 @@ public class PdpGroupDeleteTest extends End2EndBase {
         assertEquals(0, deleted.getIncompleteCount());
         assertEquals(new ToscaConceptIdentifier("onap.restart.tcaB", "1.0.0"), deleted.getPolicy());
 
+        assertThat(meterRegistry.counter(deploymentsCounterName, unDeploymentSuccessTag).count()).isEqualTo(2);
+
         rawresp = invocationBuilder.delete();
         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
index cdb1fa9..df749a8 100644 (file)
@@ -22,6 +22,7 @@
 
 package org.onap.policy.pap.main.rest.e2e;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
@@ -121,6 +122,8 @@ public class PdpGroupDeployTest extends End2EndBase {
         assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
         assertNull(resp.getErrorDetails());
 
+        assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isEqualTo(2);
+
         // repeat with unknown group - should fail
         DeploymentGroup group = groups.getGroups().get(0);
         group.setName("unknown-group");
@@ -167,6 +170,8 @@ public class PdpGroupDeployTest extends End2EndBase {
             notifications.add(msg);
         });
 
+        assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isEqualTo(0);
+
         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
 
         PdpDeployPolicies policies = loadJsonFile("deployPoliciesReq2.json", PdpDeployPolicies.class);
@@ -202,5 +207,7 @@ public class PdpGroupDeployTest extends End2EndBase {
         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
         assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
         assertNull(resp.getErrorDetails());
+
+        assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isEqualTo(2);
     }
 }
index 2204392..c11af69 100644 (file)
@@ -29,6 +29,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.nio.charset.StandardCharsets;
@@ -93,7 +94,7 @@ public class TestPapActivator {
         final PapParameterGroup parGroup = new CommonTestData().getPapParameterGroup(6969);
 
         activator = new PapActivator(parGroup, new PolicyNotifier(null), new PdpHeartbeatListener(),
-            new PdpModifyRequestMap(null, null, null, null, null));
+            new PdpModifyRequestMap(null, null, null, null, null), new SimpleMeterRegistry());
 
     }