prometheus.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define EXPORTINGS_INTERNALS
  3. #include "prometheus.h"
  4. // ----------------------------------------------------------------------------
  5. // PROMETHEUS
  6. // /api/v1/allmetrics?format=prometheus and /api/v1/allmetrics?format=prometheus_all_hosts
  7. static int is_matches_rrdset(struct instance *instance, RRDSET *st, SIMPLE_PATTERN *filter) {
  8. if (instance->config.options & EXPORTING_OPTION_SEND_NAMES) {
  9. return simple_pattern_matches_string(filter, st->name);
  10. }
  11. return simple_pattern_matches_string(filter, st->id);
  12. }
  13. /**
  14. * Check if a chart can be sent to Prometheus
  15. *
  16. * @param instance an instance data structure.
  17. * @param st a chart.
  18. * @param filter a simple pattern to match against.
  19. * @return Returns 1 if the chart can be sent, 0 otherwise.
  20. */
  21. inline int can_send_rrdset(struct instance *instance, RRDSET *st, SIMPLE_PATTERN *filter)
  22. {
  23. #ifdef NETDATA_INTERNAL_CHECKS
  24. RRDHOST *host = st->rrdhost;
  25. #endif
  26. if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_EXPORTING_IGNORE)))
  27. return 0;
  28. if (filter) {
  29. if (!is_matches_rrdset(instance, st, filter)) {
  30. return 0;
  31. }
  32. } else if (unlikely(!rrdset_flag_check(st, RRDSET_FLAG_EXPORTING_SEND))) {
  33. // we have not checked this chart
  34. if (is_matches_rrdset(instance, st, instance->config.charts_pattern)) {
  35. rrdset_flag_set(st, RRDSET_FLAG_EXPORTING_SEND);
  36. } else {
  37. rrdset_flag_set(st, RRDSET_FLAG_EXPORTING_IGNORE);
  38. debug(
  39. D_EXPORTING,
  40. "EXPORTING: not sending chart '%s' of host '%s', because it is disabled for exporting.",
  41. rrdset_id(st),
  42. rrdhost_hostname(host));
  43. return 0;
  44. }
  45. }
  46. if (unlikely(!rrdset_is_available_for_exporting_and_alarms(st))) {
  47. debug(
  48. D_EXPORTING,
  49. "EXPORTING: not sending chart '%s' of host '%s', because it is not available for exporting.",
  50. rrdset_id(st),
  51. rrdhost_hostname(host));
  52. return 0;
  53. }
  54. if (unlikely(
  55. st->rrd_memory_mode == RRD_MEMORY_MODE_NONE &&
  56. !(EXPORTING_OPTIONS_DATA_SOURCE(instance->config.options) == EXPORTING_SOURCE_DATA_AS_COLLECTED))) {
  57. debug(
  58. D_EXPORTING,
  59. "EXPORTING: not sending chart '%s' of host '%s' because its memory mode is '%s' and the exporting connector requires database access.",
  60. rrdset_id(st),
  61. rrdhost_hostname(host),
  62. rrd_memory_mode_name(host->rrd_memory_mode));
  63. return 0;
  64. }
  65. return 1;
  66. }
  67. static struct prometheus_server {
  68. const char *server;
  69. uint32_t hash;
  70. RRDHOST *host;
  71. time_t last_access;
  72. struct prometheus_server *next;
  73. } *prometheus_server_root = NULL;
  74. static netdata_mutex_t prometheus_server_root_mutex = NETDATA_MUTEX_INITIALIZER;
  75. /**
  76. * Clean server root local structure
  77. */
  78. void prometheus_clean_server_root()
  79. {
  80. if (prometheus_server_root) {
  81. netdata_mutex_lock(&prometheus_server_root_mutex);
  82. struct prometheus_server *ps;
  83. for (ps = prometheus_server_root; ps; ) {
  84. struct prometheus_server *current = ps;
  85. ps = ps->next;
  86. if(current->server)
  87. freez((void *)current->server);
  88. freez(current);
  89. }
  90. prometheus_server_root = NULL;
  91. netdata_mutex_unlock(&prometheus_server_root_mutex);
  92. }
  93. }
  94. /**
  95. * Get the last time when a Prometheus server scraped the Netdata Prometheus exporter.
  96. *
  97. * @param server the name of the Prometheus server.
  98. * @param host a data collecting host.
  99. * @param now actual time.
  100. * @return Returns the last time when the server accessed Netdata, or 0 if it is the first occurrence.
  101. */
  102. static inline time_t prometheus_server_last_access(const char *server, RRDHOST *host, time_t now)
  103. {
  104. #ifdef UNIT_TESTING
  105. return 0;
  106. #endif
  107. uint32_t hash = simple_hash(server);
  108. netdata_mutex_lock(&prometheus_server_root_mutex);
  109. struct prometheus_server *ps;
  110. for (ps = prometheus_server_root; ps; ps = ps->next) {
  111. if (host == ps->host && hash == ps->hash && !strcmp(server, ps->server)) {
  112. time_t last = ps->last_access;
  113. ps->last_access = now;
  114. netdata_mutex_unlock(&prometheus_server_root_mutex);
  115. return last;
  116. }
  117. }
  118. ps = callocz(1, sizeof(struct prometheus_server));
  119. ps->server = strdupz(server);
  120. ps->hash = hash;
  121. ps->host = host;
  122. ps->last_access = now;
  123. ps->next = prometheus_server_root;
  124. prometheus_server_root = ps;
  125. netdata_mutex_unlock(&prometheus_server_root_mutex);
  126. return 0;
  127. }
  128. /**
  129. * Copy and sanitize name.
  130. *
  131. * @param d a destination string.
  132. * @param s a source string.
  133. * @param usable the number of characters to copy.
  134. * @return Returns the length of the copied string.
  135. */
  136. inline size_t prometheus_name_copy(char *d, const char *s, size_t usable)
  137. {
  138. size_t n;
  139. for (n = 0; *s && n < usable; d++, s++, n++) {
  140. register char c = *s;
  141. if (!isalnum(c))
  142. *d = '_';
  143. else
  144. *d = c;
  145. }
  146. *d = '\0';
  147. return n;
  148. }
  149. /**
  150. * Copy and sanitize label.
  151. *
  152. * @param d a destination string.
  153. * @param s a source string.
  154. * @param usable the number of characters to copy.
  155. * @return Returns the length of the copied string.
  156. */
  157. inline size_t prometheus_label_copy(char *d, const char *s, size_t usable)
  158. {
  159. size_t n;
  160. // make sure we can escape one character without overflowing the buffer
  161. usable--;
  162. for (n = 0; *s && n < usable; d++, s++, n++) {
  163. register char c = *s;
  164. if (unlikely(c == '"' || c == '\\' || c == '\n')) {
  165. *d++ = '\\';
  166. n++;
  167. }
  168. *d = c;
  169. }
  170. *d = '\0';
  171. return n;
  172. }
  173. /**
  174. * Copy and sanitize units.
  175. *
  176. * @param d a destination string.
  177. * @param s a source string.
  178. * @param usable the number of characters to copy.
  179. * @param showoldunits set this flag to 1 to show old (before v1.12) units.
  180. * @return Returns the destination string.
  181. */
  182. inline char *prometheus_units_copy(char *d, const char *s, size_t usable, int showoldunits)
  183. {
  184. const char *sorig = s;
  185. char *ret = d;
  186. size_t n;
  187. // Fix for issue 5227
  188. if (unlikely(showoldunits)) {
  189. static struct {
  190. const char *newunit;
  191. uint32_t hash;
  192. const char *oldunit;
  193. } units[] = { { "KiB/s", 0, "kilobytes/s" },
  194. { "MiB/s", 0, "MB/s" },
  195. { "GiB/s", 0, "GB/s" },
  196. { "KiB", 0, "KB" },
  197. { "MiB", 0, "MB" },
  198. { "GiB", 0, "GB" },
  199. { "inodes", 0, "Inodes" },
  200. { "percentage", 0, "percent" },
  201. { "faults/s", 0, "page faults/s" },
  202. { "KiB/operation", 0, "kilobytes per operation" },
  203. { "milliseconds/operation", 0, "ms per operation" },
  204. { NULL, 0, NULL } };
  205. static int initialized = 0;
  206. int i;
  207. if (unlikely(!initialized)) {
  208. for (i = 0; units[i].newunit; i++)
  209. units[i].hash = simple_hash(units[i].newunit);
  210. initialized = 1;
  211. }
  212. uint32_t hash = simple_hash(s);
  213. for (i = 0; units[i].newunit; i++) {
  214. if (unlikely(hash == units[i].hash && !strcmp(s, units[i].newunit))) {
  215. // info("matched extension for filename '%s': '%s'", filename, last_dot);
  216. s = units[i].oldunit;
  217. sorig = s;
  218. break;
  219. }
  220. }
  221. }
  222. *d++ = '_';
  223. for (n = 1; *s && n < usable; d++, s++, n++) {
  224. register char c = *s;
  225. if (!isalnum(c))
  226. *d = '_';
  227. else
  228. *d = c;
  229. }
  230. if (n == 2 && sorig[0] == '%') {
  231. n = 0;
  232. d = ret;
  233. s = "_percent";
  234. for (; *s && n < usable; n++)
  235. *d++ = *s++;
  236. } else if (n > 3 && sorig[n - 3] == '/' && sorig[n - 2] == 's') {
  237. n = n - 2;
  238. d -= 2;
  239. s = "_persec";
  240. for (; *s && n < usable; n++)
  241. *d++ = *s++;
  242. }
  243. *d = '\0';
  244. return ret;
  245. }
  246. /**
  247. * Format host labels for the Prometheus exporter
  248. *
  249. * @param instance an instance data structure.
  250. * @param host a data collecting host.
  251. */
  252. struct format_prometheus_label_callback {
  253. struct instance *instance;
  254. size_t count;
  255. };
  256. static int format_prometheus_label_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
  257. struct format_prometheus_label_callback *d = (struct format_prometheus_label_callback *)data;
  258. if (!should_send_label(d->instance, ls)) return 0;
  259. char k[PROMETHEUS_ELEMENT_MAX + 1];
  260. char v[PROMETHEUS_ELEMENT_MAX + 1];
  261. prometheus_name_copy(k, name, PROMETHEUS_ELEMENT_MAX);
  262. prometheus_label_copy(v, value, PROMETHEUS_ELEMENT_MAX);
  263. if (*k && *v) {
  264. if (d->count > 0) buffer_strcat(d->instance->labels_buffer, ",");
  265. buffer_sprintf(d->instance->labels_buffer, "%s=\"%s\"", k, v);
  266. d->count++;
  267. }
  268. return 1;
  269. }
  270. void format_host_labels_prometheus(struct instance *instance, RRDHOST *host)
  271. {
  272. if (unlikely(!sending_labels_configured(instance)))
  273. return;
  274. if (!instance->labels_buffer)
  275. instance->labels_buffer = buffer_create(1024, &netdata_buffers_statistics.buffers_exporters);
  276. struct format_prometheus_label_callback tmp = {
  277. .instance = instance,
  278. .count = 0
  279. };
  280. rrdlabels_walkthrough_read(host->rrdlabels, format_prometheus_label_callback, &tmp);
  281. }
  282. struct host_variables_callback_options {
  283. RRDHOST *host;
  284. BUFFER *wb;
  285. EXPORTING_OPTIONS exporting_options;
  286. PROMETHEUS_OUTPUT_OPTIONS output_options;
  287. const char *prefix;
  288. const char *labels;
  289. time_t now;
  290. int host_header_printed;
  291. char name[PROMETHEUS_VARIABLE_MAX + 1];
  292. };
  293. /**
  294. * Print host variables.
  295. *
  296. * @param rv a variable.
  297. * @param data callback options.
  298. * @return Returns 1 if the chart can be sent, 0 otherwise.
  299. */
  300. static int print_host_variables_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rv_ptr __maybe_unused, void *data) {
  301. const RRDVAR_ACQUIRED *rv = (const RRDVAR_ACQUIRED *)item;
  302. struct host_variables_callback_options *opts = data;
  303. if (rrdvar_flags(rv) & (RRDVAR_FLAG_CUSTOM_HOST_VAR | RRDVAR_FLAG_CUSTOM_CHART_VAR)) {
  304. if (!opts->host_header_printed) {
  305. opts->host_header_printed = 1;
  306. if (opts->output_options & PROMETHEUS_OUTPUT_HELP) {
  307. buffer_sprintf(opts->wb, "\n# COMMENT global host and chart variables\n");
  308. }
  309. }
  310. NETDATA_DOUBLE value = rrdvar2number(rv);
  311. if (isnan(value) || isinf(value)) {
  312. if (opts->output_options & PROMETHEUS_OUTPUT_HELP)
  313. buffer_sprintf(
  314. opts->wb, "# COMMENT variable \"%s\" is %s. Skipped.\n", rrdvar_name(rv), (isnan(value)) ? "NAN" : "INF");
  315. return 0;
  316. }
  317. char *label_pre = "";
  318. char *label_post = "";
  319. if (opts->labels && *opts->labels) {
  320. label_pre = "{";
  321. label_post = "}";
  322. }
  323. prometheus_name_copy(opts->name, rrdvar_name(rv), sizeof(opts->name));
  324. if (opts->output_options & PROMETHEUS_OUTPUT_TIMESTAMPS)
  325. buffer_sprintf(
  326. opts->wb,
  327. "%s_%s%s%s%s " NETDATA_DOUBLE_FORMAT " %llu\n",
  328. opts->prefix,
  329. opts->name,
  330. label_pre,
  331. opts->labels,
  332. label_post,
  333. value,
  334. opts->now * 1000ULL);
  335. else
  336. buffer_sprintf(
  337. opts->wb,
  338. "%s_%s%s%s%s " NETDATA_DOUBLE_FORMAT "\n",
  339. opts->prefix,
  340. opts->name,
  341. label_pre,
  342. opts->labels,
  343. label_post,
  344. value);
  345. return 1;
  346. }
  347. return 0;
  348. }
  349. struct gen_parameters {
  350. const char *prefix;
  351. char *context;
  352. char *suffix;
  353. char *chart;
  354. char *dimension;
  355. char *family;
  356. char *labels;
  357. PROMETHEUS_OUTPUT_OPTIONS output_options;
  358. RRDSET *st;
  359. RRDDIM *rd;
  360. const char *relation;
  361. const char *type;
  362. };
  363. /**
  364. * Write an as-collected help comment to a buffer.
  365. *
  366. * @param wb the buffer to write the comment to.
  367. * @param p parameters for generating the comment string.
  368. * @param homogeneous a flag for homogeneous charts.
  369. * @param prometheus_collector a flag for metrics from prometheus collector.
  370. */
  371. static void generate_as_collected_prom_help(BUFFER *wb, struct gen_parameters *p, int homogeneous, int prometheus_collector)
  372. {
  373. buffer_sprintf(wb, "# COMMENT %s_%s", p->prefix, p->context);
  374. if (!homogeneous)
  375. buffer_sprintf(wb, "_%s", p->dimension);
  376. buffer_sprintf(
  377. wb,
  378. "%s: chart \"%s\", context \"%s\", family \"%s\", dimension \"%s\", value * ",
  379. p->suffix,
  380. (p->output_options & PROMETHEUS_OUTPUT_NAMES && p->st->name) ? rrdset_name(p->st) : rrdset_id(p->st),
  381. rrdset_context(p->st),
  382. rrdset_family(p->st),
  383. (p->output_options & PROMETHEUS_OUTPUT_NAMES && p->rd->name) ? rrddim_name(p->rd) : rrddim_id(p->rd));
  384. if (prometheus_collector)
  385. buffer_sprintf(wb, "1 / 1");
  386. else
  387. buffer_sprintf(wb, COLLECTED_NUMBER_FORMAT " / " COLLECTED_NUMBER_FORMAT, p->rd->multiplier, p->rd->divisor);
  388. buffer_sprintf(wb, " %s %s (%s)\n", p->relation, rrdset_units(p->st), p->type);
  389. }
  390. /**
  391. * Write an as-collected metric to a buffer.
  392. *
  393. * @param wb the buffer to write the metric to.
  394. * @param p parameters for generating the metric string.
  395. * @param homogeneous a flag for homogeneous charts.
  396. * @param prometheus_collector a flag for metrics from prometheus collector.
  397. */
  398. static void generate_as_collected_prom_metric(BUFFER *wb, struct gen_parameters *p, int homogeneous, int prometheus_collector)
  399. {
  400. buffer_sprintf(wb, "%s_%s", p->prefix, p->context);
  401. if (!homogeneous)
  402. buffer_sprintf(wb, "_%s", p->dimension);
  403. buffer_sprintf(wb, "%s{chart=\"%s\",family=\"%s\"", p->suffix, p->chart, p->family);
  404. if (homogeneous)
  405. buffer_sprintf(wb, ",dimension=\"%s\"", p->dimension);
  406. buffer_sprintf(wb, "%s} ", p->labels);
  407. if (prometheus_collector)
  408. buffer_sprintf(
  409. wb,
  410. NETDATA_DOUBLE_FORMAT,
  411. (NETDATA_DOUBLE)p->rd->last_collected_value * (NETDATA_DOUBLE)p->rd->multiplier /
  412. (NETDATA_DOUBLE)p->rd->divisor);
  413. else
  414. buffer_sprintf(wb, COLLECTED_NUMBER_FORMAT, p->rd->last_collected_value);
  415. if (p->output_options & PROMETHEUS_OUTPUT_TIMESTAMPS)
  416. buffer_sprintf(wb, " %llu\n", timeval_msec(&p->rd->last_collected_time));
  417. else
  418. buffer_sprintf(wb, "\n");
  419. }
  420. /**
  421. * Write metrics in Prometheus format to a buffer.
  422. *
  423. * @param instance an instance data structure.
  424. * @param host a data collecting host.
  425. * @param filter_string a simple pattern filter.
  426. * @param wb the buffer to fill with metrics.
  427. * @param prefix a prefix for every metric.
  428. * @param exporting_options options to configure what data is exported.
  429. * @param allhosts set to 1 if host instance should be in the output for tags.
  430. * @param output_options options to configure the format of the output.
  431. */
  432. static void rrd_stats_api_v1_charts_allmetrics_prometheus(
  433. struct instance *instance,
  434. RRDHOST *host,
  435. const char *filter_string,
  436. BUFFER *wb,
  437. const char *prefix,
  438. EXPORTING_OPTIONS exporting_options,
  439. int allhosts,
  440. PROMETHEUS_OUTPUT_OPTIONS output_options)
  441. {
  442. SIMPLE_PATTERN *filter = simple_pattern_create(filter_string, NULL, SIMPLE_PATTERN_EXACT, true);
  443. char hostname[PROMETHEUS_ELEMENT_MAX + 1];
  444. prometheus_label_copy(hostname, rrdhost_hostname(host), PROMETHEUS_ELEMENT_MAX);
  445. format_host_labels_prometheus(instance, host);
  446. buffer_sprintf(
  447. wb,
  448. "netdata_info{instance=\"%s\",application=\"%s\",version=\"%s\"",
  449. hostname,
  450. rrdhost_program_name(host),
  451. rrdhost_program_version(host));
  452. if (instance->labels_buffer && *buffer_tostring(instance->labels_buffer)) {
  453. buffer_sprintf(wb, ",%s", buffer_tostring(instance->labels_buffer));
  454. }
  455. if (output_options & PROMETHEUS_OUTPUT_TIMESTAMPS)
  456. buffer_sprintf(wb, "} 1 %llu\n", now_realtime_usec() / USEC_PER_MS);
  457. else
  458. buffer_sprintf(wb, "} 1\n");
  459. char labels[PROMETHEUS_LABELS_MAX + 1] = "";
  460. if (allhosts) {
  461. snprintfz(labels, PROMETHEUS_LABELS_MAX, ",instance=\"%s\"", hostname);
  462. }
  463. if (instance->labels_buffer)
  464. buffer_flush(instance->labels_buffer);
  465. // send custom variables set for the host
  466. if (output_options & PROMETHEUS_OUTPUT_VARIABLES) {
  467. struct host_variables_callback_options opts = {
  468. .host = host,
  469. .wb = wb,
  470. .labels = (labels[0] == ',') ? &labels[1] : labels,
  471. .exporting_options = exporting_options,
  472. .output_options = output_options,
  473. .prefix = prefix,
  474. .now = now_realtime_sec(),
  475. .host_header_printed = 0
  476. };
  477. rrdvar_walkthrough_read(host->rrdvars, print_host_variables_callback, &opts);
  478. }
  479. // for each chart
  480. RRDSET *st;
  481. rrdset_foreach_read(st, host) {
  482. if (likely(can_send_rrdset(instance, st, filter))) {
  483. char chart[PROMETHEUS_ELEMENT_MAX + 1];
  484. char context[PROMETHEUS_ELEMENT_MAX + 1];
  485. char family[PROMETHEUS_ELEMENT_MAX + 1];
  486. char units[PROMETHEUS_ELEMENT_MAX + 1] = "";
  487. prometheus_label_copy(chart, (output_options & PROMETHEUS_OUTPUT_NAMES && st->name) ? rrdset_name(st) : rrdset_id(st), PROMETHEUS_ELEMENT_MAX);
  488. prometheus_label_copy(family, rrdset_family(st), PROMETHEUS_ELEMENT_MAX);
  489. prometheus_name_copy(context, rrdset_context(st), PROMETHEUS_ELEMENT_MAX);
  490. int as_collected = (EXPORTING_OPTIONS_DATA_SOURCE(exporting_options) == EXPORTING_SOURCE_DATA_AS_COLLECTED);
  491. int homogeneous = 1;
  492. int prometheus_collector = 0;
  493. if (as_collected) {
  494. if (rrdset_flag_check(st, RRDSET_FLAG_HOMOGENEOUS_CHECK))
  495. rrdset_update_heterogeneous_flag(st);
  496. if (rrdset_flag_check(st, RRDSET_FLAG_HETEROGENEOUS))
  497. homogeneous = 0;
  498. if (!strcmp(rrdset_module_name(st), "prometheus"))
  499. prometheus_collector = 1;
  500. } else {
  501. if (EXPORTING_OPTIONS_DATA_SOURCE(exporting_options) == EXPORTING_SOURCE_DATA_AVERAGE &&
  502. !(output_options & PROMETHEUS_OUTPUT_HIDEUNITS))
  503. prometheus_units_copy(
  504. units, rrdset_units(st), PROMETHEUS_ELEMENT_MAX, output_options & PROMETHEUS_OUTPUT_OLDUNITS);
  505. }
  506. if (unlikely(output_options & PROMETHEUS_OUTPUT_HELP))
  507. buffer_sprintf(
  508. wb,
  509. "\n# COMMENT %s chart \"%s\", context \"%s\", family \"%s\", units \"%s\"\n",
  510. (homogeneous) ? "homogeneous" : "heterogeneous",
  511. (output_options & PROMETHEUS_OUTPUT_NAMES && st->name) ? rrdset_name(st) : rrdset_id(st),
  512. rrdset_context(st),
  513. rrdset_family(st),
  514. rrdset_units(st));
  515. // for each dimension
  516. RRDDIM *rd;
  517. rrddim_foreach_read(rd, st) {
  518. if (rd->collections_counter && !rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)) {
  519. char dimension[PROMETHEUS_ELEMENT_MAX + 1];
  520. char *suffix = "";
  521. if (as_collected) {
  522. // we need as-collected / raw data
  523. struct gen_parameters p;
  524. p.prefix = prefix;
  525. p.context = context;
  526. p.suffix = suffix;
  527. p.chart = chart;
  528. p.dimension = dimension;
  529. p.family = family;
  530. p.labels = labels;
  531. p.output_options = output_options;
  532. p.st = st;
  533. p.rd = rd;
  534. if (unlikely(rd->last_collected_time.tv_sec < instance->after))
  535. continue;
  536. p.type = "gauge";
  537. p.relation = "gives";
  538. if (rd->algorithm == RRD_ALGORITHM_INCREMENTAL ||
  539. rd->algorithm == RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL) {
  540. p.type = "counter";
  541. p.relation = "delta gives";
  542. if (!prometheus_collector)
  543. p.suffix = "_total";
  544. }
  545. if (homogeneous) {
  546. // all the dimensions of the chart, has the same algorithm, multiplier and divisor
  547. // we add all dimensions as labels
  548. prometheus_label_copy(
  549. dimension,
  550. (output_options & PROMETHEUS_OUTPUT_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
  551. PROMETHEUS_ELEMENT_MAX);
  552. if (unlikely(output_options & PROMETHEUS_OUTPUT_HELP))
  553. generate_as_collected_prom_help(wb, &p, homogeneous, prometheus_collector);
  554. if (unlikely(output_options & PROMETHEUS_OUTPUT_TYPES))
  555. buffer_sprintf(wb, "# TYPE %s_%s%s %s\n", prefix, context, suffix, p.type);
  556. generate_as_collected_prom_metric(wb, &p, homogeneous, prometheus_collector);
  557. }
  558. else {
  559. // the dimensions of the chart, do not have the same algorithm, multiplier or divisor
  560. // we create a metric per dimension
  561. prometheus_name_copy(
  562. dimension,
  563. (output_options & PROMETHEUS_OUTPUT_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
  564. PROMETHEUS_ELEMENT_MAX);
  565. if (unlikely(output_options & PROMETHEUS_OUTPUT_HELP))
  566. generate_as_collected_prom_help(wb, &p, homogeneous, prometheus_collector);
  567. if (unlikely(output_options & PROMETHEUS_OUTPUT_TYPES))
  568. buffer_sprintf(
  569. wb, "# TYPE %s_%s_%s%s %s\n", prefix, context, dimension, suffix, p.type);
  570. generate_as_collected_prom_metric(wb, &p, homogeneous, prometheus_collector);
  571. }
  572. }
  573. else {
  574. // we need average or sum of the data
  575. time_t first_time = instance->after;
  576. time_t last_time = instance->before;
  577. NETDATA_DOUBLE value = exporting_calculate_value_from_stored_data(instance, rd, &last_time);
  578. if (!isnan(value) && !isinf(value)) {
  579. if (EXPORTING_OPTIONS_DATA_SOURCE(exporting_options) == EXPORTING_SOURCE_DATA_AVERAGE)
  580. suffix = "_average";
  581. else if (EXPORTING_OPTIONS_DATA_SOURCE(exporting_options) == EXPORTING_SOURCE_DATA_SUM)
  582. suffix = "_sum";
  583. prometheus_label_copy(
  584. dimension,
  585. (output_options & PROMETHEUS_OUTPUT_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
  586. PROMETHEUS_ELEMENT_MAX);
  587. if (unlikely(output_options & PROMETHEUS_OUTPUT_HELP))
  588. buffer_sprintf(
  589. wb,
  590. "# COMMENT %s_%s%s%s: dimension \"%s\", value is %s, gauge, dt %llu to %llu inclusive\n",
  591. prefix,
  592. context,
  593. units,
  594. suffix,
  595. (output_options & PROMETHEUS_OUTPUT_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
  596. rrdset_units(st),
  597. (unsigned long long)first_time,
  598. (unsigned long long)last_time);
  599. if (unlikely(output_options & PROMETHEUS_OUTPUT_TYPES))
  600. buffer_sprintf(wb, "# TYPE %s_%s%s%s gauge\n", prefix, context, units, suffix);
  601. if (output_options & PROMETHEUS_OUTPUT_TIMESTAMPS)
  602. buffer_sprintf(
  603. wb,
  604. "%s_%s%s%s{chart=\"%s\",family=\"%s\",dimension=\"%s\"%s} " NETDATA_DOUBLE_FORMAT
  605. " %llu\n",
  606. prefix,
  607. context,
  608. units,
  609. suffix,
  610. chart,
  611. family,
  612. dimension,
  613. labels,
  614. value,
  615. last_time * MSEC_PER_SEC);
  616. else
  617. buffer_sprintf(
  618. wb,
  619. "%s_%s%s%s{chart=\"%s\",family=\"%s\",dimension=\"%s\"%s} " NETDATA_DOUBLE_FORMAT
  620. "\n",
  621. prefix,
  622. context,
  623. units,
  624. suffix,
  625. chart,
  626. family,
  627. dimension,
  628. labels,
  629. value);
  630. }
  631. }
  632. }
  633. }
  634. rrddim_foreach_done(rd);
  635. }
  636. }
  637. rrdset_foreach_done(st);
  638. simple_pattern_free(filter);
  639. }
  640. /**
  641. * Get the last time time when a server accessed Netdata. Write information about an API request to a buffer.
  642. *
  643. * @param instance an instance data structure.
  644. * @param host a data collecting host.
  645. * @param wb the buffer to write to.
  646. * @param exporting_options options to configure what data is exported.
  647. * @param server the name of a Prometheus server..
  648. * @param now actual time.
  649. * @param output_options options to configure the format of the output.
  650. * @return Returns the last time when the server accessed Netdata.
  651. */
  652. static inline time_t prometheus_preparation(
  653. struct instance *instance,
  654. RRDHOST *host,
  655. BUFFER *wb,
  656. EXPORTING_OPTIONS exporting_options,
  657. const char *server,
  658. time_t now,
  659. PROMETHEUS_OUTPUT_OPTIONS output_options)
  660. {
  661. #ifndef UNIT_TESTING
  662. analytics_log_prometheus();
  663. #endif
  664. if (!server || !*server)
  665. server = "default";
  666. time_t after = prometheus_server_last_access(server, host, now);
  667. int first_seen = 0;
  668. if (!after) {
  669. after = now - instance->config.update_every;
  670. first_seen = 1;
  671. }
  672. if (after > now) {
  673. // oops! this should never happen
  674. after = now - instance->config.update_every;
  675. }
  676. if (output_options & PROMETHEUS_OUTPUT_HELP) {
  677. char *mode;
  678. if (EXPORTING_OPTIONS_DATA_SOURCE(exporting_options) == EXPORTING_SOURCE_DATA_AS_COLLECTED)
  679. mode = "as collected";
  680. else if (EXPORTING_OPTIONS_DATA_SOURCE(exporting_options) == EXPORTING_SOURCE_DATA_AVERAGE)
  681. mode = "average";
  682. else if (EXPORTING_OPTIONS_DATA_SOURCE(exporting_options) == EXPORTING_SOURCE_DATA_SUM)
  683. mode = "sum";
  684. else
  685. mode = "unknown";
  686. buffer_sprintf(
  687. wb,
  688. "# COMMENT netdata \"%s\" to %sprometheus \"%s\", source \"%s\", last seen %lu %s, time range %lu to %lu\n\n",
  689. rrdhost_hostname(host),
  690. (first_seen) ? "FIRST SEEN " : "",
  691. server,
  692. mode,
  693. (unsigned long)((first_seen) ? 0 : (now - after)),
  694. (first_seen) ? "never" : "seconds ago",
  695. (unsigned long)after,
  696. (unsigned long)now);
  697. }
  698. return after;
  699. }
  700. /**
  701. * Write metrics and auxiliary information for one host to a buffer.
  702. *
  703. * @param host a data collecting host.
  704. * @param filter_string a simple pattern filter.
  705. * @param wb the buffer to write to.
  706. * @param server the name of a Prometheus server.
  707. * @param prefix a prefix for every metric.
  708. * @param exporting_options options to configure what data is exported.
  709. * @param output_options options to configure the format of the output.
  710. */
  711. void rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(
  712. RRDHOST *host,
  713. const char *filter_string,
  714. BUFFER *wb,
  715. const char *server,
  716. const char *prefix,
  717. EXPORTING_OPTIONS exporting_options,
  718. PROMETHEUS_OUTPUT_OPTIONS output_options)
  719. {
  720. if (unlikely(!prometheus_exporter_instance || !prometheus_exporter_instance->config.initialized))
  721. return;
  722. prometheus_exporter_instance->before = now_realtime_sec();
  723. // we start at the point we had stopped before
  724. prometheus_exporter_instance->after = prometheus_preparation(
  725. prometheus_exporter_instance,
  726. host,
  727. wb,
  728. exporting_options,
  729. server,
  730. prometheus_exporter_instance->before,
  731. output_options);
  732. rrd_stats_api_v1_charts_allmetrics_prometheus(
  733. prometheus_exporter_instance, host, filter_string, wb, prefix, exporting_options, 0, output_options);
  734. }
  735. /**
  736. * Write metrics and auxiliary information for all hosts to a buffer.
  737. *
  738. * @param host a data collecting host.
  739. * @param filter_string a simple pattern filter.
  740. * @param wb the buffer to write to.
  741. * @param server the name of a Prometheus server.
  742. * @param prefix a prefix for every metric.
  743. * @param exporting_options options to configure what data is exported.
  744. * @param output_options options to configure the format of the output.
  745. */
  746. void rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts(
  747. RRDHOST *host,
  748. const char *filter_string,
  749. BUFFER *wb,
  750. const char *server,
  751. const char *prefix,
  752. EXPORTING_OPTIONS exporting_options,
  753. PROMETHEUS_OUTPUT_OPTIONS output_options)
  754. {
  755. if (unlikely(!prometheus_exporter_instance || !prometheus_exporter_instance->config.initialized))
  756. return;
  757. prometheus_exporter_instance->before = now_realtime_sec();
  758. // we start at the point we had stopped before
  759. prometheus_exporter_instance->after = prometheus_preparation(
  760. prometheus_exporter_instance,
  761. host,
  762. wb,
  763. exporting_options,
  764. server,
  765. prometheus_exporter_instance->before,
  766. output_options);
  767. rrd_rdlock();
  768. rrdhost_foreach_read(host)
  769. {
  770. rrd_stats_api_v1_charts_allmetrics_prometheus(
  771. prometheus_exporter_instance, host, filter_string, wb, prefix, exporting_options, 1, output_options);
  772. }
  773. rrd_unlock();
  774. }