diff options
author | Costa Tsaousis <costa@netdata.cloud> | 2022-06-13 20:35:45 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-13 20:35:45 +0300 |
commit | 1b0f6c6b2296dc082d85f38c298a61442dcf2490 (patch) | |
tree | 2cfee5101d9cae338d0635f44fe62b010f3548ee /exporting/opentsdb | |
parent | 4c64b8ea4ff720d946bbb9a11ca7474c5673bb6c (diff) |
Labels with dictionary (#13070)
* squashed and rebased to master
* fix overflow and single character bug in sanitize; include rrd.h instead of node_info.h
* added unittest for UTF-8 multibyte sanitization
* Fix unit test compilation
* Fix CMake build
* remove double sanitizer for opentsdb; cleanup sanitize_json_string()
* rename error_description to error_message to avoid conflict with json-c
* revert last and undef error_description from json-c
* more unittests; attempt to fix protobuf map issue
* get rid of rrdlabels_get() and replace it with a safe version that writes the value to a buffer
* added dictionary sorting unittest; rrdlabels_to_buffer() now is sorted
* better sorted dictionary checking
* proper unittesting for sorted dictionaries
* call dictionary deletion callback when destroying the dictionary
* remove obsolete variable
* Fix exporting unit tests
* Fix k8s label parsing test
* workaround for cmocka and strdupz()
* Bypass cmocka memory allocation check
* Revert "Bypass cmocka memory allocation check"
This reverts commit 4c49923839d9229bea23ca914dd8a0be1ebe2bf4.
* Revert "workaround for cmocka and strdupz()"
This reverts commit 7bebee04801db1865c748a7896d5fa54bb7104a5.
* Bypass cmocka memory allocation checks
* respect json formatting for chart labels
* cloud sends colons
* print the value only once
* allow parenthesis in values and spaces; make stream sender send quotes for values
Co-authored-by: Vladimir Kobal <vlad@prokk.net>
Diffstat (limited to 'exporting/opentsdb')
-rw-r--r-- | exporting/opentsdb/opentsdb.c | 70 | ||||
-rw-r--r-- | exporting/opentsdb/opentsdb.h | 2 |
2 files changed, 24 insertions, 48 deletions
diff --git a/exporting/opentsdb/opentsdb.c b/exporting/opentsdb/opentsdb.c index 7ed88fd6d8..d6ce98ce45 100644 --- a/exporting/opentsdb/opentsdb.c +++ b/exporting/opentsdb/opentsdb.c @@ -124,13 +124,14 @@ int init_opentsdb_http_instance(struct instance *instance) * @param len the maximum number of characters copied. */ -void sanitize_opentsdb_label_value(char *dst, char *src, size_t len) +void sanitize_opentsdb_label_value(char *dst, const char *src, size_t len) { while (*src != '\0' && len) { - if (isalpha(*src) || isdigit(*src) || *src == '-' || *src == '_' || *src == '.' || *src == '/' || IS_UTF8_BYTE(*src)) + if (isalpha(*src) || isdigit(*src) || *src == '-' || *src == '.' || *src == '/' || IS_UTF8_BYTE(*src)) *dst++ = *src; else *dst++ = '_'; + src++; len--; } @@ -144,28 +145,18 @@ void sanitize_opentsdb_label_value(char *dst, char *src, size_t len) * @param host a data collecting host. * @return Always returns 0. */ -int format_host_labels_opentsdb_telnet(struct instance *instance, RRDHOST *host) -{ - if (!instance->labels) - instance->labels = buffer_create(1024); + +int format_host_labels_opentsdb_telnet(struct instance *instance, RRDHOST *host) { + if(!instance->labels_buffer) + instance->labels_buffer = buffer_create(1024); if (unlikely(!sending_labels_configured(instance))) return 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; - - char value[CONFIG_MAX_VALUE + 1]; - sanitize_opentsdb_label_value(value, label->value, CONFIG_MAX_VALUE); - - if (*value) - buffer_sprintf(instance->labels, " %s=%s", label->key, value); - } - netdata_rwlock_unlock(&host->labels.labels_rwlock); - + buffer_strcat(instance->labels_buffer, " "); + rrdlabels_to_buffer(host->host_labels, instance->labels_buffer, "", "=", "", " ", + exporting_labels_filter_callback, instance, + NULL, sanitize_opentsdb_label_value); return 0; } @@ -204,7 +195,7 @@ int format_dimension_collected_opentsdb_telnet(struct instance *instance, RRDDIM (host == localhost) ? instance->config.hostname : host->hostname, (host->tags) ? " " : "", (host->tags) ? host->tags : "", - (instance->labels) ? buffer_tostring(instance->labels) : ""); + (instance->labels_buffer) ? buffer_tostring(instance->labels_buffer) : ""); return 0; } @@ -250,7 +241,7 @@ int format_dimension_stored_opentsdb_telnet(struct instance *instance, RRDDIM *r (host == localhost) ? instance->config.hostname : host->hostname, (host->tags) ? " " : "", (host->tags) ? host->tags : "", - (instance->labels) ? buffer_tostring(instance->labels) : ""); + (instance->labels_buffer) ? buffer_tostring(instance->labels_buffer) : ""); return 0; } @@ -287,33 +278,18 @@ void opentsdb_http_prepare_header(struct instance *instance) * @param host a data collecting host. * @return Always returns 0. */ -int format_host_labels_opentsdb_http(struct instance *instance, RRDHOST *host) -{ - if (!instance->labels) - instance->labels = buffer_create(1024); + +int format_host_labels_opentsdb_http(struct instance *instance, RRDHOST *host) { + if (!instance->labels_buffer) + instance->labels_buffer = buffer_create(1024); if (unlikely(!sending_labels_configured(instance))) return 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; - - char escaped_value[CONFIG_MAX_VALUE * 2 + 1]; - sanitize_json_string(escaped_value, label->value, CONFIG_MAX_VALUE); - - char value[CONFIG_MAX_VALUE + 1]; - sanitize_opentsdb_label_value(value, escaped_value, CONFIG_MAX_VALUE); - - if (*value) { - buffer_strcat(instance->labels, ","); - buffer_sprintf(instance->labels, "\"%s\":\"%s\"", label->key, value); - } - } - netdata_rwlock_unlock(&host->labels.labels_rwlock); - + buffer_strcat(instance->labels_buffer, ","); + rrdlabels_to_buffer(host->host_labels, instance->labels_buffer, "", ":", "\"", ",", + exporting_labels_filter_callback, instance, + NULL, sanitize_opentsdb_label_value); return 0; } @@ -362,7 +338,7 @@ int format_dimension_collected_opentsdb_http(struct instance *instance, RRDDIM * (host == localhost) ? instance->config.hostname : host->hostname, (host->tags) ? " " : "", (host->tags) ? host->tags : "", - instance->labels ? buffer_tostring(instance->labels) : ""); + instance->labels_buffer ? buffer_tostring(instance->labels_buffer) : ""); return 0; } @@ -418,7 +394,7 @@ int format_dimension_stored_opentsdb_http(struct instance *instance, RRDDIM *rd) (host == localhost) ? instance->config.hostname : host->hostname, (host->tags) ? " " : "", (host->tags) ? host->tags : "", - instance->labels ? buffer_tostring(instance->labels) : ""); + instance->labels_buffer ? buffer_tostring(instance->labels_buffer) : ""); return 0; } diff --git a/exporting/opentsdb/opentsdb.h b/exporting/opentsdb/opentsdb.h index d53a5054f5..b544ba8c16 100644 --- a/exporting/opentsdb/opentsdb.h +++ b/exporting/opentsdb/opentsdb.h @@ -8,7 +8,7 @@ int init_opentsdb_telnet_instance(struct instance *instance); int init_opentsdb_http_instance(struct instance *instance); -void sanitize_opentsdb_label_value(char *dst, char *src, size_t len); +void sanitize_opentsdb_label_value(char *dst, const char *src, size_t len); int format_host_labels_opentsdb_telnet(struct instance *instance, RRDHOST *host); int format_host_labels_opentsdb_http(struct instance *instance, RRDHOST *host); |