Skip to content

Commit

Permalink
[Fix apache#3486] Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Apr 29, 2024
1 parent fd7c812 commit 6c1cffa
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.kogito.serverless.workflow.monitoring;

public enum ArrayStoreMode {
STRING,
MULTI_PARAM
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.kogito.serverless.workflow.monitoring;

import java.util.Iterator;

import org.kie.api.event.process.ProcessStartedEvent;
import org.kie.kogito.KogitoGAV;
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;
import org.kie.kogito.monitoring.core.common.process.MetricsProcessEventListener;
import org.kie.kogito.serverless.workflow.SWFConstants;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;

public class SonataFlowMetricProcessEventListener extends MetricsProcessEventListener {

public SonataFlowMetricProcessEventListener(KogitoGAV gav, MeterRegistry meterRegistry) {
private ArrayStoreMode arrayStoreMode;

public SonataFlowMetricProcessEventListener(KogitoGAV gav, MeterRegistry meterRegistry, ArrayStoreMode arrayStoreMode) {
super("sonataflow-process-monitoring-listener", gav, meterRegistry);
this.arrayStoreMode = arrayStoreMode;
}

@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
final KogitoProcessInstance processInstance = (KogitoProcessInstance) event.getProcessInstance();
Object node = processInstance.getVariables().get(SWFConstants.DEFAULT_WORKFLOW_VAR);
if (node instanceof ObjectNode) {
registerObject(processInstance.getProcessId(), (ObjectNode) node, null);
registerObject(processInstance.getProcessId(), null, (ObjectNode) node);
}
}

private void registerInputParam(String processId, String key, JsonNode value) {
if (value instanceof ObjectNode) {
registerObject(processId, key, (ObjectNode) value);
} else if (value instanceof ArrayNode) {
registerArray(processId, key, (ArrayNode) value);
} else {
registerValue(processId, key, value.toString());
}
}

private void registerObject(String processId, ObjectNode node, String prefix) {
node.fields().forEachRemaining(e -> registerInputParam(processId, e.getKey(), e.getValue(), prefix));
private void registerObject(String processId, String key, ObjectNode node) {
node.fields().forEachRemaining(e -> registerInputParam(processId, concat(key, e.getKey(), '.'), e.getValue()));
}

private void registerInputParam(String processId, String key, JsonNode value, String prefix) {
if (value.isObject()) {
registerObject(processId, (ObjectNode) value, concat(prefix, key));
private void registerArray(String processId, String key, ArrayNode node) {
if (arrayStoreMode == ArrayStoreMode.MULTI_PARAM) {
Iterator<JsonNode> iter = node.elements();
for (int i = 0; iter.hasNext(); i++) {
registerInputParam(processId, concat(key, "[" + i + "]"), iter.next());
}
} else {
registerInputParam(processId, concat(prefix, key), value.toString());
registerValue(processId, key, node.toString());
}
}

private String concat(String prefix, String key) {
return prefix == null ? key : prefix + "." + key;
private void registerValue(String processId, String key, String value) {
buildCounter("sonataflow_input_parameters_counter", "Input parameters", processId, Tag.of("param_name", key), Tag.of("param_value", value)).increment();
}

private void registerInputParam(String processId, String key, String value) {
buildCounter("sonataflow_input_parameters_counter", "Input parameters", processId, Tag.of("param_name", key), Tag.of("param_value", value)).increment();
private String concat(String prefix, String key, char prefixChar) {
return prefix == null ? key : prefix + prefixChar + key;
}

private String concat(String prefix, String key) {
return prefix == null ? key : prefix + key;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.sonataflow.monitoring;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.kie.kogito.KogitoGAV;
import org.kie.kogito.config.ConfigBean;
import org.kie.kogito.internal.process.event.KogitoProcessEventListener;
import org.kie.kogito.serverless.workflow.monitoring.ArrayStoreMode;
import org.kie.kogito.serverless.workflow.monitoring.SonataFlowMetricProcessEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,10 +41,13 @@ public class SonataFlowMetricEventListenerFactory {
@Inject
ConfigBean configBean;

@ConfigProperty(name = "kie.monitoring.sonataflow.arrays.store", defaultValue = "STRING")
ArrayStoreMode arrayStoreMode;

@Produces
public KogitoProcessEventListener produceProcessListener() {
LOGGER.info("Producing sonataflow listener for process monitoring.");
return new SonataFlowMetricProcessEventListener(
configBean.getGav().orElse(KogitoGAV.EMPTY_GAV), Metrics.globalRegistry);
configBean.getGav().orElse(KogitoGAV.EMPTY_GAV), Metrics.globalRegistry, arrayStoreMode);
}
}

0 comments on commit 6c1cffa

Please sign in to comment.