Skip to content

Commit 9b69f37

Browse files
shahor02sawenzel
authored andcommitted
CcdbApi sends to server unique string via CURLOPT_USERAGENT
A unique string <host>-<init_timestamp>-<hash> is created and logged at CcdbApi initialization and is sent to the server as CURLOPT_USERAGENT, to allow relating server-side logs with workflow logs.
1 parent c916d2e commit 9b69f37

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

CCDB/include/CCDB/CcdbApi.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class CcdbApi //: public DatabaseInterface
5757
{
5858
public:
5959
/// \brief Default constructor
60-
CcdbApi() = default;
60+
CcdbApi();
6161
/// \brief Default destructor
6262
virtual ~CcdbApi();
6363

@@ -294,7 +294,7 @@ class CcdbApi //: public DatabaseInterface
294294
* @param headers the headers found in the request. Will be emptied when we return false.
295295
* @return true if the headers where updated WRT last time, false if the previous results can still be used.
296296
*/
297-
static bool getCCDBEntryHeaders(std::string const& url, std::string const& etag, std::vector<std::string>& headers);
297+
static bool getCCDBEntryHeaders(std::string const& url, std::string const& etag, std::vector<std::string>& headers, const std::string& agentID = "");
298298

299299
/**
300300
* Extract the possible locations for a file and check whether or not
@@ -513,6 +513,7 @@ class CcdbApi //: public DatabaseInterface
513513
std::string getSnapshotFile(const std::string& topdir, const string& path) const { return getSnapshotDir(topdir, path) + "/snapshot.root"; }
514514

515515
/// Base URL of the CCDB (with port)
516+
std::string mUniqueAgentID{}; // Unique User-Agent ID communicated to server for logging
516517
std::string mUrl{};
517518
std::vector<std::string> hostsPool{};
518519
std::string mSnapshotTopPath{};

CCDB/src/CcdbApi.cxx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <algorithm>
3838
#include <filesystem>
3939
#include <boost/algorithm/string.hpp>
40+
#include <boost/asio/ip/host_name.hpp>
4041
#include <iostream>
4142
#include <mutex>
4243
#include <boost/interprocess/sync/named_semaphore.hpp>
@@ -51,6 +52,12 @@ using namespace std;
5152
std::mutex gIOMutex; // to protect TMemFile IO operations
5253
unique_ptr<TJAlienCredentials> CcdbApi::mJAlienCredentials = nullptr;
5354

55+
CcdbApi::CcdbApi()
56+
{
57+
std::string host = boost::asio::ip::host_name();
58+
mUniqueAgentID = fmt::format("{}-{}-{}", host, getCurrentTimestamp() / 1000, o2::utils::Str::getRandomString(6));
59+
}
60+
5461
CcdbApi::~CcdbApi()
5562
{
5663
curl_global_cleanup();
@@ -74,7 +81,6 @@ void CcdbApi::init(std::string const& host)
7481

7582
if (host.substr(0, 7).compare(SNAPSHOTPREFIX) == 0) {
7683
auto path = host.substr(7);
77-
LOG(info) << "Initializing CcdbApi in snapshot readonly mode ... reading snapshot from path " << path;
7884
initInSnapshotMode(path);
7985
} else {
8086
initHostsPool(host);
@@ -83,7 +89,7 @@ void CcdbApi::init(std::string const& host)
8389

8490
// find out if we can can in principle connect to Alien
8591
mHaveAlienToken = checkAlienToken();
86-
LOG(info) << "Is alien token present?: " << mHaveAlienToken;
92+
LOGP(info, "Init CcdApi with UserAgentID: {}, Host: {}{}, alien-token: {}", mUniqueAgentID, host, mInSnapshotMode ? "(snapshot readonly mode)" : "", mHaveAlienToken);
8793
}
8894

8995
/**
@@ -189,6 +195,7 @@ int CcdbApi::storeAsBinaryFile(const char* buffer, size_t size, const std::strin
189195
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
190196
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
191197
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
198+
curl_easy_setopt(curl, CURLOPT_USERAGENT, mUniqueAgentID.c_str());
192199

193200
CURLcode res = CURL_LAST;
194201

@@ -395,7 +402,6 @@ void CcdbApi::initCurlOptionsForRetrieve(CURL* curlHandle, void* chunk, CurlWrit
395402
{
396403
curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, writeCallback);
397404
curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, chunk);
398-
curl_easy_setopt(curlHandle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
399405
curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, followRedirect ? 1L : 0L);
400406
}
401407

@@ -441,6 +447,8 @@ void CcdbApi::initHeadersForRetrieve(CURL* curlHandle, long timestamp, std::map<
441447
if (list) {
442448
curl_easy_setopt(curlHandle, CURLOPT_HTTPHEADER, list);
443449
}
450+
451+
curl_easy_setopt(curlHandle, CURLOPT_USERAGENT, mUniqueAgentID.c_str());
444452
}
445453

446454
bool CcdbApi::receiveToFile(FILE* fileHandle, std::string const& path, std::map<std::string, std::string> const& metadata,
@@ -634,7 +642,7 @@ bool CcdbApi::retrieveBlob(std::string const& path, std::string const& targetdir
634642

635643
/* some servers don't like requests that are made without a user-agent
636644
field, so we provide one */
637-
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
645+
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, mUniqueAgentID.c_str());
638646

639647
/* if redirected , we tell libcurl to follow redirection */
640648
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
@@ -1209,6 +1217,7 @@ std::map<std::string, std::string> CcdbApi::retrieveHeaders(std::string const& p
12091217
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
12101218
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_map_callback<>);
12111219
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &headers);
1220+
curl_easy_setopt(curl, CURLOPT_USERAGENT, mUniqueAgentID.c_str());
12121221

12131222
curlSetSSLOptions(curl);
12141223

@@ -1239,7 +1248,7 @@ std::map<std::string, std::string> CcdbApi::retrieveHeaders(std::string const& p
12391248
return headers;
12401249
}
12411250

1242-
bool CcdbApi::getCCDBEntryHeaders(std::string const& url, std::string const& etag, std::vector<std::string>& headers)
1251+
bool CcdbApi::getCCDBEntryHeaders(std::string const& url, std::string const& etag, std::vector<std::string>& headers, const std::string& agentID)
12431252
{
12441253
auto curl = curl_easy_init();
12451254
headers.clear();
@@ -1258,6 +1267,9 @@ bool CcdbApi::getCCDBEntryHeaders(std::string const& url, std::string const& eta
12581267
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
12591268
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
12601269
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &headers);
1270+
if (!agentID.empty()) {
1271+
curl_easy_setopt(curl, CURLOPT_USERAGENT, agentID.c_str());
1272+
}
12611273

12621274
curlSetSSLOptions(curl);
12631275

@@ -1366,7 +1378,7 @@ void CcdbApi::updateMetadata(std::string const& path, std::map<std::string, std:
13661378
if (curl != nullptr) {
13671379
curl_easy_setopt(curl, CURLOPT_URL, fullUrl.str().c_str());
13681380
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); // make sure we use PUT
1369-
1381+
curl_easy_setopt(curl, CURLOPT_USERAGENT, mUniqueAgentID.c_str());
13701382
curlSetSSLOptions(curl);
13711383

13721384
// Perform the request, res will get the return code

0 commit comments

Comments
 (0)