From: Denis 'jawa' Pompilio Date: Thu, 12 Oct 2017 15:33:13 +0000 (+0200) Subject: Implementation of "Prefix" for write_http plugin X-Git-Tag: collectd-5.8.0~41^2~4 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=06832d2890223397e39aaee205abc5f45464529b;p=collectd.git Implementation of "Prefix" for write_http plugin This option is only available for the KAIROSDB format This option defaults to "collectd" --- diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 5f58a6e9..ac0aa2d2 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1636,6 +1636,7 @@ # Header "X-Custom-Header: custom_value" # SSLVersion "TLSv1" # Format "Command" +# Prefix "collectd" # metric prefix, only available for KAIROSDB format # Attribute "key" "value" # only available for KAIROSDB format # TTL 0 # data ttl, only available for KAIROSDB format # Metrics true diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 9548fd63..3739fe77 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -9270,6 +9270,12 @@ Sets the Cassandra ttl for the data points. Please refer to L +=item B I + +Only available for the KAIROSDB output format. + +Sets the metrics prefix I. Defaults to I. + =item B B|B Controls whether I are POSTed to this location. Defaults to B. diff --git a/src/utils_format_kairosdb.c b/src/utils_format_kairosdb.c index 0128c575..63803afb 100644 --- a/src/utils_format_kairosdb.c +++ b/src/utils_format_kairosdb.c @@ -183,7 +183,8 @@ static int value_list_to_kairosdb(char *buffer, size_t buffer_size, /* {{{ */ const data_set_t *ds, const value_list_t *vl, int store_rates, char const *const *http_attrs, - size_t http_attrs_num, int data_ttl) { + size_t http_attrs_num, int data_ttl, + char *metrics_prefix) { char temp[512]; size_t offset = 0; int status; @@ -214,7 +215,7 @@ static int value_list_to_kairosdb(char *buffer, size_t buffer_size, /* {{{ */ * a square bracket in `format_kairosdb_finalize'. */ BUFFER_ADD(",{"); - BUFFER_ADD("\"name\":\"collectd"); + BUFFER_ADD("\"name\":\"%s", metrics_prefix); BUFFER_ADD(".%s", vl->plugin); @@ -263,12 +264,14 @@ static int format_kairosdb_value_list_nocheck( char *buffer, /* {{{ */ size_t *ret_buffer_fill, size_t *ret_buffer_free, const data_set_t *ds, const value_list_t *vl, int store_rates, size_t temp_size, - char const *const *http_attrs, size_t http_attrs_num, int data_ttl) { + char const *const *http_attrs, size_t http_attrs_num, int data_ttl, + char *metrics_prefix) { char temp[temp_size]; int status; status = value_list_to_kairosdb(temp, sizeof(temp), ds, vl, store_rates, - http_attrs, http_attrs_num, data_ttl); + http_attrs, http_attrs_num, data_ttl, + metrics_prefix); if (status != 0) return status; temp_size = strlen(temp); @@ -337,7 +340,8 @@ int format_kairosdb_value_list(char *buffer, /* {{{ */ size_t *ret_buffer_fill, size_t *ret_buffer_free, const data_set_t *ds, const value_list_t *vl, int store_rates, char const *const *http_attrs, - size_t http_attrs_num, int data_ttl) { + size_t http_attrs_num, int data_ttl, + char *metrics_prefix) { if ((buffer == NULL) || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL) || (ds == NULL) || (vl == NULL)) return -EINVAL; @@ -347,7 +351,8 @@ int format_kairosdb_value_list(char *buffer, /* {{{ */ return format_kairosdb_value_list_nocheck( buffer, ret_buffer_fill, ret_buffer_free, ds, vl, store_rates, - (*ret_buffer_free) - 2, http_attrs, http_attrs_num, data_ttl); + (*ret_buffer_free) - 2, http_attrs, http_attrs_num, data_ttl, + metrics_prefix); } /* }}} int format_kairosdb_value_list */ /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/utils_format_kairosdb.h b/src/utils_format_kairosdb.h index 3a4c7c77..487103b4 100644 --- a/src/utils_format_kairosdb.h +++ b/src/utils_format_kairosdb.h @@ -41,7 +41,8 @@ int format_kairosdb_value_list(char *buffer, size_t *ret_buffer_fill, size_t *ret_buffer_free, const data_set_t *ds, const value_list_t *vl, int store_rates, char const *const *http_attrs, - size_t http_attrs_num, int data_ttl); + size_t http_attrs_num, int data_ttl, + char *metrics_prefix); int format_kairosdb_finalize(char *buffer, size_t *ret_buffer_fill, size_t *ret_buffer_free); diff --git a/src/write_http.c b/src/write_http.c index 06327edb..c391e693 100644 --- a/src/write_http.c +++ b/src/write_http.c @@ -36,6 +36,10 @@ #define WRITE_HTTP_DEFAULT_BUFFER_SIZE 4096 #endif +#ifndef WRITE_HTTP_DEFAULT_PREFIX +#define WRITE_HTTP_DEFAULT_PREFIX "collectd" +#endif + /* * Private variables */ @@ -80,6 +84,7 @@ struct wh_callback_s { pthread_mutex_t send_lock; int data_ttl; + char *metrics_prefix; }; typedef struct wh_callback_s wh_callback_t; @@ -328,6 +333,7 @@ static void wh_callback_free(void *data) /* {{{ */ sfree(cb->clientcert); sfree(cb->clientkeypass); sfree(cb->send_buffer); + sfree(cb->metrics_prefix); sfree(cb); } /* }}} void wh_callback_free */ @@ -476,7 +482,7 @@ static int wh_write_kairosdb(const data_set_t *ds, status = format_kairosdb_value_list( cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free, ds, vl, cb->store_rates, (char const *const *)http_attrs, http_attrs_num, - cb->data_ttl); + cb->data_ttl, cb->metrics_prefix); if (status == -ENOMEM) { status = wh_flush_nolock(/* timeout = */ 0, cb); if (status != 0) { @@ -488,7 +494,7 @@ static int wh_write_kairosdb(const data_set_t *ds, status = format_kairosdb_value_list( cb->send_buffer, &cb->send_buffer_fill, &cb->send_buffer_free, ds, vl, cb->store_rates, (char const *const *)http_attrs, http_attrs_num, - cb->data_ttl); + cb->data_ttl, cb->metrics_prefix); } if (status != 0) { pthread_mutex_unlock(&cb->send_lock); @@ -629,6 +635,7 @@ static int wh_config_node(oconfig_item_t *ci) /* {{{ */ cb->send_metrics = 1; cb->send_notifications = 0; cb->data_ttl = 0; + cb->metrics_prefix = strdup(WRITE_HTTP_DEFAULT_PREFIX); pthread_mutex_init(&cb->send_lock, /* attr = */ NULL); @@ -740,6 +747,8 @@ static int wh_config_node(oconfig_item_t *ci) /* {{{ */ sfree(val); } else if (strcasecmp("TTL", child->key) == 0) { status = cf_util_get_int(child, &cb->data_ttl); + } else if (strcasecmp("Prefix", child->key) == 0) { + status = cf_util_get_string(child, &cb->metrics_prefix); } else { ERROR("write_http plugin: Invalid configuration " "option: %s.",