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")
+ static final Gauge ENGINE_LAST_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")
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);
}
/**
this.averageExecutionTime = averageExecutionTime;
this.upTime = upTime;
this.lastStart = lastStart;
- initEngineMetricsWithPrometheus();
}
/**
*/
public void setEventCount(final long eventCount) {
this.eventCount = eventCount;
- ENGINE_EVENT_EXECUTIONS.labels(getKey().getParentArtifactKey().getId())
- .set(this.eventCount);
}
/**
*/
public void setLastExecutionTime(final long lastExecutionTime) {
this.lastExecutionTime = lastExecutionTime;
- ENGINE_LAST_EXECUTION_TIME.labels(getKey().getParentArtifactKey().getId())
- .observe(this.lastExecutionTime / 1000d);
}
/**
*/
public void setAverageExecutionTime(final double averageExecutionTime) {
this.averageExecutionTime = averageExecutionTime;
- ENGINE_AVG_EXECUTION_TIME.labels(getKey().getParentArtifactKey().getId())
- .set(this.averageExecutionTime / 1000d);
}
/**
*/
public void setUpTime(final long upTime) {
this.upTime = upTime;
- ENGINE_UPTIME.labels(getKey().getParentArtifactKey().getId()).set(this.upTime / 1000d);
}
/**
*/
private void setLastStart(final long lastStart) {
this.lastStart = lastStart;
- ENGINE_START_TIMESTAMP.labels(getKey().getParentArtifactKey().getId()).set(this.lastStart);
}
/**
upTime = 0;
lastEnterTime = 0;
lastStart = 0;
- initEngineMetricsWithPrometheus();
}
/**
public synchronized void executionEnter(final AxArtifactKey eventkey) {
final long now = System.currentTimeMillis();
eventCount++;
+ ENGINE_EVENT_EXECUTIONS.labels(getKey().getParentArtifactKey().getId()).inc();
if (eventCount < 0) {
eventCount = 2;
+ ENGINE_EVENT_EXECUTIONS.labels(getKey().getParentArtifactKey().getId()).set(this.eventCount);
}
lastEnterTime = now;
timeStamp = now;
- ENGINE_EVENT_EXECUTIONS.labels(getKey().getParentArtifactKey().getId()).set(this.eventCount);
}
/**
final long now = System.currentTimeMillis();
timeStamp = now;
this.setLastStart(now);
+ ENGINE_LAST_START_TIMESTAMP.labels(getKey().getParentArtifactKey().getId()).set(this.lastStart);
}
/**
timeStamp = now;
upTime += (timeStamp - this.getLastStart());
this.setLastStart(0);
- ENGINE_UPTIME.labels(getKey().getParentArtifactKey().getId()).set(this.upTime / 1000d);
+ ENGINE_LAST_START_TIMESTAMP.labels(getKey().getParentArtifactKey().getId()).set(this.lastStart);
}
/**
copy.setAverageExecutionTime(averageExecutionTime);
copy.setUpTime(upTime);
copy.setLastStart(lastStart);
- initEngineMetricsWithPrometheus();
-
return copy;
}
stats.setAverageExecutionTime(123.45);
assertEquals(Double.valueOf(123.45), Double.valueOf(stats.getAverageExecutionTime()));
- checkAvgExecTimeMetric(stats);
stats.setEventCount(987);
assertEquals(987, stats.getEventCount());
- checkEventsCountMetric(stats);
final long lastExecutionTime = System.currentTimeMillis();
stats.setLastExecutionTime(lastExecutionTime);
assertEquals(lastExecutionTime, stats.getLastExecutionTime());
- checkLastExecTimeMetric(stats);
final long timestamp = System.currentTimeMillis();
stats.setTimeStamp(timestamp);
final long upTime = System.currentTimeMillis() - timestamp;
stats.setUpTime(upTime);
assertEquals(upTime, stats.getUpTime());
- checkUpTimeMetric(stats);
stats.engineStart();
assertTrue(stats.getUpTime() > -1);
checkEngineStartTimestampMetric(stats);
- checkLastExecTimeMetric(stats);
stats.engineStop();
assertTrue(stats.getUpTime() >= 0);
stats.engineStop();
- checkUpTimeMetric(stats);
checkEngineStartTimestampMetric(stats);
stats.reset();
stats.setEventCount(10);
stats.executionEnter(new AxArtifactKey());
assertEquals(11, stats.getEventCount());
- checkEventsCountMetric(stats);
stats.reset();
stats.engineStart();
- stats.setEventCount(4);
- checkUpTimeMetric(stats);
+ stats.setEventCount(3);
stats.executionEnter(new AxArtifactKey());
checkEventsCountMetric(stats);
- checkAvgExecTimeMetric(stats);
checkEngineStartTimestampMetric(stats);
synchronized (WAIT_LOCK) {
}
stats.executionExit();
+ checkAvgExecTimeMetric(stats);
+ checkEventsCountMetric(stats);
+ checkLastExecTimeMetric(stats);
final double avExecutionTime = stats.getAverageExecutionTime();
assertTrue(avExecutionTime >= 2.0 && avExecutionTime < 10.0);
stats.engineStop();
- checkUpTimeMetric(stats);
+ checkEngineStartTimestampMetric(stats);
AxValidationResult result = new AxValidationResult();
result = stats.validate(result);
stats.clean();
stats.reset();
- checkAllPrometheusMetrics(stats);
final AxEngineStats clonedStats = new AxEngineStats(stats);
assertEquals("AxEngineStats:(engineKey=AxReferenceKey:(parentKey", clonedStats.toString().substring(0, 50));
assertEquals(stats, stats); // NOSONAR
assertEquals(stats, clonedStats);
assertNotNull(stats);
- checkAllPrometheusMetrics(clonedStats);
Object helloObject = "Hello";
assertNotEquals(stats, helloObject);
stats.setTimeStamp(0);
assertEquals(stats, new AxEngineStats(statsKey));
assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
- checkAllPrometheusMetrics(clonedStats);
stats.setEventCount(1);
assertNotEquals(stats, new AxEngineStats(statsKey));
stats.engineStart();
assertNotEquals(stats, new AxEngineStats(statsKey));
+ checkEngineStartTimestampMetric(stats);
final AxEngineStats newStats = new AxEngineStats(statsKey);
newStats.setTimeStamp(stats.getTimeStamp());
assertNotEquals(stats, newStats);
assertNotEquals(0, stats.compareTo(newStats));
stats.engineStop();
- checkUpTimeMetric(stats);
checkEngineStartTimestampMetric(stats);
stats.reset();
assertEquals(stats, new AxEngineStats(statsKey));
assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
- checkAllPrometheusMetrics(stats);
- }
-
- private void checkUpTimeMetric(AxEngineStats stats) {
- Double upTimeMetric = CollectorRegistry.defaultRegistry.getSampleValue("pdpa_engine_uptime",
- new String[]{AxEngineStats.ENGINE_INSTANCE_ID}, new String[]{ENGINE_KEY + ":" + ENGINE_VERSION}) * 1000d;
- assertEquals(upTimeMetric.longValue(), stats.getUpTime());
}
private void checkEventsCountMetric(AxEngineStats stats) {
new String[]{ENGINE_KEY + ":" + ENGINE_VERSION}) * 1000d;
assertEquals(avgExecTimeMetric, Double.valueOf(stats.getAverageExecutionTime()));
}
-
- private void checkAllPrometheusMetrics(AxEngineStats stats) {
- checkEventsCountMetric(stats);
- checkUpTimeMetric(stats);
- checkAvgExecTimeMetric(stats);
- checkEngineStartTimestampMetric(stats);
- checkEngineStartTimestampMetric(stats);
- }
-}
\ No newline at end of file
+}
final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
PdpResponseDetails pdpResponseDetails = null;
if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
- final var runningPolicies = apexEngineHandler.getRunningPolicies();
+ List<ToscaConceptIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies();
try {
apexEngineHandler.shutdown();
+ runningPolicies = apexEngineHandler.getRunningPolicies();
pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
PdpResponseStatus.SUCCESS, "Pdp update successful. No policies are running.");
} catch (final ApexStarterException e) {
private PdpResponseDetails startApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
PdpResponseDetails pdpResponseDetails = null;
- List<ToscaConceptIdentifier> runningPolicies = null;
+ List<ToscaConceptIdentifier> runningPolicies = new ArrayList<>();
try {
if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPoliciesToBeDeployed(),
private void updateDeploymentCounts(final List<ToscaConceptIdentifier> runningPolicies,
final PdpUpdate pdpUpdateMsg) {
final var statisticsManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
- if (statisticsManager != null) {
+ if (statisticsManager != null && runningPolicies != null) {
if (pdpUpdateMsg.getPoliciesToBeDeployed() != null && !pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
var policiesToDeploy = pdpUpdateMsg.getPoliciesToBeDeployed().stream()
.map(ToscaWithTypeAndObjectProperties::getIdentifier).collect(Collectors.toList());
var policiesToUndeploy = pdpUpdateMsg.getPoliciesToBeUndeployed();
if (policiesToUndeploy != null && !policiesToUndeploy.isEmpty()) {
- var policiesSuccessfullyUndeployed = new ArrayList<>(policiesToUndeploy);
- policiesSuccessfullyUndeployed.retainAll(runningPolicies);
- policiesSuccessfullyUndeployed.forEach(policy -> statisticsManager.updatePolicyUndeployCounter(true));
-
- var policiesFailedToUndeploy = new ArrayList<>(policiesToUndeploy);
- policiesFailedToUndeploy.removeIf(runningPolicies::contains);
+ var policiesFailedToUndeploy = new ArrayList<>(policiesToUndeploy);
+ policiesFailedToUndeploy.retainAll(runningPolicies);
policiesFailedToUndeploy.forEach(policy -> statisticsManager.updatePolicyUndeployCounter(false));
+
+ var policiesSuccessfullyUndeployed = new ArrayList<>(policiesToUndeploy);
+ policiesSuccessfullyUndeployed.removeIf(runningPolicies::contains);
+ policiesSuccessfullyUndeployed.forEach(policy -> statisticsManager.updatePolicyUndeployCounter(true));
}
}
}