Skip to content

Commit

Permalink
fix memory leak on prometheus exporter and code cleanup (netdata#15929)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9e85d52)
  • Loading branch information
ktsaou authored and stelfrag committed Sep 8, 2023
1 parent 26ed05b commit 12eb42b
Showing 1 changed file with 14 additions and 37 deletions.
51 changes: 14 additions & 37 deletions exporting/prometheus/prometheus.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,16 +330,11 @@ void format_host_labels_prometheus(struct instance *instance, RRDHOST *host)
* Format host labels for the Prometheus exporter
* We are using a structure instead a direct buffer to expand options quickly.
*
* @param labels_buffer is the buffer used to add labels.
* @param data is the buffer used to add labels.
*/

struct format_prometheus_chart_label_callback {
BUFFER *labels_buffer;
const char *labels_prefix;
};

static int format_prometheus_chart_label_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
struct format_prometheus_chart_label_callback *d = (struct format_prometheus_chart_label_callback *)data;
BUFFER *wb = data;

(void)ls;

Expand All @@ -352,26 +347,10 @@ static int format_prometheus_chart_label_callback(const char *name, const char *
prometheus_name_copy(k, name, PROMETHEUS_ELEMENT_MAX);
prometheus_label_copy(v, value, PROMETHEUS_ELEMENT_MAX);

if (*k && *v) {
buffer_sprintf(d->labels_buffer, ",%s=\"%s\"", k, v);
}
return 1;
}
if (*k && *v)
buffer_sprintf(wb, ",%s=\"%s\"", k, v);

void format_chart_labels_prometheus(struct format_prometheus_chart_label_callback *plabel,
const char *chart,
const char *family,
const char *dim,
RRDSET *st)
{
if (likely(plabel->labels_buffer))
buffer_reset(plabel->labels_buffer);
else {
plabel->labels_buffer = buffer_create(1024, NULL);
}
buffer_sprintf(plabel->labels_buffer, "%1$schart=\"%2$s\",%1$sdimension=\"%3$s\",%1$sfamily=\"%4$s\"", plabel->labels_prefix, chart, dim, family);

rrdlabels_walkthrough_read(st->rrdlabels, format_prometheus_chart_label_callback, plabel);
return 1;
}

struct host_variables_callback_options {
Expand Down Expand Up @@ -519,9 +498,6 @@ static void generate_as_collected_prom_metric(BUFFER *wb,
int prometheus_collector,
DICTIONARY *chart_labels)
{
struct format_prometheus_chart_label_callback local_label;
local_label.labels_buffer = wb;

buffer_sprintf(wb, "%s_%s", p->prefix, p->context);

if (!homogeneous)
Expand All @@ -534,7 +510,7 @@ static void generate_as_collected_prom_metric(BUFFER *wb,

buffer_sprintf(wb, ",%sfamily=\"%s\"", p->labels_prefix, p->family);

rrdlabels_walkthrough_read(chart_labels, format_prometheus_chart_label_callback, &local_label);
rrdlabels_walkthrough_read(chart_labels, format_prometheus_chart_label_callback, wb);

buffer_sprintf(wb, "%s} ", p->labels);

Expand Down Expand Up @@ -626,10 +602,8 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
// for each chart
RRDSET *st;

struct format_prometheus_chart_label_callback plabels = {
.labels_buffer = NULL,
.labels_prefix = instance->config.label_prefix,
};
BUFFER *plabels_buffer = buffer_create(0, NULL);
const char *plabels_prefix = instance->config.label_prefix;

STRING *prometheus = string_strdupz("prometheus");
rrdset_foreach_read(st, host) {
Expand Down Expand Up @@ -765,7 +739,9 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
(output_options & PROMETHEUS_OUTPUT_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd),
PROMETHEUS_ELEMENT_MAX);

format_chart_labels_prometheus(&plabels, chart, family, dimension, st);
buffer_flush(plabels_buffer);
buffer_sprintf(plabels_buffer, "%1$schart=\"%2$s\",%1$sdimension=\"%3$s\",%1$sfamily=\"%4$s\"", plabels_prefix, chart, dimension, family);
rrdlabels_walkthrough_read(st->rrdlabels, format_prometheus_chart_label_callback, plabels_buffer);

if (unlikely(output_options & PROMETHEUS_OUTPUT_HELP))
buffer_sprintf(
Expand All @@ -792,7 +768,7 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
context,
units,
suffix,
buffer_tostring(plabels.labels_buffer),
buffer_tostring(plabels_buffer),
labels,
value,
last_time * MSEC_PER_SEC);
Expand All @@ -805,7 +781,7 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
context,
units,
suffix,
buffer_tostring(plabels.labels_buffer),
buffer_tostring(plabels_buffer),
labels,
value);
}
Expand All @@ -817,6 +793,7 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
}
rrdset_foreach_done(st);

buffer_free(plabels_buffer);
simple_pattern_free(filter);
}

Expand Down

0 comments on commit 12eb42b

Please sign in to comment.