From 096cbe9f24ce5d89418cad1a6904f4e2967ffdf4 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 24 Nov 2023 08:28:06 +0100 Subject: [PATCH] curl_stats: fix compatibility with new versions of cURL. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Use integer based keys for metrics if available. cURL ≥ 7.55.0 provides additional keys that allow getting certain metrics as integers rather than doubles, e.g. content length. In some newer versions of cURL, the original keys (using doubles) are marked as deprecated. ChangeLog: cURL, cURL-JSON, cURL-XML, Write HTTP plugins: fix compatibility with new versions of cURL. --- src/utils/curl_stats/curl_stats.c | 62 +++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/src/utils/curl_stats/curl_stats.c b/src/utils/curl_stats/curl_stats.c index a89717194..84aea5891 100644 --- a/src/utils/curl_stats/curl_stats.c +++ b/src/utils/curl_stats/curl_stats.c @@ -70,6 +70,7 @@ static int dispatch_gauge(CURL *curl, CURLINFO info, value_list_t *vl) { return plugin_dispatch_values(vl); } /* dispatch_gauge */ +#if !CURL_AT_LEAST_VERSION(7, 55, 0) /* dispatch a speed, in bytes/second */ static int dispatch_speed(CURL *curl, CURLINFO info, value_list_t *vl) { CURLcode code; @@ -86,6 +87,7 @@ static int dispatch_speed(CURL *curl, CURLINFO info, value_list_t *vl) { return plugin_dispatch_values(vl); } /* dispatch_speed */ +#endif /* dispatch a size/count, reported as a long value */ static int dispatch_size(CURL *curl, CURLINFO info, value_list_t *vl) { @@ -105,6 +107,43 @@ static int dispatch_size(CURL *curl, CURLINFO info, value_list_t *vl) { return plugin_dispatch_values(vl); } /* dispatch_size */ +#if CURL_AT_LEAST_VERSION(7, 55, 0) +/* dispatch_gauge_t dispatches an curl_off_t as a gauge metric. */ +static int dispatch_gauge_t(CURL *curl, CURLINFO info, value_list_t *vl) { + curl_off_t v = 0; + + CURLcode code = curl_easy_getinfo(curl, info, &v); + if (code != CURLE_OK) { + return -1; + } + + vl->values = &(value_t){ + .gauge = (gauge_t)v, + }; + vl->values_len = 1; + + return plugin_dispatch_values(vl); +} + +/* dispatch_speed_t dispatches an curl_off_t representing bytes/s as a gauge + * metric in bits/s. */ +static int dispatch_speed_t(CURL *curl, CURLINFO info, value_list_t *vl) { + curl_off_t v = 0; + + CURLcode code = curl_easy_getinfo(curl, info, &v); + if (code != CURLE_OK) { + return -1; + } + + vl->values = &(value_t){ + .gauge = (gauge_t)(v * 8), + }; + vl->values_len = 1; + + return plugin_dispatch_values(vl); +} +#endif + static struct { const char *name; const char *config_key; @@ -125,6 +164,24 @@ static struct { CURLINFO_CONNECT_TIME), SPEC(pretransfer_time, "PretransferTime", dispatch_gauge, "duration", CURLINFO_PRETRANSFER_TIME), +#if CURL_AT_LEAST_VERSION(7, 55, 0) + SPEC(content_length_download, "ContentLengthDownload", dispatch_gauge_t, + "bytes", CURLINFO_CONTENT_LENGTH_DOWNLOAD_T), + SPEC(content_length_upload, "ContentLengthUpload", dispatch_gauge_t, + "bytes", CURLINFO_CONTENT_LENGTH_UPLOAD_T), + SPEC(size_upload, "SizeUpload", dispatch_gauge_t, "bytes", + CURLINFO_SIZE_UPLOAD_T), + SPEC(size_download, "SizeDownload", dispatch_gauge_t, "bytes", + CURLINFO_SIZE_DOWNLOAD_T), + SPEC(speed_download, "SpeedDownload", dispatch_speed_t, "bitrate", + CURLINFO_SPEED_DOWNLOAD_T), + SPEC(speed_upload, "SpeedUpload", dispatch_speed_t, "bitrate", + CURLINFO_SPEED_UPLOAD_T), +#else + SPEC(content_length_download, "ContentLengthDownload", dispatch_gauge, + "bytes", CURLINFO_CONTENT_LENGTH_DOWNLOAD), + SPEC(content_length_upload, "ContentLengthUpload", dispatch_gauge, "bytes", + CURLINFO_CONTENT_LENGTH_UPLOAD), SPEC(size_upload, "SizeUpload", dispatch_gauge, "bytes", CURLINFO_SIZE_UPLOAD), SPEC(size_download, "SizeDownload", dispatch_gauge, "bytes", @@ -133,14 +190,11 @@ static struct { CURLINFO_SPEED_DOWNLOAD), SPEC(speed_upload, "SpeedUpload", dispatch_speed, "bitrate", CURLINFO_SPEED_UPLOAD), +#endif SPEC(header_size, "HeaderSize", dispatch_size, "bytes", CURLINFO_HEADER_SIZE), SPEC(request_size, "RequestSize", dispatch_size, "bytes", CURLINFO_REQUEST_SIZE), - SPEC(content_length_download, "ContentLengthDownload", dispatch_gauge, - "bytes", CURLINFO_CONTENT_LENGTH_DOWNLOAD), - SPEC(content_length_upload, "ContentLengthUpload", dispatch_gauge, "bytes", - CURLINFO_CONTENT_LENGTH_UPLOAD), SPEC(starttransfer_time, "StarttransferTime", dispatch_gauge, "duration", CURLINFO_STARTTRANSFER_TIME), SPEC(redirect_time, "RedirectTime", dispatch_gauge, "duration", -- 2.47.2