add prometheus deploy counts 86/126786/2
authorjhh <jorge.hernandez-herrero@att.com>
Wed, 26 Jan 2022 18:21:46 +0000 (12:21 -0600)
committerjhh <jorge.hernandez-herrero@att.com>
Thu, 3 Feb 2022 02:25:19 +0000 (20:25 -0600)
Issue-ID: POLICY-3761
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: Ib5dd109457049ac30269d68fcb803cb4a0426c92
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java
feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java

index e0ac880..611ac24 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
  * Modifications Copyright (C) 2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,6 +22,7 @@
 package org.onap.policy.drools.lifecycle;
 
 import com.google.re2j.Pattern;
+import io.prometheus.client.Counter;
 import java.lang.reflect.InvocationTargetException;
 import java.time.Instant;
 import java.util.ArrayList;
@@ -53,6 +54,7 @@ import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
 import org.onap.policy.common.endpoints.listeners.ScoListener;
 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
 import org.onap.policy.common.utils.coder.StandardCoderObject;
+import org.onap.policy.common.utils.resources.PrometheusUtils;
 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
 import org.onap.policy.drools.policies.DomainMaker;
 import org.onap.policy.drools.system.PolicyController;
@@ -64,6 +66,7 @@ import org.onap.policy.models.pdp.concepts.PdpStatus;
 import org.onap.policy.models.pdp.concepts.PdpUpdate;
 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
 import org.onap.policy.models.pdp.enums.PdpMessageType;
+import org.onap.policy.models.pdp.enums.PdpResponseStatus;
 import org.onap.policy.models.pdp.enums.PdpState;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
@@ -100,6 +103,16 @@ public class LifecycleFsm implements Startable {
     protected static final ToscaConceptIdentifier POLICY_TYPE_DROOLS_NATIVE_CONTROLLER =
             new ToscaConceptIdentifier("onap.policies.native.drools.Controller", "1.0.0");
 
+    protected static final String PROMETHEUS_NAMESPACE = "pdpd";
+
+    protected static final Counter deploymentsCounter =
+            Counter.build().namespace(PROMETHEUS_NAMESPACE).name(PrometheusUtils.POLICY_DEPLOYMENTS_METRIC)
+                    .labelNames(PrometheusUtils.STATE_METRIC_LABEL,
+                            PrometheusUtils.OPERATION_METRIC_LABEL,
+                            PrometheusUtils.STATUS_METRIC_LABEL)
+                    .help(PrometheusUtils.POLICY_DEPLOYMENT_HELP)
+                    .register();
+
     @Getter
     protected final Properties properties;
 
@@ -436,6 +449,9 @@ public class LifecycleFsm implements Startable {
         policiesMap.computeIfAbsent(policy.getIdentifier(), key -> {
             // avoid counting reapplies in a second pass when a mix of native and non-native
             // policies are present.
+            deploymentsCounter.labels(state.state().name(),
+                    PrometheusUtils.DEPLOY_OPERATION,
+                    PdpResponseStatus.SUCCESS.name()).inc();
             getStats().setPolicyDeployCount(getStats().getPolicyDeployCount() + 1);
             getStats().setPolicyDeploySuccessCount(getStats().getPolicyDeploySuccessCount() + 1);
             return policy;
@@ -446,6 +462,9 @@ public class LifecycleFsm implements Startable {
         policiesMap.computeIfPresent(policy.getIdentifier(), (key, value) -> {
             // avoid counting reapplies in a second pass when a mix of native and non-native
             // policies are present.
+            deploymentsCounter.labels(state.state().name(),
+                    PrometheusUtils.UNDEPLOY_OPERATION,
+                    PdpResponseStatus.SUCCESS.name()).inc();
             getStats().setPolicyUndeployCount(getStats().getPolicyUndeployCount() + 1);
             getStats().setPolicyUndeploySuccessCount(getStats().getPolicyUndeploySuccessCount() + 1);
             return null;
@@ -453,11 +472,17 @@ public class LifecycleFsm implements Startable {
     }
 
     protected void failedDeployPolicyAction(@NonNull ToscaPolicy failedPolicy) {    // NOSONAR
+        deploymentsCounter.labels(state.state().name(),
+                PrometheusUtils.DEPLOY_OPERATION,
+                PdpResponseStatus.FAIL.name()).inc();
         getStats().setPolicyDeployCount(getStats().getPolicyDeployCount() + 1);
         getStats().setPolicyDeployFailCount(getStats().getPolicyDeployFailCount() + 1);
     }
 
     protected void failedUndeployPolicyAction(ToscaPolicy failedPolicy) {
+        deploymentsCounter.labels(state.state().name(),
+                PrometheusUtils.UNDEPLOY_OPERATION,
+                PdpResponseStatus.FAIL.name()).inc();
         getStats().setPolicyUndeployCount(getStats().getPolicyUndeployCount() + 1);
         getStats().setPolicyUndeployFailCount(getStats().getPolicyUndeployFailCount() + 1);
         policiesMap.remove(failedPolicy.getIdentifier());
index 65228a9..cb2d5ad 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2022 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.
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import io.prometheus.client.CollectorRegistry;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
@@ -65,7 +66,6 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
  * REST Lifecycle Manager Test.
  */
 public class RestLifecycleManagerTest {
-
     // Native Drools Policy
     private static final String EXAMPLE_NATIVE_CONTROLLER_POLICY_NAME = "example.controller";
     private static final String EXAMPLE_NATIVE_CONTROLLER_POLICY_JSON =
@@ -91,6 +91,13 @@ public class RestLifecycleManagerTest {
     private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON =
             "policies/vCPE.policy.operational.input.tosca.json";
 
+    public static final String PROM_DEPLOY_REQUESTS_TOTAL_UNDEPLOY_ACCEPTED =
+            "pdpd_policy_deployments_total{state=\"ACTIVE\",operation=\"undeploy\",status=\"SUCCESS\",}";
+    public static final String PDPD_DEPLOY_REQUESTS_TOTAL_DEPLOY_ACCEPTED =
+            "pdpd_policy_deployments_total{state=\"ACTIVE\",operation=\"deploy\",status=\"SUCCESS\",}";
+    public static final String PDPD_DEPLOY_REQUESTS_TOTAL_DEPLOY_DECLINED =
+            "pdpd_policy_deployments_total{state=\"ACTIVE\",operation=\"deploy\",status=\"FAIL\",}";
+
     private static final StandardCoder coder = new StandardCoder();
     private static final ControllerSupport controllerSupport = new ControllerSupport("lifecycle");
 
@@ -102,6 +109,8 @@ public class RestLifecycleManagerTest {
      */
     @Before
      public void setUp() throws Exception {
+        CollectorRegistry.defaultRegistry.clear();
+
         SystemPersistenceConstants.getManager().setConfigurationDir("target/test-classes");
         fsm = newFsmInstance();
 
@@ -123,8 +132,9 @@ public class RestLifecycleManagerTest {
                 .build());
 
         HttpServletServer server =
-                        HttpServletServerFactoryInstance.getServerFactory().build("lifecycle", "localhost", 8765, "/",
+            HttpServletServerFactoryInstance.getServerFactory().build("lifecycle", "localhost", 8765, "/",
                                         true, true);
+        server.setPrometheus("/policy/pdp/engine/lifecycle/metrics");
         server.setSerializationProvider(
                         String.join(",", JacksonHandler.class.getName(), YamlJacksonHandler.class.getName()));
         server.addServletClass("/*", RestLifecycleManager.class.getName());
@@ -159,6 +169,8 @@ public class RestLifecycleManagerTest {
 
         PolicyControllerConstants.getFactory().destroy();
         SystemPersistenceConstants.getManager().setConfigurationDir(null);
+
+        CollectorRegistry.defaultRegistry.clear();
     }
 
     @Test
@@ -329,6 +341,8 @@ public class RestLifecycleManagerTest {
         assertThat(
             listPost("policies/operations/validation", toString(opPolicy),
                     Status.NOT_ACCEPTABLE.getStatusCode())).isNotEmpty();
+
+        metrics();
     }
 
     private void testNotNativePolicy(ToscaPolicy toscaPolicy) throws CoderException {
@@ -439,6 +453,15 @@ public class RestLifecycleManagerTest {
         assertEquals("GG", HttpClient.getBody(response, String.class));
     }
 
+    private void metrics() {
+        Response response = client.get("metrics");
+        assertEquals(Status.OK.getStatusCode(), response.getStatus());
+        String body = HttpClient.getBody(response, String.class);
+        assertThat(body).contains(PROM_DEPLOY_REQUESTS_TOTAL_UNDEPLOY_ACCEPTED);
+        assertThat(body).contains(PDPD_DEPLOY_REQUESTS_TOTAL_DEPLOY_ACCEPTED);
+        assertThat(body).contains(PDPD_DEPLOY_REQUESTS_TOTAL_DEPLOY_DECLINED);
+    }
+
     private LifecycleFsm newFsmInstance() throws NoSuchFieldException, IllegalAccessException {
         LifecycleFsm fsm = new LifecycleFsm();
         ControllerSupport.setStaticField(LifecycleFeature.class, "fsm", fsm);