Skip to content

Commit

Permalink
[Log] - Log curl url when errors for better issue identification
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Mar 31, 2024
1 parent e4d24b2 commit dd0cbd0
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/http-request/src/curlhandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@
#include "timedef.hpp"
#include "unreachable.hpp"

namespace cct {

namespace {

/// According to RFC3986 (https://www.rfc-editor.org/rfc/rfc3986#section-2)
/// '"' cannot be used in a URI (not percent encoded), so it's a fine delimiter for our FlatQueryResponse map
using FlatQueryResponseMap = FlatKeyValueString<'\0', '"'>;

size_t CurlWriteCallback(const char *contents, size_t size, size_t nmemb, void *userp) {
extern "C" size_t CurlWriteCallback(const char *contents, size_t size, size_t nmemb, void *userp) {
try {
reinterpret_cast<string *>(userp)->append(contents, size * nmemb);
reinterpret_cast<cct::string *>(userp)->append(contents, size * nmemb);
} catch (const std::bad_alloc &e) {
// Do not throw exceptions in a function passed to a C library
// Returning 0 is a magic number that will cause CURL to raise an error
log::error("Bad alloc caught in curl write call back action, returning 0: {}", e.what());
cct::log::error("Bad alloc caught in curl write call back action, returning 0: {}", e.what());
return 0;
}
return size * nmemb;
}

namespace cct {

namespace {

/// According to RFC3986 (https://www.rfc-editor.org/rfc/rfc3986#section-2)
/// '"' cannot be used in a URI (not percent encoded), so it's a fine delimiter for our FlatQueryResponse map
using FlatQueryResponseMap = FlatKeyValueString<'\0', '"'>;

template <class T>
void CurlSetLogIfError(CURL *curl, CURLoption curlOption, T value) {
static_assert(std::is_integral_v<T> || std::is_pointer_v<T>);
Expand Down Expand Up @@ -259,8 +259,8 @@ std::string_view CurlHandle::query(std::string_view endpoint, const CurlOptions
_pMetricGateway->add(MetricType::kCounter, MetricOperation::kIncrement,
CurlMetrics::kNbRequestErrorKeys.find(opts.requestType())->second);
}
log::error("Got curl error ({}), retry {}/{} after {}", static_cast<int>(res), retryPos, _nbMaxRetries,
DurationToString(sleepingTime));
log::error("Got curl error {} for {}, retry {}/{} after {}", static_cast<int>(res), modifiedURL, retryPos,
_nbMaxRetries, DurationToString(sleepingTime));
std::this_thread::sleep_for(sleepingTime);
sleepingTime *= 2;
}
Expand All @@ -283,7 +283,7 @@ std::string_view CurlHandle::query(std::string_view endpoint, const CurlOptions
}

// Periodic memory release to avoid memory leak for a very large number of requests
static constexpr int kReleaseMemoryRequestsFrequency = 1000;
static constexpr int kReleaseMemoryRequestsFrequency = 10000;
if ((_bestURLPicker.nbRequestsDone() % kReleaseMemoryRequestsFrequency) == 0) {
_queryData.shrink_to_fit();
}
Expand Down

0 comments on commit dd0cbd0

Please sign in to comment.