rrdlabels.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. // Key OF HS ARRRAY
  5. struct {
  6. Pvoid_t JudyHS;
  7. SPINLOCK spinlock;
  8. } global_labels = {
  9. .JudyHS = (Pvoid_t) NULL,
  10. .spinlock = NETDATA_SPINLOCK_INITIALIZER
  11. };
  12. typedef struct label_registry_idx {
  13. STRING *key;
  14. STRING *value;
  15. } LABEL_REGISTRY_IDX;
  16. typedef struct labels_registry_entry {
  17. LABEL_REGISTRY_IDX index;
  18. } RRDLABEL;
  19. // Value of HS array
  20. typedef struct labels_registry_idx_entry {
  21. RRDLABEL label;
  22. size_t refcount;
  23. } RRDLABEL_IDX;
  24. typedef struct rrdlabels {
  25. SPINLOCK spinlock;
  26. size_t version;
  27. Pvoid_t JudyL;
  28. } RRDLABELS;
  29. #define lfe_start_nolock(label_list, label, ls) \
  30. do { \
  31. bool _first_then_next = true; \
  32. Pvoid_t *_PValue; \
  33. Word_t _Index = 0; \
  34. while ((_PValue = JudyLFirstThenNext((label_list)->JudyL, &_Index, &_first_then_next))) { \
  35. (ls) = *(RRDLABEL_SRC *)_PValue; \
  36. (void)(ls); \
  37. (label) = (void *)_Index;
  38. #define lfe_done_nolock() \
  39. } \
  40. } \
  41. while (0)
  42. #define lfe_start_read(label_list, label, ls) \
  43. do { \
  44. spinlock_lock(&(label_list)->spinlock); \
  45. bool _first_then_next = true; \
  46. Pvoid_t *_PValue; \
  47. Word_t _Index = 0; \
  48. while ((_PValue = JudyLFirstThenNext((label_list)->JudyL, &_Index, &_first_then_next))) { \
  49. (ls) = *(RRDLABEL_SRC *)_PValue; \
  50. (void)(ls); \
  51. (label) = (void *)_Index;
  52. #define lfe_done(label_list) \
  53. } \
  54. spinlock_unlock(&(label_list)->spinlock); \
  55. } \
  56. while (0)
  57. static inline void STATS_PLUS_MEMORY(struct dictionary_stats *stats, size_t key_size, size_t item_size, size_t value_size) {
  58. if(key_size)
  59. __atomic_fetch_add(&stats->memory.index, (long)JUDYHS_INDEX_SIZE_ESTIMATE(key_size), __ATOMIC_RELAXED);
  60. if(item_size)
  61. __atomic_fetch_add(&stats->memory.dict, (long)item_size, __ATOMIC_RELAXED);
  62. if(value_size)
  63. __atomic_fetch_add(&stats->memory.values, (long)value_size, __ATOMIC_RELAXED);
  64. }
  65. static inline void STATS_MINUS_MEMORY(struct dictionary_stats *stats, size_t key_size, size_t item_size, size_t value_size) {
  66. if(key_size)
  67. __atomic_fetch_sub(&stats->memory.index, (long)JUDYHS_INDEX_SIZE_ESTIMATE(key_size), __ATOMIC_RELAXED);
  68. if(item_size)
  69. __atomic_fetch_sub(&stats->memory.dict, (long)item_size, __ATOMIC_RELAXED);
  70. if(value_size)
  71. __atomic_fetch_sub(&stats->memory.values, (long)value_size, __ATOMIC_RELAXED);
  72. }
  73. // ----------------------------------------------------------------------------
  74. // labels sanitization
  75. /*
  76. * All labels follow these rules:
  77. *
  78. * Character Symbol Values Names
  79. * UTF-8 characters UTF-8 yes -> _
  80. * Lower case letter [a-z] yes yes
  81. * Upper case letter [A-Z] yes -> [a-z]
  82. * Digit [0-9] yes yes
  83. * Underscore _ yes yes
  84. * Minus - yes yes
  85. * Plus + yes -> _
  86. * Colon : yes -> _
  87. * Semicolon ; -> : -> _
  88. * Equal = -> : -> _
  89. * Period . yes yes
  90. * Comma , -> . -> .
  91. * Slash / yes yes
  92. * Backslash \ -> / -> /
  93. * At @ yes -> _
  94. * Space yes -> _
  95. * Opening parenthesis ( yes -> _
  96. * Closing parenthesis ) yes -> _
  97. * anything else -> _ -> _
  98. *
  99. * The above rules should allow users to set in tags (indicative):
  100. *
  101. * 1. hostnames and domain names as-is
  102. * 2. email addresses as-is
  103. * 3. floating point numbers, converted to always use a dot as the decimal point
  104. *
  105. * Leading and trailing spaces and control characters are removed from both label
  106. * names and values.
  107. *
  108. * Multiple spaces inside the label name or the value are removed (only 1 is retained).
  109. * In names spaces are also converted to underscores.
  110. *
  111. * Names that are only underscores are rejected (they do not enter the dictionary).
  112. *
  113. * The above rules do not require any conversion to be included in JSON strings.
  114. *
  115. * Label names and values are truncated to LABELS_MAX_LENGTH (200) characters.
  116. *
  117. * When parsing, label key and value are separated by the first colon (:) found.
  118. * So label:value1:value2 is parsed as key = "label", value = "value1:value2"
  119. *
  120. * This means a label key cannot contain a colon (:) - it is converted to
  121. * underscore if it does.
  122. *
  123. */
  124. #define RRDLABELS_MAX_NAME_LENGTH 200
  125. #define RRDLABELS_MAX_VALUE_LENGTH 800 // 800 in bytes, up to 200 UTF-8 characters
  126. static unsigned char label_spaces_char_map[256];
  127. static unsigned char label_names_char_map[256];
  128. static unsigned char label_values_char_map[256] = {
  129. [0] = '\0', //
  130. [1] = '_', //
  131. [2] = '_', //
  132. [3] = '_', //
  133. [4] = '_', //
  134. [5] = '_', //
  135. [6] = '_', //
  136. [7] = '_', //
  137. [8] = '_', //
  138. [9] = '_', //
  139. [10] = '_', //
  140. [11] = '_', //
  141. [12] = '_', //
  142. [13] = '_', //
  143. [14] = '_', //
  144. [15] = '_', //
  145. [16] = '_', //
  146. [17] = '_', //
  147. [18] = '_', //
  148. [19] = '_', //
  149. [20] = '_', //
  150. [21] = '_', //
  151. [22] = '_', //
  152. [23] = '_', //
  153. [24] = '_', //
  154. [25] = '_', //
  155. [26] = '_', //
  156. [27] = '_', //
  157. [28] = '_', //
  158. [29] = '_', //
  159. [30] = '_', //
  160. [31] = '_', //
  161. [32] = ' ', // SPACE keep
  162. [33] = '_', // !
  163. [34] = '_', // "
  164. [35] = '_', // #
  165. [36] = '_', // $
  166. [37] = '_', // %
  167. [38] = '_', // &
  168. [39] = '_', // '
  169. [40] = '(', // ( keep
  170. [41] = ')', // ) keep
  171. [42] = '_', // *
  172. [43] = '+', // + keep
  173. [44] = '.', // , convert , to .
  174. [45] = '-', // - keep
  175. [46] = '.', // . keep
  176. [47] = '/', // / keep
  177. [48] = '0', // 0 keep
  178. [49] = '1', // 1 keep
  179. [50] = '2', // 2 keep
  180. [51] = '3', // 3 keep
  181. [52] = '4', // 4 keep
  182. [53] = '5', // 5 keep
  183. [54] = '6', // 6 keep
  184. [55] = '7', // 7 keep
  185. [56] = '8', // 8 keep
  186. [57] = '9', // 9 keep
  187. [58] = ':', // : keep
  188. [59] = ':', // ; convert ; to :
  189. [60] = '_', // <
  190. [61] = ':', // = convert = to :
  191. [62] = '_', // >
  192. [63] = '_', // ?
  193. [64] = '@', // @
  194. [65] = 'A', // A keep
  195. [66] = 'B', // B keep
  196. [67] = 'C', // C keep
  197. [68] = 'D', // D keep
  198. [69] = 'E', // E keep
  199. [70] = 'F', // F keep
  200. [71] = 'G', // G keep
  201. [72] = 'H', // H keep
  202. [73] = 'I', // I keep
  203. [74] = 'J', // J keep
  204. [75] = 'K', // K keep
  205. [76] = 'L', // L keep
  206. [77] = 'M', // M keep
  207. [78] = 'N', // N keep
  208. [79] = 'O', // O keep
  209. [80] = 'P', // P keep
  210. [81] = 'Q', // Q keep
  211. [82] = 'R', // R keep
  212. [83] = 'S', // S keep
  213. [84] = 'T', // T keep
  214. [85] = 'U', // U keep
  215. [86] = 'V', // V keep
  216. [87] = 'W', // W keep
  217. [88] = 'X', // X keep
  218. [89] = 'Y', // Y keep
  219. [90] = 'Z', // Z keep
  220. [91] = '[', // [ keep
  221. [92] = '/', // backslash convert \ to /
  222. [93] = ']', // ] keep
  223. [94] = '_', // ^
  224. [95] = '_', // _ keep
  225. [96] = '_', // `
  226. [97] = 'a', // a keep
  227. [98] = 'b', // b keep
  228. [99] = 'c', // c keep
  229. [100] = 'd', // d keep
  230. [101] = 'e', // e keep
  231. [102] = 'f', // f keep
  232. [103] = 'g', // g keep
  233. [104] = 'h', // h keep
  234. [105] = 'i', // i keep
  235. [106] = 'j', // j keep
  236. [107] = 'k', // k keep
  237. [108] = 'l', // l keep
  238. [109] = 'm', // m keep
  239. [110] = 'n', // n keep
  240. [111] = 'o', // o keep
  241. [112] = 'p', // p keep
  242. [113] = 'q', // q keep
  243. [114] = 'r', // r keep
  244. [115] = 's', // s keep
  245. [116] = 't', // t keep
  246. [117] = 'u', // u keep
  247. [118] = 'v', // v keep
  248. [119] = 'w', // w keep
  249. [120] = 'x', // x keep
  250. [121] = 'y', // y keep
  251. [122] = 'z', // z keep
  252. [123] = '_', // {
  253. [124] = '_', // |
  254. [125] = '_', // }
  255. [126] = '_', // ~
  256. [127] = '_', //
  257. [128] = '_', //
  258. [129] = '_', //
  259. [130] = '_', //
  260. [131] = '_', //
  261. [132] = '_', //
  262. [133] = '_', //
  263. [134] = '_', //
  264. [135] = '_', //
  265. [136] = '_', //
  266. [137] = '_', //
  267. [138] = '_', //
  268. [139] = '_', //
  269. [140] = '_', //
  270. [141] = '_', //
  271. [142] = '_', //
  272. [143] = '_', //
  273. [144] = '_', //
  274. [145] = '_', //
  275. [146] = '_', //
  276. [147] = '_', //
  277. [148] = '_', //
  278. [149] = '_', //
  279. [150] = '_', //
  280. [151] = '_', //
  281. [152] = '_', //
  282. [153] = '_', //
  283. [154] = '_', //
  284. [155] = '_', //
  285. [156] = '_', //
  286. [157] = '_', //
  287. [158] = '_', //
  288. [159] = '_', //
  289. [160] = '_', //
  290. [161] = '_', //
  291. [162] = '_', //
  292. [163] = '_', //
  293. [164] = '_', //
  294. [165] = '_', //
  295. [166] = '_', //
  296. [167] = '_', //
  297. [168] = '_', //
  298. [169] = '_', //
  299. [170] = '_', //
  300. [171] = '_', //
  301. [172] = '_', //
  302. [173] = '_', //
  303. [174] = '_', //
  304. [175] = '_', //
  305. [176] = '_', //
  306. [177] = '_', //
  307. [178] = '_', //
  308. [179] = '_', //
  309. [180] = '_', //
  310. [181] = '_', //
  311. [182] = '_', //
  312. [183] = '_', //
  313. [184] = '_', //
  314. [185] = '_', //
  315. [186] = '_', //
  316. [187] = '_', //
  317. [188] = '_', //
  318. [189] = '_', //
  319. [190] = '_', //
  320. [191] = '_', //
  321. [192] = '_', //
  322. [193] = '_', //
  323. [194] = '_', //
  324. [195] = '_', //
  325. [196] = '_', //
  326. [197] = '_', //
  327. [198] = '_', //
  328. [199] = '_', //
  329. [200] = '_', //
  330. [201] = '_', //
  331. [202] = '_', //
  332. [203] = '_', //
  333. [204] = '_', //
  334. [205] = '_', //
  335. [206] = '_', //
  336. [207] = '_', //
  337. [208] = '_', //
  338. [209] = '_', //
  339. [210] = '_', //
  340. [211] = '_', //
  341. [212] = '_', //
  342. [213] = '_', //
  343. [214] = '_', //
  344. [215] = '_', //
  345. [216] = '_', //
  346. [217] = '_', //
  347. [218] = '_', //
  348. [219] = '_', //
  349. [220] = '_', //
  350. [221] = '_', //
  351. [222] = '_', //
  352. [223] = '_', //
  353. [224] = '_', //
  354. [225] = '_', //
  355. [226] = '_', //
  356. [227] = '_', //
  357. [228] = '_', //
  358. [229] = '_', //
  359. [230] = '_', //
  360. [231] = '_', //
  361. [232] = '_', //
  362. [233] = '_', //
  363. [234] = '_', //
  364. [235] = '_', //
  365. [236] = '_', //
  366. [237] = '_', //
  367. [238] = '_', //
  368. [239] = '_', //
  369. [240] = '_', //
  370. [241] = '_', //
  371. [242] = '_', //
  372. [243] = '_', //
  373. [244] = '_', //
  374. [245] = '_', //
  375. [246] = '_', //
  376. [247] = '_', //
  377. [248] = '_', //
  378. [249] = '_', //
  379. [250] = '_', //
  380. [251] = '_', //
  381. [252] = '_', //
  382. [253] = '_', //
  383. [254] = '_', //
  384. [255] = '_' //
  385. };
  386. __attribute__((constructor)) void initialize_labels_keys_char_map(void) {
  387. // copy the values char map to the names char map
  388. size_t i;
  389. for(i = 0; i < 256 ;i++)
  390. label_names_char_map[i] = label_values_char_map[i];
  391. // apply overrides to the label names map
  392. label_names_char_map['A'] = 'a';
  393. label_names_char_map['B'] = 'b';
  394. label_names_char_map['C'] = 'c';
  395. label_names_char_map['D'] = 'd';
  396. label_names_char_map['E'] = 'e';
  397. label_names_char_map['F'] = 'f';
  398. label_names_char_map['G'] = 'g';
  399. label_names_char_map['H'] = 'h';
  400. label_names_char_map['I'] = 'i';
  401. label_names_char_map['J'] = 'j';
  402. label_names_char_map['K'] = 'k';
  403. label_names_char_map['L'] = 'l';
  404. label_names_char_map['M'] = 'm';
  405. label_names_char_map['N'] = 'n';
  406. label_names_char_map['O'] = 'o';
  407. label_names_char_map['P'] = 'p';
  408. label_names_char_map['Q'] = 'q';
  409. label_names_char_map['R'] = 'r';
  410. label_names_char_map['S'] = 's';
  411. label_names_char_map['T'] = 't';
  412. label_names_char_map['U'] = 'u';
  413. label_names_char_map['V'] = 'v';
  414. label_names_char_map['W'] = 'w';
  415. label_names_char_map['X'] = 'x';
  416. label_names_char_map['Y'] = 'y';
  417. label_names_char_map['Z'] = 'z';
  418. label_names_char_map['='] = '_';
  419. label_names_char_map[':'] = '_';
  420. label_names_char_map['+'] = '_';
  421. label_names_char_map[';'] = '_';
  422. label_names_char_map['@'] = '_';
  423. label_names_char_map['('] = '_';
  424. label_names_char_map[')'] = '_';
  425. label_names_char_map[' '] = '_';
  426. label_names_char_map['\\'] = '/';
  427. // create the space map
  428. for(i = 0; i < 256 ;i++)
  429. label_spaces_char_map[i] = (isspace(i) || iscntrl(i) || !isprint(i))?1:0;
  430. }
  431. __attribute__((constructor)) void initialize_label_stats(void) {
  432. dictionary_stats_category_rrdlabels.memory.dict = 0;
  433. dictionary_stats_category_rrdlabels.memory.index = 0;
  434. dictionary_stats_category_rrdlabels.memory.values = 0;
  435. }
  436. size_t text_sanitize(unsigned char *dst, const unsigned char *src, size_t dst_size, const unsigned char *char_map, bool utf, const char *empty, size_t *multibyte_length) {
  437. if(unlikely(!src || !dst_size)) return 0;
  438. if(unlikely(!src || !*src)) {
  439. strncpyz((char *)dst, empty, dst_size);
  440. dst[dst_size - 1] = '\0';
  441. size_t len = strlen((char *)dst);
  442. if(multibyte_length) *multibyte_length = len;
  443. return len;
  444. }
  445. unsigned char *d = dst;
  446. // make room for the final string termination
  447. unsigned char *end = &d[dst_size - 1];
  448. // copy while converting, but keep only one space
  449. // we start wil last_is_space = 1 to skip leading spaces
  450. int last_is_space = 1;
  451. size_t mblen = 0;
  452. while(*src && d < end) {
  453. unsigned char c = *src;
  454. if(IS_UTF8_STARTBYTE(c) && IS_UTF8_BYTE(src[1]) && d + 2 < end) {
  455. // UTF-8 multi-byte encoded character
  456. // find how big this character is (2-4 bytes)
  457. size_t utf_character_size = 2;
  458. while(utf_character_size < 4 && src[utf_character_size] && IS_UTF8_BYTE(src[utf_character_size]) && !IS_UTF8_STARTBYTE(src[utf_character_size]))
  459. utf_character_size++;
  460. if(utf) {
  461. while(utf_character_size) {
  462. utf_character_size--;
  463. *d++ = *src++;
  464. }
  465. }
  466. else {
  467. // UTF-8 characters are not allowed.
  468. // Assume it is an underscore
  469. // and skip all except the first byte
  470. *d++ = '_';
  471. src += (utf_character_size - 1);
  472. }
  473. last_is_space = 0;
  474. mblen++;
  475. continue;
  476. }
  477. if(label_spaces_char_map[c]) {
  478. // a space character
  479. if(!last_is_space) {
  480. // add one space
  481. *d++ = char_map[c];
  482. mblen++;
  483. }
  484. last_is_space++;
  485. }
  486. else {
  487. *d++ = char_map[c];
  488. last_is_space = 0;
  489. mblen++;
  490. }
  491. src++;
  492. }
  493. // remove the last trailing space
  494. if(last_is_space && d > dst) {
  495. d--;
  496. mblen--;
  497. }
  498. // put a termination at the end of what we copied
  499. *d = '\0';
  500. // check if dst is all underscores and empty it if it is
  501. if(*dst == '_') {
  502. unsigned char *t = dst;
  503. while (*t == '_') t++;
  504. if (unlikely(*t == '\0')) {
  505. *dst = '\0';
  506. mblen = 0;
  507. }
  508. }
  509. if(unlikely(*dst == '\0')) {
  510. strncpyz((char *)dst, empty, dst_size);
  511. dst[dst_size - 1] = '\0';
  512. mblen = strlen((char *)dst);
  513. if(multibyte_length) *multibyte_length = mblen;
  514. return mblen;
  515. }
  516. if(multibyte_length) *multibyte_length = mblen;
  517. return d - dst;
  518. }
  519. static inline size_t rrdlabels_sanitize_name(char *dst, const char *src, size_t dst_size) {
  520. return text_sanitize((unsigned char *)dst, (const unsigned char *)src, dst_size, label_names_char_map, 0, "", NULL);
  521. }
  522. static inline size_t rrdlabels_sanitize_value(char *dst, const char *src, size_t dst_size) {
  523. return text_sanitize((unsigned char *)dst, (const unsigned char *)src, dst_size, label_values_char_map, 1, "[none]", NULL);
  524. }
  525. // ----------------------------------------------------------------------------
  526. // rrdlabels_create()
  527. RRDLABELS *rrdlabels_create(void)
  528. {
  529. RRDLABELS *labels = callocz(1, sizeof(*labels));
  530. STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, sizeof(RRDLABELS), 0);
  531. return labels;
  532. }
  533. static void dup_label(RRDLABEL *label_index)
  534. {
  535. if (!label_index)
  536. return;
  537. spinlock_lock(&global_labels.spinlock);
  538. Pvoid_t *PValue = JudyHSGet(global_labels.JudyHS, (void *)label_index, sizeof(*label_index));
  539. if (PValue && *PValue) {
  540. RRDLABEL_IDX *rrdlabel = *PValue;
  541. __atomic_add_fetch(&rrdlabel->refcount, 1, __ATOMIC_RELAXED);
  542. }
  543. spinlock_unlock(&global_labels.spinlock);
  544. }
  545. static RRDLABEL *add_label_name_value(const char *name, const char *value)
  546. {
  547. RRDLABEL_IDX *rrdlabel = NULL;
  548. LABEL_REGISTRY_IDX label_index;
  549. label_index.key = string_strdupz(name);
  550. label_index.value = string_strdupz(value);
  551. spinlock_lock(&global_labels.spinlock);
  552. Pvoid_t *PValue = JudyHSIns(&global_labels.JudyHS, (void *)&label_index, sizeof(label_index), PJE0);
  553. if(unlikely(!PValue || PValue == PJERR))
  554. fatal("RRDLABELS: corrupted judyHS array");
  555. if (*PValue) {
  556. rrdlabel = *PValue;
  557. string_freez(label_index.key);
  558. string_freez(label_index.value);
  559. } else {
  560. rrdlabel = callocz(1, sizeof(*rrdlabel));
  561. rrdlabel->label.index = label_index;
  562. *PValue = rrdlabel;
  563. STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, sizeof(LABEL_REGISTRY_IDX), sizeof(RRDLABEL_IDX), 0);
  564. }
  565. __atomic_add_fetch(&rrdlabel->refcount, 1, __ATOMIC_RELAXED);
  566. spinlock_unlock(&global_labels.spinlock);
  567. return &rrdlabel->label;
  568. }
  569. static void delete_label(RRDLABEL *label)
  570. {
  571. spinlock_lock(&global_labels.spinlock);
  572. Pvoid_t *PValue = JudyHSGet(global_labels.JudyHS, &label->index, sizeof(label->index));
  573. if (PValue && *PValue) {
  574. RRDLABEL_IDX *rrdlabel = *PValue;
  575. size_t refcount = __atomic_sub_fetch(&rrdlabel->refcount, 1, __ATOMIC_RELAXED);
  576. if (refcount == 0) {
  577. int ret = JudyHSDel(&global_labels.JudyHS, (void *)label, sizeof(*label), PJE0);
  578. if (unlikely(ret == JERR))
  579. STATS_MINUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, sizeof(*rrdlabel), 0);
  580. else
  581. STATS_MINUS_MEMORY(&dictionary_stats_category_rrdlabels, sizeof(LABEL_REGISTRY_IDX), sizeof(*rrdlabel), 0);
  582. string_freez(label->index.key);
  583. string_freez(label->index.value);
  584. freez(rrdlabel);
  585. }
  586. }
  587. spinlock_unlock(&global_labels.spinlock);
  588. }
  589. // ----------------------------------------------------------------------------
  590. // rrdlabels_destroy()
  591. void rrdlabels_destroy(RRDLABELS *labels)
  592. {
  593. if (unlikely(!labels))
  594. return;
  595. spinlock_lock(&labels->spinlock);
  596. Pvoid_t *PValue;
  597. Word_t Index = 0;
  598. bool first_then_next = true;
  599. while ((PValue = JudyLFirstThenNext(labels->JudyL, &Index, &first_then_next))) {
  600. delete_label((RRDLABEL *)Index);
  601. }
  602. size_t memory_freed = JudyLFreeArray(&labels->JudyL, PJE0);
  603. STATS_MINUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, memory_freed + sizeof(RRDLABELS), 0);
  604. spinlock_unlock(&labels->spinlock);
  605. freez(labels);
  606. }
  607. // Check in labels to see if we have the key specified in label
  608. static RRDLABEL *rrdlabels_find_label_with_key_unsafe(RRDLABELS *labels, RRDLABEL *label)
  609. {
  610. if (unlikely(!labels))
  611. return NULL;
  612. Pvoid_t *PValue;
  613. Word_t Index = 0;
  614. bool first_then_next = true;
  615. RRDLABEL *found = NULL;
  616. while ((PValue = JudyLFirstThenNext(labels->JudyL, &Index, &first_then_next))) {
  617. RRDLABEL *lb = (RRDLABEL *)Index;
  618. if (lb->index.key == label->index.key && lb != label) {
  619. found = (RRDLABEL *)Index;
  620. break;
  621. }
  622. }
  623. return found;
  624. }
  625. // ----------------------------------------------------------------------------
  626. // rrdlabels_add()
  627. static void labels_add_already_sanitized(RRDLABELS *labels, const char *key, const char *value, RRDLABEL_SRC ls)
  628. {
  629. RRDLABEL *new_label = add_label_name_value(key, value);
  630. spinlock_lock(&labels->spinlock);
  631. RRDLABEL_SRC new_ls = (ls & ~(RRDLABEL_FLAG_NEW | RRDLABEL_FLAG_OLD));
  632. size_t mem_before_judyl = JudyLMemUsed(labels->JudyL);
  633. Pvoid_t *PValue = JudyLIns(&labels->JudyL, (Word_t)new_label, PJE0);
  634. if (!PValue || PValue == PJERR)
  635. fatal("RRDLABELS: corrupted labels JudyL array");
  636. if(*PValue) {
  637. new_ls |= RRDLABEL_FLAG_OLD;
  638. *((RRDLABEL_SRC *)PValue) = new_ls;
  639. delete_label(new_label);
  640. }
  641. else {
  642. new_ls |= RRDLABEL_FLAG_NEW;
  643. *((RRDLABEL_SRC *)PValue) = new_ls;
  644. RRDLABEL *old_label_with_same_key = rrdlabels_find_label_with_key_unsafe(labels, new_label);
  645. if (old_label_with_same_key) {
  646. (void) JudyLDel(&labels->JudyL, (Word_t) old_label_with_same_key, PJE0);
  647. delete_label(old_label_with_same_key);
  648. }
  649. }
  650. labels->version++;
  651. size_t mem_after_judyl = JudyLMemUsed(labels->JudyL);
  652. STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_after_judyl - mem_before_judyl, 0);
  653. spinlock_unlock(&labels->spinlock);
  654. }
  655. void rrdlabels_add(RRDLABELS *labels, const char *name, const char *value, RRDLABEL_SRC ls)
  656. {
  657. if(!labels) {
  658. netdata_log_error("%s(): called with NULL dictionary.", __FUNCTION__ );
  659. return;
  660. }
  661. char n[RRDLABELS_MAX_NAME_LENGTH + 1], v[RRDLABELS_MAX_VALUE_LENGTH + 1];
  662. rrdlabels_sanitize_name(n, name, RRDLABELS_MAX_NAME_LENGTH);
  663. rrdlabels_sanitize_value(v, value, RRDLABELS_MAX_VALUE_LENGTH);
  664. if(!*n) {
  665. netdata_log_error("%s: cannot add name '%s' (value '%s') which is sanitized as empty string", __FUNCTION__, name, value);
  666. return;
  667. }
  668. labels_add_already_sanitized(labels, n, v, ls);
  669. }
  670. bool rrdlabels_exist(RRDLABELS *labels, const char *key)
  671. {
  672. if (!labels)
  673. return false;
  674. STRING *this_key = string_strdupz(key);
  675. RRDLABEL *lb;
  676. RRDLABEL_SRC ls;
  677. bool found = false;
  678. lfe_start_read(labels, lb, ls)
  679. {
  680. if (lb->index.key == this_key) {
  681. found = true;
  682. break;
  683. }
  684. }
  685. lfe_done(labels);
  686. string_freez(this_key);
  687. return found;
  688. }
  689. static const char *get_quoted_string_up_to(char *dst, size_t dst_size, const char *string, char upto1, char upto2) {
  690. size_t len = 0;
  691. char *d = dst, quote = 0;
  692. while(*string && len++ < dst_size) {
  693. if(unlikely(!quote && (*string == '\'' || *string == '"'))) {
  694. quote = *string++;
  695. continue;
  696. }
  697. if(unlikely(quote && *string == quote)) {
  698. quote = 0;
  699. string++;
  700. continue;
  701. }
  702. if(unlikely(quote && *string == '\\' && string[1])) {
  703. string++;
  704. *d++ = *string++;
  705. continue;
  706. }
  707. if(unlikely(!quote && (*string == upto1 || *string == upto2))) break;
  708. *d++ = *string++;
  709. }
  710. *d = '\0';
  711. if(*string) string++;
  712. return string;
  713. }
  714. void rrdlabels_add_pair(RRDLABELS *labels, const char *string, RRDLABEL_SRC ls)
  715. {
  716. if(!labels) {
  717. netdata_log_error("%s(): called with NULL dictionary.", __FUNCTION__ );
  718. return;
  719. }
  720. char name[RRDLABELS_MAX_NAME_LENGTH + 1];
  721. string = get_quoted_string_up_to(name, RRDLABELS_MAX_NAME_LENGTH, string, '=', ':');
  722. char value[RRDLABELS_MAX_VALUE_LENGTH + 1];
  723. get_quoted_string_up_to(value, RRDLABELS_MAX_VALUE_LENGTH, string, '\0', '\0');
  724. rrdlabels_add(labels, name, value, ls);
  725. }
  726. // ----------------------------------------------------------------------------
  727. void rrdlabels_value_to_buffer_array_item_or_null(RRDLABELS *labels, BUFFER *wb, const char *key) {
  728. if(!labels) return;
  729. STRING *this_key = string_strdupz(key);
  730. RRDLABEL *lb;
  731. RRDLABEL_SRC ls;
  732. lfe_start_read(labels, lb, ls)
  733. {
  734. if (lb->index.key == this_key) {
  735. if (lb->index.value)
  736. buffer_json_add_array_item_string(wb, string2str(lb->index.value));
  737. else
  738. buffer_json_add_array_item_string(wb, NULL);
  739. break;
  740. }
  741. }
  742. lfe_done(labels);
  743. string_freez(this_key);
  744. }
  745. // ----------------------------------------------------------------------------
  746. void rrdlabels_get_value_strcpyz(RRDLABELS *labels, char *dst, size_t dst_len, const char *key) {
  747. if(!labels) return;
  748. STRING *this_key = string_strdupz(key);
  749. RRDLABEL *lb;
  750. RRDLABEL_SRC ls;
  751. lfe_start_read(labels, lb, ls)
  752. {
  753. if (lb->index.key == this_key) {
  754. if (lb->index.value)
  755. strncpyz(dst, string2str(lb->index.value), dst_len);
  756. else
  757. dst[0] = '\0';
  758. break;
  759. }
  760. }
  761. lfe_done(labels);
  762. string_freez(this_key);
  763. }
  764. void rrdlabels_get_value_strdup_or_null(RRDLABELS *labels, char **value, const char *key)
  765. {
  766. if(!labels) return;
  767. STRING *this_key = string_strdupz(key);
  768. RRDLABEL *lb;
  769. RRDLABEL_SRC ls;
  770. lfe_start_read(labels, lb, ls)
  771. {
  772. if (lb->index.key == this_key) {
  773. *value = (lb->index.value) ? strdupz(string2str(lb->index.value)) : NULL;
  774. break;
  775. }
  776. }
  777. lfe_done(labels);
  778. string_freez(this_key);
  779. }
  780. void rrdlabels_get_value_to_buffer_or_unset(RRDLABELS *labels, BUFFER *wb, const char *key, const char *unset)
  781. {
  782. if(!labels || !key || !wb) return;
  783. STRING *this_key = string_strdupz(key);
  784. RRDLABEL *lb;
  785. RRDLABEL_SRC ls;
  786. lfe_start_read(labels, lb, ls)
  787. {
  788. if (lb->index.key == this_key) {
  789. if (lb->index.value)
  790. buffer_strcat(wb, string2str(lb->index.value));
  791. else
  792. buffer_strcat(wb, unset);
  793. break;
  794. }
  795. }
  796. lfe_done(labels);
  797. string_freez(this_key);
  798. }
  799. static void rrdlabels_unmark_all_unsafe(RRDLABELS *labels)
  800. {
  801. Pvoid_t *PValue;
  802. Word_t Index = 0;
  803. bool first_then_next = true;
  804. while ((PValue = JudyLFirstThenNext(labels->JudyL, &Index, &first_then_next)))
  805. *((RRDLABEL_SRC *)PValue) &= ~(RRDLABEL_FLAG_OLD | RRDLABEL_FLAG_NEW);
  806. }
  807. void rrdlabels_unmark_all(RRDLABELS *labels)
  808. {
  809. spinlock_lock(&labels->spinlock);
  810. rrdlabels_unmark_all_unsafe(labels);
  811. spinlock_unlock(&labels->spinlock);
  812. }
  813. static void rrdlabels_remove_all_unmarked_unsafe(RRDLABELS *labels)
  814. {
  815. Pvoid_t *PValue;
  816. Word_t Index = 0;
  817. bool first_then_next = true;
  818. while ((PValue = JudyLFirstThenNext(labels->JudyL, &Index, &first_then_next))) {
  819. if (!((*((RRDLABEL_SRC *)PValue)) & (RRDLABEL_FLAG_INTERNAL))) {
  820. size_t mem_before_judyl = JudyLMemUsed(labels->JudyL);
  821. (void)JudyLDel(&labels->JudyL, Index, PJE0);
  822. size_t mem_after_judyl = JudyLMemUsed(labels->JudyL);
  823. STATS_MINUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_before_judyl - mem_after_judyl, 0);
  824. delete_label((RRDLABEL *)Index);
  825. if (labels->JudyL != (Pvoid_t) NULL) {
  826. Index = 0;
  827. first_then_next = true;
  828. }
  829. }
  830. }
  831. }
  832. void rrdlabels_remove_all_unmarked(RRDLABELS *labels)
  833. {
  834. spinlock_lock(&labels->spinlock);
  835. rrdlabels_remove_all_unmarked_unsafe(labels);
  836. spinlock_unlock(&labels->spinlock);
  837. }
  838. // ----------------------------------------------------------------------------
  839. // rrdlabels_walkthrough_read()
  840. int rrdlabels_walkthrough_read(RRDLABELS *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data)
  841. {
  842. int ret = 0;
  843. if(unlikely(!labels || !callback)) return 0;
  844. RRDLABEL *lb;
  845. RRDLABEL_SRC ls;
  846. lfe_start_read(labels, lb, ls)
  847. {
  848. ret = callback(string2str(lb->index.key), string2str(lb->index.value), ls, data);
  849. if (ret < 0)
  850. break;
  851. }
  852. lfe_done(labels);
  853. return ret;
  854. }
  855. // ----------------------------------------------------------------------------
  856. // rrdlabels_migrate_to_these()
  857. // migrate an existing label list to a new list
  858. void rrdlabels_migrate_to_these(RRDLABELS *dst, RRDLABELS *src) {
  859. if (!dst || !src || (dst == src))
  860. return;
  861. spinlock_lock(&dst->spinlock);
  862. spinlock_lock(&src->spinlock);
  863. rrdlabels_unmark_all_unsafe(dst);
  864. RRDLABEL *label;
  865. Pvoid_t *PValue;
  866. RRDLABEL_SRC ls;
  867. lfe_start_nolock(src, label, ls)
  868. {
  869. size_t mem_before_judyl = JudyLMemUsed(dst->JudyL);
  870. PValue = JudyLIns(&dst->JudyL, (Word_t)label, PJE0);
  871. if(unlikely(!PValue || PValue == PJERR))
  872. fatal("RRDLABELS migrate: corrupted labels array");
  873. RRDLABEL_SRC flag = RRDLABEL_FLAG_NEW;
  874. if (!*PValue) {
  875. dup_label(label);
  876. size_t mem_after_judyl = JudyLMemUsed(dst->JudyL);
  877. STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_after_judyl - mem_before_judyl, 0);
  878. }
  879. else
  880. flag = RRDLABEL_FLAG_OLD;
  881. *((RRDLABEL_SRC *)PValue) |= flag;
  882. }
  883. lfe_done_nolock();
  884. rrdlabels_remove_all_unmarked_unsafe(dst);
  885. dst->version = src->version;
  886. spinlock_unlock(&src->spinlock);
  887. spinlock_unlock(&dst->spinlock);
  888. }
  889. void rrdlabels_copy(RRDLABELS *dst, RRDLABELS *src)
  890. {
  891. if (!dst || !src || (dst == src))
  892. return;
  893. RRDLABEL *label;
  894. RRDLABEL_SRC ls;
  895. spinlock_lock(&dst->spinlock);
  896. spinlock_lock(&src->spinlock);
  897. size_t mem_before_judyl = JudyLMemUsed(dst->JudyL);
  898. bool update_statistics = false;
  899. lfe_start_nolock(src, label, ls)
  900. {
  901. RRDLABEL *old_label_with_key = rrdlabels_find_label_with_key_unsafe(dst, label);
  902. Pvoid_t *PValue = JudyLIns(&dst->JudyL, (Word_t)label, PJE0);
  903. if(unlikely(!PValue || PValue == PJERR))
  904. fatal("RRDLABELS: corrupted labels array");
  905. if (!*PValue) {
  906. dup_label(label);
  907. ls = (ls & ~(RRDLABEL_FLAG_OLD)) | RRDLABEL_FLAG_NEW;
  908. dst->version++;
  909. update_statistics = true;
  910. if (old_label_with_key) {
  911. (void)JudyLDel(&dst->JudyL, (Word_t)old_label_with_key, PJE0);
  912. delete_label((RRDLABEL *)old_label_with_key);
  913. }
  914. }
  915. else
  916. ls = (ls & ~(RRDLABEL_FLAG_NEW)) | RRDLABEL_FLAG_OLD;
  917. *((RRDLABEL_SRC *)PValue) = ls;
  918. }
  919. lfe_done_nolock();
  920. if (update_statistics) {
  921. size_t mem_after_judyl = JudyLMemUsed(dst->JudyL);
  922. STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_after_judyl - mem_before_judyl, 0);
  923. }
  924. spinlock_unlock(&src->spinlock);
  925. spinlock_unlock(&dst->spinlock);
  926. }
  927. // ----------------------------------------------------------------------------
  928. // rrdlabels_match_simple_pattern()
  929. // returns true when there are keys in the dictionary matching a simple pattern
  930. struct simple_pattern_match_name_value {
  931. size_t searches;
  932. SIMPLE_PATTERN *pattern;
  933. char equal;
  934. };
  935. static int simple_pattern_match_name_only_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
  936. struct simple_pattern_match_name_value *t = (struct simple_pattern_match_name_value *)data;
  937. (void)value;
  938. // we return -1 to stop the walkthrough on first match
  939. t->searches++;
  940. if(simple_pattern_matches(t->pattern, name)) return -1;
  941. return 0;
  942. }
  943. static int simple_pattern_match_name_and_value_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
  944. struct simple_pattern_match_name_value *t = (struct simple_pattern_match_name_value *)data;
  945. // we return -1 to stop the walkthrough on first match
  946. t->searches++;
  947. if(simple_pattern_matches(t->pattern, name)) return -1;
  948. size_t len = RRDLABELS_MAX_NAME_LENGTH + RRDLABELS_MAX_VALUE_LENGTH + 2; // +1 for =, +1 for \0
  949. char tmp[len], *dst = &tmp[0];
  950. const char *v = value;
  951. // copy the name
  952. while(*name) *dst++ = *name++;
  953. // add the equal
  954. *dst++ = t->equal;
  955. // add the value
  956. while(*v) *dst++ = *v++;
  957. // terminate it
  958. *dst = '\0';
  959. t->searches++;
  960. if(simple_pattern_matches_length_extract(t->pattern, tmp, dst - tmp, NULL, 0) == SP_MATCHED_POSITIVE)
  961. return -1;
  962. return 0;
  963. }
  964. bool rrdlabels_match_simple_pattern_parsed(RRDLABELS *labels, SIMPLE_PATTERN *pattern, char equal, size_t *searches) {
  965. if (!labels) return false;
  966. struct simple_pattern_match_name_value t = {
  967. .searches = 0,
  968. .pattern = pattern,
  969. .equal = equal
  970. };
  971. int ret = rrdlabels_walkthrough_read(labels, equal?simple_pattern_match_name_and_value_callback:simple_pattern_match_name_only_callback, &t);
  972. if(searches)
  973. *searches = t.searches;
  974. return (ret == -1)?true:false;
  975. }
  976. bool rrdlabels_match_simple_pattern(RRDLABELS *labels, const char *simple_pattern_txt) {
  977. if (!labels) return false;
  978. SIMPLE_PATTERN *pattern = simple_pattern_create(simple_pattern_txt, " ,|\t\r\n\f\v", SIMPLE_PATTERN_EXACT, true);
  979. char equal = '\0';
  980. const char *s;
  981. for(s = simple_pattern_txt; *s ; s++) {
  982. if (*s == '=' || *s == ':') {
  983. equal = *s;
  984. break;
  985. }
  986. }
  987. bool ret = rrdlabels_match_simple_pattern_parsed(labels, pattern, equal, NULL);
  988. simple_pattern_free(pattern);
  989. return ret;
  990. }
  991. // ----------------------------------------------------------------------------
  992. // Log all labels
  993. static int rrdlabels_log_label_to_buffer_callback(const char *name, const char *value, void *data) {
  994. BUFFER *wb = (BUFFER *)data;
  995. buffer_sprintf(wb, "Label: %s: \"%s\" (", name, value);
  996. buffer_strcat(wb, "unknown");
  997. buffer_strcat(wb, ")\n");
  998. return 1;
  999. }
  1000. void rrdlabels_log_to_buffer(RRDLABELS *labels, BUFFER *wb)
  1001. {
  1002. RRDLABEL *lb;
  1003. RRDLABEL_SRC ls;
  1004. lfe_start_read(labels, lb, ls)
  1005. rrdlabels_log_label_to_buffer_callback((void *) string2str(lb->index.key), (void *) string2str(lb->index.value), wb);
  1006. lfe_done(labels);
  1007. }
  1008. // ----------------------------------------------------------------------------
  1009. // rrdlabels_to_buffer()
  1010. struct labels_to_buffer {
  1011. BUFFER *wb;
  1012. bool (*filter_callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data);
  1013. void *filter_data;
  1014. void (*name_sanitizer)(char *dst, const char *src, size_t dst_size);
  1015. void (*value_sanitizer)(char *dst, const char *src, size_t dst_size);
  1016. const char *before_each;
  1017. const char *quote;
  1018. const char *equal;
  1019. const char *between_them;
  1020. size_t count;
  1021. };
  1022. static int label_to_buffer_callback(const RRDLABEL *lb, void *value __maybe_unused, RRDLABEL_SRC ls, void *data)
  1023. {
  1024. struct labels_to_buffer *t = (struct labels_to_buffer *)data;
  1025. size_t n_size = (t->name_sanitizer ) ? ( RRDLABELS_MAX_NAME_LENGTH * 2 ) : 1;
  1026. size_t v_size = (t->value_sanitizer) ? ( RRDLABELS_MAX_VALUE_LENGTH * 2 ) : 1;
  1027. char n[n_size];
  1028. char v[v_size];
  1029. const char *name = string2str(lb->index.key);
  1030. const char *nn = name, *vv = string2str(lb->index.value);
  1031. if(t->name_sanitizer) {
  1032. t->name_sanitizer(n, name, n_size);
  1033. nn = n;
  1034. }
  1035. if(t->value_sanitizer) {
  1036. t->value_sanitizer(v, string2str(lb->index.value), v_size);
  1037. vv = v;
  1038. }
  1039. if(!t->filter_callback || t->filter_callback(name, string2str(lb->index.value), ls, t->filter_data)) {
  1040. buffer_sprintf(t->wb, "%s%s%s%s%s%s%s%s%s", t->count++?t->between_them:"", t->before_each, t->quote, nn, t->quote, t->equal, t->quote, vv, t->quote);
  1041. return 1;
  1042. }
  1043. return 0;
  1044. }
  1045. int label_walkthrough_read(RRDLABELS *labels, int (*callback)(const RRDLABEL *item, void *entry, RRDLABEL_SRC ls, void *data), void *data)
  1046. {
  1047. int ret = 0;
  1048. if(unlikely(!labels || !callback)) return 0;
  1049. RRDLABEL *lb;
  1050. RRDLABEL_SRC ls;
  1051. lfe_start_read(labels, lb, ls)
  1052. {
  1053. ret = callback((const RRDLABEL *)lb, (void *)string2str(lb->index.value), ls, data);
  1054. if (ret < 0)
  1055. break;
  1056. }
  1057. lfe_done(labels);
  1058. return ret;
  1059. }
  1060. int rrdlabels_to_buffer(RRDLABELS *labels, BUFFER *wb, const char *before_each, const char *equal, const char *quote, const char *between_them, bool (*filter_callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *filter_data, void (*name_sanitizer)(char *dst, const char *src, size_t dst_size), void (*value_sanitizer)(char *dst, const char *src, size_t dst_size)) {
  1061. struct labels_to_buffer tmp = {
  1062. .wb = wb,
  1063. .filter_callback = filter_callback,
  1064. .filter_data = filter_data,
  1065. .name_sanitizer = name_sanitizer,
  1066. .value_sanitizer = value_sanitizer,
  1067. .before_each = before_each,
  1068. .equal = equal,
  1069. .quote = quote,
  1070. .between_them = between_them,
  1071. .count = 0
  1072. };
  1073. return label_walkthrough_read(labels, label_to_buffer_callback, (void *)&tmp);
  1074. }
  1075. void rrdlabels_to_buffer_json_members(RRDLABELS *labels, BUFFER *wb)
  1076. {
  1077. RRDLABEL *lb;
  1078. RRDLABEL_SRC ls;
  1079. lfe_start_read(labels, lb, ls)
  1080. buffer_json_member_add_string(wb, string2str(lb->index.key), string2str(lb->index.value));
  1081. lfe_done(labels);
  1082. }
  1083. size_t rrdlabels_entries(RRDLABELS *labels __maybe_unused)
  1084. {
  1085. if (unlikely(!labels))
  1086. return 0;
  1087. size_t count;
  1088. spinlock_lock(&labels->spinlock);
  1089. count = JudyLCount(labels->JudyL, 0, -1, PJE0);
  1090. spinlock_unlock(&labels->spinlock);
  1091. return count;
  1092. }
  1093. size_t rrdlabels_version(RRDLABELS *labels __maybe_unused)
  1094. {
  1095. if (unlikely(!labels))
  1096. return 0;
  1097. return (size_t) labels->version;
  1098. }
  1099. void rrdset_update_rrdlabels(RRDSET *st, RRDLABELS *new_rrdlabels) {
  1100. if(!st->rrdlabels)
  1101. st->rrdlabels = rrdlabels_create();
  1102. if (new_rrdlabels)
  1103. rrdlabels_migrate_to_these(st->rrdlabels, new_rrdlabels);
  1104. rrdset_flag_set(st, RRDSET_FLAG_METADATA_UPDATE);
  1105. rrdhost_flag_set(st->rrdhost, RRDHOST_FLAG_METADATA_UPDATE);
  1106. rrdset_metadata_updated(st);
  1107. }
  1108. // ----------------------------------------------------------------------------
  1109. // rrdlabels unit test
  1110. struct rrdlabels_unittest_add_a_pair {
  1111. const char *pair;
  1112. const char *expected_name;
  1113. const char *expected_value;
  1114. const char *name;
  1115. const char *value;
  1116. int errors;
  1117. };
  1118. RRDLABEL *rrdlabels_find_label_with_key(RRDLABELS *labels, const char *key, RRDLABEL_SRC *source)
  1119. {
  1120. if (!labels || !key)
  1121. return NULL;
  1122. STRING *this_key = string_strdupz(key);
  1123. RRDLABEL *lb = NULL;
  1124. RRDLABEL_SRC ls;
  1125. lfe_start_read(labels, lb, ls)
  1126. {
  1127. if (lb->index.key == this_key) {
  1128. if (source)
  1129. *source = ls;
  1130. break;
  1131. }
  1132. }
  1133. lfe_done(labels);
  1134. string_freez(this_key);
  1135. return lb;
  1136. }
  1137. static int rrdlabels_unittest_add_a_pair_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
  1138. struct rrdlabels_unittest_add_a_pair *t = (struct rrdlabels_unittest_add_a_pair *)data;
  1139. t->name = name;
  1140. t->value = value;
  1141. if(strcmp(name, t->expected_name) != 0) {
  1142. fprintf(stderr, "name is wrong, found \"%s\", expected \"%s\"", name, t->expected_name);
  1143. t->errors++;
  1144. }
  1145. if(value == NULL && t->expected_value == NULL) {
  1146. ;
  1147. }
  1148. else if(value == NULL || t->expected_value == NULL) {
  1149. fprintf(stderr, "value is wrong, found \"%s\", expected \"%s\"", value?value:"(null)", t->expected_value?t->expected_value:"(null)");
  1150. t->errors++;
  1151. }
  1152. else if(strcmp(value, t->expected_value) != 0) {
  1153. fprintf(stderr, "values don't match, found \"%s\", expected \"%s\"", value, t->expected_value);
  1154. t->errors++;
  1155. }
  1156. return 1;
  1157. }
  1158. static int rrdlabels_unittest_add_a_pair(const char *pair, const char *name, const char *value) {
  1159. RRDLABELS *labels = rrdlabels_create();
  1160. int errors;
  1161. fprintf(stderr, "rrdlabels_add_pair(labels, %s) ... ", pair);
  1162. rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
  1163. struct rrdlabels_unittest_add_a_pair tmp = {
  1164. .pair = pair,
  1165. .expected_name = name,
  1166. .expected_value = value,
  1167. .errors = 0
  1168. };
  1169. int ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
  1170. errors = tmp.errors;
  1171. if(ret != 1) {
  1172. fprintf(stderr, "failed to get \"%s\" label", name);
  1173. errors++;
  1174. }
  1175. if(!errors)
  1176. fprintf(stderr, " OK, name='%s' and value='%s'\n", tmp.name, tmp.value?tmp.value:"(null)");
  1177. else
  1178. fprintf(stderr, " FAILED\n");
  1179. rrdlabels_destroy(labels);
  1180. return errors;
  1181. }
  1182. static int rrdlabels_unittest_add_pairs() {
  1183. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  1184. int errors = 0;
  1185. // basic test
  1186. errors += rrdlabels_unittest_add_a_pair("tag=value", "tag", "value");
  1187. errors += rrdlabels_unittest_add_a_pair("tag:value", "tag", "value");
  1188. // test newlines
  1189. errors += rrdlabels_unittest_add_a_pair(" tag = \t value \r\n", "tag", "value");
  1190. // test : in values
  1191. errors += rrdlabels_unittest_add_a_pair("tag=:value", "tag", ":value");
  1192. errors += rrdlabels_unittest_add_a_pair("tag::value", "tag", ":value");
  1193. errors += rrdlabels_unittest_add_a_pair(" tag = :value ", "tag", ":value");
  1194. errors += rrdlabels_unittest_add_a_pair(" tag : :value ", "tag", ":value");
  1195. errors += rrdlabels_unittest_add_a_pair("tag:5", "tag", "5");
  1196. errors += rrdlabels_unittest_add_a_pair("tag:55", "tag", "55");
  1197. errors += rrdlabels_unittest_add_a_pair("tag:aa", "tag", "aa");
  1198. errors += rrdlabels_unittest_add_a_pair("tag:a", "tag", "a");
  1199. // test empty values
  1200. errors += rrdlabels_unittest_add_a_pair("tag", "tag", "[none]");
  1201. errors += rrdlabels_unittest_add_a_pair("tag:", "tag", "[none]");
  1202. errors += rrdlabels_unittest_add_a_pair("tag:\"\"", "tag", "[none]");
  1203. errors += rrdlabels_unittest_add_a_pair("tag:''", "tag", "[none]");
  1204. errors += rrdlabels_unittest_add_a_pair("tag:\r\n", "tag", "[none]");
  1205. errors += rrdlabels_unittest_add_a_pair("tag\r\n", "tag", "[none]");
  1206. // test UTF-8 in values
  1207. errors += rrdlabels_unittest_add_a_pair("tag: country:Ελλάδα", "tag", "country:Ελλάδα");
  1208. errors += rrdlabels_unittest_add_a_pair("\"tag\": \"country:Ελλάδα\"", "tag", "country:Ελλάδα");
  1209. errors += rrdlabels_unittest_add_a_pair("\"tag\": country:\"Ελλάδα\"", "tag", "country:Ελλάδα");
  1210. errors += rrdlabels_unittest_add_a_pair("\"tag=1\": country:\"Gre\\\"ece\"", "tag_1", "country:Gre_ece");
  1211. errors += rrdlabels_unittest_add_a_pair("\"tag=1\" = country:\"Gre\\\"ece\"", "tag_1", "country:Gre_ece");
  1212. errors += rrdlabels_unittest_add_a_pair("\t'LABE=L'\t=\t\"World\" peace", "labe_l", "World peace");
  1213. errors += rrdlabels_unittest_add_a_pair("\t'LA\\'B:EL'\t=\tcountry:\"World\":\"Europe\":\"Greece\"", "la_b_el", "country:World:Europe:Greece");
  1214. errors += rrdlabels_unittest_add_a_pair("\t'LA\\'B:EL'\t=\tcountry\\\"World\"\\\"Europe\"\\\"Greece\"", "la_b_el", "country/World/Europe/Greece");
  1215. errors += rrdlabels_unittest_add_a_pair("NAME=\"VALUE\"", "name", "VALUE");
  1216. errors += rrdlabels_unittest_add_a_pair("\"NAME\" : \"VALUE\"", "name", "VALUE");
  1217. errors += rrdlabels_unittest_add_a_pair("NAME: \"VALUE\"", "name", "VALUE");
  1218. return errors;
  1219. }
  1220. static int rrdlabels_unittest_expect_value(RRDLABELS *labels, const char *key, const char *value, RRDLABEL_SRC required_source)
  1221. {
  1222. RRDLABEL_SRC source;
  1223. RRDLABEL *label = rrdlabels_find_label_with_key(labels, key, &source);
  1224. return (!label || strcmp(string2str(label->index.value), value) != 0 || !(source & required_source));
  1225. }
  1226. static int rrdlabels_unittest_double_check()
  1227. {
  1228. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  1229. int ret = 0;
  1230. RRDLABELS *labels = rrdlabels_create();
  1231. rrdlabels_add(labels, "key1", "value1", RRDLABEL_SRC_CONFIG);
  1232. ret += rrdlabels_unittest_expect_value(labels, "key1", "value1", RRDLABEL_FLAG_NEW);
  1233. rrdlabels_add(labels, "key1", "value2", RRDLABEL_SRC_K8S);
  1234. ret += !rrdlabels_unittest_expect_value(labels, "key1", "value2", RRDLABEL_FLAG_OLD);
  1235. rrdlabels_add(labels, "key1", "value3", RRDLABEL_SRC_ACLK);
  1236. ret += !rrdlabels_unittest_expect_value(labels, "key1", "value3", RRDLABEL_FLAG_OLD);
  1237. ret += (rrdlabels_entries(labels) != 1);
  1238. rrdlabels_destroy(labels);
  1239. if (ret)
  1240. fprintf(stderr, "\n%s() tests failed\n", __FUNCTION__);
  1241. return ret;
  1242. }
  1243. static int rrdlabels_walkthrough_index_read(RRDLABELS *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, size_t index, void *data), void *data)
  1244. {
  1245. int ret = 0;
  1246. if(unlikely(!labels || !callback)) return 0;
  1247. RRDLABEL *lb;
  1248. RRDLABEL_SRC ls;
  1249. size_t index = 0;
  1250. lfe_start_read(labels, lb, ls)
  1251. {
  1252. ret = callback(string2str(lb->index.key), string2str(lb->index.value), ls, index, data);
  1253. if (ret < 0)
  1254. break;
  1255. index++;
  1256. }
  1257. lfe_done(labels);
  1258. return ret;
  1259. }
  1260. static int unittest_dump_labels(const char *name, const char *value, RRDLABEL_SRC ls, size_t index, void *data __maybe_unused)
  1261. {
  1262. if (!index && data) {
  1263. fprintf(stderr, "%s\n", (char *) data);
  1264. }
  1265. fprintf(stderr, "LABEL \"%s\" = %d \"%s\"\n", name, ls & (~RRDLABEL_FLAG_INTERNAL), value);
  1266. return 1;
  1267. }
  1268. static int rrdlabels_unittest_migrate_check()
  1269. {
  1270. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  1271. RRDLABELS *labels1 = NULL;
  1272. RRDLABELS *labels2 = NULL;
  1273. labels1 = rrdlabels_create();
  1274. labels2 = rrdlabels_create();
  1275. rrdlabels_add(labels1, "key1", "value1", RRDLABEL_SRC_CONFIG);
  1276. rrdlabels_add(labels1, "key1", "value2", RRDLABEL_SRC_CONFIG);
  1277. rrdlabels_add(labels2, "new_key1", "value2", RRDLABEL_SRC_CONFIG);
  1278. rrdlabels_add(labels2, "new_key2", "value2", RRDLABEL_SRC_CONFIG);
  1279. rrdlabels_add(labels2, "key1", "value2", RRDLABEL_SRC_CONFIG);
  1280. fprintf(stderr, "Labels1 entries found %zu (should be 1)\n", rrdlabels_entries(labels1));
  1281. fprintf(stderr, "Labels2 entries found %zu (should be 3)\n", rrdlabels_entries(labels2));
  1282. rrdlabels_migrate_to_these(labels1, labels2);
  1283. fprintf(stderr, "labels1 (migrated) entries found %zu (should be 3)\n", rrdlabels_entries(labels1));
  1284. size_t entries = rrdlabels_entries(labels1);
  1285. rrdlabels_destroy(labels1);
  1286. rrdlabels_destroy(labels2);
  1287. if (entries != 3)
  1288. return 1;
  1289. // Copy test
  1290. labels1 = rrdlabels_create();
  1291. labels2 = rrdlabels_create();
  1292. rrdlabels_add(labels1, "key1", "value1", RRDLABEL_SRC_CONFIG);
  1293. rrdlabels_add(labels1, "key2", "value2", RRDLABEL_SRC_CONFIG);
  1294. rrdlabels_add(labels1, "key3", "value3", RRDLABEL_SRC_CONFIG);
  1295. rrdlabels_add(labels1, "key4", "value4", RRDLABEL_SRC_CONFIG); // 4 keys
  1296. rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nlabels1");
  1297. rrdlabels_add(labels2, "key0", "value0", RRDLABEL_SRC_CONFIG);
  1298. rrdlabels_add(labels2, "key1", "value1", RRDLABEL_SRC_CONFIG);
  1299. rrdlabels_add(labels2, "key2", "value2", RRDLABEL_SRC_CONFIG);
  1300. int rc = 0;
  1301. rc = rrdlabels_unittest_expect_value(labels1, "key1", "value1", RRDLABEL_FLAG_NEW);
  1302. if (rc)
  1303. return rc;
  1304. rrdlabels_walkthrough_index_read(labels2, unittest_dump_labels, "\nlabels2");
  1305. rrdlabels_copy(labels1, labels2); // labels1 should have 5 keys
  1306. rc = rrdlabels_unittest_expect_value(labels1, "key1", "value1", RRDLABEL_FLAG_OLD);
  1307. if (rc)
  1308. return rc;
  1309. rc = rrdlabels_unittest_expect_value(labels1, "key0", "value0", RRDLABEL_FLAG_NEW);
  1310. if (rc)
  1311. return rc;
  1312. rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nlabels1 after copy from labels2");
  1313. entries = rrdlabels_entries(labels1);
  1314. fprintf(stderr, "labels1 (copied) entries found %zu (should be 5)\n", rrdlabels_entries(labels1));
  1315. if (entries != 5)
  1316. return 1;
  1317. rrdlabels_add(labels1, "key0", "value0", RRDLABEL_SRC_CONFIG);
  1318. rc = rrdlabels_unittest_expect_value(labels1, "key0", "value0", RRDLABEL_FLAG_OLD);
  1319. rrdlabels_destroy(labels1);
  1320. rrdlabels_destroy(labels2);
  1321. return rc;
  1322. }
  1323. static int rrdlabels_unittest_check_simple_pattern(RRDLABELS *labels, const char *pattern, bool expected) {
  1324. fprintf(stderr, "rrdlabels_match_simple_pattern(labels, \"%s\") ... ", pattern);
  1325. bool ret = rrdlabels_match_simple_pattern(labels, pattern);
  1326. fprintf(stderr, "%s, got %s expected %s\n", (ret == expected)?"OK":"FAILED", ret?"true":"false", expected?"true":"false");
  1327. return (ret == expected)?0:1;
  1328. }
  1329. static int rrdlabels_unittest_simple_pattern() {
  1330. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  1331. int errors = 0;
  1332. RRDLABELS *labels = rrdlabels_create();
  1333. rrdlabels_add(labels, "tag1", "value1", RRDLABEL_SRC_CONFIG);
  1334. rrdlabels_add(labels, "tag2", "value2", RRDLABEL_SRC_CONFIG);
  1335. rrdlabels_add(labels, "tag3", "value3", RRDLABEL_SRC_CONFIG);
  1336. errors += rrdlabels_unittest_check_simple_pattern(labels, "*", true);
  1337. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag", false);
  1338. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag*", true);
  1339. errors += rrdlabels_unittest_check_simple_pattern(labels, "*1", true);
  1340. errors += rrdlabels_unittest_check_simple_pattern(labels, "value*", false);
  1341. errors += rrdlabels_unittest_check_simple_pattern(labels, "*=value*", true);
  1342. errors += rrdlabels_unittest_check_simple_pattern(labels, "*:value*", true);
  1343. errors += rrdlabels_unittest_check_simple_pattern(labels, "*2", true);
  1344. errors += rrdlabels_unittest_check_simple_pattern(labels, "*2 *3", true);
  1345. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag3 *2", true);
  1346. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1 tag2", true);
  1347. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1tag2", false);
  1348. errors += rrdlabels_unittest_check_simple_pattern(labels, "invalid1 invalid2 tag3", true);
  1349. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag1 tag4", false);
  1350. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1=value1", true);
  1351. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1=value2", false);
  1352. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag*=value*", true);
  1353. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag*=value*", false);
  1354. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag2=something2 tag2=*2", true);
  1355. rrdlabels_destroy(labels);
  1356. return errors;
  1357. }
  1358. int rrdlabels_unittest_sanitize_value(const char *src, const char *expected) {
  1359. char buf[RRDLABELS_MAX_VALUE_LENGTH + 1];
  1360. size_t len = rrdlabels_sanitize_value(buf, src, RRDLABELS_MAX_VALUE_LENGTH);
  1361. size_t expected_len = strlen(expected);
  1362. int err = 0;
  1363. if(strcmp(buf, expected) != 0) err = 1;
  1364. if(len != expected_len) err = 1;
  1365. fprintf(stderr, "%s(%s): %s, expected '%s', got '%s', expected bytes = %zu, got bytes = %zu\n", __FUNCTION__, src, (err==1)?"FAILED":"OK", expected, buf, expected_len, strlen(buf));
  1366. return err;
  1367. }
  1368. int rrdlabels_unittest_sanitization() {
  1369. int errors = 0;
  1370. errors += rrdlabels_unittest_sanitize_value("", "[none]");
  1371. errors += rrdlabels_unittest_sanitize_value("1", "1");
  1372. errors += rrdlabels_unittest_sanitize_value(" hello world ", "hello world");
  1373. errors += rrdlabels_unittest_sanitize_value("[none]", "[none]");
  1374. // 2-byte UTF-8
  1375. errors += rrdlabels_unittest_sanitize_value(" Ελλάδα ", "Ελλάδα");
  1376. errors += rrdlabels_unittest_sanitize_value("aŰbŲcŴ", "aŰbŲcŴ");
  1377. errors += rrdlabels_unittest_sanitize_value("Ű b Ų c Ŵ", "Ű b Ų c Ŵ");
  1378. // 3-byte UTF-8
  1379. errors += rrdlabels_unittest_sanitize_value("‱", "‱");
  1380. errors += rrdlabels_unittest_sanitize_value("a‱b", "a‱b");
  1381. errors += rrdlabels_unittest_sanitize_value("a ‱ b", "a ‱ b");
  1382. // 4-byte UTF-8
  1383. errors += rrdlabels_unittest_sanitize_value("𩸽", "𩸽");
  1384. errors += rrdlabels_unittest_sanitize_value("a𩸽b", "a𩸽b");
  1385. errors += rrdlabels_unittest_sanitize_value("a 𩸽 b", "a 𩸽 b");
  1386. // mixed multi-byte
  1387. errors += rrdlabels_unittest_sanitize_value("Ű‱𩸽‱Ű", "Ű‱𩸽‱Ű");
  1388. return errors;
  1389. }
  1390. int rrdlabels_unittest(void) {
  1391. int errors = 0;
  1392. errors += rrdlabels_unittest_sanitization();
  1393. errors += rrdlabels_unittest_add_pairs();
  1394. errors += rrdlabels_unittest_simple_pattern();
  1395. errors += rrdlabels_unittest_double_check();
  1396. errors += rrdlabels_unittest_migrate_check();
  1397. fprintf(stderr, "%d errors found\n", errors);
  1398. return errors;
  1399. }