diff options
author | a.sreekumar <ajith.sreekumar@bell.ca> | 2022-02-21 13:13:20 +0000 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@bell.ca> | 2022-02-23 10:29:36 +0000 |
commit | f0262c356f164c9a84bd3199e5f32b16356726fa (patch) | |
tree | fedc98bd66f3ba3223e646f145206bf9c961bad5 | |
parent | beef773a297cbeb5c5abfb5e75e678e39d3d1523 (diff) |
Adding deployment metrics to PAP
Change-Id: I4b6a93045c1ddfd7fff037e7568b029e2e45b0b3
Issue-ID: POLICY-3757
Signed-off-by: a.sreekumar <ajith.sreekumar@bell.ca>
9 files changed, 88 insertions, 3 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/PapConstants.java b/main/src/main/java/org/onap/policy/pap/main/PapConstants.java index df43c69f..36d5eeb2 100644 --- a/main/src/main/java/org/onap/policy/pap/main/PapConstants.java +++ b/main/src/main/java/org/onap/policy/pap/main/PapConstants.java @@ -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"; diff --git a/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentStatus.java b/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentStatus.java index 20e59339..7a1aa107 100644 --- a/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentStatus.java +++ b/main/src/main/java/org/onap/policy/pap/main/notification/DeploymentStatus.java @@ -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. */ diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java index 00207220..f8c4fb32 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java @@ -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())); diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/DeploymentStatusTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/DeploymentStatusTest.java index 3b7f7e16..cc1f74b4 100644 --- a/main/src/test/java/org/onap/policy/pap/main/notification/DeploymentStatusTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/notification/DeploymentStatusTest.java @@ -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; @@ -81,6 +86,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. */ @Before @@ -97,6 +118,7 @@ public class DeploymentStatusTest { .deploy(true) .state(State.SUCCESS); // @formatter:on + } @Test diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java b/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java index 153a2bfa..b5667145 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java @@ -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); + } /** diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java index 9c9f36ad..575e33ff 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java @@ -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(); } diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeleteTest.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeleteTest.java index e3ad0044..a8387881 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeleteTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeleteTest.java @@ -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()); diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeployTest.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeployTest.java index cdb1fa9d..df749a88 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeployTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeployTest.java @@ -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); } } diff --git a/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java index 2204392a..c11af694 100644 --- a/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java +++ b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java @@ -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()); } |