diff options
Diffstat (limited to 'exporting/prometheus/prometheus.c')
-rw-r--r-- | exporting/prometheus/prometheus.c | 153 |
1 files changed, 57 insertions, 96 deletions
diff --git a/exporting/prometheus/prometheus.c b/exporting/prometheus/prometheus.c index c7f3f1d3..7d632164 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, |