rrdlabels.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  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 spaces 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, unsigned char *char_map, bool utf, const char *empty, size_t *multibyte_length) {
  437. if(unlikely(!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 white 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. delete_label(new_label);
  639. }
  640. else {
  641. new_ls |= RRDLABEL_FLAG_NEW;
  642. RRDLABEL *old_label_with_same_key = rrdlabels_find_label_with_key_unsafe(labels, new_label);
  643. if (old_label_with_same_key) {
  644. (void) JudyLDel(&labels->JudyL, (Word_t) old_label_with_same_key, PJE0);
  645. delete_label(old_label_with_same_key);
  646. }
  647. }
  648. labels->version++;
  649. *((RRDLABEL_SRC *)PValue) = new_ls;
  650. size_t mem_after_judyl = JudyLMemUsed(labels->JudyL);
  651. STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_after_judyl - mem_before_judyl, 0);
  652. spinlock_unlock(&labels->spinlock);
  653. }
  654. void rrdlabels_add(RRDLABELS *labels, const char *name, const char *value, RRDLABEL_SRC ls)
  655. {
  656. if(!labels) {
  657. netdata_log_error("%s(): called with NULL dictionary.", __FUNCTION__ );
  658. return;
  659. }
  660. char n[RRDLABELS_MAX_NAME_LENGTH + 1], v[RRDLABELS_MAX_VALUE_LENGTH + 1];
  661. rrdlabels_sanitize_name(n, name, RRDLABELS_MAX_NAME_LENGTH);
  662. rrdlabels_sanitize_value(v, value, RRDLABELS_MAX_VALUE_LENGTH);
  663. if(!*n) {
  664. netdata_log_error("%s: cannot add name '%s' (value '%s') which is sanitized as empty string", __FUNCTION__, name, value);
  665. return;
  666. }
  667. labels_add_already_sanitized(labels, n, v, ls);
  668. }
  669. bool rrdlabels_exist(RRDLABELS *labels, const char *key)
  670. {
  671. if (!labels)
  672. return false;
  673. STRING *this_key = string_strdupz(key);
  674. RRDLABEL *lb;
  675. RRDLABEL_SRC ls;
  676. bool found = false;
  677. lfe_start_read(labels, lb, ls)
  678. {
  679. if (lb->index.key == this_key) {
  680. found = true;
  681. break;
  682. }
  683. }
  684. lfe_done(labels);
  685. string_freez(this_key);
  686. return found;
  687. }
  688. static const char *get_quoted_string_up_to(char *dst, size_t dst_size, const char *string, char upto1, char upto2) {
  689. size_t len = 0;
  690. char *d = dst, quote = 0;
  691. while(*string && len++ < dst_size) {
  692. if(unlikely(!quote && (*string == '\'' || *string == '"'))) {
  693. quote = *string++;
  694. continue;
  695. }
  696. if(unlikely(quote && *string == quote)) {
  697. quote = 0;
  698. string++;
  699. continue;
  700. }
  701. if(unlikely(quote && *string == '\\' && string[1])) {
  702. string++;
  703. *d++ = *string++;
  704. continue;
  705. }
  706. if(unlikely(!quote && (*string == upto1 || *string == upto2))) break;
  707. *d++ = *string++;
  708. }
  709. *d = '\0';
  710. if(*string) string++;
  711. return string;
  712. }
  713. void rrdlabels_add_pair(RRDLABELS *labels, const char *string, RRDLABEL_SRC ls)
  714. {
  715. if(!labels) {
  716. netdata_log_error("%s(): called with NULL dictionary.", __FUNCTION__ );
  717. return;
  718. }
  719. char name[RRDLABELS_MAX_NAME_LENGTH + 1];
  720. string = get_quoted_string_up_to(name, RRDLABELS_MAX_NAME_LENGTH, string, '=', ':');
  721. char value[RRDLABELS_MAX_VALUE_LENGTH + 1];
  722. get_quoted_string_up_to(value, RRDLABELS_MAX_VALUE_LENGTH, string, '\0', '\0');
  723. rrdlabels_add(labels, name, value, ls);
  724. }
  725. // ----------------------------------------------------------------------------
  726. void rrdlabels_value_to_buffer_array_item_or_null(RRDLABELS *labels, BUFFER *wb, const char *key) {
  727. if(!labels) return;
  728. STRING *this_key = string_strdupz(key);
  729. RRDLABEL *lb;
  730. RRDLABEL_SRC ls;
  731. lfe_start_read(labels, lb, ls)
  732. {
  733. if (lb->index.key == this_key) {
  734. if (lb->index.value)
  735. buffer_json_add_array_item_string(wb, string2str(lb->index.value));
  736. else
  737. buffer_json_add_array_item_string(wb, NULL);
  738. break;
  739. }
  740. }
  741. lfe_done(labels);
  742. string_freez(this_key);
  743. }
  744. // ----------------------------------------------------------------------------
  745. void rrdlabels_get_value_strcpyz(RRDLABELS *labels, char *dst, size_t dst_len, const char *key) {
  746. if(!labels) return;
  747. STRING *this_key = string_strdupz(key);
  748. RRDLABEL *lb;
  749. RRDLABEL_SRC ls;
  750. lfe_start_read(labels, lb, ls)
  751. {
  752. if (lb->index.key == this_key) {
  753. if (lb->index.value)
  754. strncpyz(dst, string2str(lb->index.value), dst_len);
  755. else
  756. dst[0] = '\0';
  757. break;
  758. }
  759. }
  760. lfe_done(labels);
  761. string_freez(this_key);
  762. }
  763. void rrdlabels_get_value_strdup_or_null(RRDLABELS *labels, char **value, const char *key)
  764. {
  765. if(!labels) return;
  766. STRING *this_key = string_strdupz(key);
  767. RRDLABEL *lb;
  768. RRDLABEL_SRC ls;
  769. lfe_start_read(labels, lb, ls)
  770. {
  771. if (lb->index.key == this_key) {
  772. *value = (lb->index.value) ? strdupz(string2str(lb->index.value)) : NULL;
  773. break;
  774. }
  775. }
  776. lfe_done(labels);
  777. string_freez(this_key);
  778. }
  779. void rrdlabels_get_value_to_buffer_or_unset(RRDLABELS *labels, BUFFER *wb, const char *key, const char *unset)
  780. {
  781. if(!labels || !key || !wb) return;
  782. STRING *this_key = string_strdupz(key);
  783. RRDLABEL *lb;
  784. RRDLABEL_SRC ls;
  785. lfe_start_read(labels, lb, ls)
  786. {
  787. if (lb->index.key == this_key) {
  788. if (lb->index.value)
  789. buffer_strcat(wb, string2str(lb->index.value));
  790. else
  791. buffer_strcat(wb, unset);
  792. break;
  793. }
  794. }
  795. lfe_done(labels);
  796. string_freez(this_key);
  797. }
  798. static void rrdlabels_unmark_all_unsafe(RRDLABELS *labels)
  799. {
  800. Pvoid_t *PValue;
  801. Word_t Index = 0;
  802. bool first_then_next = true;
  803. while ((PValue = JudyLFirstThenNext(labels->JudyL, &Index, &first_then_next)))
  804. *((RRDLABEL_SRC *)PValue) &= ~(RRDLABEL_FLAG_OLD | RRDLABEL_FLAG_NEW);
  805. }
  806. void rrdlabels_unmark_all(RRDLABELS *labels)
  807. {
  808. spinlock_lock(&labels->spinlock);
  809. rrdlabels_unmark_all_unsafe(labels);
  810. spinlock_unlock(&labels->spinlock);
  811. }
  812. static void rrdlabels_remove_all_unmarked_unsafe(RRDLABELS *labels)
  813. {
  814. Pvoid_t *PValue;
  815. Word_t Index = 0;
  816. bool first_then_next = true;
  817. while ((PValue = JudyLFirstThenNext(labels->JudyL, &Index, &first_then_next))) {
  818. if (!((*((RRDLABEL_SRC *)PValue)) & (RRDLABEL_FLAG_OLD | RRDLABEL_FLAG_NEW | RRDLABEL_FLAG_PERMANENT))) {
  819. size_t mem_before_judyl = JudyLMemUsed(labels->JudyL);
  820. (void)JudyLDel(&labels->JudyL, Index, PJE0);
  821. size_t mem_after_judyl = JudyLMemUsed(labels->JudyL);
  822. STATS_MINUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_before_judyl - mem_after_judyl, 0);
  823. delete_label((RRDLABEL *)Index);
  824. if (labels->JudyL != (Pvoid_t) NULL) {
  825. Index = 0;
  826. first_then_next = true;
  827. }
  828. }
  829. }
  830. }
  831. void rrdlabels_remove_all_unmarked(RRDLABELS *labels)
  832. {
  833. spinlock_lock(&labels->spinlock);
  834. rrdlabels_remove_all_unmarked_unsafe(labels);
  835. spinlock_unlock(&labels->spinlock);
  836. }
  837. // ----------------------------------------------------------------------------
  838. // rrdlabels_walkthrough_read()
  839. int rrdlabels_walkthrough_read(RRDLABELS *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data)
  840. {
  841. int ret = 0;
  842. if(unlikely(!labels || !callback)) return 0;
  843. RRDLABEL *lb;
  844. RRDLABEL_SRC ls;
  845. lfe_start_read(labels, lb, ls)
  846. {
  847. ret = callback(string2str(lb->index.key), string2str(lb->index.value), ls, data);
  848. if (ret < 0)
  849. break;
  850. }
  851. lfe_done(labels);
  852. return ret;
  853. }
  854. // ----------------------------------------------------------------------------
  855. // rrdlabels_migrate_to_these()
  856. // migrate an existing label list to a new list
  857. void rrdlabels_migrate_to_these(RRDLABELS *dst, RRDLABELS *src) {
  858. if (!dst || !src || (dst == src))
  859. return;
  860. spinlock_lock(&dst->spinlock);
  861. spinlock_lock(&src->spinlock);
  862. rrdlabels_unmark_all_unsafe(dst);
  863. RRDLABEL *label;
  864. Pvoid_t *PValue;
  865. RRDLABEL_SRC ls;
  866. lfe_start_nolock(src, label, ls)
  867. {
  868. size_t mem_before_judyl = JudyLMemUsed(dst->JudyL);
  869. PValue = JudyLIns(&dst->JudyL, (Word_t)label, PJE0);
  870. if(unlikely(!PValue || PValue == PJERR))
  871. fatal("RRDLABELS migrate: corrupted labels array");
  872. RRDLABEL_SRC flag = RRDLABEL_FLAG_NEW;
  873. if (!*PValue) {
  874. dup_label(label);
  875. size_t mem_after_judyl = JudyLMemUsed(dst->JudyL);
  876. STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_after_judyl - mem_before_judyl, 0);
  877. }
  878. else
  879. flag = RRDLABEL_FLAG_OLD;
  880. *((RRDLABEL_SRC *)PValue) |= flag;
  881. }
  882. lfe_done_nolock();
  883. rrdlabels_remove_all_unmarked_unsafe(dst);
  884. dst->version = src->version;
  885. spinlock_unlock(&src->spinlock);
  886. spinlock_unlock(&dst->spinlock);
  887. }
  888. void rrdlabels_copy(RRDLABELS *dst, RRDLABELS *src)
  889. {
  890. if (!dst || !src || (dst == src))
  891. return;
  892. RRDLABEL *label;
  893. RRDLABEL_SRC ls;
  894. spinlock_lock(&dst->spinlock);
  895. spinlock_lock(&src->spinlock);
  896. size_t mem_before_judyl = JudyLMemUsed(dst->JudyL);
  897. bool update_statistics = false;
  898. lfe_start_nolock(src, label, ls)
  899. {
  900. RRDLABEL *old_label_with_key = rrdlabels_find_label_with_key_unsafe(dst, label);
  901. Pvoid_t *PValue = JudyLIns(&dst->JudyL, (Word_t)label, PJE0);
  902. if(unlikely(!PValue || PValue == PJERR))
  903. fatal("RRDLABELS: corrupted labels array");
  904. if (!*PValue) {
  905. dup_label(label);
  906. *((RRDLABEL_SRC *)PValue) = ls;
  907. dst->version++;
  908. update_statistics = true;
  909. if (old_label_with_key) {
  910. (void)JudyLDel(&dst->JudyL, (Word_t)old_label_with_key, PJE0);
  911. delete_label((RRDLABEL *)old_label_with_key);
  912. }
  913. }
  914. }
  915. lfe_done_nolock();
  916. if (update_statistics) {
  917. size_t mem_after_judyl = JudyLMemUsed(dst->JudyL);
  918. STATS_PLUS_MEMORY(&dictionary_stats_category_rrdlabels, 0, mem_after_judyl - mem_before_judyl, 0);
  919. }
  920. spinlock_unlock(&src->spinlock);
  921. spinlock_unlock(&dst->spinlock);
  922. }
  923. // ----------------------------------------------------------------------------
  924. // rrdlabels_match_simple_pattern()
  925. // returns true when there are keys in the dictionary matching a simple pattern
  926. struct simple_pattern_match_name_value {
  927. size_t searches;
  928. SIMPLE_PATTERN *pattern;
  929. char equal;
  930. };
  931. static int simple_pattern_match_name_only_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
  932. struct simple_pattern_match_name_value *t = (struct simple_pattern_match_name_value *)data;
  933. (void)value;
  934. // we return -1 to stop the walkthrough on first match
  935. t->searches++;
  936. if(simple_pattern_matches(t->pattern, name)) return -1;
  937. return 0;
  938. }
  939. static int simple_pattern_match_name_and_value_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
  940. struct simple_pattern_match_name_value *t = (struct simple_pattern_match_name_value *)data;
  941. // we return -1 to stop the walkthrough on first match
  942. t->searches++;
  943. if(simple_pattern_matches(t->pattern, name)) return -1;
  944. size_t len = RRDLABELS_MAX_NAME_LENGTH + RRDLABELS_MAX_VALUE_LENGTH + 2; // +1 for =, +1 for \0
  945. char tmp[len], *dst = &tmp[0];
  946. const char *v = value;
  947. // copy the name
  948. while(*name) *dst++ = *name++;
  949. // add the equal
  950. *dst++ = t->equal;
  951. // add the value
  952. while(*v) *dst++ = *v++;
  953. // terminate it
  954. *dst = '\0';
  955. t->searches++;
  956. if(simple_pattern_matches_length_extract(t->pattern, tmp, dst - tmp, NULL, 0) == SP_MATCHED_POSITIVE)
  957. return -1;
  958. return 0;
  959. }
  960. bool rrdlabels_match_simple_pattern_parsed(RRDLABELS *labels, SIMPLE_PATTERN *pattern, char equal, size_t *searches) {
  961. if (!labels) return false;
  962. struct simple_pattern_match_name_value t = {
  963. .searches = 0,
  964. .pattern = pattern,
  965. .equal = equal
  966. };
  967. int ret = rrdlabels_walkthrough_read(labels, equal?simple_pattern_match_name_and_value_callback:simple_pattern_match_name_only_callback, &t);
  968. if(searches)
  969. *searches = t.searches;
  970. return (ret == -1)?true:false;
  971. }
  972. bool rrdlabels_match_simple_pattern(RRDLABELS *labels, const char *simple_pattern_txt) {
  973. if (!labels) return false;
  974. SIMPLE_PATTERN *pattern = simple_pattern_create(simple_pattern_txt, " ,|\t\r\n\f\v", SIMPLE_PATTERN_EXACT, true);
  975. char equal = '\0';
  976. const char *s;
  977. for(s = simple_pattern_txt; *s ; s++) {
  978. if (*s == '=' || *s == ':') {
  979. equal = *s;
  980. break;
  981. }
  982. }
  983. bool ret = rrdlabels_match_simple_pattern_parsed(labels, pattern, equal, NULL);
  984. simple_pattern_free(pattern);
  985. return ret;
  986. }
  987. // ----------------------------------------------------------------------------
  988. // Log all labels
  989. static int rrdlabels_log_label_to_buffer_callback(const char *name, const char *value, void *data) {
  990. BUFFER *wb = (BUFFER *)data;
  991. buffer_sprintf(wb, "Label: %s: \"%s\" (", name, value);
  992. buffer_strcat(wb, "unknown");
  993. buffer_strcat(wb, ")\n");
  994. return 1;
  995. }
  996. void rrdlabels_log_to_buffer(RRDLABELS *labels, BUFFER *wb)
  997. {
  998. RRDLABEL *lb;
  999. RRDLABEL_SRC ls;
  1000. lfe_start_read(labels, lb, ls)
  1001. rrdlabels_log_label_to_buffer_callback((void *) string2str(lb->index.key), (void *) string2str(lb->index.value), wb);
  1002. lfe_done(labels);
  1003. }
  1004. // ----------------------------------------------------------------------------
  1005. // rrdlabels_to_buffer()
  1006. struct labels_to_buffer {
  1007. BUFFER *wb;
  1008. bool (*filter_callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data);
  1009. void *filter_data;
  1010. void (*name_sanitizer)(char *dst, const char *src, size_t dst_size);
  1011. void (*value_sanitizer)(char *dst, const char *src, size_t dst_size);
  1012. const char *before_each;
  1013. const char *quote;
  1014. const char *equal;
  1015. const char *between_them;
  1016. size_t count;
  1017. };
  1018. static int label_to_buffer_callback(const RRDLABEL *lb, void *value __maybe_unused, RRDLABEL_SRC ls, void *data)
  1019. {
  1020. struct labels_to_buffer *t = (struct labels_to_buffer *)data;
  1021. size_t n_size = (t->name_sanitizer ) ? ( RRDLABELS_MAX_NAME_LENGTH * 2 ) : 1;
  1022. size_t v_size = (t->value_sanitizer) ? ( RRDLABELS_MAX_VALUE_LENGTH * 2 ) : 1;
  1023. char n[n_size];
  1024. char v[v_size];
  1025. const char *name = string2str(lb->index.key);
  1026. const char *nn = name, *vv = string2str(lb->index.value);
  1027. if(t->name_sanitizer) {
  1028. t->name_sanitizer(n, name, n_size);
  1029. nn = n;
  1030. }
  1031. if(t->value_sanitizer) {
  1032. t->value_sanitizer(v, string2str(lb->index.value), v_size);
  1033. vv = v;
  1034. }
  1035. if(!t->filter_callback || t->filter_callback(name, string2str(lb->index.value), ls, t->filter_data)) {
  1036. 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);
  1037. return 1;
  1038. }
  1039. return 0;
  1040. }
  1041. int label_walkthrough_read(RRDLABELS *labels, int (*callback)(const RRDLABEL *item, void *entry, RRDLABEL_SRC ls, void *data), void *data)
  1042. {
  1043. int ret = 0;
  1044. if(unlikely(!labels || !callback)) return 0;
  1045. RRDLABEL *lb;
  1046. RRDLABEL_SRC ls;
  1047. lfe_start_read(labels, lb, ls)
  1048. {
  1049. ret = callback((const RRDLABEL *)lb, (void *)string2str(lb->index.value), ls, data);
  1050. if (ret < 0)
  1051. break;
  1052. }
  1053. lfe_done(labels);
  1054. return ret;
  1055. }
  1056. 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)) {
  1057. struct labels_to_buffer tmp = {
  1058. .wb = wb,
  1059. .filter_callback = filter_callback,
  1060. .filter_data = filter_data,
  1061. .name_sanitizer = name_sanitizer,
  1062. .value_sanitizer = value_sanitizer,
  1063. .before_each = before_each,
  1064. .equal = equal,
  1065. .quote = quote,
  1066. .between_them = between_them,
  1067. .count = 0
  1068. };
  1069. return label_walkthrough_read(labels, label_to_buffer_callback, (void *)&tmp);
  1070. }
  1071. void rrdlabels_to_buffer_json_members(RRDLABELS *labels, BUFFER *wb)
  1072. {
  1073. RRDLABEL *lb;
  1074. RRDLABEL_SRC ls;
  1075. lfe_start_read(labels, lb, ls)
  1076. buffer_json_member_add_string(wb, string2str(lb->index.key), string2str(lb->index.value));
  1077. lfe_done(labels);
  1078. }
  1079. size_t rrdlabels_entries(RRDLABELS *labels __maybe_unused)
  1080. {
  1081. if (unlikely(!labels))
  1082. return 0;
  1083. size_t count;
  1084. spinlock_lock(&labels->spinlock);
  1085. count = JudyLCount(labels->JudyL, 0, -1, PJE0);
  1086. spinlock_unlock(&labels->spinlock);
  1087. return count;
  1088. }
  1089. size_t rrdlabels_version(RRDLABELS *labels __maybe_unused)
  1090. {
  1091. if (unlikely(!labels))
  1092. return 0;
  1093. return (size_t) labels->version;
  1094. }
  1095. void rrdset_update_rrdlabels(RRDSET *st, RRDLABELS *new_rrdlabels) {
  1096. if(!st->rrdlabels)
  1097. st->rrdlabels = rrdlabels_create();
  1098. if (new_rrdlabels)
  1099. rrdlabels_migrate_to_these(st->rrdlabels, new_rrdlabels);
  1100. rrdset_flag_set(st, RRDSET_FLAG_METADATA_UPDATE);
  1101. rrdhost_flag_set(st->rrdhost, RRDHOST_FLAG_METADATA_UPDATE);
  1102. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  1103. }
  1104. // ----------------------------------------------------------------------------
  1105. // rrdlabels unit test
  1106. struct rrdlabels_unittest_add_a_pair {
  1107. const char *pair;
  1108. const char *expected_name;
  1109. const char *expected_value;
  1110. const char *name;
  1111. const char *value;
  1112. int errors;
  1113. };
  1114. int rrdlabels_unittest_add_a_pair_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
  1115. struct rrdlabels_unittest_add_a_pair *t = (struct rrdlabels_unittest_add_a_pair *)data;
  1116. t->name = name;
  1117. t->value = value;
  1118. if(strcmp(name, t->expected_name) != 0) {
  1119. fprintf(stderr, "name is wrong, found \"%s\", expected \"%s\"", name, t->expected_name);
  1120. t->errors++;
  1121. }
  1122. if(value == NULL && t->expected_value == NULL) {
  1123. ;
  1124. }
  1125. else if(value == NULL || t->expected_value == NULL) {
  1126. fprintf(stderr, "value is wrong, found \"%s\", expected \"%s\"", value?value:"(null)", t->expected_value?t->expected_value:"(null)");
  1127. t->errors++;
  1128. }
  1129. else if(strcmp(value, t->expected_value) != 0) {
  1130. fprintf(stderr, "values don't match, found \"%s\", expected \"%s\"", value, t->expected_value);
  1131. t->errors++;
  1132. }
  1133. return 1;
  1134. }
  1135. int rrdlabels_unittest_add_a_pair(const char *pair, const char *name, const char *value) {
  1136. RRDLABELS *labels = rrdlabels_create();
  1137. int errors;
  1138. fprintf(stderr, "rrdlabels_add_pair(labels, %s) ... ", pair);
  1139. rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
  1140. struct rrdlabels_unittest_add_a_pair tmp = {
  1141. .pair = pair,
  1142. .expected_name = name,
  1143. .expected_value = value,
  1144. .errors = 0
  1145. };
  1146. int ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
  1147. errors = tmp.errors;
  1148. if(ret != 1) {
  1149. fprintf(stderr, "failed to get \"%s\" label", name);
  1150. errors++;
  1151. }
  1152. if(!errors)
  1153. fprintf(stderr, " OK, name='%s' and value='%s'\n", tmp.name, tmp.value?tmp.value:"(null)");
  1154. else
  1155. fprintf(stderr, " FAILED\n");
  1156. rrdlabels_destroy(labels);
  1157. return errors;
  1158. }
  1159. int rrdlabels_unittest_add_pairs() {
  1160. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  1161. int errors = 0;
  1162. // basic test
  1163. errors += rrdlabels_unittest_add_a_pair("tag=value", "tag", "value");
  1164. errors += rrdlabels_unittest_add_a_pair("tag:value", "tag", "value");
  1165. // test newlines
  1166. errors += rrdlabels_unittest_add_a_pair(" tag = \t value \r\n", "tag", "value");
  1167. // test : in values
  1168. errors += rrdlabels_unittest_add_a_pair("tag=:value", "tag", ":value");
  1169. errors += rrdlabels_unittest_add_a_pair("tag::value", "tag", ":value");
  1170. errors += rrdlabels_unittest_add_a_pair(" tag = :value ", "tag", ":value");
  1171. errors += rrdlabels_unittest_add_a_pair(" tag : :value ", "tag", ":value");
  1172. errors += rrdlabels_unittest_add_a_pair("tag:5", "tag", "5");
  1173. errors += rrdlabels_unittest_add_a_pair("tag:55", "tag", "55");
  1174. errors += rrdlabels_unittest_add_a_pair("tag:aa", "tag", "aa");
  1175. errors += rrdlabels_unittest_add_a_pair("tag:a", "tag", "a");
  1176. // test empty values
  1177. errors += rrdlabels_unittest_add_a_pair("tag", "tag", "[none]");
  1178. errors += rrdlabels_unittest_add_a_pair("tag:", "tag", "[none]");
  1179. errors += rrdlabels_unittest_add_a_pair("tag:\"\"", "tag", "[none]");
  1180. errors += rrdlabels_unittest_add_a_pair("tag:''", "tag", "[none]");
  1181. errors += rrdlabels_unittest_add_a_pair("tag:\r\n", "tag", "[none]");
  1182. errors += rrdlabels_unittest_add_a_pair("tag\r\n", "tag", "[none]");
  1183. // test UTF-8 in values
  1184. errors += rrdlabels_unittest_add_a_pair("tag: country:Ελλάδα", "tag", "country:Ελλάδα");
  1185. errors += rrdlabels_unittest_add_a_pair("\"tag\": \"country:Ελλάδα\"", "tag", "country:Ελλάδα");
  1186. errors += rrdlabels_unittest_add_a_pair("\"tag\": country:\"Ελλάδα\"", "tag", "country:Ελλάδα");
  1187. errors += rrdlabels_unittest_add_a_pair("\"tag=1\": country:\"Gre\\\"ece\"", "tag_1", "country:Gre_ece");
  1188. errors += rrdlabels_unittest_add_a_pair("\"tag=1\" = country:\"Gre\\\"ece\"", "tag_1", "country:Gre_ece");
  1189. errors += rrdlabels_unittest_add_a_pair("\t'LABE=L'\t=\t\"World\" peace", "labe_l", "World peace");
  1190. errors += rrdlabels_unittest_add_a_pair("\t'LA\\'B:EL'\t=\tcountry:\"World\":\"Europe\":\"Greece\"", "la_b_el", "country:World:Europe:Greece");
  1191. errors += rrdlabels_unittest_add_a_pair("\t'LA\\'B:EL'\t=\tcountry\\\"World\"\\\"Europe\"\\\"Greece\"", "la_b_el", "country/World/Europe/Greece");
  1192. errors += rrdlabels_unittest_add_a_pair("NAME=\"VALUE\"", "name", "VALUE");
  1193. errors += rrdlabels_unittest_add_a_pair("\"NAME\" : \"VALUE\"", "name", "VALUE");
  1194. errors += rrdlabels_unittest_add_a_pair("NAME: \"VALUE\"", "name", "VALUE");
  1195. return errors;
  1196. }
  1197. int rrdlabels_unittest_double_check() {
  1198. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  1199. int errors = 1;
  1200. int ret = 0;
  1201. RRDLABELS *labels = rrdlabels_create();
  1202. const char *pair = "key1=value1";
  1203. struct rrdlabels_unittest_add_a_pair tmp = {
  1204. .pair = pair,
  1205. .expected_name = "key1",
  1206. .expected_value = NULL,
  1207. .errors = 0
  1208. };
  1209. fprintf(stderr, "rrdlabels_add_pair(labels, %s) ...\n ", pair);
  1210. rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
  1211. size_t count = rrdlabels_entries(labels);
  1212. fprintf(stderr, "Added one key with \"value1\", entries found %zu\n", count);
  1213. tmp.expected_value = "value1";
  1214. ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
  1215. fprintf(stderr, "Adding key with same value \"value1\" (collision check)\n");
  1216. rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
  1217. count = rrdlabels_entries(labels);
  1218. fprintf(stderr, "Added same key again \"value1\", entries found %zu\n", count);
  1219. ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
  1220. // Add same key with different value
  1221. pair = "key1=value2";
  1222. rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
  1223. count = rrdlabels_entries(labels);
  1224. fprintf(stderr, "Added same key again with \"value2\", entries found %zu\n", count);
  1225. tmp.expected_value = "value2";
  1226. ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
  1227. fprintf(stderr, "Adding key with same value \"value2\" (collision check)\n");
  1228. rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
  1229. count = rrdlabels_entries(labels);
  1230. fprintf(stderr, "Added same key again with \"value2\", entries found %zu\n", count);
  1231. ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
  1232. errors = tmp.errors;
  1233. if(ret != 1) {
  1234. fprintf(stderr, "failed to get \"%s\" label", "key1");
  1235. errors++;
  1236. }
  1237. if(!errors)
  1238. fprintf(stderr, " OK, name='%s' and value='%s'\n", tmp.name, tmp.value?tmp.value:"(null)");
  1239. else
  1240. fprintf(stderr, " FAILED\n");
  1241. rrdlabels_destroy(labels);
  1242. return errors;
  1243. }
  1244. int rrdlabels_unittest_migrate_check() {
  1245. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  1246. RRDLABELS *labels1 = NULL;
  1247. RRDLABELS *labels2 = NULL;
  1248. labels1 = rrdlabels_create();
  1249. labels2 = rrdlabels_create();
  1250. rrdlabels_add(labels1, "key1", "value1", RRDLABEL_SRC_CONFIG);
  1251. rrdlabels_add(labels1, "key1", "value2", RRDLABEL_SRC_CONFIG);
  1252. rrdlabels_add(labels2, "new_key1", "value2", RRDLABEL_SRC_CONFIG);
  1253. rrdlabels_add(labels2, "new_key2", "value2", RRDLABEL_SRC_CONFIG);
  1254. rrdlabels_add(labels2, "key1", "value2", RRDLABEL_SRC_CONFIG);
  1255. fprintf(stderr, "Labels1 entries found %zu (should be 1)\n", rrdlabels_entries(labels1));
  1256. fprintf(stderr, "Labels2 entries found %zu (should be 3)\n", rrdlabels_entries(labels2));
  1257. rrdlabels_migrate_to_these(labels1, labels2);
  1258. fprintf(stderr, "labels1 (migrated) entries found %zu (should be 3)\n", rrdlabels_entries(labels1));
  1259. size_t entries = rrdlabels_entries(labels1);
  1260. rrdlabels_destroy(labels1);
  1261. rrdlabels_destroy(labels2);
  1262. if (entries != 3)
  1263. return 1;
  1264. // Copy test
  1265. labels1 = rrdlabels_create();
  1266. labels2 = rrdlabels_create();
  1267. rrdlabels_add(labels1, "key1", "value1", RRDLABEL_SRC_CONFIG);
  1268. rrdlabels_add(labels1, "key2", "value1", RRDLABEL_SRC_CONFIG);
  1269. rrdlabels_add(labels1, "key3", "value1", RRDLABEL_SRC_CONFIG);
  1270. rrdlabels_add(labels1, "key4", "value1", RRDLABEL_SRC_CONFIG); // 4 keys
  1271. rrdlabels_add(labels2, "key1", "value10", RRDLABEL_SRC_CONFIG);
  1272. rrdlabels_add(labels2, "key2", "value1", RRDLABEL_SRC_CONFIG);
  1273. rrdlabels_add(labels2, "key0", "value1", RRDLABEL_SRC_CONFIG);
  1274. rrdlabels_copy(labels1, labels2); // labels1 should have 5 keys
  1275. entries = rrdlabels_entries(labels1);
  1276. fprintf(stderr, "labels1 (copied) entries found %zu (should be 5)\n", rrdlabels_entries(labels1));
  1277. if (entries != 5)
  1278. return 1;
  1279. rrdlabels_add(labels1, "key100", "value1", RRDLABEL_SRC_CONFIG);
  1280. rrdlabels_copy(labels2, labels1); // labels2 should have 6 keys
  1281. entries = rrdlabels_entries(labels1);
  1282. fprintf(stderr, "labels2 (copied) entries found %zu (should be 6)\n", rrdlabels_entries(labels1));
  1283. rrdlabels_destroy(labels1);
  1284. rrdlabels_destroy(labels2);
  1285. return entries != 6;
  1286. }
  1287. int rrdlabels_unittest_check_simple_pattern(RRDLABELS *labels, const char *pattern, bool expected) {
  1288. fprintf(stderr, "rrdlabels_match_simple_pattern(labels, \"%s\") ... ", pattern);
  1289. bool ret = rrdlabels_match_simple_pattern(labels, pattern);
  1290. fprintf(stderr, "%s, got %s expected %s\n", (ret == expected)?"OK":"FAILED", ret?"true":"false", expected?"true":"false");
  1291. return (ret == expected)?0:1;
  1292. }
  1293. int rrdlabels_unittest_simple_pattern() {
  1294. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  1295. int errors = 0;
  1296. RRDLABELS *labels = rrdlabels_create();
  1297. rrdlabels_add(labels, "tag1", "value1", RRDLABEL_SRC_CONFIG);
  1298. rrdlabels_add(labels, "tag2", "value2", RRDLABEL_SRC_CONFIG);
  1299. rrdlabels_add(labels, "tag3", "value3", RRDLABEL_SRC_CONFIG);
  1300. errors += rrdlabels_unittest_check_simple_pattern(labels, "*", true);
  1301. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag", false);
  1302. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag*", true);
  1303. errors += rrdlabels_unittest_check_simple_pattern(labels, "*1", true);
  1304. errors += rrdlabels_unittest_check_simple_pattern(labels, "value*", false);
  1305. errors += rrdlabels_unittest_check_simple_pattern(labels, "*=value*", true);
  1306. errors += rrdlabels_unittest_check_simple_pattern(labels, "*:value*", true);
  1307. errors += rrdlabels_unittest_check_simple_pattern(labels, "*2", true);
  1308. errors += rrdlabels_unittest_check_simple_pattern(labels, "*2 *3", true);
  1309. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag3 *2", true);
  1310. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1 tag2", true);
  1311. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1tag2", false);
  1312. errors += rrdlabels_unittest_check_simple_pattern(labels, "invalid1 invalid2 tag3", true);
  1313. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag1 tag4", false);
  1314. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1=value1", true);
  1315. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1=value2", false);
  1316. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag*=value*", true);
  1317. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag*=value*", false);
  1318. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag2=something2 tag2=*2", true);
  1319. rrdlabels_destroy(labels);
  1320. return errors;
  1321. }
  1322. int rrdlabels_unittest_sanitize_value(const char *src, const char *expected) {
  1323. char buf[RRDLABELS_MAX_VALUE_LENGTH + 1];
  1324. size_t len = rrdlabels_sanitize_value(buf, src, RRDLABELS_MAX_VALUE_LENGTH);
  1325. size_t expected_len = strlen(expected);
  1326. int err = 0;
  1327. if(strcmp(buf, expected) != 0) err = 1;
  1328. if(len != expected_len) err = 1;
  1329. 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));
  1330. return err;
  1331. }
  1332. int rrdlabels_unittest_sanitization() {
  1333. int errors = 0;
  1334. errors += rrdlabels_unittest_sanitize_value("", "[none]");
  1335. errors += rrdlabels_unittest_sanitize_value("1", "1");
  1336. errors += rrdlabels_unittest_sanitize_value(" hello world ", "hello world");
  1337. errors += rrdlabels_unittest_sanitize_value("[none]", "[none]");
  1338. // 2-byte UTF-8
  1339. errors += rrdlabels_unittest_sanitize_value(" Ελλάδα ", "Ελλάδα");
  1340. errors += rrdlabels_unittest_sanitize_value("aŰbŲcŴ", "aŰbŲcŴ");
  1341. errors += rrdlabels_unittest_sanitize_value("Ű b Ų c Ŵ", "Ű b Ų c Ŵ");
  1342. // 3-byte UTF-8
  1343. errors += rrdlabels_unittest_sanitize_value("‱", "‱");
  1344. errors += rrdlabels_unittest_sanitize_value("a‱b", "a‱b");
  1345. errors += rrdlabels_unittest_sanitize_value("a ‱ b", "a ‱ b");
  1346. // 4-byte UTF-8
  1347. errors += rrdlabels_unittest_sanitize_value("𩸽", "𩸽");
  1348. errors += rrdlabels_unittest_sanitize_value("a𩸽b", "a𩸽b");
  1349. errors += rrdlabels_unittest_sanitize_value("a 𩸽 b", "a 𩸽 b");
  1350. // mixed multi-byte
  1351. errors += rrdlabels_unittest_sanitize_value("Ű‱𩸽‱Ű", "Ű‱𩸽‱Ű");
  1352. return errors;
  1353. }
  1354. int rrdlabels_unittest(void) {
  1355. int errors = 0;
  1356. errors += rrdlabels_unittest_sanitization();
  1357. errors += rrdlabels_unittest_add_pairs();
  1358. errors += rrdlabels_unittest_simple_pattern();
  1359. errors += rrdlabels_unittest_double_check();
  1360. errors += rrdlabels_unittest_migrate_check();
  1361. fprintf(stderr, "%d errors found\n", errors);
  1362. return errors;
  1363. }