Skip to content

Commit ae15206

Browse files
committed
Enable curl verbose output and add debug function callback
1 parent 5e62859 commit ae15206

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ class HttpOperation
102102

103103
static size_t ReadMemoryCallback(char *buffer, size_t size, size_t nitems, void *userp);
104104

105+
static int CurlLoggerCallback(const CURL * /* handle */,
106+
curl_infotype type,
107+
const char *data,
108+
size_t size,
109+
void * /* clientp */) noexcept;
110+
105111
#if LIBCURL_VERSION_NUM >= 0x075000
106112
static int PreRequestCallback(void *clientp,
107113
char *conn_primary_ip,

ext/src/http/client/curl/http_operation_curl.cc

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "opentelemetry/ext/http/client/curl/http_client_curl.h"
2323
#include "opentelemetry/ext/http/client/curl/http_operation_curl.h"
2424
#include "opentelemetry/ext/http/client/http_client.h"
25+
#include "opentelemetry/nostd/string_view.h"
2526
#include "opentelemetry/sdk/common/global_log_handler.h"
2627
#include "opentelemetry/version.h"
2728

@@ -569,6 +570,60 @@ CURLcode HttpOperation::SetCurlOffOption(CURLoption option, curl_off_t value)
569570
return rc;
570571
}
571572

573+
int HttpOperation::CurlLoggerCallback(const CURL * /* handle */,
574+
curl_infotype type,
575+
const char *data,
576+
size_t size,
577+
void * /* clientp */) noexcept
578+
{
579+
static const auto kTlsInfo = nostd::string_view("SSL connection using");
580+
static const auto kFailureMsg = nostd::string_view("Recv failure:");
581+
static const auto kHeaderSent = nostd::string_view("Send header => ");
582+
static const auto kHeaderRecv = nostd::string_view("Recv header => ");
583+
584+
if (nostd::string_view text_to_log{data, size};
585+
text_to_log.empty() || std::isspace(text_to_log[0]))
586+
{
587+
return 0;
588+
}
589+
else if (type == CURLINFO_TEXT)
590+
{
591+
if (text_to_log.substr(0, kTlsInfo.size()) == kTlsInfo)
592+
{
593+
OTEL_INTERNAL_LOG_INFO(text_to_log);
594+
}
595+
else if (text_to_log.substr(0, kFailureMsg.size()) == kFailureMsg)
596+
{
597+
OTEL_INTERNAL_LOG_ERROR(text_to_log);
598+
}
599+
#ifdef CURL_DEBUG
600+
else
601+
{
602+
OTEL_INTERNAL_LOG_DEBUG(text_to_log);
603+
}
604+
#endif // CURL_DEBUG
605+
}
606+
#ifdef CURL_DEBUG
607+
else if (type == CURLINFO_HEADER_OUT)
608+
{
609+
while (!text_to_log.empty() && !std::isspace(text_to_log[0]))
610+
{
611+
if (const auto pos = text_to_log.find('\n'); pos != nostd::string_view::npos)
612+
{
613+
OTEL_INTERNAL_LOG_DEBUG(kHeaderSent << text_to_log.substr(0, pos));
614+
text_to_log = text_to_log.substr(pos + 1);
615+
}
616+
}
617+
}
618+
else if (type == CURLINFO_HEADER_IN)
619+
{
620+
OTEL_INTERNAL_LOG_DEBUG(kHeaderRecv << text_to_log);
621+
}
622+
#endif // CURL_DEBUG
623+
624+
return 0;
625+
}
626+
572627
CURLcode HttpOperation::Setup()
573628
{
574629
if (!curl_resource_.easy_handle)
@@ -581,7 +636,14 @@ CURLcode HttpOperation::Setup()
581636
curl_error_message_[0] = '\0';
582637
curl_easy_setopt(curl_resource_.easy_handle, CURLOPT_ERRORBUFFER, curl_error_message_);
583638

584-
rc = SetCurlLongOption(CURLOPT_VERBOSE, 0L);
639+
rc = SetCurlLongOption(CURLOPT_VERBOSE, 1L);
640+
if (rc != CURLE_OK)
641+
{
642+
return rc;
643+
}
644+
645+
rc = SetCurlPtrOption(CURLOPT_DEBUGFUNCTION,
646+
reinterpret_cast<void *>(&HttpOperation::CurlLoggerCallback));
585647
if (rc != CURLE_OK)
586648
{
587649
return rc;

0 commit comments

Comments
 (0)