This is an automated email from the ASF dual-hosted git repository.
zhaoqingran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new a6eb6be1ab [feat] Monitoring templates support JSON nested parsing.
(#3645)
a6eb6be1ab is described below
commit a6eb6be1abc1d3347749914f2da2935fe8ae0fe3
Author: Duansg <[email protected]>
AuthorDate: Fri Aug 8 21:54:38 2025 +0800
[feat] Monitoring templates support JSON nested parsing. (#3645)
Co-authored-by: Logic <[email protected]>
---
.../common/constants/CommonConstants.java | 5 +++++
.../common/util/JexlExpressionRunner.java | 23 +++++++++++++++++++---
.../common/util/JexlExpressionRunnerTest.java | 18 +++++++++++++++++
3 files changed, 43 insertions(+), 3 deletions(-)
diff --git
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/CommonConstants.java
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/CommonConstants.java
index ac6392b09c..8eb2aac9c0 100644
---
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/CommonConstants.java
+++
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/CommonConstants.java
@@ -372,4 +372,9 @@ public interface CommonConstants {
* status page incident state resolved
*/
byte MONITOR_TYPE_DISCOVERY_AUTO_CREATE = 2;
+
+ /**
+ * JEXL custom function `json`
+ */
+ String JEXL_CUSTOM_JSON_FUNCTION = "json";
}
diff --git
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlExpressionRunner.java
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlExpressionRunner.java
index 273ff46f48..d21562dc52 100644
---
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlExpressionRunner.java
+++
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlExpressionRunner.java
@@ -19,15 +19,19 @@
package org.apache.hertzbeat.common.util;
+import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Maps;
-import java.nio.charset.StandardCharsets;
-import java.util.Map;
import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlExpression;
import org.apache.commons.jexl3.JexlFeatures;
import org.apache.commons.jexl3.MapContext;
+import org.apache.hertzbeat.common.constants.CommonConstants;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+import java.util.function.Function;
/**
* jexl express runner
@@ -41,6 +45,7 @@ public class JexlExpressionRunner {
Map<String, Object> functions = Maps.newLinkedHashMap();
// set the root namespace function
functions.put(null, new JexlCommonFunction());
+ functions.put(CommonConstants.JEXL_CUSTOM_JSON_FUNCTION,
jsonFunction());
ClassLoader classLoader = new ClassLoader() {
@Override
public String getName() {
@@ -78,5 +83,17 @@ public class JexlExpressionRunner {
public static JexlExpression compile(String expression) {
return jexlEngine.createExpression(expression);
}
-
+
+ /**
+ * custom function `json:apply(xx)`
+ *
+ * @return java.util.function.Function
+ */
+ private static Function<String, Map<String, Object>> jsonFunction() {
+ return json -> {
+ TypeReference<Map<String, Object>> typeReference = new
TypeReference<>() {
+ };
+ return JsonUtil.fromJson(json, typeReference);
+ };
+ }
}
diff --git
a/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlExpressionRunnerTest.java
b/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlExpressionRunnerTest.java
index f701f62504..b9b7fd9bd6 100644
---
a/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlExpressionRunnerTest.java
+++
b/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlExpressionRunnerTest.java
@@ -21,6 +21,7 @@ package org.apache.hertzbeat.common.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.HashMap;
@@ -69,4 +70,21 @@ public class JexlExpressionRunnerTest {
contains("Abc", "a")
"""));
}
+
+ @Test
+ void testJsonFunction() {
+ Map<String, Object> context = new HashMap<String, Object>();
+ context.put("$.info",
"{\"current\":537132,\"grantTotal\":63129,\"instanceCode\":\"CSDZMC\",\"timeMillis\":1754579874357}");
+
+ String expression = "json:apply($.info).current";
+
+ JexlExpression expObj = JexlExpressionRunner.compile(expression);
+ assertEquals(537132, JexlExpressionRunner.evaluate(expObj, context));
+
+ context.put("$.info",
"{\"grantTotal\":63129,\"instanceCode\":\"CSDZMC\",\"timeMillis\":1754579874357}");
+ assertNull(JexlExpressionRunner.evaluate(expObj, context));
+
+ context.clear();
+ assertNull(JexlExpressionRunner.evaluate(expObj, context));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]