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

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


The following commit(s) were added to refs/heads/master by this push:
     new 4c1a9d86dec [Metrics]: Add metrics for RestApi (#13260)
4c1a9d86dec is described below

commit 4c1a9d86dec0f3112e4669219c1aef834c72bb7b
Author: 133tosakarin <[email protected]>
AuthorDate: Tue Aug 27 16:23:24 2024 +0800

    [Metrics]: Add metrics for RestApi (#13260)
---
 .../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 392768f592f..8b7c7814b3f 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
@@ -41,9 +41,6 @@ import org.apache.iotdb.commons.path.IFullPath;
 import org.apache.iotdb.commons.path.MeasurementPath;
 import org.apache.iotdb.commons.path.NonAlignedFullPath;
 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;
@@ -122,10 +119,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;
@@ -422,14 +419,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();
@@ -512,13 +510,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);
       }
@@ -604,13 +602,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);
       }
@@ -693,13 +691,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);
       }
@@ -1038,13 +1036,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);
       }
@@ -1191,13 +1189,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);
       }
@@ -1709,7 +1707,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();
@@ -1717,7 +1715,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
         }
       }
     } finally {
-      addStatementExecutionLatency(
+      CommonUtils.addStatementExecutionLatency(
           OperationType.EXECUTE_BATCH_STATEMENT, StatementType.NULL.name(), 
System.nanoTime() - t1);
       SESSION_MANAGER.updateIdleTime();
     }
@@ -1796,13 +1794,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);
       }
@@ -1869,7 +1867,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);
@@ -1938,7 +1936,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);
@@ -2009,7 +2007,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);
@@ -2091,7 +2089,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) {
@@ -2148,7 +2146,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);
@@ -2223,7 +2221,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) {
@@ -2288,7 +2286,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);
@@ -2631,7 +2629,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);
@@ -2921,7 +2919,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) {
@@ -2952,43 +2950,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 13e3da8ff3f..3b3e6311164 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
@@ -20,12 +20,18 @@
 package org.apache.iotdb.db.utils;
 
 import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
+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.CommonDateTimeUtils;
 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;
@@ -54,6 +60,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 {
@@ -477,4 +484,40 @@ public class CommonUtils {
     array[i] = array[j];
     array[j] = tmp;
   }
+
+  /** 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