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

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


The following commit(s) were added to refs/heads/master by this push:
     new 53a14d0389d [Improvement]Add query start datetime (#48906)
53a14d0389d is described below

commit 53a14d0389d968c51aef6bbc2eab996e5cf3a43a
Author: wangbo <wan...@selectdb.com>
AuthorDate: Wed Mar 12 23:51:09 2025 +0800

    [Improvement]Add query start datetime (#48906)
    
    ### What problem does this PR solve?
    Add query start datetime in FE audit log.
---
 .../apache/doris/plugin/audit/AuditLogBuilder.java | 29 +++++++++----
 .../doris/plugin/audit/AuditLogBuilderTest.java    | 48 ++++++++++++++++++++++
 2 files changed, 68 insertions(+), 9 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLogBuilder.java 
b/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLogBuilder.java
index 161a5830b9c..56465f9c17b 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLogBuilder.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLogBuilder.java
@@ -19,7 +19,9 @@ package org.apache.doris.plugin.audit;
 
 import org.apache.doris.common.AuditLog;
 import org.apache.doris.common.Config;
+import org.apache.doris.common.FeConstants;
 import org.apache.doris.common.util.DigitalVersion;
+import org.apache.doris.common.util.TimeUtils;
 import org.apache.doris.plugin.AuditEvent;
 import org.apache.doris.plugin.AuditEvent.AuditField;
 import org.apache.doris.plugin.AuditEvent.EventType;
@@ -97,9 +99,8 @@ public class AuditLogBuilder extends Plugin implements 
AuditPlugin {
         }
     }
 
-    private void auditQueryLog(AuditEvent event) throws IllegalAccessException 
{
+    private String getAuditLogString(AuditEvent event) throws 
IllegalAccessException {
         StringBuilder sb = new StringBuilder();
-        long queryTime = 0;
         // get each field with annotation "AuditField" in AuditEvent
         // and assemble them into a string.
         Field[] fields = event.getClass().getFields();
@@ -109,20 +110,30 @@ public class AuditLogBuilder extends Plugin implements 
AuditPlugin {
                 continue;
             }
 
+            Object fieldValue = f.get(event);
+
             if (af.value().equals("Timestamp")) {
-                continue;
+                try {
+                    fieldValue = TimeUtils.longToTimeStringWithms((Long) 
fieldValue);
+                } catch (Throwable t) {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("convert query start time failed", t);
+                    }
+                    fieldValue = FeConstants.null_string;
+                }
             }
 
-            if (af.value().equals("Time(ms)")) {
-                queryTime = (long) f.get(event);
-            }
-            sb.append("|").append(af.value()).append("=").append(f.get(event));
+            sb.append("|").append(af.value()).append("=").append(fieldValue);
         }
 
-        String auditLog = sb.toString();
+        return sb.toString();
+    }
+
+    private void auditQueryLog(AuditEvent event) throws IllegalAccessException 
{
+        String auditLog = getAuditLogString(event);
         AuditLog.getQueryAudit().log(auditLog);
         // slow query
-        if (queryTime > Config.qe_slow_log_ms) {
+        if (event != null && event.queryTime > Config.qe_slow_log_ms) {
             AuditLog.getSlowAudit().log(auditLog);
         }
     }
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLogBuilderTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLogBuilderTest.java
new file mode 100644
index 00000000000..f0b00dcdfb9
--- /dev/null
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLogBuilderTest.java
@@ -0,0 +1,48 @@
+// 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.doris.plugin.audit;
+
+import org.apache.doris.common.jmockit.Deencapsulation;
+import org.apache.doris.plugin.AuditEvent;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class AuditLogBuilderTest {
+
+    @Test
+    public void testTimestampOutput() {
+        AuditLogBuilder auditLogBuilder = new AuditLogBuilder();
+            // 1 set a valid value
+            {
+                long currentTime = 1741760376000L;
+                AuditEvent auditEvent = new AuditEvent.AuditEventBuilder()
+                        .setTimestamp(currentTime).build();
+                String result = Deencapsulation.invoke(auditLogBuilder, 
"getAuditLogString", auditEvent);
+                Assert.assertTrue(result.contains("Timestamp=2025-03-12 
14:19:36.000"));
+            }
+
+            // 2 not set value
+            {
+                AuditEvent auditEvent = new 
AuditEvent.AuditEventBuilder().build();
+                String result = Deencapsulation.invoke(auditLogBuilder, 
"getAuditLogString", auditEvent);
+                Assert.assertTrue(result.contains("Timestamp=\\N"));
+            }
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to