This is an automated email from the ASF dual-hosted git repository.
gavinchou 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 32d40262c3f [fix](metrics) Fix prepared statement QPS metrics not
counted when audit log disabled (#61621)
32d40262c3f is described below
commit 32d40262c3f76ac73b63b3740c9c2386fd62892d
Author: Jimmy <[email protected]>
AuthorDate: Tue Apr 7 17:05:37 2026 +0800
[fix](metrics) Fix prepared statement QPS metrics not counted when audit
log disabled (#61621)
## Summary
- When `enable_prepared_stmt_audit_log = false` (default),
`auditAfterExec()` is entirely skipped for prepared statements. Since QPS
metric counting lives inside `AuditLogHelper.logAuditLog()`, these metrics are
also lost for prepared statement SELECT queries.
- Add `AuditLogHelper.updateMetrics()` to count metrics independently of
audit log writing, and call it in the `else` branch when audit log is disabled.
- This decouples the metric counting from audit log writing without
modifying the existing `logAuditLogImpl()` logic.
## Changes
- **AuditLogHelper.java**: Add `updateMetrics()` + `updateMetricsImpl()`
methods for standalone metric counting
- **MysqlConnectProcessor.java**: Add `else` branch in `handleExecute()` to
call `updateMetrics()` when audit log is disabled.
- **AuditLogHelperTest.java**: Unit tests covering OK query, ERR query,
non-query (INSERT), and debug mode short-circuit scenarios.
## Test plan
- [x] Unit tests pass (`AuditLogHelperTest` - 4 cases)
---
.../java/org/apache/doris/qe/AuditLogHelper.java | 150 +++++++++++++--------
.../org/apache/doris/qe/MysqlConnectProcessor.java | 3 +
.../org/apache/doris/qe/AuditLogHelperTest.java | 104 ++++++++++++++
3 files changed, 199 insertions(+), 58 deletions(-)
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 fb5040be30e..8b6b307634a 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
@@ -345,64 +345,13 @@ public class AuditLogHelper {
}
if (ctx.getState().isQuery()) {
- if (MetricRepo.isInit) {
- if (!ctx.getState().isInternal()) {
- MetricRepo.COUNTER_QUERY_ALL.increase(1L);
-
MetricRepo.USER_COUNTER_QUERY_ALL.getOrAdd(ctx.getQualifiedUser()).increase(1L);
- }
- String physicalClusterName = "";
- try {
- if (Config.isCloudMode()) {
- cloudCluster = ctx.getCloudCluster(false);
- physicalClusterName = ((CloudSystemInfoService)
Env.getCurrentSystemInfo())
- .getPhysicalCluster(cloudCluster);
- if (!cloudCluster.equals(physicalClusterName)) {
- // vcg
-
MetricRepo.increaseClusterQueryAll(physicalClusterName);
- }
- }
- } catch (ComputeGroupException e) {
- LOG.warn("Failed to get cloud cluster, cloudCluster={},
physicalClusterName={} ",
- cloudCluster, physicalClusterName, e);
- return;
- }
-
- MetricRepo.increaseClusterQueryAll(cloudCluster);
- if (!ctx.getState().isInternal()) {
- if (ctx.getState().getStateType() == MysqlStateType.ERR
- && ctx.getState().getErrType() !=
QueryState.ErrType.ANALYSIS_ERR) {
- // err query
- MetricRepo.COUNTER_QUERY_ERR.increase(1L);
-
MetricRepo.USER_COUNTER_QUERY_ERR.getOrAdd(ctx.getQualifiedUser()).increase(1L);
- if (cloudCluster.equals(physicalClusterName)) {
- // not vcg
- MetricRepo.increaseClusterQueryErr(cloudCluster);
- } else {
- // vcg
- MetricRepo.increaseClusterQueryErr(cloudCluster);
-
MetricRepo.increaseClusterQueryErr(physicalClusterName);
- }
- } else if (ctx.getState().getStateType() ==
MysqlStateType.OK
- || ctx.getState().getStateType() ==
MysqlStateType.EOF) {
- // ok query
- MetricRepo.HISTO_QUERY_LATENCY.update(elapseMs);
-
MetricRepo.USER_HISTO_QUERY_LATENCY.getOrAdd(ctx.getQualifiedUser()).update(elapseMs);
- if (cloudCluster.equals(physicalClusterName)) {
- // not vcg
- MetricRepo.updateClusterQueryLatency(cloudCluster,
elapseMs);
- } else {
- // vcg
- MetricRepo.updateClusterQueryLatency(cloudCluster,
elapseMs);
-
MetricRepo.updateClusterQueryLatency(physicalClusterName, elapseMs);
- }
- if (elapseMs > Config.qe_slow_log_ms) {
- MetricRepo.COUNTER_QUERY_SLOW.increase(1L);
- }
- if (elapseMs >
Config.sql_digest_generation_threshold_ms) {
- String sqlDigest = DigestUtils.md5Hex(((Queriable)
parsedStmt).toDigest());
- auditEventBuilder.setSqlDigest(sqlDigest);
- }
- }
+ updateMetricsImpl(ctx);
+ if (MetricRepo.isInit && !ctx.getState().isInternal()) {
+ if ((ctx.getState().getStateType() == MysqlStateType.OK
+ || ctx.getState().getStateType() == MysqlStateType.EOF)
+ && elapseMs >
Config.sql_digest_generation_threshold_ms) {
+ String sqlDigest = DigestUtils.md5Hex(((Queriable)
parsedStmt).toDigest());
+ auditEventBuilder.setSqlDigest(sqlDigest);
}
}
auditEventBuilder.setScanBytesFromLocalStorage(
@@ -458,6 +407,91 @@ public class AuditLogHelper {
return queueToken == null ? -1 : queueToken.getQueueEndTime() -
queueToken.getQueueStartTime();
}
+ /**
+ * Update query metrics without writing audit log. This is used when
+ * enable_prepared_stmt_audit_log is disabled, to ensure QPS metrics
+ * are still counted for prepared statement executions.
+ */
+ public static void updateMetrics(ConnectContext ctx) {
+ if (Config.enable_bdbje_debug_mode) {
+ return;
+ }
+ try {
+ updateMetricsImpl(ctx);
+ } catch (Throwable t) {
+ LOG.warn("Failed to update query metrics.", t);
+ }
+ }
+
+ private static void updateMetricsImpl(ConnectContext ctx) {
+ if (!ctx.getState().isQuery()) {
+ return;
+ }
+ if (!MetricRepo.isInit) {
+ return;
+ }
+
+ long elapseMs = System.currentTimeMillis() - ctx.getStartTime();
+
+ if (!ctx.getState().isInternal()) {
+ MetricRepo.COUNTER_QUERY_ALL.increase(1L);
+
MetricRepo.USER_COUNTER_QUERY_ALL.getOrAdd(ctx.getQualifiedUser()).increase(1L);
+ }
+
+ String cloudCluster = "";
+ String physicalClusterName = "";
+ try {
+ if (Config.isCloudMode()) {
+ cloudCluster = ctx.getCloudCluster(false);
+ physicalClusterName = ((CloudSystemInfoService)
Env.getCurrentSystemInfo())
+ .getPhysicalCluster(cloudCluster);
+ if (!cloudCluster.equals(physicalClusterName)) {
+ // vcg
+ MetricRepo.increaseClusterQueryAll(physicalClusterName);
+ }
+ }
+ } catch (ComputeGroupException e) {
+ LOG.warn("Failed to get cloud cluster, cloudCluster={},
physicalClusterName={} ",
+ cloudCluster, physicalClusterName, e);
+ return;
+ }
+
+ MetricRepo.increaseClusterQueryAll(cloudCluster);
+
+ if (!ctx.getState().isInternal()) {
+ if (ctx.getState().getStateType() == MysqlStateType.ERR
+ && ctx.getState().getErrType() !=
QueryState.ErrType.ANALYSIS_ERR) {
+ // err query
+ MetricRepo.COUNTER_QUERY_ERR.increase(1L);
+
MetricRepo.USER_COUNTER_QUERY_ERR.getOrAdd(ctx.getQualifiedUser()).increase(1L);
+ if (cloudCluster.equals(physicalClusterName)) {
+ // not vcg
+ MetricRepo.increaseClusterQueryErr(cloudCluster);
+ } else {
+ // vcg
+ MetricRepo.increaseClusterQueryErr(cloudCluster);
+ MetricRepo.increaseClusterQueryErr(physicalClusterName);
+ }
+ } else if (ctx.getState().getStateType() == MysqlStateType.OK
+ || ctx.getState().getStateType() == MysqlStateType.EOF) {
+ // ok query
+ MetricRepo.HISTO_QUERY_LATENCY.update(elapseMs);
+
MetricRepo.USER_HISTO_QUERY_LATENCY.getOrAdd(ctx.getQualifiedUser()).update(elapseMs);
+ if (cloudCluster.equals(physicalClusterName)) {
+ // not vcg
+ MetricRepo.updateClusterQueryLatency(cloudCluster,
elapseMs);
+ } else {
+ // vcg
+ MetricRepo.updateClusterQueryLatency(cloudCluster,
elapseMs);
+ MetricRepo.updateClusterQueryLatency(physicalClusterName,
elapseMs);
+ }
+ if (elapseMs > Config.qe_slow_log_ms) {
+ MetricRepo.COUNTER_QUERY_SLOW.increase(1L);
+ }
+ }
+ }
+ }
+
private static String getStmtType(StatementBase stmt) {
if (stmt == null) {
return StmtType.OTHER.name();
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 50d3bed474a..5fe5223f9a1 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
@@ -186,6 +186,9 @@ public class MysqlConnectProcessor extends ConnectProcessor
{
}
if (ctx.getSessionVariable().isEnablePreparedStmtAuditLog()) {
auditAfterExec(stmtStr, executor.getParsedStmt(),
executor.getQueryStatisticsForAuditLog(), true);
+ } else {
+ // When audit log is disabled for prepared statements, still
update QPS metrics.
+ AuditLogHelper.updateMetrics(ctx);
}
}
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java
b/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java
new file mode 100644
index 00000000000..e1b6b8c961c
--- /dev/null
+++ b/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java
@@ -0,0 +1,104 @@
+// 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.qe;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.Config;
+import org.apache.doris.common.FeConstants;
+import org.apache.doris.metric.MetricRepo;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class AuditLogHelperTest {
+
+ @BeforeClass
+ public static void setUp() {
+ FeConstants.runningUnitTest = true;
+ MetricRepo.init();
+ }
+
+ private ConnectContext createMockContext(boolean isQuery, boolean
isInternal) {
+ ConnectContext ctx = new ConnectContext();
+ ctx.setStartTime();
+ ctx.setCurrentUserIdentity(UserIdentity.ROOT);
+ ctx.getState().setIsQuery(isQuery);
+ ctx.getState().setInternal(isInternal);
+ return ctx;
+ }
+
+ @Test
+ public void testUpdateMetricsQueryOk() {
+ ConnectContext ctx = createMockContext(true, false);
+ ctx.getState().setOk(0, 0, "");
+
+ long before = MetricRepo.COUNTER_QUERY_ALL.getValue();
+ AuditLogHelper.updateMetrics(ctx);
+ long after = MetricRepo.COUNTER_QUERY_ALL.getValue();
+
+ Assert.assertEquals(1, after - before);
+ }
+
+ @Test
+ public void testUpdateMetricsQueryErr() {
+ ConnectContext ctx = createMockContext(true, false);
+ ctx.getState().setError("test error");
+
+ long beforeAll = MetricRepo.COUNTER_QUERY_ALL.getValue();
+ long beforeErr = MetricRepo.COUNTER_QUERY_ERR.getValue();
+ AuditLogHelper.updateMetrics(ctx);
+ long afterAll = MetricRepo.COUNTER_QUERY_ALL.getValue();
+ long afterErr = MetricRepo.COUNTER_QUERY_ERR.getValue();
+
+ Assert.assertEquals(1, afterAll - beforeAll);
+ Assert.assertEquals(1, afterErr - beforeErr);
+ }
+
+ @Test
+ public void testUpdateMetricsNotQuery() {
+ // INSERT scenario: isQuery = false, counters should not change
+ ConnectContext ctx = createMockContext(false, false);
+ ctx.getState().setOk(1, 0, "");
+
+ long before = MetricRepo.COUNTER_QUERY_ALL.getValue();
+ AuditLogHelper.updateMetrics(ctx);
+ long after = MetricRepo.COUNTER_QUERY_ALL.getValue();
+
+ Assert.assertEquals(0, after - before);
+ }
+
+ @Test
+ public void testUpdateMetricsDebugModeShortCircuit() {
+ boolean original = Config.enable_bdbje_debug_mode;
+ try {
+ Config.enable_bdbje_debug_mode = true;
+
+ ConnectContext ctx = createMockContext(true, false);
+ ctx.getState().setOk(0, 0, "");
+
+ long before = MetricRepo.COUNTER_QUERY_ALL.getValue();
+ AuditLogHelper.updateMetrics(ctx);
+ long after = MetricRepo.COUNTER_QUERY_ALL.getValue();
+
+ Assert.assertEquals(0, after - before);
+ } finally {
+ Config.enable_bdbje_debug_mode = original;
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]