Fix incorrect deployments counter on parallel execution 26/128426/6
authora.sreekumar <ajith.sreekumar@bell.ca>
Mon, 11 Apr 2022 10:18:04 +0000 (11:18 +0100)
committera.sreekumar <ajith.sreekumar@bell.ca>
Tue, 12 Apr 2022 10:11:07 +0000 (11:11 +0100)
Change-Id: I72bde10eae615e2c89ccc1a211c6385404b9b3c7
Issue-ID: POLICY-4088
Signed-off-by: a.sreekumar <ajith.sreekumar@bell.ca>
main/src/main/java/org/onap/policy/pap/main/notification/DeploymentStatus.java
main/src/test/java/org/onap/policy/pap/main/comm/CommonRequestBase.java
main/src/test/java/org/onap/policy/pap/main/notification/DeploymentStatusTest.java
main/src/test/java/org/onap/policy/pap/main/notification/PolicyNotifierTest.java

index 7a1aa10..24b75ab 100644 (file)
@@ -21,6 +21,7 @@
 
 package org.onap.policy.pap.main.notification;
 
+import io.micrometer.core.instrument.Counter;
 import io.micrometer.core.instrument.MeterRegistry;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -77,6 +78,10 @@ public class DeploymentStatus {
 
     private PolicyStatusService policyStatusService;
 
+    private Counter deploymentSuccessCounter;
+    private Counter unDeploymentSuccessCounter;
+    private Counter deploymentFailureCounter;
+    private Counter unDeploymentFailureCounter;
 
     /**
      * Constructs the object.
@@ -85,6 +90,27 @@ public class DeploymentStatus {
      */
     public DeploymentStatus(PolicyStatusService policyStatusService) {
         this.policyStatusService = policyStatusService;
+        initializeMetrics();
+    }
+
+    private void initializeMetrics() {
+        String counterName = "pap_" + PrometheusUtils.POLICY_DEPLOYMENTS_METRIC;
+        MeterRegistry meterRegistry = Registry.get(PapConstants.REG_METER_REGISTRY, MeterRegistry.class);
+        deploymentSuccessCounter =
+            Counter.builder(counterName).tags(PrometheusUtils.OPERATION_METRIC_LABEL, PrometheusUtils.DEPLOY_OPERATION,
+                PrometheusUtils.STATUS_METRIC_LABEL, State.SUCCESS.name()).register(meterRegistry);
+
+        unDeploymentSuccessCounter = Counter.builder(counterName).tags(PrometheusUtils.OPERATION_METRIC_LABEL,
+            PrometheusUtils.UNDEPLOY_OPERATION, PrometheusUtils.STATUS_METRIC_LABEL, State.SUCCESS.name())
+            .register(meterRegistry);
+
+        deploymentFailureCounter =
+            Counter.builder(counterName).tags(PrometheusUtils.OPERATION_METRIC_LABEL, PrometheusUtils.DEPLOY_OPERATION,
+                PrometheusUtils.STATUS_METRIC_LABEL, State.FAILURE.name()).register(meterRegistry);
+
+        unDeploymentFailureCounter = Counter.builder(counterName).tags(PrometheusUtils.OPERATION_METRIC_LABEL,
+            PrometheusUtils.UNDEPLOY_OPERATION, PrometheusUtils.STATUS_METRIC_LABEL, State.FAILURE.name())
+            .register(meterRegistry);
     }
 
     /**
@@ -178,20 +204,12 @@ 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();
+                    (value.getStatus().isDeploy() ? deploymentSuccessCounter : unDeploymentSuccessCounter).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();
+                    (value.getStatus().isDeploy() ? deploymentFailureCounter : unDeploymentFailureCounter).increment();
                 }
             }
         });
index dd20606..4086b6a 100644 (file)
@@ -28,17 +28,20 @@ import static org.mockito.Mockito.mock;
 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.LinkedList;
 import java.util.Queue;
 import java.util.function.Consumer;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.mockito.ArgumentCaptor;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher;
 import org.onap.policy.common.endpoints.listeners.TypedMessageListener;
+import org.onap.policy.common.utils.services.Registry;
 import org.onap.policy.models.pdp.concepts.PdpMessage;
 import org.onap.policy.models.pdp.concepts.PdpStateChange;
 import org.onap.policy.models.pdp.concepts.PdpStatus;
@@ -86,6 +89,11 @@ public class CommonRequestBase {
     protected RequestParams reqParams;
     protected PdpModifyRequestMapParams mapParams;
 
+    @BeforeClass
+    public static void setupBeforeAll() {
+        Registry.registerOrReplace(PapConstants.REG_METER_REGISTRY, new SimpleMeterRegistry());
+    }
+
     /**
      * Sets up.
      *
index cc1f74b..306ec8c 100644 (file)
@@ -90,7 +90,7 @@ public class DeploymentStatusTest {
      */
     @BeforeClass
     public static void setUpBeforeClass() {
-        Registry.register(PapConstants.REG_METER_REGISTRY, new SimpleMeterRegistry());
+        Registry.registerOrReplace(PapConstants.REG_METER_REGISTRY, new SimpleMeterRegistry());
     }
 
     /**
index 5fec269..48e3f57 100644 (file)
@@ -31,6 +31,7 @@ import static org.mockito.Mockito.never;
 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.Set;
 import javax.ws.rs.core.Response.Status;
@@ -41,11 +42,13 @@ 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.base.PfModelRuntimeException;
 import org.onap.policy.models.pap.concepts.PolicyNotification;
 import org.onap.policy.models.pap.concepts.PolicyStatus;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.pap.main.PapConstants;
 import org.onap.policy.pap.main.PolicyPapRuntimeException;
 import org.onap.policy.pap.main.comm.Publisher;
 import org.onap.policy.pap.main.comm.QueueToken;
@@ -91,7 +94,7 @@ public class PolicyNotifierTest {
     public void setUp() {
         try {
             when(policyStatusService.getGroupPolicyStatus(anyString())).thenReturn(Collections.emptyList());
-
+            Registry.registerOrReplace(PapConstants.REG_METER_REGISTRY, new SimpleMeterRegistry());
             notifier = new MyNotifier(publisher);
 
         } catch (PfModelException e) {