prometheus.c 31 KB

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