master
c 1,041 lines 34.9 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "prometheus.h"
4
5 DEFINE_JUDYL_TYPED(PROM_CONTEXT_OPTIONS, PROMETHEUS_OUTPUT_OPTIONS);
6
7 static void PROM_CONTEXT_OPTIONS_free_cb(Word_t index, PROMETHEUS_OUTPUT_OPTIONS options __maybe_unused, void *data __maybe_unused) {
8 STRING *context_id = (STRING *)index;
9 string_freez(context_id);
10 }
11
12 // ----------------------------------------------------------------------------
13 // PROMETHEUS
14 // /api/v1/allmetrics?format=prometheus and /api/v1/allmetrics?format=prometheus_all_hosts
15
16 static int is_matches_rrdset(struct instance *instance, RRDSET *st, SIMPLE_PATTERN *filter) {
17 if (instance->config.options & EXPORTING_OPTION_SEND_NAMES) {
18 return simple_pattern_matches_string(filter, st->name);
19 }
20 return simple_pattern_matches_string(filter, st->id);
21 }
22
23 /**
24 * Check if a chart can be sent to Prometheus
25 *
26 * @param instance an instance data structure.
27 * @param st a chart.
28 * @param filter a simple pattern to match against.
29 * @return Returns 1 if the chart can be sent, 0 otherwise.
30 */
31 inline int can_send_rrdset(struct instance *instance, RRDSET *st, SIMPLE_PATTERN *filter)
32 {
33 #ifdef NETDATA_INTERNAL_CHECKS
34 RRDHOST *host = st->rrdhost;
35 #endif
36
37 if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_EXPORTING_IGNORE)))
38 return 0;
39
40 if (filter) {
41 if (!is_matches_rrdset(instance, st, filter)) {
42 return 0;
43 }
44 } else if (unlikely(!rrdset_flag_check(st, RRDSET_FLAG_EXPORTING_SEND))) {
45 // we have not checked this chart
46 if (is_matches_rrdset(instance, st, instance->config.charts_pattern)) {
47 rrdset_flag_set(st, RRDSET_FLAG_EXPORTING_SEND);
48 } else {
49 rrdset_flag_set(st, RRDSET_FLAG_EXPORTING_IGNORE);
50 netdata_log_debug(
51 D_EXPORTING,
52 "EXPORTING: not sending chart '%s' of host '%s', because it is disabled for exporting.",
53 rrdset_id(st),
54 rrdhost_hostname(host));
55 return 0;
56 }
57 }
58
59 if (unlikely(!rrdset_is_available_for_exporting_and_alarms(st))) {
60 netdata_log_debug(
61 D_EXPORTING,
62 "EXPORTING: not sending chart '%s' of host '%s', because it is not available for exporting.",
63 rrdset_id(st),
64 rrdhost_hostname(host));
65 return 0;
66 }
67
68 if (unlikely(
69 st->rrd_memory_mode == RRD_DB_MODE_NONE &&
70 !(EXPORTING_OPTIONS_DATA_SOURCE(instance->config.options) == EXPORTING_SOURCE_DATA_AS_COLLECTED))) {
71 netdata_log_debug(
72 D_EXPORTING,
73 "EXPORTING: not sending chart '%s' of host '%s' because its memory mode is '%s' and the exporting connector requires database access.",
74 rrdset_id(st),
75 rrdhost_hostname(host),
76 rrd_memory_mode_name(host->rrd_memory_mode));
77 return 0;
78 }
79
80 return 1;
81 }
82
83 static struct prometheus_server {
84 const char *server;
85 uint32_t hash;
86 RRDHOST *host;
87 time_t last_access;
88 struct prometheus_server *next;
89 } *prometheus_server_root = NULL;
90
91 static netdata_mutex_t prometheus_server_root_mutex;
92
93 static void __attribute__((constructor)) init_mutex(void) {
94 netdata_mutex_init(&prometheus_server_root_mutex);
95 }
96
97 static void __attribute__((destructor)) destroy_mutex(void) {
98 netdata_mutex_destroy(&prometheus_server_root_mutex);
99 }
100 /**
101 * Clean server root local structure
102 */
103 void prometheus_clean_server_root()
104 {
105 netdata_mutex_lock(&prometheus_server_root_mutex);
106 if (prometheus_server_root) {
107 struct prometheus_server *ps;
108 for (ps = prometheus_server_root; ps; ) {
109 struct prometheus_server *current = ps;
110 ps = ps->next;
111 if(current->server)
112 freez((void *)current->server);
113
114 freez(current);
115 }
116 prometheus_server_root = NULL;
117 }
118 netdata_mutex_unlock(&prometheus_server_root_mutex);
119 }
120
121 /**
122 * Get the last time when a Prometheus server scraped the Netdata Prometheus exporter.
123 *
124 * @param server the name of the Prometheus server.
125 * @param host a data collecting host.
126 * @param now actual time.
127 * @return Returns the last time when the server accessed Netdata, or 0 if it is the first occurrence.
128 */
129 static inline time_t prometheus_server_last_access(const char *server, RRDHOST *host, time_t now)
130 {
131 #ifdef UNIT_TESTING
132 return 0;
133 #endif
134 uint32_t hash = simple_hash(server);
135
136 netdata_mutex_lock(&prometheus_server_root_mutex);
137
138 struct prometheus_server *ps;
139 for (ps = prometheus_server_root; ps; ps = ps->next) {
140 if (host == ps->host && hash == ps->hash && !strcmp(server, ps->server)) {
141 time_t last = ps->last_access;
142 ps->last_access = now;
143 netdata_mutex_unlock(&prometheus_server_root_mutex);
144 return last;
145 }
146 }
147
148 ps = callocz(1, sizeof(struct prometheus_server));
149 ps->server = strdupz(server);
150 ps->hash = hash;
151 ps->host = host;
152 ps->last_access = now;
153 ps->next = prometheus_server_root;
154 prometheus_server_root = ps;
155
156 netdata_mutex_unlock(&prometheus_server_root_mutex);
157 return 0;
158 }
159
160 /**
161 * Copy and sanitize name.
162 *
163 * @param d a destination string.
164 * @param s a source string.
165 * @param size the number of characters to copy.
166 * @return Returns the length of the copied string.
167 */
168 inline void prometheus_name_copy(char *d, const char *s, size_t size) {
169 prometheus_rrdlabels_sanitize_name(d, s, size);
170 }
171
172 /**
173 * Copy and sanitize label.
174 *
175 * @param d a destination string.
176 * @param s a source string.
177 * @param size the number of characters to copy.
178 * @return Returns the length of the copied string.
179 */
180 inline void prometheus_label_copy(char *d, const char *s, size_t size) {
181 // our label values are already compatible with prometheus label values
182 // so, just copy them
183 strncpyz(d, s, size - 1);
184 }
185
186 /**
187 * Copy and sanitize units.
188 *
189 * @param d a destination string.
190 * @param s a source string.
191 * @param usable the number of characters to copy.
192 * @param showoldunits set this flag to 1 to show old (before v1.12) units.
193 * @return Returns the destination string.
194 */
195 inline char *prometheus_units_copy(char *d, const char *s, size_t usable, int showoldunits)
196 {
197 const char *sorig = s;
198 char *ret = d;
199 size_t n;
200
201 // Fix for issue 5227
202 if (unlikely(showoldunits)) {
203 static struct {
204 const char *newunit;
205 uint32_t hash;
206 const char *oldunit;
207 } units[] = { { "KiB/s", 0, "kilobytes/s" },
208 { "MiB/s", 0, "MB/s" },
209 { "GiB/s", 0, "GB/s" },
210 { "KiB", 0, "KB" },
211 { "MiB", 0, "MB" },
212 { "GiB", 0, "GB" },
213 { "inodes", 0, "Inodes" },
214 { "percentage", 0, "percent" },
215 { "faults/s", 0, "page faults/s" },
216 { "KiB/operation", 0, "kilobytes per operation" },
217 { "milliseconds/operation", 0, "ms per operation" },
218 { NULL, 0, NULL } };
219 static int initialized = 0;
220 int i;
221
222 if (unlikely(!initialized)) {
223 for (i = 0; units[i].newunit; i++)
224 units[i].hash = simple_hash(units[i].newunit);
225 initialized = 1;
226 }
227
228 uint32_t hash = simple_hash(s);
229 for (i = 0; units[i].newunit; i++) {
230 if (unlikely(hash == units[i].hash && !strcmp(s, units[i].newunit))) {
231 // netdata_log_info("matched extension for filename '%s': '%s'", filename, last_dot);
232 s = units[i].oldunit;
233 sorig = s;
234 break;
235 }
236 }
237 }
238 *d++ = '_';
239 for (n = 1; *s && n < usable; d++, s++, n++) {
240 register char c = *s;
241
242 if (!isalnum(c))
243 *d = '_';
244 else
245 *d = c;
246 }
247
248 if (n == 2 && sorig[0] == '%') {
249 n = 0;
250 d = ret;
251 s = "_percent";
252 for (; *s && n < usable; n++)
253 *d++ = *s++;
254 } else if (n > 3 && sorig[n - 3] == '/' && sorig[n - 2] == 's') {
255 n = n - 2;
256 d -= 2;
257 s = "_persec";
258 for (; *s && n < usable; n++)
259 *d++ = *s++;
260 }
261
262 *d = '\0';
263
264 return ret;
265 }
266
267 /**
268 * Format host labels for the Prometheus exporter
269 *
270 * @param instance an instance data structure.
271 * @param host a data collecting host.
272 */
273
274 struct format_prometheus_label_callback {
275 struct instance *instance;
276 size_t count;
277 };
278
279 static int format_prometheus_label_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
280 struct format_prometheus_label_callback *d = (struct format_prometheus_label_callback *)data;
281
282 if (!should_send_label(d->instance, ls)) return 0;
283
284 char k[PROMETHEUS_ELEMENT_MAX + 1];
285 char v[PROMETHEUS_ELEMENT_MAX + 1];
286
287 prometheus_name_copy(k, name, sizeof(k));
288 prometheus_label_copy(v, value, sizeof(v));
289
290 if (*k && *v) {
291 if (d->count > 0) buffer_strcat(d->instance->labels_buffer, ",");
292 buffer_sprintf(d->instance->labels_buffer, "%s=\"%s\"", k, v);
293 d->count++;
294 }
295 return 1;
296 }
297
298 void format_host_labels_prometheus(struct instance *instance, RRDHOST *host)
299 {
300 if (unlikely(!sending_labels_configured(instance)))
301 return;
302
303 if (!instance->labels_buffer)
304 instance->labels_buffer = buffer_create(1024, &netdata_buffers_statistics.buffers_exporters);
305
306 struct format_prometheus_label_callback tmp = {
307 .instance = instance,
308 .count = 0
309 };
310 rrdlabels_walkthrough_read(host->rrdlabels, format_prometheus_label_callback, &tmp);
311 }
312
313 /**
314 * Format host labels for the Prometheus exporter
315 * We are using a structure instead a direct buffer to expand options quickly.
316 *
317 * @param data is the buffer used to add labels.
318 */
319
320 static int format_prometheus_chart_label_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
321 BUFFER *wb = data;
322
323 if (name[0] == '_' )
324 return 1;
325
326 char k[PROMETHEUS_ELEMENT_MAX + 1];
327 char v[PROMETHEUS_ELEMENT_MAX + 1];
328
329 prometheus_name_copy(k, name, sizeof(k));
330 prometheus_label_copy(v, value, sizeof(v));
331
332 if (*k && *v)
333 buffer_sprintf(wb, ",%s=\"%s\"", k, v);
334
335 return 1;
336 }
337
338 struct host_variables_callback_options {
339 RRDHOST *host;
340 BUFFER *wb;
341 BUFFER *plabels_buffer;
342 EXPORTING_OPTIONS exporting_options;
343 PROMETHEUS_OUTPUT_OPTIONS output_options;
344 const char *prefix;
345 const char *labels;
346 time_t now;
347 int host_header_printed;
348 char name[PROMETHEUS_VARIABLE_MAX + 1];
349 SIMPLE_PATTERN *pattern;
350 struct instance *instance;
351 STRING *prometheus;
352 PROM_CONTEXT_OPTIONS_JudyLSet *context_options;
353 };
354
355 /**
356 * Print host variables.
357 *
358 * @param rv a variable.
359 * @param data callback options.
360 * @return Returns 1 if the chart can be sent, 0 otherwise.
361 */
362 static int print_host_variables_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rv_ptr __maybe_unused, void *data) {
363 const RRDVAR_ACQUIRED *rv = (const RRDVAR_ACQUIRED *)item;
364
365 struct host_variables_callback_options *opts = data;
366
367 if (!opts->host_header_printed) {
368 opts->host_header_printed = 1;
369 }
370
371 NETDATA_DOUBLE value = rrdvar2number(rv);
372 if (isnan(value) || isinf(value)) {
373 return 0;
374 }
375
376 char *label_pre = "";
377 char *label_post = "";
378 if (opts->labels && *opts->labels) {
379 label_pre = "{";
380 label_post = "}";
381 }
382
383 prometheus_name_copy(opts->name, rrdvar_name(rv), sizeof(opts->name));
384
385 if (opts->output_options & PROMETHEUS_OUTPUT_TIMESTAMPS)
386 buffer_sprintf(
387 opts->wb,
388 "%s_%s%s%s%s " NETDATA_DOUBLE_FORMAT " %llu\n",
389 opts->prefix,
390 opts->name,
391 label_pre,
392 (opts->labels[0] == ',') ? &opts->labels[1] : opts->labels,
393 label_post,
394 value,
395 opts->now * 1000ULL);
396 else
397 buffer_sprintf(
398 opts->wb,
399 "%s_%s%s%s%s " NETDATA_DOUBLE_FORMAT "\n",
400 opts->prefix,
401 opts->name,
402 label_pre,
403 (opts->labels[0] == ',') ? &opts->labels[1] : opts->labels,
404 label_post,
405 value);
406
407 return 1;
408 }
409
410 struct gen_parameters {
411 const char *prefix;
412 const char *labels_prefix;
413 char *context;
414 char *suffix;
415
416 char *chart;
417 char *dimension;
418 char *family;
419 char *labels;
420
421 PROMETHEUS_OUTPUT_OPTIONS output_options;
422 RRDSET *st;
423 RRDDIM *rd;
424
425 const char *relation;
426 const char *type;
427 };
428
429 /**
430 * Write an as-collected help comment to a buffer.
431 *
432 * @param wb the buffer to write the comment to.
433 * @param context context name we are using
434 */
435 static inline void generate_as_collected_prom_help(BUFFER *wb,
436 const char *prefix,
437 char *context,
438 char *units,
439 char *suffix,
440 RRDSET *st)
441 {
442 buffer_sprintf(wb, "# HELP %s_%s%s%s %s\n", prefix, context, units, suffix, rrdset_title(st));
443 }
444
445 /**
446 * Write an as-collected help comment to a buffer.
447 *
448 * @param wb the buffer to write the comment to.
449 * @param context context name we are using
450 */
451 static inline void generate_as_collected_prom_type(BUFFER *wb,
452 const char *prefix,
453 char *context,
454 char *units,
455 char *suffix,
456 const char *type)
457 {
458 buffer_sprintf(wb, "# TYPE %s_%s%s%s %s\n", prefix, context, units, suffix, type);
459 }
460
461 /**
462 * Write an as-collected metric to a buffer.
463 *
464 * @param wb the buffer to write the metric to.
465 * @param p parameters for generating the metric string.
466 * @param homogeneous a flag for homogeneous charts.
467 * @param prometheus_collector a flag for metrics from prometheus collector.
468 * @param chart_labels the dictionary with chart labels
469 */
470 static void generate_as_collected_from_metric(BUFFER *wb,
471 struct gen_parameters *p,
472 int homogeneous,
473 int prometheus_collector,
474 RRDLABELS *chart_labels)
475 {
476 buffer_strcat(wb, p->prefix);
477 buffer_putc(wb, '_');
478 buffer_strcat(wb, p->context);
479
480 if (!homogeneous) {
481 buffer_putc(wb, '_');
482 buffer_strcat(wb, p->dimension);
483 }
484
485 buffer_sprintf(wb, "%s{%schart=\"%s\"", p->suffix, p->labels_prefix, p->chart);
486
487 if (homogeneous)
488 buffer_sprintf(wb, ",%sdimension=\"%s\"", p->labels_prefix, p->dimension);
489
490 buffer_sprintf(wb, ",%sfamily=\"%s\"", p->labels_prefix, p->family);
491
492 rrdlabels_walkthrough_read(chart_labels, format_prometheus_chart_label_callback, wb);
493
494 buffer_strcat(wb, p->labels);
495 buffer_putc(wb, '}');
496 buffer_putc(wb, ' ');
497
498 if (prometheus_collector || rrddim_is_float(p->rd))
499 buffer_print_netdata_double(wb,
500 rrddim_last_collected_as_double(p->rd) * (NETDATA_DOUBLE)p->rd->multiplier /
501 (NETDATA_DOUBLE)p->rd->divisor);
502 else
503 buffer_print_int64(wb, p->rd->collector.collected.i.last_collected_value);
504
505 if (p->output_options & PROMETHEUS_OUTPUT_TIMESTAMPS) {
506 buffer_putc(wb, ' ');
507 buffer_print_uint64(wb, timeval_msec(&p->rd->collector.last_collected_time));
508 }
509
510 buffer_putc(wb, '\n');
511 }
512
513 static void prometheus_print_os_info(
514 BUFFER *wb,
515 RRDHOST *host,
516 PROMETHEUS_OUTPUT_OPTIONS output_options)
517 {
518 FILE *fp;
519 char filename[FILENAME_MAX + 1];
520 char buf[BUFSIZ + 1];
521
522 snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/etc/os-release");
523 fp = fopen(filename, "r");
524 if (!fp) {
525 /* Fallback to lsb-release */
526 snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/etc/lsb-release");
527 fp = fopen(filename, "r");
528 }
529 if (!fp) {
530 return;
531 }
532
533 buffer_sprintf(wb, "netdata_os_info{instance=\"%s\"", rrdhost_hostname(host));
534
535 while (fgets(buf, BUFSIZ, fp)) {
536 char *in, *sanitized;
537 char *key, *val;
538 int in_val_part = 0;
539
540 /* sanitize the line */
541 sanitized = in = buf;
542 in_val_part = 0;
543 while (*in && *in != '\n') {
544 if (!in_val_part) {
545 /* Only accepts alphabetic characters and '_'
546 * in key part */
547 if (isalpha((uint8_t)*in) || *in == '_') {
548 *(sanitized++) = tolower((uint8_t)*in);
549 } else if (*in == '=') {
550 in_val_part = 1;
551 *(sanitized++) = '=';
552 }
553 } else {
554 /* Don't accept special characters in
555 * value part */
556 switch (*in) {
557 case '"':
558 case '\'':
559 case '\r':
560 case '\t':
561 break;
562 default:
563 if (isprint((uint8_t)*in)) {
564 *(sanitized++) = *in;
565 }
566 }
567 }
568 in++;
569 }
570 /* Terminate the string */
571 *(sanitized++) = '\0';
572
573 /* Split key/val */
574 key = buf;
575 val = strchr(buf, '=');
576
577 /* If we have a key/value pair, add it as a label */
578 if (val) {
579 *val = '\0';
580 val++;
581 buffer_sprintf(wb, ",%s=\"%s\"", key, val);
582 }
583 }
584
585 /* Finish the line */
586 if (output_options & PROMETHEUS_OUTPUT_TIMESTAMPS)
587 buffer_sprintf(wb, "} 1 %llu\n", now_realtime_usec() / USEC_PER_MS);
588 else
589 buffer_sprintf(wb, "} 1\n");
590
591 fclose(fp);
592 }
593
594 /**
595 * RRDSET to JSON
596 *
597 * From RRDSET extract content necessary to write JSON output.
598 *
599 * @param st netdata chart structure
600 * @param data structure with necessary data and to build expected result.
601 *
602 * @return I returns 1 when content was used and 0 otherwise.
603 */
604 static int prometheus_rrdset_to_json(RRDSET *st, void *data)
605 {
606 struct host_variables_callback_options *opts = data;
607
608 if (likely(can_send_rrdset(opts->instance, st, opts->pattern))) {
609 PROMETHEUS_OUTPUT_OPTIONS output_options = opts->output_options;
610 BUFFER *wb = opts->wb;
611 const char *prefix = opts->prefix;
612
613 BUFFER *plabels_buffer = opts->plabels_buffer;
614 const char *plabels_prefix = opts->instance->config.label_prefix;
615
616 STRING *prometheus = opts->prometheus;
617
618 char chart[PROMETHEUS_ELEMENT_MAX + 1];
619 char context[PROMETHEUS_ELEMENT_MAX + 1];
620 char family[PROMETHEUS_ELEMENT_MAX + 1];
621 char units[PROMETHEUS_ELEMENT_MAX + 1] = "";
622
623 prometheus_label_copy(chart,
624 (output_options & PROMETHEUS_OUTPUT_NAMES && st->name) ?
625 rrdset_name(st) : rrdset_id(st), sizeof(chart));
626 prometheus_label_copy(family, rrdset_family(st), sizeof(family));
627 prometheus_name_copy(context, rrdset_context(st), sizeof(context));
628
629 if(opts->output_options & PROMETHEUS_OUTPUT_HELP_TYPE) {
630 // we do not want to print HELP and TYPE for the same context twice
631 STRING *context_id = string_strdupz(context);
632 PROMETHEUS_OUTPUT_OPTIONS ctx_opts = PROM_CONTEXT_OPTIONS_GET(opts->context_options, (Word_t)context_id);
633 if (!(ctx_opts & PROMETHEUS_OUTPUT_HELP_TYPE)) {
634 // it is not printed for this context yet
635 ctx_opts = opts->output_options;
636 PROM_CONTEXT_OPTIONS_SET(opts->context_options, (Word_t)context_id, ctx_opts);
637 }
638 else {
639 // we have printed HELP and TYPE for this context already
640 opts->output_options &= ~PROMETHEUS_OUTPUT_HELP_TYPE;
641 string_freez(context_id);
642 }
643 }
644
645 int as_collected = (EXPORTING_OPTIONS_DATA_SOURCE(opts->exporting_options)
646 == EXPORTING_SOURCE_DATA_AS_COLLECTED);
647 int homogeneous = 1;
648 int prometheus_collector = 0;
649 RRDSET_FLAGS flags = rrdset_flag_get(st);
650 if (as_collected) {
651 if (flags & RRDSET_FLAG_HOMOGENEOUS_CHECK)
652 rrdset_update_heterogeneous_flag(st);
653
654 if (flags & RRDSET_FLAG_HETEROGENEOUS)
655 homogeneous = 0;
656
657 if (st->module_name == prometheus)
658 prometheus_collector = 1;
659 }
660 else {
661 if (EXPORTING_OPTIONS_DATA_SOURCE(opts->exporting_options) == EXPORTING_SOURCE_DATA_AVERAGE &&
662 !(output_options & PROMETHEUS_OUTPUT_HIDEUNITS))
663 prometheus_units_copy(units,
664 rrdset_units(st),
665 PROMETHEUS_ELEMENT_MAX,
666 output_options & PROMETHEUS_OUTPUT_OLDUNITS);
667 }
668
669 // for each dimension
670 RRDDIM *rd;
671 rrddim_foreach_read(rd, st) {
672
673 if (rd->collector.counter && !rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)) {
674 char dimension[PROMETHEUS_ELEMENT_MAX + 1];
675 char *suffix = "";
676
677 struct gen_parameters p;
678 p.prefix = prefix;
679 p.labels_prefix = plabels_prefix;
680 p.context = context;
681 p.suffix = suffix;
682 p.chart = chart;
683 p.dimension = dimension;
684 p.family = family;
685 p.labels = (char *)opts->labels;
686 p.output_options = output_options;
687 p.st = st;
688 p.rd = rd;
689
690 if (as_collected) {
691 // we need as-collected / raw data
692
693 if (unlikely(rd->collector.last_collected_time.tv_sec < opts->instance->after))
694 continue;
695
696 p.type = "gauge";
697 p.relation = "gives";
698 if (rd->algorithm == RRD_ALGORITHM_INCREMENTAL ||
699 rd->algorithm == RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL) {
700 p.type = "counter";
701 p.relation = "delta gives";
702 if (!prometheus_collector)
703 p.suffix = "_total";
704 }
705
706 if (opts->output_options & PROMETHEUS_OUTPUT_HELP_TYPE) {
707 generate_as_collected_prom_help(wb, prefix, context, units, p.suffix, st);
708 generate_as_collected_prom_type(wb, prefix, context, units, p.suffix, p.type);
709 opts->output_options &= ~PROMETHEUS_OUTPUT_HELP_TYPE;
710 }
711
712 if (homogeneous) {
713 // all the dimensions of the chart, has the same algorithm, multiplier and divisor
714 // we add all dimensions as labels
715
716 prometheus_label_copy(
717 dimension,
718 (output_options & PROMETHEUS_OUTPUT_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
719 sizeof(dimension));
720 }
721 else {
722 // the dimensions of the chart, do not have the same algorithm, multiplier or divisor
723 // we create a metric per dimension
724
725 prometheus_name_copy(
726 dimension,
727 (output_options & PROMETHEUS_OUTPUT_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
728 sizeof(dimension));
729 }
730 generate_as_collected_from_metric(wb, &p, homogeneous, prometheus_collector, st->rrdlabels);
731 }
732 else {
733 // we need average or sum of the data
734
735 time_t last_time = opts->instance->before;
736 NETDATA_DOUBLE value = exporting_calculate_value_from_stored_data(opts->instance, rd, &last_time);
737
738 if (!isnan(value) && !isinf(value)) {
739 if (EXPORTING_OPTIONS_DATA_SOURCE(opts->exporting_options) == EXPORTING_SOURCE_DATA_AVERAGE)
740 suffix = "_average";
741 else if (EXPORTING_OPTIONS_DATA_SOURCE(opts->exporting_options)
742 == EXPORTING_SOURCE_DATA_SUM)
743 suffix = "_sum";
744
745 prometheus_label_copy(
746 dimension,
747 (output_options & PROMETHEUS_OUTPUT_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
748 sizeof(dimension));
749
750 if (opts->output_options & PROMETHEUS_OUTPUT_HELP_TYPE) {
751 generate_as_collected_prom_help(wb, prefix, context, units, suffix, st);
752 generate_as_collected_prom_type(wb, prefix, context, units, suffix, "gauge");
753 opts->output_options &= ~PROMETHEUS_OUTPUT_HELP_TYPE;
754 }
755
756 buffer_flush(plabels_buffer);
757 buffer_sprintf(plabels_buffer,
758 "%1$schart=\"%2$s\",%1$sdimension=\"%3$s\",%1$sfamily=\"%4$s\"",
759 plabels_prefix,
760 chart,
761 dimension,
762 family);
763 rrdlabels_walkthrough_read(st->rrdlabels,
764 format_prometheus_chart_label_callback,
765 plabels_buffer);
766
767 if (output_options & PROMETHEUS_OUTPUT_TIMESTAMPS)
768 buffer_sprintf(wb,
769 "%s_%s%s%s{%s%s} " NETDATA_DOUBLE_FORMAT " %llu\n",
770 prefix,
771 context,
772 units,
773 suffix,
774 buffer_tostring(plabels_buffer),
775 opts->labels,
776 value,
777 last_time * MSEC_PER_SEC);
778 else
779 buffer_sprintf(wb, "%s_%s%s%s{%s%s} " NETDATA_DOUBLE_FORMAT "\n",
780 prefix,
781 context,
782 units,
783 suffix,
784 buffer_tostring(plabels_buffer),
785 opts->labels,
786 value);
787 }
788 }
789 }
790 }
791 rrddim_foreach_done(rd);
792
793 return 1;
794 }
795
796 return 0;
797 }
798
799 /**
800 * RRDCONTEXT callback
801 *
802 * Callback used to parse dictionary
803 *
804 * @param item the dictionary structure
805 * @param value unused element
806 * @param data structure used to store data.
807 *
808 * @return It always returns HTTP_RESP_OK
809 */
810 static inline int prometheus_rrdcontext_callback(const DICTIONARY_ITEM *item, void *value, void *data)
811 {
812 const char *context_name = dictionary_acquired_item_name(item);
813 struct host_variables_callback_options *opts = data;
814 (void)value;
815
816 opts->output_options |= PROMETHEUS_OUTPUT_HELP_TYPE;
817 (void)rrdcontext_foreach_instance_with_rrdset_in_context(opts->host, context_name, prometheus_rrdset_to_json, data);
818
819 return HTTP_RESP_OK;
820 }
821
822
823
824 /**
825 * Write metrics in Prometheus format to a buffer.
826 *
827 * @param instance an instance data structure.
828 * @param host a data collecting host.
829 * @param filter_string a simple pattern filter.
830 * @param wb the buffer to fill with metrics.
831 * @param prefix a prefix for every metric.
832 * @param exporting_options options to configure what data is exported.
833 * @param allhosts set to 1 if host instance should be in the output for tags.
834 * @param output_options options to configure the format of the output.
835 */
836 static void rrd_stats_api_v1_charts_allmetrics_prometheus(
837 struct instance *instance,
838 RRDHOST *host,
839 const char *filter_string,
840 BUFFER *wb,
841 const char *prefix,
842 EXPORTING_OPTIONS exporting_options,
843 int allhosts,
844 PROMETHEUS_OUTPUT_OPTIONS output_options,
845 PROM_CONTEXT_OPTIONS_JudyLSet *context_options)
846 {
847 SIMPLE_PATTERN *filter = simple_pattern_create(filter_string, NULL, SIMPLE_PATTERN_EXACT, true);
848
849 char hostname[PROMETHEUS_ELEMENT_MAX + 1];
850 prometheus_label_copy(hostname, rrdhost_hostname(host), sizeof(hostname));
851
852 format_host_labels_prometheus(instance, host);
853
854 buffer_sprintf(
855 wb,
856 "netdata_info{instance=\"%s\",application=\"%s\",version=\"%s\"",
857 hostname,
858 rrdhost_program_name(host),
859 rrdhost_program_version(host));
860
861 if (instance->labels_buffer && *buffer_tostring(instance->labels_buffer)) {
862 buffer_sprintf(wb, ",%s", buffer_tostring(instance->labels_buffer));
863 }
864
865 if (output_options & PROMETHEUS_OUTPUT_TIMESTAMPS)
866 buffer_sprintf(wb, "} 1 %llu\n", now_realtime_usec() / USEC_PER_MS);
867 else
868 buffer_sprintf(wb, "} 1\n");
869
870 char labels[PROMETHEUS_LABELS_MAX + 1] = "";
871 if (allhosts) {
872 snprintfz(labels, PROMETHEUS_LABELS_MAX, ",%sinstance=\"%s\"", instance->config.label_prefix, hostname);
873 }
874
875 if (instance->labels_buffer)
876 buffer_flush(instance->labels_buffer);
877
878 if (instance->config.options & EXPORTING_OPTION_SEND_AUTOMATIC_LABELS)
879 prometheus_print_os_info(wb, host, output_options);
880
881
882 BUFFER *plabels_buffer = buffer_create(0, NULL);
883
884 struct host_variables_callback_options opts = {
885 .host = host,
886 .wb = wb,
887 .plabels_buffer = plabels_buffer,
888 .labels = labels, // FIX: very misleading name and poor implementation of adding the "instance" label
889 .exporting_options = exporting_options,
890 .output_options = output_options,
891 .prefix = prefix,
892 .now = now_realtime_sec(),
893 .host_header_printed = 0,
894 .pattern = filter,
895 .instance = instance,
896 .prometheus = string_strdupz("prometheus"),
897 .context_options = context_options,
898 };
899
900 // send custom variables set for the host
901 if (output_options & PROMETHEUS_OUTPUT_VARIABLES) {
902 rrdvar_walkthrough_read(host->rrdvars, print_host_variables_callback, &opts);
903 }
904
905 // for each context
906 if (!host->rrdctx.contexts) {
907 netdata_log_error("%s(): request for host '%s' that does not have rrdcontexts initialized.", __FUNCTION__, rrdhost_hostname(host));
908 goto allmetrics_cleanup;
909 }
910
911 dictionary_walkthrough_read(host->rrdctx.contexts, prometheus_rrdcontext_callback, &opts);
912
913 allmetrics_cleanup:
914 simple_pattern_free(filter);
915 buffer_free(plabels_buffer);
916 string_freez(opts.prometheus);
917 }
918
919 /**
920 * Get the last time time when a server accessed Netdata. Write information about an API request to a buffer.
921 *
922 * @param instance an instance data structure.
923 * @param host a data collecting host.
924 * @param wb the buffer to write to.
925 * @param exporting_options options to configure what data is exported.
926 * @param server the name of a Prometheus server..
927 * @param now actual time.
928 * @param output_options options to configure the format of the output.
929 * @return Returns the last time when the server accessed Netdata.
930 */
931 static inline time_t prometheus_preparation(
932 struct instance *instance,
933 RRDHOST *host,
934 const char *server,
935 time_t now)
936 {
937 #ifndef UNIT_TESTING
938 analytics_log_prometheus();
939 #endif
940 if (!server || !*server)
941 server = "default";
942
943 time_t after = prometheus_server_last_access(server, host, now);
944
945 if (!after) {
946 after = now - instance->config.update_every;
947 }
948
949 if (after > now) {
950 // oops! this should never happen
951 after = now - instance->config.update_every;
952 }
953
954 return after;
955 }
956
957 /**
958 * Write metrics and auxiliary information for one host to a buffer.
959 *
960 * @param host a data collecting host.
961 * @param filter_string a simple pattern filter.
962 * @param wb the buffer to write to.
963 * @param server the name of a Prometheus server.
964 * @param prefix a prefix for every metric.
965 * @param exporting_options options to configure what data is exported.
966 * @param output_options options to configure the format of the output.
967 */
968 void rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(
969 RRDHOST *host,
970 const char *filter_string,
971 BUFFER *wb,
972 const char *server,
973 const char *prefix,
974 EXPORTING_OPTIONS exporting_options,
975 PROMETHEUS_OUTPUT_OPTIONS output_options)
976 {
977 if (unlikely(!prometheus_exporter_instance || !prometheus_exporter_instance->config.initialized))
978 return;
979
980 prometheus_exporter_instance->before = now_realtime_sec();
981
982 // we start at the point we had stopped before
983 prometheus_exporter_instance->after = prometheus_preparation(
984 prometheus_exporter_instance,
985 host,
986 server,
987 prometheus_exporter_instance->before);
988
989 PROM_CONTEXT_OPTIONS_JudyLSet context_options;
990 PROM_CONTEXT_OPTIONS_INIT(&context_options);
991
992 rrd_stats_api_v1_charts_allmetrics_prometheus(
993 prometheus_exporter_instance, host, filter_string, wb, prefix, exporting_options, 0, output_options, &context_options);
994
995 PROM_CONTEXT_OPTIONS_FREE(&context_options, PROM_CONTEXT_OPTIONS_free_cb, NULL);
996 }
997
998 /**
999 * Write metrics and auxiliary information for all hosts to a buffer.
1000 *
1001 * @param host a data collecting host.
1002 * @param filter_string a simple pattern filter.
1003 * @param wb the buffer to write to.
1004 * @param server the name of a Prometheus server.
1005 * @param prefix a prefix for every metric.
1006 * @param exporting_options options to configure what data is exported.
1007 * @param output_options options to configure the format of the output.
1008 */
1009 void rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts(
1010 RRDHOST *host,
1011 const char *filter_string,
1012 BUFFER *wb,
1013 const char *server,
1014 const char *prefix,
1015 EXPORTING_OPTIONS exporting_options,
1016 PROMETHEUS_OUTPUT_OPTIONS output_options)
1017 {
1018 if (unlikely(!prometheus_exporter_instance || !prometheus_exporter_instance->config.initialized))
1019 return;
1020
1021 prometheus_exporter_instance->before = now_realtime_sec();
1022
1023 // we start at the point we had stopped before
1024 prometheus_exporter_instance->after = prometheus_preparation(
1025 prometheus_exporter_instance,
1026 host,
1027 server,
1028 prometheus_exporter_instance->before);
1029
1030 PROM_CONTEXT_OPTIONS_JudyLSet context_options;
1031 PROM_CONTEXT_OPTIONS_INIT(&context_options);
1032
1033 dfe_start_reentrant(rrdhost_root_index, host)
1034 {
1035 rrd_stats_api_v1_charts_allmetrics_prometheus(
1036 prometheus_exporter_instance, host, filter_string, wb, prefix, exporting_options, 1, output_options, &context_options);
1037 }
1038 dfe_done(host);
1039
1040 PROM_CONTEXT_OPTIONS_FREE(&context_options, PROM_CONTEXT_OPTIONS_free_cb, NULL);
1041 }