• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.31
/source/dnode/mnode/impl/src/mndTelem.c
1
/*
2
 * Copyright (c) 2020 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "mndTelem.h"
18
#include "mndCluster.h"
19
#include "mndSync.h"
20
#include "thttp.h"
21
#include "tjson.h"
22
#include "mndAnode.h"
23

24

25
typedef struct {
26
  int64_t numOfDnode;
27
  int64_t numOfMnode;
28
  int64_t numOfVgroup;
29
  int64_t numOfDatabase;
30
  int64_t numOfSuperTable;
31
  int64_t numOfChildTable;
32
  int64_t numOfNormalTable;
33
  int64_t numOfColumn;
34
  int64_t totalPoints;
35
  int64_t totalStorage;
36
  int64_t compStorage;
37
  int32_t numOfAnalysisAlgos;
38
} SMnodeStat;
39

40
static void mndGetStat(SMnode* pMnode, SMnodeStat* pStat) {
346✔
41
  memset(pStat, 0, sizeof(SMnodeStat));
346!
42

43
  SSdb* pSdb = pMnode->pSdb;
346✔
44
  pStat->numOfDnode = sdbGetSize(pSdb, SDB_DNODE);
346✔
45
  pStat->numOfMnode = sdbGetSize(pSdb, SDB_MNODE);
346✔
46
  pStat->numOfVgroup = sdbGetSize(pSdb, SDB_VGROUP);
346✔
47
  pStat->numOfDatabase = sdbGetSize(pSdb, SDB_DB);
346✔
48
  pStat->numOfSuperTable = sdbGetSize(pSdb, SDB_STB);
346✔
49

50
  void* pIter = NULL;
346✔
51
  while (1) {
1,384✔
52
    SVgObj* pVgroup = NULL;
1,730✔
53
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
1,730✔
54
    if (pIter == NULL) break;
1,730✔
55

56
    if (pVgroup->mountVgId) {
1,384!
57
      sdbRelease(pSdb, pVgroup);
×
58
      continue;
×
59
    }
60
    pStat->numOfChildTable += pVgroup->numOfTables;
1,384✔
61
    pStat->numOfColumn += pVgroup->numOfTimeSeries;
1,384✔
62
    pStat->totalPoints += pVgroup->pointsWritten;
1,384✔
63
    pStat->totalStorage += pVgroup->totalStorage;
1,384✔
64
    pStat->compStorage += pVgroup->compStorage;
1,384✔
65

66
    sdbRelease(pSdb, pVgroup);
1,384✔
67
  }
68
}
346✔
69

70
static int32_t algoToJson(const void* pObj, SJson* pJson) {
×
71
  const SAnodeAlgo* pNode = (const SAnodeAlgo*)pObj;
×
72
  int32_t code = tjsonAddStringToObject(pJson, "name", pNode->name);
×
73
  return code;
×
74
}
75

76
static void mndBuildRuntimeInfo(SMnode* pMnode, SJson* pJson) {
346✔
77
  SMnodeStat mstat = {0};
346✔
78
  int32_t    code = 0;
346✔
79
  int32_t    lino = 0;
346✔
80
  SArray*    pFcList = NULL;
346✔
81
  SArray*    pAdList = NULL;
346✔
82

83
  mndGetStat(pMnode, &mstat);
346✔
84

85
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfDnode", mstat.numOfDnode), &lino, _OVER);
346!
86
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfMnode", mstat.numOfMnode), &lino, _OVER);
346!
87
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfVgroup", mstat.numOfVgroup), &lino, _OVER);
346!
88
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfDatabase", mstat.numOfDatabase), &lino, _OVER);
346!
89
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfSuperTable", mstat.numOfSuperTable), &lino, _OVER);
346!
90
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfChildTable", mstat.numOfChildTable), &lino, _OVER);
346!
91
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfColumn", mstat.numOfColumn), &lino, _OVER);
346!
92
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfPoint", mstat.totalPoints), &lino, _OVER);
346!
93
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "totalStorage", mstat.totalStorage), &lino, _OVER);
346!
94
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "compStorage", mstat.compStorage), &lino, _OVER);
346!
95

96
  pFcList = taosArrayInit(4, sizeof(SAnodeAlgo));
346✔
97
  pAdList = taosArrayInit(4, sizeof(SAnodeAlgo));
346✔
98
  if (pFcList == NULL || pAdList == NULL) {
346!
99
    lino = __LINE__;
×
100
    goto _OVER;
×
101
  }
102

103
  mndRetrieveAlgoList(pMnode, pFcList, pAdList);
346✔
104

105
  if (taosArrayGetSize(pFcList) > 0) {
346!
106
    SJson* items = tjsonAddArrayToObject(pJson, "forecast");
×
107
    TSDB_CHECK_NULL(items, code, lino, _OVER, terrno);
×
108

109
    for (int32_t i = 0; i < taosArrayGetSize(pFcList); ++i) {
×
110
      SJson* item = tjsonCreateObject();
×
111

112
      TSDB_CHECK_NULL(item, code, lino, _OVER, terrno);
×
113
      TAOS_CHECK_GOTO(tjsonAddItemToArray(items, item), &lino, _OVER);
×
114

115
      SAnodeAlgo* p = taosArrayGet(pFcList, i);
×
116
      TSDB_CHECK_NULL(p, code, lino, _OVER, terrno);
×
117
      TAOS_CHECK_GOTO(tjsonAddStringToObject(item, "name", p->name), &lino, _OVER);
×
118
    }
119
  }
120

121
  if (taosArrayGetSize(pAdList) > 0) {
346!
122
    SJson* items1 = tjsonAddArrayToObject(pJson, "anomaly_detection");
×
123
    TSDB_CHECK_NULL(items1, code, lino, _OVER, terrno);
×
124

125
    for (int32_t i = 0; i < taosArrayGetSize(pAdList); ++i) {
×
126
      SJson* item = tjsonCreateObject();
×
127

128
      TSDB_CHECK_NULL(item, code, lino, _OVER, terrno);
×
129
      TAOS_CHECK_GOTO(tjsonAddItemToArray(items1, item), &lino, _OVER);
×
130

131
      SAnodeAlgo* p = taosArrayGet(pAdList, i);
×
132
      TSDB_CHECK_NULL(p, code, lino, _OVER, terrno);
×
133
      TAOS_CHECK_GOTO(tjsonAddStringToObject(item, "name", p->name), &lino, _OVER);
×
134
    }
135
  }
136

137
_OVER:
346✔
138
  taosArrayDestroy(pFcList);
346✔
139
  taosArrayDestroy(pAdList);
346✔
140

141
  if (code != 0) {
346!
142
    mError("failed to mndBuildRuntimeInfo at line:%d since %s", lino, tstrerror(code));
×
143
  }
144
}
346✔
145

146
static char* mndBuildTelemetryReport(SMnode* pMnode) {
346✔
147
  char        tmp[4096] = {0};
346✔
148
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
346✔
149
  int32_t     code = 0;
346✔
150
  int32_t     lino = 0;
346✔
151

152
  SJson* pJson = tjsonCreateObject();
346✔
153
  if (pJson == NULL) return NULL;
346!
154

155
  char clusterName[64] = {0};
346✔
156
  if ((terrno = mndGetClusterName(pMnode, clusterName, sizeof(clusterName))) != 0) return NULL;
346!
157
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "instanceId", clusterName), &lino, _OVER);
346!
158
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "reportVersion", 1), &lino, _OVER);
346!
159

160
  if (taosGetOsReleaseName(tmp, NULL, NULL, sizeof(tmp)) == 0) {
346!
161
    TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "os", tmp), &lino, _OVER);
346!
162
  }
163

164
  float numOfCores = 0;
346✔
165
  if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores) == 0) {
346!
166
    TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "cpuModel", tmp), &lino, _OVER);
346!
167
    TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfCpu", numOfCores), &lino, _OVER);
346!
168
  } else {
169
    TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfCpu", tsNumOfCores), &lino, _OVER);
×
170
  }
171

172
  snprintf(tmp, sizeof(tmp), "%" PRId64 " kB", tsTotalMemoryKB);
346!
173
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "memory", tmp), &lino, _OVER);
346!
174

175
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "version", td_version), &lino, _OVER);
346!
176
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "buildInfo", td_buildinfo), &lino, _OVER);
346!
177
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "gitInfo", td_gitinfo), &lino, _OVER);
346!
178
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "email", pMgmt->email), &lino, _OVER);
346!
179

180
  mndBuildRuntimeInfo(pMnode, pJson);
346✔
181

182
_OVER:
346✔
183
  if (code != 0) {
346!
184
    mError("failed to build telemetry report at lino:%d, since %s", lino, tstrerror(code));
×
185
  }
186
  char* pCont = tjsonToString(pJson);
346✔
187
  tjsonDelete(pJson);
346✔
188
  return pCont;
346✔
189
}
190

191
static int32_t mndProcessTelemTimer(SRpcMsg* pReq) {
346✔
192
  int32_t     code = 0;
346✔
193
  int32_t     line = 0;
346✔
194
  SMnode*     pMnode = pReq->info.node;
346✔
195
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
346✔
196

197
  if (!tsEnableTelem) {
346!
198
    return 0;
×
199
  }
200

201
  (void)taosThreadMutexLock(&pMgmt->lock);
346✔
202
  char* pCont = mndBuildTelemetryReport(pMnode);
346✔
203
  (void)taosThreadMutexUnlock(&pMgmt->lock);
346✔
204

205
  TSDB_CHECK_NULL(pCont, code, line, _end, terrno);
346!
206

207
  code = taosSendTelemReport(&pMgmt->addrMgt, tsTelemUri, tsTelemPort, pCont, strlen(pCont), HTTP_FLAT);
346!
208
  taosMemoryFree(pCont);
346!
209
  return code;
346✔
210

211
_end:
×
212
  if (code != 0) {
×
213
    mError("%s failed to send telemetry report, line %d since %s", __func__, line, tstrerror(code));
×
214
  }
215
  taosMemoryFree(pCont);
×
216
  return code;
×
217
}
218

219
int32_t mndInitTelem(SMnode* pMnode) {
550,067✔
220
  int32_t     code = 0;
550,067✔
221
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
550,067✔
222

223
  (void)taosThreadMutexInit(&pMgmt->lock, NULL);
550,067✔
224
  if ((code = taosGetEmail(pMgmt->email, sizeof(pMgmt->email))) != 0) {
550,067!
225
    mWarn("failed to get email since %s", tstrerror(code));
550,067!
226
  }
227

228
  code = taosTelemetryMgtInit(&pMgmt->addrMgt, tsTelemServer);
550,067✔
229
  if (code != 0) {
550,067!
230
    mError("failed to init telemetry management since %s", tstrerror(code));
×
231
    return code;
×
232
  }
233

234
  mndSetMsgHandle(pMnode, TDMT_MND_TELEM_TIMER, mndProcessTelemTimer);
550,067✔
235
  return 0;
550,067✔
236
}
237

238
void mndCleanupTelem(SMnode* pMnode) {
549,843✔
239
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
549,843✔
240
  taosTelemetryDestroy(&pMgmt->addrMgt);
549,843✔
241
  (void)taosThreadMutexDestroy(&pMgmt->lock);
549,843✔
242
}
549,843✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc