Skip to content

Commit

Permalink
feat(engine): add new pricing model metrics (camunda#791)
Browse files Browse the repository at this point in the history
- executed decision instances (dmn tables and literal expressions)
- root process instances (i.e. instances not started by call activity)
- refactors involved tests from JUnit 3 to 4

related to CAM-11186
  • Loading branch information
koevskinikola authored Apr 29, 2020
1 parent 7201455 commit f09166c
Show file tree
Hide file tree
Showing 20 changed files with 560 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public interface DmnDecisionEvaluationEvent {
*/
Collection<DmnDecisionLogicEvaluationEvent> getRequiredDecisionResults();

/**
* @return the number of executed decision instances during the evaluation
*/
long getExecutedDecisionInstances();

/**
* @return the number of executed decision elements during the evaluation
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ protected void generateDecisionEvaluationEvent(List<DmnDecisionLogicEvaluationEv
}

decisionEvaluationEvent.setDecisionResult(rootEvaluatedEvent);
decisionEvaluationEvent.setExecutedDecisionInstances(evaluatedEvents.size());
decisionEvaluationEvent.setExecutedDecisionElements(executedDecisionElements);

evaluatedEvents.remove(rootEvaluatedEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public class DmnDecisionEvaluationEventImpl implements DmnDecisionEvaluationEven

protected DmnDecisionLogicEvaluationEvent decisionResult;
protected Collection<DmnDecisionLogicEvaluationEvent> requiredDecisionResults = new ArrayList<DmnDecisionLogicEvaluationEvent>();
protected long executedDecisionInstances;
protected long executedDecisionElements;

@Override
public DmnDecisionLogicEvaluationEvent getDecisionResult() {
return decisionResult;
}
Expand All @@ -37,6 +39,7 @@ public void setDecisionResult(DmnDecisionLogicEvaluationEvent decisionResult) {
this.decisionResult = decisionResult;
}

@Override
public Collection<DmnDecisionLogicEvaluationEvent> getRequiredDecisionResults() {
return requiredDecisionResults;
}
Expand All @@ -45,6 +48,16 @@ public void setRequiredDecisionResults(Collection<DmnDecisionLogicEvaluationEven
this.requiredDecisionResults = requiredDecisionResults;
}

@Override
public long getExecutedDecisionInstances() {
return executedDecisionInstances;
}

public void setExecutedDecisionInstances(long executedDecisionInstances) {
this.executedDecisionInstances = executedDecisionInstances;
}

@Override
public long getExecutedDecisionElements() {
return executedDecisionElements;
}
Expand All @@ -61,6 +74,7 @@ public String toString() {
", name="+ dmnDecision.getName() +
", decisionLogic=" + dmnDecision.getDecisionLogic() +
", requiredDecisionResults=" + requiredDecisionResults +
", executedDecisionInstances=" + executedDecisionInstances +
", executedDecisionElements=" + executedDecisionElements +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,36 @@

public class DefaultEngineMetricCollector implements DmnEngineMetricCollector, DmnDecisionEvaluationListener {

protected AtomicLong executedDecisionInstances = new AtomicLong();
protected AtomicLong executedDecisionElements = new AtomicLong();

public void notify(DmnDecisionTableEvaluationEvent evaluationEvent) {
// collector is registered as decision evaluation listener
}

public void notify(DmnDecisionEvaluationEvent evaluationEvent) {
long executedDecisionInstances = evaluationEvent.getExecutedDecisionInstances();
long executedDecisionElements = evaluationEvent.getExecutedDecisionElements();
this.executedDecisionInstances.getAndAdd(executedDecisionInstances);
this.executedDecisionElements.getAndAdd(executedDecisionElements);
}

@Override
public long getExecutedDecisionInstances() {
return executedDecisionInstances.get();
}

@Override
public long getExecutedDecisionElements() {
return executedDecisionElements.get();
}

@Override
public long clearExecutedDecisionInstances() {
return executedDecisionInstances.getAndSet(0);
}

@Override
public long clearExecutedDecisionElements() {
return executedDecisionElements.getAndSet(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,21 @@ protected void notifyCollector(DmnDecisionLogicEvaluationEvent evaluationEvent)
// ignore other evaluation events since the collector is implemented as decision table evaluation listener
}

@Override
public long getExecutedDecisionInstances() {
return collector.getExecutedDecisionInstances();
}

@Override
public long getExecutedDecisionElements() {
return collector.getExecutedDecisionElements();
}

@Override
public long clearExecutedDecisionInstances() {
return collector.clearExecutedDecisionInstances();
}

@Override
public long clearExecutedDecisionElements() {
return collector.clearExecutedDecisionElements();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
*/
public interface DmnEngineMetricCollector extends DmnDecisionTableEvaluationListener {

/**
* @return the number of executed decision instances since creation of this engine
*/
long getExecutedDecisionInstances();

/**
* @return the number of executed decision elements since creation of this engine
*/
Expand All @@ -36,4 +41,11 @@ public interface DmnEngineMetricCollector extends DmnDecisionTableEvaluationList
*/
long clearExecutedDecisionElements();

/**
* Resets the executed decision instances to 0.
*
* @return the number of executed decision elements before resetting.
*/
long clearExecutedDecisionInstances();

}
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,9 @@ protected void initDefaultMetrics(MetricsRegistry metricsRegistry) {
metricsRegistry.createMeter(Metrics.JOB_LOCKED_EXCLUSIVE);
metricsRegistry.createMeter(Metrics.JOB_EXECUTION_REJECTED);

metricsRegistry.createMeter(Metrics.ROOT_PROCESS_INSTANCE_START);

metricsRegistry.createMeter(Metrics.EXECUTED_DECISION_INSTANCES);
metricsRegistry.createMeter(Metrics.EXECUTED_DECISION_ELEMENTS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.camunda.bpm.dmn.engine.delegate.DmnDecisionEvaluationListener;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.impl.context.Context;
import org.camunda.bpm.engine.impl.metrics.MetricsRegistry;
import org.camunda.bpm.engine.management.Metrics;

public class MetricsDecisionEvaluationListener implements DmnDecisionEvaluationListener {
Expand All @@ -28,9 +29,11 @@ public void notify(DmnDecisionEvaluationEvent evaluationEvent) {
ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();

if (processEngineConfiguration != null && processEngineConfiguration.isMetricsEnabled()) {
processEngineConfiguration
.getMetricsRegistry()
.markOccurrence(Metrics.EXECUTED_DECISION_ELEMENTS, evaluationEvent.getExecutedDecisionElements());
MetricsRegistry metricsRegistry = processEngineConfiguration.getMetricsRegistry();
metricsRegistry.markOccurrence(Metrics.EXECUTED_DECISION_INSTANCES,
evaluationEvent.getExecutedDecisionInstances());
metricsRegistry.markOccurrence(Metrics.EXECUTED_DECISION_ELEMENTS,
evaluationEvent.getExecutedDecisionElements());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,41 @@

import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.impl.bpmn.parser.AbstractBpmnParseListener;
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.camunda.bpm.engine.impl.pvm.process.ActivityImpl;
import org.camunda.bpm.engine.impl.pvm.process.ScopeImpl;
import org.camunda.bpm.engine.impl.util.xml.Element;
import org.camunda.bpm.engine.management.Metrics;
import org.camunda.bpm.engine.runtime.Execution;

/**
* @author Daniel Meyer
*
*/
public class MetricsBpmnParseListener extends AbstractBpmnParseListener {

public static MetricsExecutionListener ACTIVITY_INSTANCE_START_COUNTER = new MetricsExecutionListener(Metrics.ACTIVTY_INSTANCE_START);
public static MetricsExecutionListener ACTIVITY_INSTANCE_END_COUNTER = new MetricsExecutionListener(Metrics.ACTIVTY_INSTANCE_END);
public static MetricsExecutionListener ROOT_PROCESS_INSTANCE_START_COUNTER =
new MetricsExecutionListener(Metrics.ROOT_PROCESS_INSTANCE_START,
delegateExecution -> (delegateExecution.getId().equals(
((ExecutionEntity) delegateExecution)
.getRootProcessInstanceId())));
public static MetricsExecutionListener ACTIVITY_INSTANCE_START_COUNTER =
new MetricsExecutionListener(Metrics.ACTIVTY_INSTANCE_START);
public static MetricsExecutionListener ACTIVITY_INSTANCE_END_COUNTER =
new MetricsExecutionListener(Metrics.ACTIVTY_INSTANCE_END);

protected void addListeners(ActivityImpl activity) {
activity.addBuiltInListener(ExecutionListener.EVENTNAME_START, ACTIVITY_INSTANCE_START_COUNTER);
activity.addBuiltInListener(ExecutionListener.EVENTNAME_END, ACTIVITY_INSTANCE_END_COUNTER);
}

@Override
public void parseProcess(Element processElement, ProcessDefinitionEntity processDefinition) {
processDefinition.addBuiltInListener(ExecutionListener.EVENTNAME_START,
ROOT_PROCESS_INSTANCE_START_COUNTER);
}

public void parseStartEvent(Element startEventElement, ScopeImpl scope, ActivityImpl activity) {
addListeners(activity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.camunda.bpm.engine.impl.metrics.parser;

import java.util.function.Function;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.impl.context.Context;
Expand All @@ -27,15 +29,24 @@
public class MetricsExecutionListener implements ExecutionListener {

protected String metricsName;
protected Function<DelegateExecution, Boolean> condition;

public MetricsExecutionListener(String metricsName) {
this(metricsName, delegateExecution -> true);
}

public MetricsExecutionListener(String metricsName,
Function<DelegateExecution, Boolean> condition) {
this.metricsName = metricsName;
this.condition = condition;
}

public void notify(DelegateExecution execution) throws Exception {
Context.getProcessEngineConfiguration()
.getMetricsRegistry()
.markOccurrence(metricsName);
if (condition.apply(execution)) {
Context.getProcessEngineConfiguration()
.getMetricsRegistry()
.markOccurrence(metricsName);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ public class Metrics {
*/
public final static String JOB_LOCKED_EXCLUSIVE = "job-locked-exclusive";

/**
* Number of executed Root Process Instance executions.
*/
public final static String ROOT_PROCESS_INSTANCE_START = "root-process-instance-start";

/**
* Number of executed decision elements in the DMN engine.
*/
public final static String EXECUTED_DECISION_INSTANCES = "executed-decision-instances";
public final static String EXECUTED_DECISION_ELEMENTS = "executed-decision-elements";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,57 @@

import java.util.Collection;

import org.camunda.bpm.engine.CaseService;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.ManagementService;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.impl.metrics.Meter;
import org.camunda.bpm.engine.impl.test.PluggableProcessEngineTestCase;
import org.camunda.bpm.engine.test.ProcessEngineRule;
import org.camunda.bpm.engine.test.util.ProcessEngineTestRule;
import org.camunda.bpm.engine.test.util.ProvidedProcessEngineRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.RuleChain;

/**
* @author Thorben Lindhauer
*
*/
public abstract class AbstractMetricsTest extends PluggableProcessEngineTestCase {
public abstract class AbstractMetricsTest {

protected final ProcessEngineRule engineRule = new ProvidedProcessEngineRule();
protected final ProcessEngineTestRule testRule = new ProcessEngineTestRule(engineRule);

@Rule
public RuleChain ruleChain = RuleChain.outerRule(engineRule).around(testRule);

protected ProcessEngineConfigurationImpl processEngineConfiguration;
protected RuntimeService runtimeService;
protected TaskService taskService;
protected CaseService caseService;
protected HistoryService historyService;
protected RepositoryService repositoryService;
protected ManagementService managementService;

@Before
public void initializeServices() throws Exception {
processEngineConfiguration = engineRule.getProcessEngineConfiguration();
runtimeService = engineRule.getRuntimeService();
taskService = engineRule.getTaskService();
caseService = engineRule.getCaseService();
historyService = engineRule.getHistoryService();
repositoryService = engineRule.getRepositoryService();
managementService = engineRule.getManagementService();

protected void setUp() throws Exception {
clearMetrics();
}

protected void tearDown() throws Exception {
@After
public void cleanUpMetrics() throws Exception {
clearMetrics();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.camunda.bpm.engine.test.api.mgmt.metrics;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.camunda.bpm.engine.history.HistoricTaskInstance;
Expand All @@ -24,15 +26,17 @@
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.test.Deployment;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.junit.Test;

/**
* @author Daniel Meyer
*
*/
public class ActivityInstanceCountMetricsTest extends AbstractMetricsTest {

@Test
public void testBpmnActivityInstances() {
deployment(Bpmn.createExecutableProcess("testProcess")
testRule.deploy(Bpmn.createExecutableProcess("testProcess")
.startEvent()
.manualTask()
.endEvent()
Expand Down Expand Up @@ -63,6 +67,7 @@ public void testBpmnActivityInstances() {
.sum());
}

@Test
public void testStandaloneTask() {

// given
Expand Down Expand Up @@ -100,6 +105,7 @@ public void testStandaloneTask() {
}

@Deployment
@Test
public void testCmmnActivitiyInstances() {
// given
// that no activity instances have been executed
Expand Down
Loading

0 comments on commit f09166c

Please sign in to comment.