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

tanxinyu pushed a commit to branch rc/1.3.3
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/rc/1.3.3 by this push:
     new 72e5506ea6e [Metrics]: Add metrics for RestApi (#13260) (#13324)
72e5506ea6e is described below

commit 72e5506ea6e2305bf99ddeaaf34adbe44c94c287
Author: 133tosakarin <[email protected]>
AuthorDate: Wed Aug 28 14:35:43 2024 +0800

    [Metrics]: Add metrics for RestApi (#13260) (#13324)
---
 .../protocol/rest/v1/impl/RestApiServiceImpl.java  | 61 +++++++++++---
 .../protocol/rest/v2/impl/RestApiServiceImpl.java  | 72 ++++++++++++++---
 .../protocol/thrift/impl/ClientRPCServiceImpl.java | 93 +++++++---------------
 .../org/apache/iotdb/db/utils/CommonUtils.java     | 43 ++++++++++
 4 files changed, 184 insertions(+), 85 deletions(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
index df8b8464712..11f4ad03ad4 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
@@ -32,6 +32,7 @@ import 
org.apache.iotdb.db.protocol.rest.v1.model.ExecutionStatus;
 import org.apache.iotdb.db.protocol.rest.v1.model.InsertTabletRequest;
 import org.apache.iotdb.db.protocol.rest.v1.model.SQL;
 import org.apache.iotdb.db.protocol.session.SessionManager;
+import org.apache.iotdb.db.protocol.thrift.OperationType;
 import org.apache.iotdb.db.queryengine.plan.Coordinator;
 import org.apache.iotdb.db.queryengine.plan.analyze.ClusterPartitionFetcher;
 import org.apache.iotdb.db.queryengine.plan.analyze.IPartitionFetcher;
@@ -42,6 +43,7 @@ import 
org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution;
 import org.apache.iotdb.db.queryengine.plan.parser.StatementGenerator;
 import org.apache.iotdb.db.queryengine.plan.statement.Statement;
 import 
org.apache.iotdb.db.queryengine.plan.statement.crud.InsertTabletStatement;
+import org.apache.iotdb.db.utils.CommonUtils;
 import org.apache.iotdb.db.utils.SetThreadName;
 import org.apache.iotdb.rpc.TSStatusCode;
 
@@ -49,6 +51,7 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.SecurityContext;
 
 import java.time.ZoneId;
+import java.util.Optional;
 
 public class RestApiServiceImpl extends RestApiService {
 
@@ -76,11 +79,12 @@ public class RestApiServiceImpl extends RestApiService {
   @Override
   public Response executeNonQueryStatement(SQL sql, SecurityContext 
securityContext) {
     Long queryId = null;
+    long startTime = 0;
+    boolean finish = false;
+    Statement statement = null;
     try {
       RequestValidationHandler.validateSQL(sql);
-
-      Statement statement =
-          StatementGenerator.createStatement(sql.getSql(), 
ZoneId.systemDefault());
+      statement = StatementGenerator.createStatement(sql.getSql(), 
ZoneId.systemDefault());
       if (statement == null) {
         return Response.ok()
             .entity(
@@ -89,6 +93,7 @@ public class RestApiServiceImpl extends RestApiService {
                     .message("This operation type is not supported"))
             .build();
       }
+      startTime = System.nanoTime();
       if (!ExecuteStatementHandler.validateStatement(statement)) {
         return Response.ok()
             .entity(
@@ -112,7 +117,7 @@ public class RestApiServiceImpl extends RestApiService {
               partitionFetcher,
               schemaFetcher,
               config.getQueryTimeoutThreshold());
-
+      finish = true;
       return Response.ok()
           .entity(
               (result.status.code == 
TSStatusCode.SUCCESS_STATUS.getStatusCode()
@@ -125,9 +130,22 @@ public class RestApiServiceImpl extends RestApiService {
                       .message(result.status.getMessage()))
           .build();
     } catch (Exception e) {
+      finish = true;
       return 
Response.ok().entity(ExceptionHandler.tryCatchException(e)).build();
     } finally {
+      long costTime = System.nanoTime() - startTime;
+      Optional.ofNullable(statement)
+          .ifPresent(
+              s -> {
+                CommonUtils.addStatementExecutionLatency(
+                    OperationType.EXECUTE_NON_QUERY_PLAN, s.getType().name(), 
costTime);
+              });
       if (queryId != null) {
+        if (finish) {
+          long executeTime = COORDINATOR.getTotalExecutionTime(queryId);
+          CommonUtils.addQueryLatency(
+              statement.getType(), executeTime > 0 ? executeTime : costTime);
+        }
         COORDINATOR.cleanupQueryExecution(queryId);
       }
     }
@@ -136,11 +154,13 @@ public class RestApiServiceImpl extends RestApiService {
   @Override
   public Response executeQueryStatement(SQL sql, SecurityContext 
securityContext) {
     Long queryId = null;
+    long startTime = 0;
+    boolean finish = false;
+    Statement statement = null;
     try {
       RequestValidationHandler.validateSQL(sql);
-
-      Statement statement =
-          StatementGenerator.createStatement(sql.getSql(), 
ZoneId.systemDefault());
+      startTime = System.nanoTime();
+      statement = StatementGenerator.createStatement(sql.getSql(), 
ZoneId.systemDefault());
       if (statement == null) {
         return Response.ok()
             .entity(
@@ -173,6 +193,7 @@ public class RestApiServiceImpl extends RestApiService {
               partitionFetcher,
               schemaFetcher,
               config.getQueryTimeoutThreshold());
+      finish = true;
       if (result.status.code != TSStatusCode.SUCCESS_STATUS.getStatusCode()
           && result.status.code != 
TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode()) {
         return Response.ok()
@@ -190,9 +211,22 @@ public class RestApiServiceImpl extends RestApiService {
             sql.getRowLimit() == null ? defaultQueryRowLimit : 
sql.getRowLimit());
       }
     } catch (Exception e) {
+      finish = true;
       return 
Response.ok().entity(ExceptionHandler.tryCatchException(e)).build();
     } finally {
+      long costTime = System.nanoTime() - startTime;
+      Optional.ofNullable(statement)
+          .ifPresent(
+              s -> {
+                CommonUtils.addStatementExecutionLatency(
+                    OperationType.EXECUTE_QUERY_STATEMENT, s.getType().name(), 
costTime);
+              });
       if (queryId != null) {
+        if (finish) {
+          long executeTime = COORDINATOR.getTotalExecutionTime(queryId);
+          CommonUtils.addQueryLatency(
+              statement.getType(), executeTime > 0 ? executeTime : costTime);
+        }
         COORDINATOR.cleanupQueryExecution(queryId);
       }
     }
@@ -202,6 +236,8 @@ public class RestApiServiceImpl extends RestApiService {
   public Response insertTablet(
       InsertTabletRequest insertTabletRequest, SecurityContext 
securityContext) {
     Long queryId = null;
+    long startTime = 0;
+    InsertTabletStatement insertTabletStatement = null;
     try {
       
RequestValidationHandler.validateInsertTabletRequest(insertTabletRequest);
 
@@ -214,8 +250,8 @@ public class RestApiServiceImpl extends RestApiService {
             InsertTabletSortDataUtils.sortList(
                 insertTabletRequest.getValues(), index, 
insertTabletRequest.getDataTypes().size()));
       }
-
-      InsertTabletStatement insertTabletStatement =
+      startTime = System.nanoTime();
+      insertTabletStatement =
           
StatementConstructionHandler.constructInsertTabletStatement(insertTabletRequest);
 
       Response response =
@@ -248,6 +284,13 @@ public class RestApiServiceImpl extends RestApiService {
     } catch (Exception e) {
       return 
Response.ok().entity(ExceptionHandler.tryCatchException(e)).build();
     } finally {
+      long costTime = System.nanoTime() - startTime;
+      Optional.ofNullable(insertTabletStatement)
+          .ifPresent(
+              s -> {
+                CommonUtils.addStatementExecutionLatency(
+                    OperationType.INSERT_TABLET, s.getType().name(), costTime);
+              });
       if (queryId != null) {
         COORDINATOR.cleanupQueryExecution(queryId);
       }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
index 574f0520938..67d6ed1a033 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
@@ -34,6 +34,7 @@ import 
org.apache.iotdb.db.protocol.rest.v2.model.InsertRecordsRequest;
 import org.apache.iotdb.db.protocol.rest.v2.model.InsertTabletRequest;
 import org.apache.iotdb.db.protocol.rest.v2.model.SQL;
 import org.apache.iotdb.db.protocol.session.SessionManager;
+import org.apache.iotdb.db.protocol.thrift.OperationType;
 import org.apache.iotdb.db.queryengine.plan.Coordinator;
 import org.apache.iotdb.db.queryengine.plan.analyze.ClusterPartitionFetcher;
 import org.apache.iotdb.db.queryengine.plan.analyze.IPartitionFetcher;
@@ -45,6 +46,7 @@ import 
org.apache.iotdb.db.queryengine.plan.parser.StatementGenerator;
 import org.apache.iotdb.db.queryengine.plan.statement.Statement;
 import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowsStatement;
 import 
org.apache.iotdb.db.queryengine.plan.statement.crud.InsertTabletStatement;
+import org.apache.iotdb.db.utils.CommonUtils;
 import org.apache.iotdb.db.utils.SetThreadName;
 import org.apache.iotdb.rpc.TSStatusCode;
 
@@ -53,6 +55,7 @@ import javax.ws.rs.core.SecurityContext;
 
 import java.time.ZoneId;
 import java.util.List;
+import java.util.Optional;
 
 public class RestApiServiceImpl extends RestApiService {
 
@@ -80,11 +83,13 @@ public class RestApiServiceImpl extends RestApiService {
   @Override
   public Response executeNonQueryStatement(SQL sql, SecurityContext 
securityContext) {
     Long queryId = null;
+    Statement statement = null;
+    long startTime = 0;
+    boolean finish = false;
     try {
       RequestValidationHandler.validateSQL(sql);
-
-      Statement statement =
-          StatementGenerator.createStatement(sql.getSql(), 
ZoneId.systemDefault());
+      startTime = System.nanoTime();
+      statement = StatementGenerator.createStatement(sql.getSql(), 
ZoneId.systemDefault());
       if (statement == null) {
         return Response.ok()
             .entity(
@@ -116,12 +121,25 @@ public class RestApiServiceImpl extends RestApiService {
               partitionFetcher,
               schemaFetcher,
               config.getQueryTimeoutThreshold());
-
+      finish = true;
       return responseGenerateHelper(result);
     } catch (Exception e) {
+      finish = true;
       return 
Response.ok().entity(ExceptionHandler.tryCatchException(e)).build();
     } finally {
+      long costTime = System.nanoTime() - startTime;
+      Optional.ofNullable(statement)
+          .ifPresent(
+              s -> {
+                CommonUtils.addStatementExecutionLatency(
+                    OperationType.EXECUTE_NON_QUERY_PLAN, s.getType().name(), 
costTime);
+              });
       if (queryId != null) {
+        if (finish) {
+          long executionTime = COORDINATOR.getTotalExecutionTime(queryId);
+          CommonUtils.addQueryLatency(
+              statement.getType(), executionTime > 0 ? executionTime : 
costTime);
+        }
         COORDINATOR.cleanupQueryExecution(queryId);
       }
     }
@@ -130,11 +148,13 @@ public class RestApiServiceImpl extends RestApiService {
   @Override
   public Response executeQueryStatement(SQL sql, SecurityContext 
securityContext) {
     Long queryId = null;
+    Statement statement = null;
+    long startTime = 0;
+    boolean finish = false;
     try {
       RequestValidationHandler.validateSQL(sql);
-
-      Statement statement =
-          StatementGenerator.createStatement(sql.getSql(), 
ZoneId.systemDefault());
+      startTime = System.nanoTime();
+      statement = StatementGenerator.createStatement(sql.getSql(), 
ZoneId.systemDefault());
 
       if (statement == null) {
         return Response.ok()
@@ -170,6 +190,7 @@ public class RestApiServiceImpl extends RestApiService {
               partitionFetcher,
               schemaFetcher,
               config.getQueryTimeoutThreshold());
+      finish = true;
       if (result.status.code != TSStatusCode.SUCCESS_STATUS.getStatusCode()
           && result.status.code != 
TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode()) {
         return Response.ok()
@@ -187,9 +208,22 @@ public class RestApiServiceImpl extends RestApiService {
             sql.getRowLimit() == null ? defaultQueryRowLimit : 
sql.getRowLimit());
       }
     } catch (Exception e) {
+      finish = true;
       return 
Response.ok().entity(ExceptionHandler.tryCatchException(e)).build();
     } finally {
+      long costTime = System.nanoTime() - startTime;
+      Optional.ofNullable(statement)
+          .ifPresent(
+              s -> {
+                CommonUtils.addStatementExecutionLatency(
+                    OperationType.EXECUTE_QUERY_STATEMENT, s.getType().name(), 
costTime);
+              });
       if (queryId != null) {
+        if (finish) {
+          long executionTime = COORDINATOR.getTotalExecutionTime(queryId);
+          CommonUtils.addQueryLatency(
+              statement.getType(), executionTime > 0 ? executionTime : 
costTime);
+        }
         COORDINATOR.cleanupQueryExecution(queryId);
       }
     }
@@ -199,10 +233,13 @@ public class RestApiServiceImpl extends RestApiService {
   public Response insertRecords(
       InsertRecordsRequest insertRecordsRequest, SecurityContext 
securityContext) {
     Long queryId = null;
+    long startTime = 0;
+    InsertRowsStatement insertRowsStatement = null;
     try {
+      startTime = System.nanoTime();
       
RequestValidationHandler.validateInsertRecordsRequest(insertRecordsRequest);
 
-      InsertRowsStatement insertRowsStatement =
+      insertRowsStatement =
           
StatementConstructionHandler.createInsertRowsStatement(insertRecordsRequest);
 
       Response response = authorizationHandler.checkAuthority(securityContext, 
insertRowsStatement);
@@ -224,6 +261,13 @@ public class RestApiServiceImpl extends RestApiService {
     } catch (Exception e) {
       return 
Response.ok().entity(ExceptionHandler.tryCatchException(e)).build();
     } finally {
+      long costTime = System.nanoTime() - startTime;
+      Optional.ofNullable(insertRowsStatement)
+          .ifPresent(
+              s -> {
+                CommonUtils.addStatementExecutionLatency(
+                    OperationType.INSERT_RECORDS, s.getType().name(), 
costTime);
+              });
       if (queryId != null) {
         COORDINATOR.cleanupQueryExecution(queryId);
       }
@@ -234,6 +278,8 @@ public class RestApiServiceImpl extends RestApiService {
   public Response insertTablet(
       InsertTabletRequest insertTabletRequest, SecurityContext 
securityContext) {
     Long queryId = null;
+    long startTime = 0;
+    InsertTabletStatement insertTabletStatement = null;
     try {
       
RequestValidationHandler.validateInsertTabletRequest(insertTabletRequest);
 
@@ -247,7 +293,7 @@ public class RestApiServiceImpl extends RestApiService {
                 insertTabletRequest.getValues(), index, 
insertTabletRequest.getDataTypes().size()));
       }
 
-      InsertTabletStatement insertTabletStatement =
+      insertTabletStatement =
           
StatementConstructionHandler.constructInsertTabletStatement(insertTabletRequest);
 
       Response response =
@@ -265,11 +311,17 @@ public class RestApiServiceImpl extends RestApiService {
               partitionFetcher,
               schemaFetcher,
               config.getQueryTimeoutThreshold());
-
       return responseGenerateHelper(result);
     } catch (Exception e) {
       return 
Response.ok().entity(ExceptionHandler.tryCatchException(e)).build();
     } finally {
+      long costTime = System.nanoTime() - startTime;
+      Optional.ofNullable(insertTabletStatement)
+          .ifPresent(
+              s -> {
+                CommonUtils.addStatementExecutionLatency(
+                    OperationType.INSERT_TABLET, s.getType().name(), costTime);
+              });
       if (queryId != null) {
         COORDINATOR.cleanupQueryExecution(queryId);
       }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
index 5c2b3f1f492..e7b9d19b4e5 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
@@ -39,9 +39,6 @@ import 
org.apache.iotdb.commons.partition.DataPartitionQueryParam;
 import org.apache.iotdb.commons.path.AlignedPath;
 import org.apache.iotdb.commons.path.MeasurementPath;
 import org.apache.iotdb.commons.path.PartialPath;
-import org.apache.iotdb.commons.service.metric.MetricService;
-import org.apache.iotdb.commons.service.metric.enums.Metric;
-import org.apache.iotdb.commons.service.metric.enums.Tag;
 import org.apache.iotdb.commons.utils.PathUtils;
 import org.apache.iotdb.commons.utils.TimePartitionUtils;
 import org.apache.iotdb.db.audit.AuditLogger;
@@ -115,10 +112,10 @@ import 
org.apache.iotdb.db.storageengine.dataregion.DataRegion;
 import 
org.apache.iotdb.db.storageengine.rescon.quotas.DataNodeThrottleQuotaManager;
 import org.apache.iotdb.db.storageengine.rescon.quotas.OperationQuota;
 import org.apache.iotdb.db.subscription.agent.SubscriptionAgent;
+import org.apache.iotdb.db.utils.CommonUtils;
 import org.apache.iotdb.db.utils.QueryDataSetUtils;
 import org.apache.iotdb.db.utils.SchemaUtils;
 import org.apache.iotdb.db.utils.SetThreadName;
-import org.apache.iotdb.metrics.utils.MetricLevel;
 import org.apache.iotdb.rpc.RpcUtils;
 import org.apache.iotdb.rpc.TSStatusCode;
 import org.apache.iotdb.service.rpc.thrift.ServerProperties;
@@ -358,14 +355,15 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
 
       // record each operation time cost
       if (statementType != null) {
-        addStatementExecutionLatency(
+        CommonUtils.addStatementExecutionLatency(
             OperationType.EXECUTE_QUERY_STATEMENT, statementType.name(), 
currentOperationCost);
       }
 
       if (finished) {
         // record total time cost for one query
         long executionTime = COORDINATOR.getTotalExecutionTime(queryId);
-        addQueryLatency(statementType, executionTime > 0 ? executionTime : 
currentOperationCost);
+        CommonUtils.addQueryLatency(
+            statementType, executionTime > 0 ? executionTime : 
currentOperationCost);
         COORDINATOR.cleanupQueryExecution(queryId, req, t);
       }
       SESSION_MANAGER.updateIdleTime();
@@ -448,13 +446,13 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       COORDINATOR.recordExecutionTime(queryId, currentOperationCost);
 
       // record each operation time cost
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.EXECUTE_RAW_DATA_QUERY, StatementType.QUERY.name(), 
currentOperationCost);
 
       if (finished) {
         // record total time cost for one query
         long executionTime = COORDINATOR.getTotalExecutionTime(queryId);
-        addQueryLatency(
+        CommonUtils.addQueryLatency(
             StatementType.QUERY, executionTime > 0 ? executionTime : 
currentOperationCost);
         COORDINATOR.cleanupQueryExecution(queryId, req, t);
       }
@@ -540,13 +538,13 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       COORDINATOR.recordExecutionTime(queryId, currentOperationCost);
 
       // record each operation time cost
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.EXECUTE_LAST_DATA_QUERY, StatementType.QUERY.name(), 
currentOperationCost);
 
       if (finished) {
         // record total time cost for one query
         long executionTime = COORDINATOR.getTotalExecutionTime(queryId);
-        addQueryLatency(
+        CommonUtils.addQueryLatency(
             StatementType.QUERY, executionTime > 0 ? executionTime : 
currentOperationCost);
         COORDINATOR.cleanupQueryExecution(queryId, req, t);
       }
@@ -629,13 +627,13 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       COORDINATOR.recordExecutionTime(queryId, currentOperationCost);
 
       // record each operation time cost
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.EXECUTE_AGG_QUERY, StatementType.QUERY.name(), 
currentOperationCost);
 
       if (finished) {
         // record total time cost for one query
         long executionTime = COORDINATOR.getTotalExecutionTime(queryId);
-        addQueryLatency(
+        CommonUtils.addQueryLatency(
             StatementType.QUERY, executionTime > 0 ? executionTime : 
currentOperationCost);
         COORDINATOR.cleanupQueryExecution(queryId, req, t);
       }
@@ -972,13 +970,13 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       COORDINATOR.recordExecutionTime(queryId, currentOperationCost);
 
       // record each operation time cost
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.EXECUTE_LAST_DATA_QUERY, StatementType.QUERY.name(), 
currentOperationCost);
 
       if (finished) {
         // record total time cost for one query
         long executionTime = COORDINATOR.getTotalExecutionTime(queryId);
-        addQueryLatency(
+        CommonUtils.addQueryLatency(
             StatementType.QUERY, executionTime > 0 ? executionTime : 
currentOperationCost);
         COORDINATOR.cleanupQueryExecution(queryId, req, t);
       }
@@ -1125,13 +1123,13 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       COORDINATOR.recordExecutionTime(req.queryId, currentOperationCost);
 
       // record each operation time cost
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.FETCH_RESULTS, statementType, currentOperationCost);
 
       if (finished) {
         // record total time cost for one query
         long executionTime = COORDINATOR.getTotalExecutionTime(req.queryId);
-        addQueryLatency(
+        CommonUtils.addQueryLatency(
             StatementType.QUERY, executionTime > 0 ? executionTime : 
currentOperationCost);
         COORDINATOR.cleanupQueryExecution(req.queryId, req, t);
       }
@@ -1568,7 +1566,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
           isAllSuccessful = false;
           results.add(status);
         } finally {
-          addStatementExecutionLatency(
+          CommonUtils.addStatementExecutionLatency(
               OperationType.EXECUTE_STATEMENT, type, System.nanoTime() - t2);
           if (quota != null) {
             quota.close();
@@ -1576,7 +1574,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
         }
       }
     } finally {
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.EXECUTE_BATCH_STATEMENT, StatementType.NULL.name(), 
System.nanoTime() - t1);
       SESSION_MANAGER.updateIdleTime();
     }
@@ -1646,13 +1644,13 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       COORDINATOR.recordExecutionTime(req.queryId, currentOperationCost);
 
       // record each operation time cost
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.FETCH_RESULTS, statementType, currentOperationCost);
 
       if (finished) {
         // record total time cost for one query
         long executionTime = COORDINATOR.getTotalExecutionTime(req.queryId);
-        addQueryLatency(
+        CommonUtils.addQueryLatency(
             StatementType.QUERY, executionTime > 0 ? executionTime : 
currentOperationCost);
         COORDINATOR.cleanupQueryExecution(req.queryId, req, t);
       }
@@ -1719,7 +1717,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       return onNpeOrUnexpectedException(
           e, OperationType.INSERT_RECORDS, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.INSERT_RECORDS,
           StatementType.BATCH_INSERT_ROWS.name(),
           System.nanoTime() - t1);
@@ -1788,7 +1786,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       return onNpeOrUnexpectedException(
           e, OperationType.INSERT_RECORDS_OF_ONE_DEVICE, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.INSERT_RECORDS_OF_ONE_DEVICE,
           StatementType.BATCH_INSERT_ONE_DEVICE.name(),
           System.nanoTime() - t1);
@@ -1859,7 +1857,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
           OperationType.INSERT_STRING_RECORDS_OF_ONE_DEVICE,
           TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.INSERT_STRING_RECORDS_OF_ONE_DEVICE,
           StatementType.BATCH_INSERT_ONE_DEVICE.name(),
           System.nanoTime() - t1);
@@ -1925,7 +1923,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       return onNpeOrUnexpectedException(
           e, OperationType.INSERT_RECORD, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.INSERT_RECORD, StatementType.INSERT.name(), 
System.nanoTime() - t1);
       SESSION_MANAGER.updateIdleTime();
       if (quota != null) {
@@ -1982,7 +1980,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       return onNpeOrUnexpectedException(
           e, OperationType.INSERT_TABLETS, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.INSERT_TABLETS,
           StatementType.MULTI_BATCH_INSERT.name(),
           System.nanoTime() - t1);
@@ -2040,7 +2038,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       return onNpeOrUnexpectedException(
           e, OperationType.INSERT_TABLET, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.INSERT_TABLET, StatementType.BATCH_INSERT.name(), 
System.nanoTime() - t1);
       SESSION_MANAGER.updateIdleTime();
       if (quota != null) {
@@ -2105,7 +2103,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       return onNpeOrUnexpectedException(
           e, OperationType.INSERT_STRING_RECORDS, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.INSERT_STRING_RECORDS,
           StatementType.BATCH_INSERT_ROWS.name(),
           System.nanoTime() - t1);
@@ -2448,7 +2446,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
           onQueryException(e, "\"" + statement + "\". " + 
OperationType.EXECUTE_STATEMENT));
       return null;
     } finally {
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.EXECUTE_STATEMENT,
           statement.getType().name(),
           System.nanoTime() - startTime);
@@ -2738,7 +2736,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       return onNpeOrUnexpectedException(
           e, OperationType.INSERT_STRING_RECORD, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.INSERT_STRING_RECORD, StatementType.INSERT.name(), 
System.nanoTime() - t1);
       SESSION_MANAGER.updateIdleTime();
       if (quota != null) {
@@ -2766,43 +2764,6 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
         "Log in failed. Either you are not authorized or the session has timed 
out.");
   }
 
-  /** Add stat of whole stage query into metrics */
-  private void addQueryLatency(StatementType statementType, long 
costTimeInNanos) {
-    if (statementType == null) {
-      return;
-    }
-
-    MetricService.getInstance()
-        .timer(
-            costTimeInNanos,
-            TimeUnit.NANOSECONDS,
-            Metric.PERFORMANCE_OVERVIEW.toString(),
-            MetricLevel.CORE,
-            Tag.INTERFACE.toString(),
-            OperationType.QUERY_LATENCY.toString(),
-            Tag.TYPE.toString(),
-            statementType.name());
-  }
-
-  /** Add stat of operation into metrics */
-  private void addStatementExecutionLatency(
-      OperationType operation, String statementType, long costTime) {
-    if (statementType == null) {
-      return;
-    }
-
-    MetricService.getInstance()
-        .timer(
-            costTime,
-            TimeUnit.NANOSECONDS,
-            Metric.PERFORMANCE_OVERVIEW.toString(),
-            MetricLevel.CORE,
-            Tag.INTERFACE.toString(),
-            operation.toString(),
-            Tag.TYPE.toString(),
-            statementType);
-  }
-
   private String checkIdentifierAndRemoveBackQuotesIfNecessary(String 
identifier) {
     return identifier == null ? null : ASTVisitor.parseIdentifier(identifier);
   }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/CommonUtils.java 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/CommonUtils.java
index cfca21019a8..e0819843cc2 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/CommonUtils.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/CommonUtils.java
@@ -18,11 +18,17 @@
  */
 package org.apache.iotdb.db.utils;
 
+import org.apache.iotdb.commons.service.metric.MetricService;
+import org.apache.iotdb.commons.service.metric.enums.Metric;
+import org.apache.iotdb.commons.service.metric.enums.Tag;
 import org.apache.iotdb.db.exception.query.QueryProcessException;
 import org.apache.iotdb.db.exception.sql.SemanticException;
+import org.apache.iotdb.db.protocol.thrift.OperationType;
 import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution;
+import org.apache.iotdb.db.queryengine.plan.statement.StatementType;
 import org.apache.iotdb.db.queryengine.plan.statement.literal.BinaryLiteral;
 import org.apache.iotdb.db.utils.constant.SqlConstant;
+import org.apache.iotdb.metrics.utils.MetricLevel;
 import org.apache.iotdb.service.rpc.thrift.TSAggregationQueryReq;
 import org.apache.iotdb.service.rpc.thrift.TSFastLastDataQueryForOneDeviceReq;
 import org.apache.iotdb.service.rpc.thrift.TSFetchResultsReq;
@@ -48,6 +54,7 @@ import java.time.ZoneId;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
+import java.util.concurrent.TimeUnit;
 
 @SuppressWarnings("java:S106") // for console outputs
 public class CommonUtils {
@@ -399,4 +406,40 @@ public class CommonUtils {
     System.err.println("-- StackTrace --");
     System.err.println(Throwables.getStackTraceAsString(e));
   }
+
+  /** Add stat of operation into metrics */
+  public static void addStatementExecutionLatency(
+      OperationType operation, String statementType, long costTime) {
+    if (statementType == null) {
+      return;
+    }
+
+    MetricService.getInstance()
+        .timer(
+            costTime,
+            TimeUnit.NANOSECONDS,
+            Metric.PERFORMANCE_OVERVIEW.toString(),
+            MetricLevel.CORE,
+            Tag.INTERFACE.toString(),
+            operation.toString(),
+            Tag.TYPE.toString(),
+            statementType);
+  }
+
+  public static void addQueryLatency(StatementType statementType, long 
costTimeInNanos) {
+    if (statementType == null) {
+      return;
+    }
+
+    MetricService.getInstance()
+        .timer(
+            costTimeInNanos,
+            TimeUnit.NANOSECONDS,
+            Metric.PERFORMANCE_OVERVIEW.toString(),
+            MetricLevel.CORE,
+            Tag.INTERFACE.toString(),
+            OperationType.QUERY_LATENCY.toString(),
+            Tag.TYPE.toString(),
+            statementType.name());
+  }
 }

Reply via email to