TDengine/source/client/src/clientMonitorSlow.c

77 lines
2.6 KiB
C
Raw Normal View History

2023-11-02 10:44:18 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "clientInt.h"
#include "clientMonitor.h"
#include "clientLog.h"
#include "tglobal.h"
2024-02-02 06:16:28 +00:00
const char* slowQueryName = "taos_slow_sql:count";
2024-02-02 05:39:31 +00:00
const char* slowQueryHelp = "slow query log when cost over than config duration";
const int slowQueryLabelCount = 4;
const char* slowQueryLabels[] = {"cluster_id", "username", "result", "duration"};
2023-11-02 10:44:18 +00:00
2024-02-22 08:13:50 +00:00
const int64_t usInSeconds = 1000 * 1000;
2023-11-08 01:19:51 +00:00
const int64_t msInMinutes = 60 * 1000;
2023-11-02 10:44:18 +00:00
static const char* getSlowQueryLableCostDesc(int64_t cost) {
2024-02-22 08:13:50 +00:00
if (cost >= 1000 * usInSeconds) {
2024-02-22 03:42:34 +00:00
return "1000s-";
2024-02-22 08:13:50 +00:00
} else if (cost >= 100 * usInSeconds) {
2024-02-22 03:42:34 +00:00
return "100-1000s";
2024-02-22 08:13:50 +00:00
} else if (cost >= 10 * usInSeconds) {
2024-02-22 03:42:34 +00:00
return "10-100s";
2024-02-22 08:13:50 +00:00
} else if (cost >= 3 * usInSeconds) {
2024-02-22 03:42:34 +00:00
return "3-10s";
2023-11-02 10:44:18 +00:00
}
2024-02-22 03:42:34 +00:00
return "0-3s";
2023-11-02 10:44:18 +00:00
}
2024-06-19 10:54:26 +00:00
void monitorClientSlowQueryInit(int64_t clusterid) {
monitorCreateClient(clusterid);
monitorCreateClientCounter(clusterid, slowQueryName, slowQueryHelp, slowQueryLabelCount, slowQueryLabels);
2023-11-02 10:44:18 +00:00
}
2024-06-19 10:54:26 +00:00
void clientSlowQueryLog(int64_t clusterId, const char* user, SQL_RESULT_CODE result, int32_t cost) {
char clusterIdStr[32] = {0};
if (snprintf(clusterIdStr, sizeof(clusterIdStr), "%" PRId64, clusterId) < 0){
uError("failed to generate clusterId:%" PRId64, clusterId);
}
const char* slowQueryLabelValues[] = {clusterIdStr, user, monitorResultStr(result), getSlowQueryLableCostDesc(cost)};
monitorCounterInc(clusterId, slowQueryName, slowQueryLabelValues);
2023-11-02 10:44:18 +00:00
}
2024-06-19 10:54:26 +00:00
void slowQueryLog(int64_t rid, bool killed, int32_t code, int32_t cost) {
2024-02-02 05:39:31 +00:00
SQL_RESULT_CODE result = SQL_RESULT_SUCCESS;
if (TSDB_CODE_SUCCESS != code) {
result = SQL_RESULT_FAILED;
}
// to do Distinguish active Kill events
// else if (killed) {
// result = SQL_RESULT_CANCEL;
// }
2023-12-14 03:41:47 +00:00
STscObj* pTscObj = acquireTscObj(rid);
2023-11-02 10:44:18 +00:00
if (pTscObj != NULL) {
if(pTscObj->pAppInfo == NULL) {
2024-06-19 10:54:26 +00:00
tscLog("slowQueryLog, not found pAppInfo");
2024-02-05 07:18:09 +00:00
} else {
2024-06-19 10:54:26 +00:00
clientSlowQueryLog(pTscObj->pAppInfo->clusterId, pTscObj->user, result, cost);
2023-11-02 10:44:18 +00:00
}
(void)releaseTscObj(rid);
2023-11-02 10:44:18 +00:00
} else {
2024-06-19 10:54:26 +00:00
tscLog("slowQueryLog, not found rid");
2023-11-02 10:44:18 +00:00
}
}