Skip to content

Commit

Permalink
[Fix #3483] Adding state duration summary (#3485)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado authored Apr 25, 2024
1 parent bf34f12 commit 64b3242
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;
import org.kie.kogito.internal.process.runtime.KogitoWorkItemNodeInstance;
import org.kie.kogito.internal.process.runtime.KogitoWorkflowProcessInstance;
import org.kie.kogito.internal.utils.KogitoTags;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -120,6 +121,11 @@ private DistributionSummary getWorkItemsDurationSummary(String name) {
"Work Items Duration", Tag.of("name", name));
}

private DistributionSummary getNodeInstancesDurationSummary(String processId, String nodeName) {
return buildDistributionSummary("kogito_node_instance_duration_milliseconds", "Relevant nodes duration in milliseconds", Tag.of("process_id", processId),
Tag.of("node_name", nodeName));
}

protected void recordRunningProcessInstance(String processId) {
getRunningProcessInstancesGauge(processId).incrementAndGet();
}
Expand Down Expand Up @@ -164,12 +170,19 @@ public void beforeNodeLeft(ProcessNodeLeftEvent event) {
final KogitoNodeInstance nodeInstance = (KogitoNodeInstance) event.getNodeInstance();
if (nodeInstance instanceof KogitoWorkItemNodeInstance) {
KogitoWorkItemNodeInstance wi = (KogitoWorkItemNodeInstance) nodeInstance;
if (wi.getTriggerTime() != null) {
final String name = (String) wi.getWorkItem().getParameters().getOrDefault("TaskName", wi.getWorkItem().getName());
final double duration = millisToSeconds(wi.getLeaveTime().getTime() - wi.getTriggerTime().getTime());
getWorkItemsDurationSummary(name).record(duration);
LOGGER.debug("Work Item {}, duration: {}s", name, duration);
}
recordNodeDuration(getWorkItemsDurationSummary((String) wi.getWorkItem().getParameters().getOrDefault("TaskName", wi.getWorkItem().getName())), nodeInstance, TimeUnit.SECONDS);
}
String nodeName = (String) nodeInstance.getNode().getMetaData().get(KogitoTags.METRIC_NAME_METADATA);
if (nodeName != null) {
recordNodeDuration(getNodeInstancesDurationSummary(event.getProcessInstance().getProcessId(), nodeName), nodeInstance, TimeUnit.MILLISECONDS);
}
}

private void recordNodeDuration(DistributionSummary summary, KogitoNodeInstance instance, TimeUnit target) {
if (instance.getTriggerTime() != null) {
double duration = target.convert(instance.getLeaveTime().getTime() - instance.getTriggerTime().getTime(), TimeUnit.MILLISECONDS);
summary.record(duration);
LOGGER.debug("Recorded {} {} because of node {} for summary {}", duration, target, instance.getNode().getName(), summary.getId().getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public class KogitoTags {
public static final String INPUT_TAG = "input";
public static final String OUTPUT_TAG = "output";

public static final String METRIC_NAME_METADATA = "MetricName";

}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void handleState() {

protected void handleState(RuleFlowNodeContainerFactory<?, ?> factory) {
MakeNodeResult result = makeNode(factory);
node = result.getIncomingNode().metaData(SWFConstants.STATE_NAME, state.getName());
node = result.getIncomingNode().metaData(SWFConstants.STATE_NAME, state.getName()).metaData(KogitoTags.METRIC_NAME_METADATA, state.getName());
outgoingNode = result.getOutgoingNode().metaData(SWFConstants.STATE_NAME, state.getName());
if (state.getCompensatedBy() != null) {
handleCompensation(factory);
Expand All @@ -208,7 +208,7 @@ protected void handleState(RuleFlowNodeContainerFactory<?, ?> factory) {
if (output != null) {
ActionNodeFactory<?> actionNode = handleStateFilter(factory, output);
factory.connection(outgoingNode.getNode().getId(), actionNode.getNode().getId());
outgoingNode = actionNode.metaData(SWFConstants.STATE_NAME, state.getName());
outgoingNode = actionNode.metaData(SWFConstants.STATE_NAME, state.getName()).metaData(KogitoTags.METRIC_NAME_METADATA, state.getName());
}
}
connectStart(factory);
Expand Down

0 comments on commit 64b3242

Please sign in to comment.