2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2021 Bell Canada Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.service.engine.main;
25 import io.prometheus.client.Counter;
26 import java.util.concurrent.atomic.AtomicLong;
27 import lombok.NoArgsConstructor;
28 import org.onap.policy.common.utils.resources.PrometheusUtils;
29 import org.onap.policy.common.utils.services.Registry;
30 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 public class ApexPolicyStatisticsManager {
36 private static final Logger LOGGER = LoggerFactory.getLogger(ApexPolicyStatisticsManager.class);
38 static final Counter POLICY_DEPLOYMENTS_COUNTER =
39 Counter.build().namespace(PrometheusUtils.PdpType.PDPA.getNamespace())
40 .name(PrometheusUtils.POLICY_DEPLOYMENTS_METRIC)
41 .labelNames(PrometheusUtils.OPERATION_METRIC_LABEL, PrometheusUtils.STATUS_METRIC_LABEL)
42 .help(PrometheusUtils.POLICY_DEPLOYMENT_HELP).register();
44 public static final String REG_APEX_PDP_POLICY_COUNTER = "object:pdp/statistics/policy/counter";
45 private static final String PROMETHEUS_TOTAL_LABEL_VALUE = "TOTAL";
47 private final AtomicLong policyDeployCount = new AtomicLong(0);
48 private final AtomicLong policyDeploySuccessCount = new AtomicLong(0);
49 private final AtomicLong policyDeployFailCount = new AtomicLong(0);
50 private final AtomicLong policyUndeployCount = new AtomicLong(0);
51 private final AtomicLong policyUndeploySuccessCount = new AtomicLong(0);
52 private final AtomicLong policyUndeployFailCount = new AtomicLong(0);
53 private final AtomicLong policyExecutedCount = new AtomicLong(0);
54 private final AtomicLong policyExecutedSuccessCount = new AtomicLong(0);
55 private final AtomicLong policyExecutedFailCount = new AtomicLong(0);
58 * To get the ApexPolicyStatisticsManager in Registry.
60 * @return ApexPolicyStatisticsManager The obj in Registry.
62 public static ApexPolicyStatisticsManager getInstanceFromRegistry() {
63 ApexPolicyStatisticsManager instance = null;
65 instance = Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER);
66 } catch (IllegalArgumentException e) {
67 LOGGER.debug("ApexPolicyStatisticsManager is not registered yet");
73 * Update the policy deploy count.
75 public void updatePolicyDeployCounter(final boolean isSuccessful) {
76 POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.DEPLOY_OPERATION, PROMETHEUS_TOTAL_LABEL_VALUE).inc();
77 this.policyDeployCount.incrementAndGet();
79 POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.DEPLOY_OPERATION, PdpResponseStatus.FAIL.name()).inc();
80 this.policyDeployFailCount.incrementAndGet();
82 POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.DEPLOY_OPERATION, PdpResponseStatus.SUCCESS.name()).inc();
83 this.policyDeploySuccessCount.incrementAndGet();
88 * Update the policy executed count.
90 public void updatePolicyExecutedCounter(final boolean isSuccessful) {
91 this.policyExecutedCount.incrementAndGet();
93 this.policyExecutedSuccessCount.incrementAndGet();
95 this.policyExecutedFailCount.incrementAndGet();
100 * Update the policy undeploy count.
102 public void updatePolicyUndeployCounter(final boolean isSuccessful) {
103 POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.UNDEPLOY_OPERATION, PROMETHEUS_TOTAL_LABEL_VALUE).inc();
104 this.policyUndeployCount.incrementAndGet();
106 POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.UNDEPLOY_OPERATION, PdpResponseStatus.SUCCESS.name())
108 this.policyUndeploySuccessCount.incrementAndGet();
110 POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.UNDEPLOY_OPERATION, PdpResponseStatus.FAIL.name()).inc();
111 this.policyUndeployFailCount.incrementAndGet();
115 public long getPolicyDeployCount() {
116 return policyDeployCount.get();
119 public long getPolicyDeployFailCount() {
120 return policyDeployFailCount.get();
123 public long getPolicyDeploySuccessCount() {
124 return policyDeploySuccessCount.get();
127 public long getPolicyExecutedCount() {
128 return policyExecutedCount.get();
131 public long getPolicyExecutedSuccessCount() {
132 return policyExecutedSuccessCount.get();
135 public long getPolicyExecutedFailCount() {
136 return policyExecutedFailCount.get();
139 public long getPolicyUndeployCount() {
140 return policyUndeployCount.get();
143 public long getPolicyUndeploySuccessCount() {
144 return policyUndeploySuccessCount.get();
147 public long getPolicyUndeployFailCount() {
148 return policyUndeployFailCount.get();