From 3c315f0fff93aa072472abc10815963ac0035268 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 12 Aug 2022 09:26:11 +0200 Subject: Adding upstream version 1.36.0. Signed-off-by: Daniel Baumann --- exporting/prometheus/remote_write/remote_write.c | 88 +++++++++++++++++----- exporting/prometheus/remote_write/remote_write.h | 7 ++ .../remote_write/remote_write_request.cc | 32 ++++++++ .../prometheus/remote_write/remote_write_request.h | 3 + 4 files changed, 112 insertions(+), 18 deletions(-) (limited to 'exporting/prometheus/remote_write') 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 @@ -137,6 +137,38 @@ void add_metric( sample->set_timestamp(timestamp); } +/** + * 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 * 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); -- cgit v1.2.3