X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fwrite_stackdriver.c;h=dfa1d7c0785a9db6ff5207f24aaa1b3728ae0b54;hp=eb60ea9ceb45d3a7260c359c50c44420196b8416;hb=a811574a6acbf87f23948411876a231fecaeb491;hpb=74d6bcc2d388d191f856b4905ea333dd36702a4b diff --git a/src/write_stackdriver.c b/src/write_stackdriver.c index eb60ea9c..dfa1d7c0 100644 --- a/src/write_stackdriver.c +++ b/src/write_stackdriver.c @@ -22,12 +22,12 @@ #include "collectd.h" -#include "common.h" #include "configfile.h" #include "plugin.h" -#include "utils_format_stackdriver.h" -#include "utils_gce.h" -#include "utils_oauth.h" +#include "utils/common/common.h" +#include "utils/format_stackdriver/format_stackdriver.h" +#include "utils/gce/gce.h" +#include "utils/oauth/oauth.h" #include #include @@ -109,8 +109,8 @@ static char *wg_get_authorization_header(wg_callback_t *cb) { /* {{{ */ return NULL; } - status = snprintf(authorization_header, sizeof(authorization_header), - "Authorization: Bearer %s", access_token); + status = ssnprintf(authorization_header, sizeof(authorization_header), + "Authorization: Bearer %s", access_token); if ((status < 1) || ((size_t)status >= sizeof(authorization_header))) return NULL; @@ -139,14 +139,17 @@ static api_error_t *parse_api_error(char const *body) { yajl_val code = yajl_tree_get(root, (char const *[]){"error", "code", NULL}, yajl_t_number); - if (code != NULL) { + if (YAJL_IS_INTEGER(code)) { err->code = YAJL_GET_INTEGER(code); } yajl_val message = yajl_tree_get( root, (char const *[]){"error", "message", NULL}, yajl_t_string); - if (message != NULL) { - err->message = strdup(YAJL_GET_STRING(message)); + if (YAJL_IS_STRING(message)) { + char const *m = YAJL_GET_STRING(message); + if (m != NULL) { + err->message = strdup(m); + } } return err; @@ -157,9 +160,9 @@ static char *api_error_string(api_error_t *err, char *buffer, if (err == NULL) { strncpy(buffer, "Unknown error (API error is NULL)", buffer_size); } else if (err->message == NULL) { - snprintf(buffer, buffer_size, "API error %d", err->code); + ssnprintf(buffer, buffer_size, "API error %d", err->code); } else { - snprintf(buffer, buffer_size, "API error %d: %s", err->code, err->message); + ssnprintf(buffer, buffer_size, "API error %d: %s", err->code, err->message); } return buffer; @@ -187,6 +190,12 @@ static int do_post(wg_callback_t *cb, char const *url, void const *payload, curl_easy_setopt(cb->curl, CURLOPT_POST, 1L); curl_easy_setopt(cb->curl, CURLOPT_URL, url); + long timeout_ms = 2 * CDTIME_T_TO_MS(plugin_get_interval()); + if (timeout_ms < 10000) { + timeout_ms = 10000; + } + curl_easy_setopt(cb->curl, CURLOPT_TIMEOUT_MS, timeout_ms); + /* header */ char *auth_header = wg_get_authorization_header(cb); if (auth_header == NULL) { @@ -216,8 +225,10 @@ static int do_post(wg_callback_t *cb, char const *url, void const *payload, if (status != CURLE_OK) { ERROR("write_stackdriver plugin: POST %s failed: %s", url, cb->curl_errbuf); - sfree(ret_content->memory); - ret_content->size = 0; + if (ret_content != NULL) { + sfree(ret_content->memory); + ret_content->size = 0; + } return -1; } @@ -225,10 +236,10 @@ static int do_post(wg_callback_t *cb, char const *url, void const *payload, curl_easy_getinfo(cb->curl, CURLINFO_RESPONSE_CODE, &http_code); if (ret_content != NULL) { - if ((status >= 400) && (status < 500)) { + if ((http_code >= 400) && (http_code < 500)) { ERROR("write_stackdriver plugin: POST %s: %s", url, API_ERROR_STRING(parse_api_error(ret_content->memory))); - } else if (status >= 500) { + } else if (http_code >= 500) { WARNING("write_stackdriver plugin: POST %s: %s", url, ret_content->memory); } @@ -240,8 +251,8 @@ static int do_post(wg_callback_t *cb, char const *url, void const *payload, static int wg_call_metricdescriptor_create(wg_callback_t *cb, char const *payload) { char url[1024]; - snprintf(url, sizeof(url), "%s/projects/%s/metricDescriptors", cb->url, - cb->project); + ssnprintf(url, sizeof(url), "%s/projects/%s/metricDescriptors", cb->url, + cb->project); wg_memory_t response = {0}; int status = do_post(cb, url, payload, &response); @@ -262,7 +273,8 @@ static int wg_call_metricdescriptor_create(wg_callback_t *cb, static int wg_call_timeseries_write(wg_callback_t *cb, char const *payload) { char url[1024]; - snprintf(url, sizeof(url), "%s/projects/%s/timeSeries", cb->url, cb->project); + ssnprintf(url, sizeof(url), "%s/projects/%s/timeSeries", cb->url, + cb->project); wg_memory_t response = {0}; int status = do_post(cb, url, payload, &response); @@ -330,11 +342,6 @@ static int wg_flush_nolock(cdtime_t timeout, wg_callback_t *cb) /* {{{ */ char *payload = sd_output_reset(cb->formatter); int status = wg_call_timeseries_write(cb, payload); - if (status != 0) { - ERROR("write_stackdriver plugin: Sending buffer failed with status %d.", - status); - } - wg_reset_buffer(cb); return status; } /* }}} wg_flush_nolock */