@cryptotaxi247 / netdata-1 / commits / 9e85d52a3

fix memory leak on prometheus exporter and code cleanup (#15929)

Costa Tsaousis committed Sep 8, 2023 at 18:49 UTC 9e85d52a393c830cdd72da4b29ff40598717bac0
1 file changed +14 -37
exporting/prometheus/prometheus.c
+14 -37
@@ -330,16 +330,11 @@ void format_host_labels_prometheus(struct instance *instance, RRDHOST *host)
330 * Format host labels for the Prometheus exporter
331 * We are using a structure instead a direct buffer to expand options quickly.
332 *
333 - * @param labels_buffer is the buffer used to add labels.
333 + * @param data is the buffer used to add labels.
334 */
335
336 -struct format_prometheus_chart_label_callback {
337 - BUFFER *labels_buffer;
338 - const char *labels_prefix;
339 -};
340 -
336 static int format_prometheus_chart_label_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
342 - struct format_prometheus_chart_label_callback *d = (struct format_prometheus_chart_label_callback *)data;
337 + BUFFER *wb = data;
338
339 if (name[0] == '_' )
340 return 1;
@@ -350,26 +345,10 @@ static int format_prometheus_chart_label_callback(const char *name, const char *
345 prometheus_name_copy(k, name, PROMETHEUS_ELEMENT_MAX);
346 prometheus_label_copy(v, value, PROMETHEUS_ELEMENT_MAX);
347
353 - if (*k && *v) {
354 - buffer_sprintf(d->labels_buffer, ",%s=\"%s\"", k, v);
355 - }
356 - return 1;
357 -}
348 + if (*k && *v)
349 + buffer_sprintf(wb, ",%s=\"%s\"", k, v);
350
359 -void format_chart_labels_prometheus(struct format_prometheus_chart_label_callback *plabel,
360 - const char *chart,
361 - const char *family,
362 - const char *dim,
363 - RRDSET *st)
364 -{
365 - if (likely(plabel->labels_buffer))
366 - buffer_reset(plabel->labels_buffer);
367 - else {
368 - plabel->labels_buffer = buffer_create(1024, NULL);
369 - }
370 - buffer_sprintf(plabel->labels_buffer, "%1$schart=\"%2$s\",%1$sdimension=\"%3$s\",%1$sfamily=\"%4$s\"", plabel->labels_prefix, chart, dim, family);
371 -
372 - rrdlabels_walkthrough_read(st->rrdlabels, format_prometheus_chart_label_callback, plabel);
351 + return 1;
352 }
353
354 struct host_variables_callback_options {
@@ -517,9 +496,6 @@ static void generate_as_collected_prom_metric(BUFFER *wb,
496 int prometheus_collector,
497 RRDLABELS *chart_labels)
498 {
520 - struct format_prometheus_chart_label_callback local_label;
521 - local_label.labels_buffer = wb;
522 -
499 buffer_sprintf(wb, "%s_%s", p->prefix, p->context);
500
501 if (!homogeneous)
@@ -532,7 +508,7 @@ static void generate_as_collected_prom_metric(BUFFER *wb,
508
509 buffer_sprintf(wb, ",%sfamily=\"%s\"", p->labels_prefix, p->family);
510
535 - rrdlabels_walkthrough_read(chart_labels, format_prometheus_chart_label_callback, &local_label);
511 + rrdlabels_walkthrough_read(chart_labels, format_prometheus_chart_label_callback, wb);
512
513 buffer_sprintf(wb, "%s} ", p->labels);
514
@@ -624,10 +600,8 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
600 // for each chart
601 RRDSET *st;
602
627 - struct format_prometheus_chart_label_callback plabels = {
628 - .labels_buffer = NULL,
629 - .labels_prefix = instance->config.label_prefix,
630 - };
603 + BUFFER *plabels_buffer = buffer_create(0, NULL);
604 + const char *plabels_prefix = instance->config.label_prefix;
605
606 STRING *prometheus = string_strdupz("prometheus");
607 rrdset_foreach_read(st, host) {
@@ -763,7 +737,9 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
737 (output_options & PROMETHEUS_OUTPUT_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
738 PROMETHEUS_ELEMENT_MAX);
739
766 - format_chart_labels_prometheus(&plabels, chart, family, dimension, st);
740 + buffer_flush(plabels_buffer);
741 + buffer_sprintf(plabels_buffer, "%1$schart=\"%2$s\",%1$sdimension=\"%3$s\",%1$sfamily=\"%4$s\"", plabels_prefix, chart, dimension, family);
742 + rrdlabels_walkthrough_read(st->rrdlabels, format_prometheus_chart_label_callback, plabels_buffer);
743
744 if (unlikely(output_options & PROMETHEUS_OUTPUT_HELP))
745 buffer_sprintf(
@@ -790,7 +766,7 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
766 context,
767 units,
768 suffix,
793 - buffer_tostring(plabels.labels_buffer),
769 + buffer_tostring(plabels_buffer),
770 labels,
771 value,
772 last_time * MSEC_PER_SEC);
@@ -803,7 +779,7 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
779 context,
780 units,
781 suffix,
806 - buffer_tostring(plabels.labels_buffer),
782 + buffer_tostring(plabels_buffer),
783 labels,
784 value);
785 }
@@ -815,6 +791,7 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
791 }
792 rrdset_foreach_done(st);
793
794 + buffer_free(plabels_buffer);
795 simple_pattern_free(filter);
796 }
797