Skip to content

Commit

Permalink
[Improve](auditlog) audit log print real sql in prepared statement (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
HexyinUESTC authored and eldenmoon committed Nov 26, 2024
1 parent 5eb433e commit 0eaf8dc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0eaf8dc

Please sign in to comment.