* ============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");
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;
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;
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;
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;
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;
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;
}
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());
/*-
* ============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.
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;
* 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 =
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");
*/
@Before
public void setUp() throws Exception {
+ CollectorRegistry.defaultRegistry.clear();
+
SystemPersistenceConstants.getManager().setConfigurationDir("target/test-classes");
fsm = newFsmInstance();
.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());
PolicyControllerConstants.getFactory().destroy();
SystemPersistenceConstants.getManager().setConfigurationDir(null);
+
+ CollectorRegistry.defaultRegistry.clear();
}
@Test
assertThat(
listPost("policies/operations/validation", toString(opPolicy),
Status.NOT_ACCEPTABLE.getStatusCode())).isNotEmpty();
+
+ metrics();
}
private void testNotNativePolicy(ToscaPolicy toscaPolicy) throws CoderException {
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);