From 0eaf8dc2a3dcd6aba031fee3d1490b9e6ec1f2bd Mon Sep 17 00:00:00 2001 From: He xueyu <112704062+HexyinUESTC@users.noreply.github.com> Date: Wed, 6 Nov 2024 13:45:53 +0800 Subject: [PATCH] [Improve](auditlog) audit log print real sql in prepared statement (#43038) 1. Use the "execute *** using *** /*original sql = */" in the audit log instead of "execute *** using ***". 2. Add a CommandType parameter to the audit log. 3. When the prepared statement is ready, it should log OK instead of NOOP --- .../src/main/java/org/apache/doris/plugin/AuditEvent.java | 7 +++++++ .../src/main/java/org/apache/doris/qe/AuditLogHelper.java | 7 ++++++- .../java/org/apache/doris/qe/MysqlConnectProcessor.java | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java b/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java index 5fc735a0bb882a..7d64b600d8a9f1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java +++ b/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java @@ -60,6 +60,8 @@ public enum EventType { public String ctl = ""; @AuditField(value = "Db") public String db = ""; + @AuditField(value = "CommandType") + public String commandType = ""; @AuditField(value = "State") public String state = ""; @AuditField(value = "ErrorCode") @@ -242,6 +244,11 @@ public AuditEventBuilder setWorkloadGroup(String workloadGroup) { return this; } + public AuditEventBuilder setCommandType(String commandType) { + auditEvent.commandType = commandType; + return this; + } + public AuditEvent build() { return this.auditEvent; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java b/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java index 02a66a5f6e1c55..d10e1bbfbb87c5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java @@ -30,6 +30,7 @@ import org.apache.doris.datasource.CatalogIf; import org.apache.doris.datasource.InternalCatalog; import org.apache.doris.metric.MetricRepo; +import org.apache.doris.mysql.MysqlCommand; import org.apache.doris.nereids.analyzer.UnboundOneRowRelation; import org.apache.doris.nereids.analyzer.UnboundTableSink; import org.apache.doris.nereids.glue.LogicalPlanAdapter; @@ -197,7 +198,8 @@ private static void logAuditLogImpl(ConnectContext ctx, String origStmt, Stateme .setStmtId(ctx.getStmtId()) .setQueryId(ctx.queryId() == null ? "NaN" : DebugUtil.printId(ctx.queryId())) .setWorkloadGroup(ctx.getWorkloadGroupName()) - .setFuzzyVariables(!printFuzzyVariables ? "" : ctx.getSessionVariable().printFuzzyVariables()); + .setFuzzyVariables(!printFuzzyVariables ? "" : ctx.getSessionVariable().printFuzzyVariables()) + .setCommandType(ctx.getCommand().toString()); if (ctx.getState().isQuery()) { if (!ctx.getSessionVariable().internalSession) { @@ -248,6 +250,9 @@ private static void logAuditLogImpl(ConnectContext ctx, String origStmt, Stateme } } } + if (ctx.getCommand() == MysqlCommand.COM_STMT_PREPARE && ctx.getState().getErrorCode() == null) { + auditEventBuilder.setState(String.valueOf(MysqlStateType.OK)); + } Env.getCurrentEnv().getWorkloadRuntimeStatusMgr().submitFinishQueryToAudit(auditEventBuilder.build()); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java index 138f4202ab2771..931dc3d4717020 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java @@ -221,6 +221,7 @@ private void handleExecute(PrepareCommand prepareCommand, long stmtId, PreparedS executor.execute(); if (ctx.getSessionVariable().isEnablePreparedStmtAuditLog()) { stmtStr = executeStmt.toSql(); + stmtStr = stmtStr + " /*originalSql = " + prepareCommand.getOriginalStmt().originStmt + "*/"; } } catch (Throwable e) { // Catch all throwable.