Cherry-picked from #48906 Co-authored-by: wangbo <wangbo@selectdb.com>
This commit is contained in:
committed by
GitHub
parent
ea59465ec7
commit
6d2abf9df3
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user