This is an automated email from the ASF dual-hosted git repository.

zhangxiaowei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozhera.git


The following commit(s) were added to refs/heads/master by this push:
     new 45b01b52 feat: Add placeholder parsing function (#579)
45b01b52 is described below

commit 45b01b524fdd9eaadd97dad7d27ce00e7306e308
Author: wtt <[email protected]>
AuthorDate: Thu Apr 10 16:43:20 2025 +0800

    feat: Add placeholder parsing function (#579)
---
 .../apache/ozhera/log/parse/LogParserFactory.java  |   5 +-
 .../apache/ozhera/log/parse/PlaceholderParser.java | 143 +++++++++++++++++++++
 .../apache/ozhera/log/common/LogParserTest.java    |  21 +++
 3 files changed, 168 insertions(+), 1 deletion(-)

diff --git 
a/ozhera-log/log-common/src/main/java/org/apache/ozhera/log/parse/LogParserFactory.java
 
b/ozhera-log/log-common/src/main/java/org/apache/ozhera/log/parse/LogParserFactory.java
index 0c0a634e..e5d0ff69 100644
--- 
a/ozhera-log/log-common/src/main/java/org/apache/ozhera/log/parse/LogParserFactory.java
+++ 
b/ozhera-log/log-common/src/main/java/org/apache/ozhera/log/parse/LogParserFactory.java
@@ -59,6 +59,8 @@ public class LogParserFactory {
                 return new JsonLogParser(logParserData);
             case NGINX_PARSE:
                 return new NginxLogParser(logParserData);
+            case PLACEHOLDER_PARSE:
+                return new PlaceholderParser(logParserData);
             default:
                 return new RawLogParser(logParserData);
         }
@@ -76,7 +78,8 @@ public class LogParserFactory {
         CUSTOM_PARSE(5, "自定义脚本解析"),
         REGEX_PARSE(6, "正则表达式"),
         JSON_PARSE(7, "JSON解析"),
-        NGINX_PARSE(8, "Nginx解析");
+        NGINX_PARSE(8, "Nginx解析"),
+        PLACEHOLDER_PARSE(9, "占位符解析");
 
         private Integer code;
         private String name;
diff --git 
a/ozhera-log/log-common/src/main/java/org/apache/ozhera/log/parse/PlaceholderParser.java
 
b/ozhera-log/log-common/src/main/java/org/apache/ozhera/log/parse/PlaceholderParser.java
new file mode 100644
index 00000000..307fc3d4
--- /dev/null
+++ 
b/ozhera-log/log-common/src/main/java/org/apache/ozhera/log/parse/PlaceholderParser.java
@@ -0,0 +1,143 @@
+/*
+ * 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.apache.ozhera.log.parse;
+
+import java.util.*;
+
+/**
+ * @author wtt
+ * @version 1.0
+ * @description
+ * @date 2025/4/7 15:36
+ */
+public class PlaceholderParser extends AbstractLogParser {
+    private final String parseScript;
+    private final List<String> fieldNames;
+    private final List<String> staticParts;
+
+    public PlaceholderParser(LogParserData parserData) {
+        super(parserData);
+        parseScript = parserData.getParseScript();
+        fieldNames = valueMap.entrySet().stream()
+                .sorted(Map.Entry.comparingByValue())
+                .map(Map.Entry::getKey)
+                .toList();
+        this.staticParts = splitAndFormat();
+    }
+
+    private List<String> splitAndFormat() {
+        List<String> parts = new ArrayList<>();
+        String[] tokens = parseScript.split("%s", -1);
+        for (String token : tokens) {
+            if (!token.isEmpty()) {
+                parts.add(token);
+            }
+        }
+        return parts;
+    }
+
+    @Override
+    public Map<String, Object> doParse(String logData, String ip, Long 
lineNum, Long collectStamp, String fileName) {
+        return parse(logData);
+    }
+
+    @Override
+    public Map<String, Object> doParseSimple(String logData, Long 
collectStamp) {
+        return parse(logData);
+    }
+
+    @Override
+    public List<String> parseLogData(String logData) throws Exception {
+        List<String> result = new ArrayList<>();
+        String remaining = logData;
+        int staticPartIndex = 0;
+
+        try {
+            if (!staticParts.isEmpty() && 
remaining.startsWith(staticParts.get(0))) {
+                remaining = remaining.substring(staticParts.get(0).length());
+                staticPartIndex++;
+            }
+
+            while (staticPartIndex < staticParts.size()) {
+                String nextStatic = staticParts.get(staticPartIndex);
+                int endPos = remaining.indexOf(nextStatic);
+
+                if (endPos == -1) {
+                    if (!remaining.trim().isEmpty()) {
+                        result.add(remaining.trim());
+                    }
+                    break;
+                }
+
+                String fieldValue = remaining.substring(0, endPos).trim();
+                if (!fieldValue.isEmpty()) {
+                    result.add(fieldValue);
+                }
+
+                remaining = remaining.substring(endPos + nextStatic.length());
+                staticPartIndex++;
+            }
+
+            if (!remaining.trim().isEmpty()) {
+                result.add(remaining.trim());
+            }
+
+        } catch (Exception ignored) {
+        }
+        return result;
+    }
+
+
+    public Map<String, Object> parse(String logLine) {
+        Map<String, Object> result = new LinkedHashMap<>();
+        String remaining = logLine;
+        int fieldIndex = 0;
+        int staticPartIndex = 0;
+
+        try {
+            if (!staticParts.isEmpty() && 
remaining.startsWith(staticParts.get(0))) {
+                remaining = remaining.substring(staticParts.get(0).length());
+                staticPartIndex++;
+            }
+
+            while (fieldIndex < fieldNames.size()) {
+                String nextStatic = staticPartIndex < staticParts.size() ? 
staticParts.get(staticPartIndex) : null;
+                int endPos = nextStatic != null ? 
remaining.indexOf(nextStatic) : remaining.length();
+
+                if (endPos == -1) return result;
+
+                String fieldValue = remaining.substring(0, endPos).trim();
+                result.put(fieldNames.get(fieldIndex++), fieldValue);
+                remaining = remaining.substring(endPos);
+
+                if (nextStatic != null) {
+                    if (!remaining.startsWith(nextStatic)) {
+                        return Collections.emptyMap();
+                    }
+                    remaining = remaining.substring(nextStatic.length());
+                    staticPartIndex++;
+                }
+            }
+
+            return result;
+        } catch (Exception e) {
+            return Collections.emptyMap();
+        }
+    }
+}
diff --git 
a/ozhera-log/log-common/src/test/java/org/apache/ozhera/log/common/LogParserTest.java
 
b/ozhera-log/log-common/src/test/java/org/apache/ozhera/log/common/LogParserTest.java
index e2867cd3..befc7e06 100644
--- 
a/ozhera-log/log-common/src/test/java/org/apache/ozhera/log/common/LogParserTest.java
+++ 
b/ozhera-log/log-common/src/test/java/org/apache/ozhera/log/common/LogParserTest.java
@@ -29,6 +29,7 @@ import org.junit.Assert;
 import org.junit.Test;
 
 import java.time.Instant;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -140,4 +141,24 @@ public class LogParserTest {
         DateParser dateFormat1 = FastDateFormat.getInstance("yyyy-MM-dd 
HH:mm:ss");
         System.out.println(dateFormat1.getPattern().length());
     }
+
+    @Test
+    public void LogPlaceholderParserTest() throws Exception {
+        Stopwatch stopwatch = Stopwatch.createStarted();
+        String keyList = 
"level:keyword,message:text,message_body:text,hostname:keyword,trace_id:keyword,thread:keyword,timestamp:date,time:date,linenumber:long,mqtag:keyword,tail:keyword,filename:keyword,logstore:keyword,logsource:keyword,logip:keyword,mqtopic:keyword";
+        String keyOrderList = 
"level:1,message:1,message_body:1,hostname:1,trace_id:1,thread:1,timestamp:1,time:1,linenumber:3,mqtag:3,tail:3,filename:3,logstore:3,logsource:3,logip:3,mqtopic:3";
+        String valueList = "2,5,-1,1,3,4,-1,0";
+        String parseScript = "[%s] [%s] [%s] [%s] [%s] %s %s";
+        String logData = "[2025-04-09T10:56:55.259+08:00] [kfs-test-123] 
[ERROR] [485bf6a9b5898ecdfd22696325b11b05] [DubboServerHandler-thread-495] 
c.x.k.w.s.i.DataDictServiceImpl - cache is not found. CacheLoader returned null 
for key DataDictServiceImpl$TenantKey@2f123a.";
+        Long collectStamp = Instant.now().toEpochMilli();
+        Integer parserType = 
LogParserFactory.LogParserEnum.PLACEHOLDER_PARSE.getCode();
+        LogParser customParse = LogParserFactory.getLogParser(parserType, 
keyList, valueList, parseScript, topicName, tailName, tag, logStoreName, 
keyOrderList);
+        Map<String, Object> parse = customParse.parse(logData, "", 1l, 
collectStamp, "");
+        System.out.println(parse);
+        List<String> dataList = customParse.parseLogData(logData);
+
+        System.out.println(customParse.getTimestampFromString("2023-08-25 
10:46:09.239", collectStamp));
+        stopwatch.stop();
+        log.info("cost time:{}", stopwatch.elapsed().toMillis());
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to