summaryrefslogtreecommitdiffstats
path: root/exporting/prometheus
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2022-08-12 07:26:17 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2022-08-12 07:26:17 +0000
commit7877a98bd9c00db5e81dd2f8c734cba2bab20be7 (patch)
treed18b767250f7c7ced9b8abe2ece784ac1fe24d3e /exporting/prometheus
parentReleasing debian version 1.35.1-2. (diff)
downloadnetdata-7877a98bd9c00db5e81dd2f8c734cba2bab20be7.tar.xz
netdata-7877a98bd9c00db5e81dd2f8c734cba2bab20be7.zip
Merging upstream version 1.36.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'exporting/prometheus')
-rw-r--r--exporting/prometheus/prometheus.c153
-rw-r--r--exporting/prometheus/remote_write/remote_write.c88
-rw-r--r--exporting/prometheus/remote_write/remote_write.h7
-rw-r--r--exporting/prometheus/remote_write/remote_write_request.cc32
-rw-r--r--exporting/prometheus/remote_write/remote_write_request.h3
5 files changed, 169 insertions, 114 deletions
diff --git a/exporting/prometheus/prometheus.c b/exporting/prometheus/prometheus.c
index c7f3f1d38..7d632164f 100644
--- a/exporting/prometheus/prometheus.c
+++ b/exporting/prometheus/prometheus.c
@@ -290,35 +290,44 @@ inline char *prometheus_units_copy(char *d, const char *s, size_t usable, int sh
* @param instance an instance data structure.
* @param host a data collecting host.
*/
-void format_host_labels_prometheus(struct instance *instance, RRDHOST *host)
-{
- if (unlikely(!sending_labels_configured(instance)))
- return;
- if (!instance->labels)
- instance->labels = buffer_create(1024);
+struct format_prometheus_label_callback {
+ struct instance *instance;
+ size_t count;
+};
- int count = 0;
- rrdhost_check_rdlock(host);
- netdata_rwlock_rdlock(&host->labels.labels_rwlock);
- for (struct label *label = host->labels.head; label; label = label->next) {
- if (!should_send_label(instance, label))
- continue;
+static int format_prometheus_label_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
+ struct format_prometheus_label_callback *d = (struct format_prometheus_label_callback *)data;
- char key[PROMETHEUS_ELEMENT_MAX + 1];
- char value[PROMETHEUS_ELEMENT_MAX + 1];
+ if (!should_send_label(d->instance, ls)) return 0;
- prometheus_name_copy(key, label->key, PROMETHEUS_ELEMENT_MAX);
- prometheus_label_copy(value, label->value, PROMETHEUS_ELEMENT_MAX);
+ char k[PROMETHEUS_ELEMENT_MAX + 1];
+ char v[PROMETHEUS_ELEMENT_MAX + 1];
- if (*key && *value) {
- if (count > 0)
- buffer_strcat(instance->labels, ",");
- buffer_sprintf(instance->labels, "%s=\"%s\"", key, value);
- count++;
- }
+ prometheus_name_copy(k, name, PROMETHEUS_ELEMENT_MAX);
+ prometheus_label_copy(v, value, PROMETHEUS_ELEMENT_MAX);
+
+ if (*k && *v) {
+ if (d->count > 0) buffer_strcat(d->instance->labels_buffer, ",");
+ buffer_sprintf(d->instance->labels_buffer, "%s=\"%s\"", k, v);
+ d->count++;
}
- netdata_rwlock_unlock(&host->labels.labels_rwlock);
+ return 1;
+}
+
+void format_host_labels_prometheus(struct instance *instance, RRDHOST *host)
+{
+ if (unlikely(!sending_labels_configured(instance)))
+ return;
+
+ if (!instance->labels_buffer)
+ instance->labels_buffer = buffer_create(1024);
+
+ struct format_prometheus_label_callback tmp = {
+ .instance = instance,
+ .count = 0
+ };
+ rrdlabels_walkthrough_read(host->host_labels, format_prometheus_label_callback, &tmp);
}
struct host_variables_callback_options {
@@ -353,7 +362,7 @@ static int print_host_variables(RRDVAR *rv, void *data)
}
}
- calculated_number value = rrdvar2number(rv);
+ NETDATA_DOUBLE value = rrdvar2number(rv);
if (isnan(value) || isinf(value)) {
if (opts->output_options & PROMETHEUS_OUTPUT_HELP)
buffer_sprintf(
@@ -374,7 +383,7 @@ static int print_host_variables(RRDVAR *rv, void *data)
if (opts->output_options & PROMETHEUS_OUTPUT_TIMESTAMPS)
buffer_sprintf(
opts->wb,
- "%s_%s%s%s%s " CALCULATED_NUMBER_FORMAT " %llu\n",
+ "%s_%s%s%s%s " NETDATA_DOUBLE_FORMAT " %llu\n",
opts->prefix,
opts->name,
label_pre,
@@ -385,7 +394,7 @@ static int print_host_variables(RRDVAR *rv, void *data)
else
buffer_sprintf(
opts->wb,
- "%s_%s%s%s%s " CALCULATED_NUMBER_FORMAT "\n",
+ "%s_%s%s%s%s " NETDATA_DOUBLE_FORMAT "\n",
opts->prefix,
opts->name,
label_pre,
@@ -474,9 +483,9 @@ static void generate_as_collected_prom_metric(BUFFER *wb, struct gen_parameters
if (prometheus_collector)
buffer_sprintf(
wb,
- CALCULATED_NUMBER_FORMAT,
- (calculated_number)p->rd->last_collected_value * (calculated_number)p->rd->multiplier /
- (calculated_number)p->rd->divisor);
+ NETDATA_DOUBLE_FORMAT,
+ (NETDATA_DOUBLE)p->rd->last_collected_value * (NETDATA_DOUBLE)p->rd->multiplier /
+ (NETDATA_DOUBLE)p->rd->divisor);
else
buffer_sprintf(wb, COLLECTED_NUMBER_FORMAT, p->rd->last_collected_value);
@@ -516,77 +525,29 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
format_host_labels_prometheus(instance, host);
+ buffer_sprintf(
+ wb,
+ "netdata_info{instance=\"%s\",application=\"%s\",version=\"%s\"",
+ hostname,
+ host->program_name,
+ host->program_version);
+
+ if (instance->labels_buffer && *buffer_tostring(instance->labels_buffer)) {
+ buffer_sprintf(wb, ",%s", buffer_tostring(instance->labels_buffer));
+ }
+
if (output_options & PROMETHEUS_OUTPUT_TIMESTAMPS)
- buffer_sprintf(
- wb,
- "netdata_info{instance=\"%s\",application=\"%s\",version=\"%s\"} 1 %llu\n",
- hostname,
- host->program_name,
- host->program_version,
- now_realtime_usec() / USEC_PER_MS);
+ buffer_sprintf(wb, "} 1 %llu\n", now_realtime_usec() / USEC_PER_MS);
else
- buffer_sprintf(
- wb,
- "netdata_info{instance=\"%s\",application=\"%s\",version=\"%s\"} 1\n",
- hostname,
- host->program_name,
- host->program_version);
+ buffer_sprintf(wb, "} 1\n");
char labels[PROMETHEUS_LABELS_MAX + 1] = "";
if (allhosts) {
- if (instance->labels && buffer_tostring(instance->labels)) {
- if (output_options & PROMETHEUS_OUTPUT_TIMESTAMPS) {
- buffer_sprintf(
- wb,
- "netdata_host_tags_info{instance=\"%s\",%s} 1 %llu\n",
- hostname,
- buffer_tostring(instance->labels),
- now_realtime_usec() / USEC_PER_MS);
-
- // deprecated, exists only for compatibility with older queries
- buffer_sprintf(
- wb,
- "netdata_host_tags{instance=\"%s\",%s} 1 %llu\n",
- hostname,
- buffer_tostring(instance->labels),
- now_realtime_usec() / USEC_PER_MS);
- } else {
- buffer_sprintf(
- wb, "netdata_host_tags_info{instance=\"%s\",%s} 1\n", hostname, buffer_tostring(instance->labels));
-
- // deprecated, exists only for compatibility with older queries
- buffer_sprintf(
- wb, "netdata_host_tags{instance=\"%s\",%s} 1\n", hostname, buffer_tostring(instance->labels));
- }
- }
-
snprintfz(labels, PROMETHEUS_LABELS_MAX, ",instance=\"%s\"", hostname);
- } else {
- if (instance->labels && buffer_tostring(instance->labels)) {
- if (output_options & PROMETHEUS_OUTPUT_TIMESTAMPS) {
- buffer_sprintf(
- wb,
- "netdata_host_tags_info{%s} 1 %llu\n",
- buffer_tostring(instance->labels),
- now_realtime_usec() / USEC_PER_MS);
-
- // deprecated, exists only for compatibility with older queries
- buffer_sprintf(
- wb,
- "netdata_host_tags{%s} 1 %llu\n",
- buffer_tostring(instance->labels),
- now_realtime_usec() / USEC_PER_MS);
- } else {
- buffer_sprintf(wb, "netdata_host_tags_info{%s} 1\n", buffer_tostring(instance->labels));
-
- // deprecated, exists only for compatibility with older queries
- buffer_sprintf(wb, "netdata_host_tags{%s} 1\n", buffer_tostring(instance->labels));
- }
- }
- }
+ }
- if (instance->labels)
- buffer_flush(instance->labels);
+ if (instance->labels_buffer)
+ buffer_flush(instance->labels_buffer);
// send custom variables set for the host
if (output_options & PROMETHEUS_OUTPUT_VARIABLES) {
@@ -723,7 +684,7 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
time_t first_time = instance->after;
time_t last_time = instance->before;
- calculated_number value = exporting_calculate_value_from_stored_data(instance, rd, &last_time);
+ NETDATA_DOUBLE value = exporting_calculate_value_from_stored_data(instance, rd, &last_time);
if (!isnan(value) && !isinf(value)) {
if (EXPORTING_OPTIONS_DATA_SOURCE(exporting_options) == EXPORTING_SOURCE_DATA_AVERAGE)
@@ -755,7 +716,7 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
if (output_options & PROMETHEUS_OUTPUT_TIMESTAMPS)
buffer_sprintf(
wb,
- "%s_%s%s%s{chart=\"%s\",family=\"%s\",dimension=\"%s\"%s} " CALCULATED_NUMBER_FORMAT
+ "%s_%s%s%s{chart=\"%s\",family=\"%s\",dimension=\"%s\"%s} " NETDATA_DOUBLE_FORMAT
" %llu\n",
prefix,
context,
@@ -770,7 +731,7 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(
else
buffer_sprintf(
wb,
- "%s_%s%s%s{chart=\"%s\",family=\"%s\",dimension=\"%s\"%s} " CALCULATED_NUMBER_FORMAT
+ "%s_%s%s%s{chart=\"%s\",family=\"%s\",dimension=\"%s\"%s} " NETDATA_DOUBLE_FORMAT
"\n",
prefix,
context,
diff --git a/exporting/prometheus/remote_write/remote_write.c b/exporting/prometheus/remote_write/remote_write.c
index 59a488e1b..03feb2c08 100644
--- a/exporting/prometheus/remote_write/remote_write.c
+++ b/exporting/prometheus/remote_write/remote_write.c
@@ -97,6 +97,7 @@ int init_prometheus_remote_write_instance(struct instance *instance)
instance->start_chart_formatting = format_chart_prometheus_remote_write;
instance->metric_formatting = format_dimension_prometheus_remote_write;
instance->end_chart_formatting = NULL;
+ instance->variables_formatting = format_variables_prometheus_remote_write;
instance->end_host_formatting = NULL;
instance->end_batch_formatting = format_batch_prometheus_remote_write;
@@ -134,6 +135,25 @@ int init_prometheus_remote_write_instance(struct instance *instance)
return 0;
}
+struct format_remote_write_label_callback {
+ struct instance *instance;
+ void *write_request;
+};
+
+static int format_remote_write_label_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
+ struct format_remote_write_label_callback *d = (struct format_remote_write_label_callback *)data;
+
+ if (!should_send_label(d->instance, ls)) return 0;
+
+ char k[PROMETHEUS_ELEMENT_MAX + 1];
+ char v[PROMETHEUS_ELEMENT_MAX + 1];
+
+ prometheus_name_copy(k, name, PROMETHEUS_ELEMENT_MAX);
+ prometheus_label_copy(v, value, PROMETHEUS_ELEMENT_MAX);
+ add_label(d->write_request, k, v);
+ return 1;
+}
+
/**
* Format host data for Prometheus Remote Write connector
*
@@ -157,23 +177,13 @@ int format_host_prometheus_remote_write(struct instance *instance, RRDHOST *host
add_host_info(
connector_specific_data->write_request,
"netdata_info", hostname, host->program_name, host->program_version, now_realtime_usec() / USEC_PER_MS);
-
+
if (unlikely(sending_labels_configured(instance))) {
- rrdhost_check_rdlock(host);
- netdata_rwlock_rdlock(&host->labels.labels_rwlock);
- for (struct label *label = host->labels.head; label; label = label->next) {
- if (!should_send_label(instance, label))
- continue;
-
- char key[PROMETHEUS_ELEMENT_MAX + 1];
- prometheus_name_copy(key, label->key, PROMETHEUS_ELEMENT_MAX);
-
- char value[PROMETHEUS_ELEMENT_MAX + 1];
- prometheus_label_copy(value, label->value, PROMETHEUS_ELEMENT_MAX);
-
- add_label(connector_specific_data->write_request, key, value);
- }
- netdata_rwlock_unlock(&host->labels.labels_rwlock);
+ struct format_remote_write_label_callback tmp = {
+ .write_request = connector_specific_data->write_request,
+ .instance = instance
+ };
+ rrdlabels_walkthrough_read(host->host_labels, format_remote_write_label_callback, &tmp);
}
return 0;
@@ -284,7 +294,7 @@ int format_dimension_prometheus_remote_write(struct instance *instance, RRDDIM *
// we need average or sum of the data
time_t last_t = instance->before;
- calculated_number value = exporting_calculate_value_from_stored_data(instance, rd, &last_t);
+ NETDATA_DOUBLE value = exporting_calculate_value_from_stored_data(instance, rd, &last_t);
if (!isnan(value) && !isinf(value)) {
if (EXPORTING_OPTIONS_DATA_SOURCE(instance->config.options) == EXPORTING_SOURCE_DATA_AVERAGE)
@@ -311,6 +321,49 @@ int format_dimension_prometheus_remote_write(struct instance *instance, RRDDIM *
return 0;
}
+int format_variable_prometheus_remote_write_callback(RRDVAR *rv, void *data) {
+ struct prometheus_remote_write_variables_callback_options *opts = data;
+
+ if (rv->options & (RRDVAR_OPTION_CUSTOM_HOST_VAR | RRDVAR_OPTION_CUSTOM_CHART_VAR)) {
+ RRDHOST *host = opts->host;
+ struct instance *instance = opts->instance;
+ struct simple_connector_data *simple_connector_data =
+ (struct simple_connector_data *)instance->connector_specific_data;
+ struct prometheus_remote_write_specific_data *connector_specific_data =
+ (struct prometheus_remote_write_specific_data *)simple_connector_data->connector_specific_data;
+
+ char name[PROMETHEUS_LABELS_MAX + 1];
+ char *suffix = "";
+
+ prometheus_name_copy(context, rv->name, PROMETHEUS_ELEMENT_MAX);
+ snprintf(name, PROMETHEUS_LABELS_MAX, "%s_%s%s", instance->config.prefix, context, suffix);
+
+ NETDATA_DOUBLE value = rrdvar2number(rv);
+ add_variable(connector_specific_data->write_request, name,
+ (host == localhost) ? instance->config.hostname : host->hostname, value, opts->now / USEC_PER_MS);
+ }
+
+ return 0;
+}
+
+/**
+ * Format a variable for Prometheus Remote Write connector
+ *
+ * @param rv a variable.
+ * @param instance an instance data structure.
+ * @return Always returns 0.
+ */
+int format_variables_prometheus_remote_write(struct instance *instance, RRDHOST *host)
+{
+ struct prometheus_remote_write_variables_callback_options opt = {
+ .host = host,
+ .instance = instance,
+ .now = now_realtime_usec(),
+ };
+
+ return foreach_host_variable_callback(host, format_variable_prometheus_remote_write_callback, &opt);
+}
+
/**
* Format a batch for Prometheus Remote Write connector
*
@@ -339,7 +392,6 @@ int format_batch_prometheus_remote_write(struct instance *instance)
return 1;
}
buffer->len = data_size;
- instance->stats.buffered_bytes = (collected_number)buffer_strlen(buffer);
simple_connector_end_batch(instance);
diff --git a/exporting/prometheus/remote_write/remote_write.h b/exporting/prometheus/remote_write/remote_write.h
index d738f5126..4740772d0 100644
--- a/exporting/prometheus/remote_write/remote_write.h
+++ b/exporting/prometheus/remote_write/remote_write.h
@@ -11,12 +11,19 @@ struct prometheus_remote_write_specific_data {
void *write_request;
};
+struct prometheus_remote_write_variables_callback_options {
+ RRDHOST *host;
+ time_t now;
+ struct instance *instance;
+};
+
int init_prometheus_remote_write_instance(struct instance *instance);
extern void clean_prometheus_remote_write(struct instance *instance);
int format_host_prometheus_remote_write(struct instance *instance, RRDHOST *host);
int format_chart_prometheus_remote_write(struct instance *instance, RRDSET *st);
int format_dimension_prometheus_remote_write(struct instance *instance, RRDDIM *rd);
+int format_variables_prometheus_remote_write(struct instance *instance, RRDHOST *host);
int format_batch_prometheus_remote_write(struct instance *instance);
void prometheus_remote_write_prepare_header(struct instance *instance);
diff --git a/exporting/prometheus/remote_write/remote_write_request.cc b/exporting/prometheus/remote_write/remote_write_request.cc
index cfd61271e..ecfa11fa8 100644
--- a/exporting/prometheus/remote_write/remote_write_request.cc
+++ b/exporting/prometheus/remote_write/remote_write_request.cc
@@ -138,6 +138,38 @@ void add_metric(
}
/**
+ * Adds a metric to a write request
+ *
+ * @param write_request_p the write request
+ * @param name the name of the metric
+ * @param instance the name of the host, the metric belongs to
+ * @param value the value of the metric
+ * @param timestamp the timestamp for the metric in milliseconds
+ */
+void add_variable(
+ void *write_request_p, const char *name, const char *instance, const double value, const int64_t timestamp)
+{
+ WriteRequest *write_request = (WriteRequest *)write_request_p;
+ TimeSeries *timeseries;
+ Sample *sample;
+ Label *label;
+
+ timeseries = write_request->add_timeseries();
+
+ label = timeseries->add_labels();
+ label->set_name("__name__");
+ label->set_value(name);
+
+ label = timeseries->add_labels();
+ label->set_name("instance");
+ label->set_value(instance);
+
+ sample = timeseries->add_samples();
+ sample->set_value(value);
+ sample->set_timestamp(timestamp);
+}
+
+/**
* Gets the size of a write request
*
* @param write_request_p the write request
diff --git a/exporting/prometheus/remote_write/remote_write_request.h b/exporting/prometheus/remote_write/remote_write_request.h
index 5f242b941..b25370133 100644
--- a/exporting/prometheus/remote_write/remote_write_request.h
+++ b/exporting/prometheus/remote_write/remote_write_request.h
@@ -20,6 +20,9 @@ void add_metric(
const char *name, const char *chart, const char *family, const char *dimension,
const char *instance, const double value, const int64_t timestamp);
+void add_variable(
+ void *write_request_p, const char *name, const char *instance, const double value, const int64_t timestamp);
+
size_t get_write_request_size(void *write_request_p);
int pack_and_clear_write_request(void *write_request_p, char *buffer, size_t *size);