Merge "rename metrics as per global constants for prometheus"
[policy/apex-pdp.git] / model / engine-model / src / main / java / org / onap / policy / apex / model / enginemodel / concepts / AxEngineStats.java
index d01da46..1420d1e 100644 (file)
@@ -1,7 +1,8 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020,2022 Nordix Foundation.
+ *  Modifications Copyright (C) 2022 Bell Canada.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.apex.model.enginemodel.concepts;
 
+import io.prometheus.client.Gauge;
+import io.prometheus.client.Histogram;
 import java.text.SimpleDateFormat;
 import java.util.List;
-
-import javax.persistence.Column;
-import javax.persistence.EmbeddedId;
-import javax.persistence.Entity;
-import javax.persistence.Table;
-import javax.persistence.Transient;
-import javax.xml.bind.annotation.XmlAccessType;
-import javax.xml.bind.annotation.XmlAccessorType;
-import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.bind.annotation.XmlType;
-
+import lombok.Getter;
 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
@@ -42,62 +34,49 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
+import org.onap.policy.common.utils.resources.PrometheusUtils;
 import org.onap.policy.common.utils.validation.Assertions;
-import lombok.Getter;
 
 /**
- * This class is a java bean that is used to record statistics on Apex engines as they execute.
- * Statistics on the number of events, the amount of time taken to execute the last policy, the
- * average policy execution time, the up time of the engine, and the time stamp of the last engine
- * start are recorded.
+ * This class is a java bean that is used to record statistics on Apex engines as they execute. Statistics on the number
+ * of events, the amount of time taken to execute the last policy, the average policy execution time, the up time of the
+ * engine, and the time stamp of the last engine start are recorded.
  */
 
-@Entity
-@Table(name = "AxEngineStats")
-
-@XmlAccessorType(XmlAccessType.FIELD)
-@XmlRootElement(name = "apexEngineStats", namespace = "http://www.onap.org/policy/apex-pdp")
-@XmlType(name = "AxEngineStats", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = {"key", "timeStamp",
-        "eventCount", "lastExecutionTime", "averageExecutionTime", "upTime", "lastStart"})
 public class AxEngineStats extends AxConcept {
     private static final long serialVersionUID = -6981129081962785368L;
     private static final int HASH_CODE_PRIME = 32;
+    static final String ENGINE_INSTANCE_ID = "engine_instance_id";
+    static final Gauge ENGINE_EVENT_EXECUTIONS = Gauge.build().name("engine_event_executions")
+        .namespace(PrometheusUtils.PdpType.PDPA.getNamespace()).labelNames(ENGINE_INSTANCE_ID)
+        .help("Total number of APEX events processed by the engine.").register();
+    static final Gauge ENGINE_UPTIME = Gauge.build().name("engine_uptime")
+        .namespace(PrometheusUtils.PdpType.PDPA.getNamespace()).labelNames(ENGINE_INSTANCE_ID)
+        .help("Time elapsed since the engine was started.").register();
+    static final Gauge ENGINE_START_TIMESTAMP = Gauge.build().name("engine_last_start_timestamp_epoch")
+        .namespace(PrometheusUtils.PdpType.PDPA.getNamespace()).labelNames(ENGINE_INSTANCE_ID)
+        .help("Epoch timestamp of the instance when engine was last started.").register();
+    static final Gauge ENGINE_AVG_EXECUTION_TIME = Gauge.build().name("engine_average_execution_time_seconds")
+        .namespace(PrometheusUtils.PdpType.PDPA.getNamespace()).labelNames(ENGINE_INSTANCE_ID)
+        .help("Average time taken to execute an APEX policy in seconds.").register();
+    static final Histogram ENGINE_LAST_EXECUTION_TIME = Histogram.build()
+        .namespace(PrometheusUtils.PdpType.PDPA.getNamespace())
+        .name("engine_last_execution_time").labelNames(ENGINE_INSTANCE_ID)
+        .help("Time taken to execute the last APEX policy in seconds.").register();
 
-    @EmbeddedId
-    @XmlElement(name = "key", required = true)
     private AxReferenceKey key;
-
-    @Column
-    @XmlElement(required = true)
     private long timeStamp;
-
-    @Column
-    @XmlElement(required = true)
     private long eventCount;
-
-    @Column
-    @XmlElement(required = true)
     private long lastExecutionTime;
-
-    @Column
-    @XmlElement(required = true)
     private double averageExecutionTime;
-
-    @Column
-    @XmlElement(required = true)
     private long upTime;
 
-    @Transient
     @Getter
     private transient long lastEnterTime;
-
-    @Column
-    @XmlElement(required = true)
     private long lastStart;
 
     /**
-     * The Default Constructor creates an engine statistics instance with a null key and with all
-     * values cleared.
+     * The Default Constructor creates an engine statistics instance with a null key and with all values cleared.
      */
     public AxEngineStats() {
         this(new AxReferenceKey());
@@ -108,6 +87,22 @@ public class AxEngineStats extends AxConcept {
         upTime = 0;
         lastEnterTime = 0;
         lastStart = 0;
+        initEngineMetricsWithPrometheus();
+    }
+
+    /**
+     * Register the APEX engine metrics with Prometheus.
+     */
+    private void initEngineMetricsWithPrometheus() {
+        var engineId = getKey().getParentArtifactKey().getId();
+        if (engineId.startsWith(AxKey.NULL_KEY_NAME)) {
+            return;
+        }
+        ENGINE_UPTIME.labels(engineId).set(upTime / 1000d);
+        ENGINE_EVENT_EXECUTIONS.labels(engineId).set(this.eventCount);
+        ENGINE_START_TIMESTAMP.labels(engineId).set(this.lastStart);
+        ENGINE_AVG_EXECUTION_TIME.labels(engineId).set(this.averageExecutionTime / 1000d);
+        ENGINE_LAST_EXECUTION_TIME.labels(engineId).observe(this.lastExecutionTime / 1000d);
     }
 
     /**
@@ -120,8 +115,7 @@ public class AxEngineStats extends AxConcept {
     }
 
     /**
-     * The Keyed Constructor creates an engine statistics instance with the given key and all values
-     * cleared.
+     * The Keyed Constructor creates an engine statistics instance with the given key and all values cleared.
      *
      * @param key the key
      */
@@ -152,6 +146,7 @@ public class AxEngineStats extends AxConcept {
         this.averageExecutionTime = averageExecutionTime;
         this.upTime = upTime;
         this.lastStart = lastStart;
+        initEngineMetricsWithPrometheus();
     }
 
     /**
@@ -223,6 +218,8 @@ public class AxEngineStats extends AxConcept {
      */
     public void setEventCount(final long eventCount) {
         this.eventCount = eventCount;
+        ENGINE_EVENT_EXECUTIONS.labels(getKey().getParentArtifactKey().getId())
+                .set(this.eventCount);
     }
 
     /**
@@ -241,6 +238,8 @@ public class AxEngineStats extends AxConcept {
      */
     public void setLastExecutionTime(final long lastExecutionTime) {
         this.lastExecutionTime = lastExecutionTime;
+        ENGINE_LAST_EXECUTION_TIME.labels(getKey().getParentArtifactKey().getId())
+                .observe(this.lastExecutionTime / 1000d);
     }
 
     /**
@@ -259,6 +258,8 @@ public class AxEngineStats extends AxConcept {
      */
     public void setAverageExecutionTime(final double averageExecutionTime) {
         this.averageExecutionTime = averageExecutionTime;
+        ENGINE_AVG_EXECUTION_TIME.labels(getKey().getParentArtifactKey().getId())
+                .set(this.averageExecutionTime / 1000d);
     }
 
     /**
@@ -280,6 +281,7 @@ public class AxEngineStats extends AxConcept {
      */
     public void setUpTime(final long upTime) {
         this.upTime = upTime;
+        ENGINE_UPTIME.labels(getKey().getParentArtifactKey().getId()).set(this.upTime / 1000d);
     }
 
     /**
@@ -289,6 +291,7 @@ public class AxEngineStats extends AxConcept {
      */
     private void setLastStart(final long lastStart) {
         this.lastStart = lastStart;
+        ENGINE_START_TIMESTAMP.labels(getKey().getParentArtifactKey().getId()).set(this.lastStart);
     }
 
     /**
@@ -311,11 +314,11 @@ public class AxEngineStats extends AxConcept {
         upTime = 0;
         lastEnterTime = 0;
         lastStart = 0;
+        initEngineMetricsWithPrometheus();
     }
 
     /**
-     * Updates the statistics when called, used by the Apex engine when it starts executing a
-     * policy.
+     * Updates the statistics when called, used by the Apex engine when it starts executing a policy.
      *
      * @param eventkey the key of the event that is being executed
      */
@@ -327,19 +330,23 @@ public class AxEngineStats extends AxConcept {
         }
         lastEnterTime = now;
         timeStamp = now;
+        ENGINE_EVENT_EXECUTIONS.labels(getKey().getParentArtifactKey().getId()).set(this.eventCount);
     }
 
     /**
-     * Updates the statistics when called, used by the Apex engine when it completes executing a
-     * policy.
+     * Updates the statistics when called, used by the Apex engine when it completes executing a policy.
      */
     public synchronized void executionExit() {
         final long now = System.currentTimeMillis();
         lastExecutionTime = now - lastEnterTime;
+        ENGINE_LAST_EXECUTION_TIME.labels(getKey().getParentArtifactKey().getId())
+                .observe(this.lastExecutionTime / 1000d);
 
         averageExecutionTime = ((averageExecutionTime * (eventCount - 1.0)) + lastExecutionTime) / eventCount;
         lastEnterTime = 0;
         timeStamp = System.currentTimeMillis();
+        ENGINE_AVG_EXECUTION_TIME.labels(getKey().getParentArtifactKey().getId())
+                .set(this.averageExecutionTime / 1000d);
     }
 
     /**
@@ -359,6 +366,7 @@ public class AxEngineStats extends AxConcept {
         timeStamp = now;
         upTime += (timeStamp - this.getLastStart());
         this.setLastStart(0);
+        ENGINE_UPTIME.labels(getKey().getParentArtifactKey().getId()).set(this.upTime / 1000d);
     }
 
     /**
@@ -424,6 +432,7 @@ public class AxEngineStats extends AxConcept {
         copy.setAverageExecutionTime(averageExecutionTime);
         copy.setUpTime(upTime);
         copy.setLastStart(lastStart);
+        initEngineMetricsWithPrometheus();
 
         return copy;
     }