Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #3483] Adding state duration summary #3485

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading