diff options
author | Rashmi Pujar <rashmi.pujar1@bell.ca> | 2022-01-07 01:38:40 -0500 |
---|---|---|
committer | Rashmi Pujar <rashmi.pujar1@bell.ca> | 2022-01-10 16:08:30 -0500 |
commit | 58eae5d260f4e41ae439b733404fd1bc7dca453b (patch) | |
tree | e48dd9fc1a81191cf61184e2e43b7ef2e55bb671 /core/core-engine | |
parent | 63cd91c92e53349283ef920bb1dfd9efa1ca3a48 (diff) |
Prometheues metrics for APEX engine stats
Issue-ID: POLICY-3846
Signed-off-by: Rashmi Pujar <rashmi.pujar1@bell.ca>
Change-Id: I385446683372548f3fed6d609a9e85633ec2916d
Diffstat (limited to 'core/core-engine')
2 files changed, 82 insertions, 9 deletions
diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImpl.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImpl.java index 0d9d34d39..7bd9087df 100644 --- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImpl.java +++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImpl.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. - * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -25,6 +25,7 @@ package org.onap.policy.apex.core.engine.engine.impl; import static org.onap.policy.common.utils.validation.Assertions.argumentNotNull; +import io.prometheus.client.Gauge; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; @@ -72,6 +73,11 @@ public class ApexEngineImpl implements ApexEngine { // Logger for this class private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEngineImpl.class); + // Register state changes with prometheus + static final Gauge ENGINE_STATE = Gauge.build().name("apex_engine_state").labelNames("engine_instance_id") + .help("State of the APEX engine as integers mapped as - 0:UNDEFINED, 1:STOPPED, 2:READY," + + " 3:EXECUTING, 4:STOPPING").register(); + // Recurring string constants private static final String UPDATE_MODEL = "updateModel()<-"; private static final String START = "start()<-"; @@ -123,6 +129,7 @@ public class ApexEngineImpl implements ApexEngine { */ @Override public void updateModel(final AxPolicyModel apexModel, final boolean isSubsequentInstance) throws ApexException { + updateStatePrometheusMetric(); if (apexModel != null) { LOGGER.entry("updateModel()->{}, apexPolicyModel {}", key.getId(), apexModel.getKey().getId()); } else { @@ -249,6 +256,7 @@ public class ApexEngineImpl implements ApexEngine { // OK, we are good to go state = AxEngineState.READY; + updateStatePrometheusMetric(); LOGGER.exit("start()" + key); } @@ -278,6 +286,7 @@ public class ApexEngineImpl implements ApexEngine { case READY: case STOPPED: state = AxEngineState.STOPPED; + updateStatePrometheusMetric(); stateMachineHandler.stop(); engineStats.engineStop(); LOGGER.exit("stop()" + key); @@ -286,6 +295,7 @@ public class ApexEngineImpl implements ApexEngine { // Engine is executing a policy, wait for it to stop case EXECUTING: state = AxEngineState.STOPPING; + updateStatePrometheusMetric(); break; // Wait for the engine to stop @@ -303,6 +313,7 @@ public class ApexEngineImpl implements ApexEngine { synchronized (stateLockObj) { state = AxEngineState.STOPPED; } + updateStatePrometheusMetric(); throw new ApexException(STOP + key.getId() + "," + state + ", error stopping engine, engine stop timed out"); } @@ -370,6 +381,7 @@ public class ApexEngineImpl implements ApexEngine { state = AxEngineState.EXECUTING; } + updateStatePrometheusMetric(); String message = "execute(): triggered by event " + incomingEvent.toString(); LOGGER.debug(message); @@ -413,6 +425,7 @@ public class ApexEngineImpl implements ApexEngine { state = AxEngineState.STOPPED; } } + updateStatePrometheusMetric(); return ret; } @@ -507,4 +520,11 @@ public class ApexEngineImpl implements ApexEngine { return Set.of(exceptionEvent); } -} + + /** + * Update the APEX engine state to prometheus for monitoring. + */ + private void updateStatePrometheusMetric() { + ENGINE_STATE.labels(getKey().getId()).set(state.getStateIdentifier()); + } +}
\ No newline at end of file diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java index c9d9b9c8d..88602206d 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java @@ -30,6 +30,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import io.prometheus.client.CollectorRegistry; import java.io.IOException; import java.lang.reflect.Field; import java.util.HashMap; @@ -70,6 +71,8 @@ import org.onap.policy.common.parameters.ParameterService; * Test the engine implementation. */ public class ApexEngineImplTest { + private static final String ENGINE_ID = "Engine:0.0.1"; + private AxPolicyModel policyModel; private AxPolicyModel incompatiblePolicyModel; private AxPolicyModel policyModelWithStates; @@ -170,7 +173,7 @@ public class ApexEngineImplTest { @Test public void testSanity() throws ApexException { - AxArtifactKey engineKey = new AxArtifactKey("Engine:0.0.1"); + AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); assertNotNull(engine); assertEquals(engineKey, engine.getKey()); @@ -182,6 +185,7 @@ public class ApexEngineImplTest { .hasMessage("stop()<-Engine:0.0.1,STOPPED, cannot stop engine, " + "engine is already stopped"); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); assertEquals(0, engine.getEngineContext().size()); assertEquals(engineKey, engine.getEngineStatus().getKey()); assertNull(engine.getInternalContext()); @@ -200,7 +204,7 @@ public class ApexEngineImplTest { @Test public void testListener() throws ApexException { - AxArtifactKey engineKey = new AxArtifactKey("Engine:0.0.1"); + AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); engine.addEventListener("myListener", new DummyListener()); @@ -227,6 +231,7 @@ public class ApexEngineImplTest { engine.start(); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); assertThatThrownBy(engine::start) .hasMessage("start()<-Engine:0.0.1,READY, cannot start engine, engine not in state STOPPED"); @@ -236,25 +241,29 @@ public class ApexEngineImplTest { engine.stop(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.clear(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); assertThatThrownBy(engine::start).hasMessage("start()<-Engine:0.0.1,STOPPED, cannot start engine, " + "engine has not been initialized, its model is not loaded"); engine.updateModel(policyModel, false); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.start(); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); assertNull(engine.createEvent(null)); } @Test public void testEventKey() throws ApexException { - AxArtifactKey engineKey = new AxArtifactKey("Engine:0.0.1"); + AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); engine.updateModel(policyModel, false); engine.start(); @@ -265,33 +274,41 @@ public class ApexEngineImplTest { assertTrue(engine.handleEvent(event)); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); engine.stop(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.addEventListener("myListener", new DummyListener()); engine.start(); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); assertThatThrownBy(() -> engine.updateModel(policyModel, false)).hasMessage( "updateModel()<-Engine:0.0.1, cannot update model, engine should be stopped but is in state READY"); assertTrue(engine.handleEvent(event)); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); engine.stop(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.addEventListener("badListener", new DummyEnEventListener()); engine.start(); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); assertFalse(engine.handleEvent(event)); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); engine.stop(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.removeEventListener("badListener"); engine.addEventListener("slowListener", new DummySlowEnEventListener()); @@ -299,21 +316,24 @@ public class ApexEngineImplTest { @Test public void testState() throws InterruptedException, ApexException { - AxArtifactKey engineKey = new AxArtifactKey("Engine:0.0.1"); + AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); assertNotNull(engine); assertEquals(engineKey, engine.getKey()); engine.updateModel(policyModel, false); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); DummySlowEnEventListener slowListener = new DummySlowEnEventListener(); engine.addEventListener("slowListener", slowListener); engine.start(); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); AxArtifactKey eventKey = new AxArtifactKey("Event:0.0.1"); EnEvent event = engine.createEvent(eventKey); @@ -329,15 +349,18 @@ public class ApexEngineImplTest { }).start(); await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> engine.getState().equals(AxEngineState.EXECUTING)); assertEquals(AxEngineState.EXECUTING, engine.getState()); + checkAxEngineStateMetric(AxEngineState.EXECUTING); assertFalse(engine.handleEvent(event)); assertNotNull(engine.createEvent(eventKey)); engine.stop(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.start(); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); // 4 seconds is more than the 3 second wait on engine stopping slowListener.setWaitTime(4000); @@ -350,24 +373,28 @@ public class ApexEngineImplTest { await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> engine.getState().equals(AxEngineState.EXECUTING)); assertEquals(AxEngineState.EXECUTING, engine.getState()); + checkAxEngineStateMetric(AxEngineState.EXECUTING); assertThatThrownBy(engine::stop) .hasMessage("stop()<-Engine:0.0.1,STOPPED, error stopping engine, engine stop timed out"); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.clear(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); } @Test public void testStateMachineError() throws InterruptedException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ApexException { - AxArtifactKey engineKey = new AxArtifactKey("Engine:0.0.1"); + AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); assertNotNull(engine); assertEquals(engineKey, engine.getKey()); engine.updateModel(policyModel, false); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); final Field smHandlerField = engine.getClass().getDeclaredField("stateMachineHandler"); smHandlerField.setAccessible(true); @@ -375,8 +402,10 @@ public class ApexEngineImplTest { engine.start(); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); AxArtifactKey eventKey = new AxArtifactKey("Event:0.0.1"); EnEvent event = engine.createEvent(eventKey); @@ -384,32 +413,38 @@ public class ApexEngineImplTest { assertFalse(engine.handleEvent(event)); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); engine.stop(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); Mockito.doThrow(new StateMachineException("mocked state machine exception", new IOException("nexted exception"))).when(smHandlerMock).start(); assertThatThrownBy(engine::start).hasMessage("updateModel()<-Engine:0.0.1, error starting the engine state " + "machines \"Engine:0.0.1\""); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.clear(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); } @Test public void testStateMachineHandler() throws InterruptedException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ApexException { - AxArtifactKey engineKey = new AxArtifactKey("Engine:0.0.1"); + AxArtifactKey engineKey = new AxArtifactKey(ENGINE_ID); ApexEngineImpl engine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(engineKey); assertNotNull(engine); assertEquals(engineKey, engine.getKey()); engine.updateModel(policyModelWithStates, false); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.start(); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); AxArtifactKey eventKey = new AxArtifactKey("Event:0.0.1"); EnEvent event = engine.createEvent(eventKey); @@ -417,17 +452,22 @@ public class ApexEngineImplTest { engine.stop(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.start(); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); // Can't work, state is not fully defined assertFalse(engine.handleEvent(event)); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); final Field smHandlerField = engine.getClass().getDeclaredField("stateMachineHandler"); smHandlerField.setAccessible(true); @@ -448,22 +488,35 @@ public class ApexEngineImplTest { engine.stop(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); assertThatThrownBy(engine::start).hasMessageContaining("updateModel()<-Engine:0.0.1, error starting the " + "engine state machines \"Engine:0.0.1\""); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.start(); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); // Works, Dummy executor fakes event execution assertTrue(engine.handleEvent(event)); assertEquals(AxEngineState.READY, engine.getState()); + checkAxEngineStateMetric(AxEngineState.READY); engine.stop(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); engine.clear(); assertEquals(AxEngineState.STOPPED, engine.getState()); + checkAxEngineStateMetric(AxEngineState.STOPPED); + } + + private void checkAxEngineStateMetric(AxEngineState state) { + Double stateMetric = CollectorRegistry.defaultRegistry + .getSampleValue("apex_engine_state", new String[]{"engine_instance_id"}, + new String[]{ENGINE_ID}); + assertEquals(stateMetric.intValue(), state.getStateIdentifier()); } -} +}
\ No newline at end of file |