Skip to content

Commit 2a9c1a9

Browse files
Add a GUC to log HTTP requests
Especially useful for #68
1 parent 6019711 commit 2a9c1a9

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

pg_lake_iceberg/include/pg_lake/http/http_client.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ typedef struct
3434
const char *errorMsg; /* error message */
3535
} HttpResult;
3636

37+
extern bool HttpClientTraceTraffic;
38+
3739
/* plain C API (no PostgreSQL types) */
3840
extern PGDLLEXPORT HttpResult HttpGet(const char *url, List *headers);
3941
extern PGDLLEXPORT HttpResult HttpHead(const char *url, List *headers);

pg_lake_iceberg/src/http/http_client.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static void CurlGlobalCleanup(int code, Datum arg);
7070
static void CurlCleanup(CURL * curl, struct curl_slist *headerList);
7171
static HttpResult CurlReturnError(CURL * curl, struct curl_slist *headerList,
7272
CURLcode curlCode, const char *errorMsg);
73+
static const char * HttpRequestMethodToString(HttpMethod method);
7374

7475
#define CURL_SETOPT(curl, opt, value) do { \
7576
curlCode = curl_easy_setopt((curl), (opt), (value)); \
@@ -82,6 +83,9 @@ static HttpResult CurlReturnError(CURL * curl, struct curl_slist *headerList,
8283
static bool curlInitialized = false;
8384

8485

86+
bool HttpClientTraceTraffic = false;
87+
88+
8589
/*
8690
* CurlGloballyInitIfNotInitialized globally initiates curl state if not initialized.
8791
*/
@@ -447,6 +451,21 @@ HttpCommonNoThrows(HttpMethod method, const char *url, const char *postData, con
447451
struct curl_slist *curlHeaders = NULL;
448452
CURLcode curlCode = CURLE_OK;
449453

454+
if (HttpClientTraceTraffic && message_level_is_interesting(INFO))
455+
{
456+
StringInfo postDataInfo = NULL;
457+
458+
if (postData)
459+
{
460+
postDataInfo = makeStringInfo();
461+
appendStringInfo(postDataInfo, " : {%s}", postData);
462+
}
463+
464+
ereport(INFO, (errmsg("making %s request to URL %s%s",
465+
HttpRequestMethodToString(method), url,
466+
postDataInfo ? postDataInfo->data : "")));
467+
}
468+
450469
if (!CheckMinCurlVersion(curl_version_info(CURLVERSION_NOW)))
451470
return CurlReturnError(curl, curlHeaders, CURLE_FAILED_INIT, "pg_lake_iceberg requires Curl version 7.20.0 or higher");
452471

@@ -508,5 +527,32 @@ HttpCommonNoThrows(HttpMethod method, const char *url, const char *postData, con
508527

509528
ereport(DEBUG4, (errmsg("libcurl request completed successfully")));
510529

530+
if (HttpClientTraceTraffic && message_level_is_interesting(INFO))
531+
{
532+
ereport(INFO, (errmsg("received response with status code %ld, body: %s",
533+
res.status, res.body ? res.body : "<empty>")));
534+
}
535+
511536
return res;
512537
}
538+
539+
540+
static const char *
541+
HttpRequestMethodToString(HttpMethod method)
542+
{
543+
switch (method)
544+
{
545+
case HTTP_GET:
546+
return "GET";
547+
case HTTP_HEAD:
548+
return "HEAD";
549+
case HTTP_POST:
550+
return "POST";
551+
case HTTP_PUT:
552+
return "PUT";
553+
case HTTP_DELETE:
554+
return "DELETE";
555+
default:
556+
return "UNKNOWN";
557+
}
558+
}

pg_lake_iceberg/src/init.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ _PG_init(void)
139139
GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE,
140140
NULL, NULL, NULL);
141141

142+
143+
DefineCustomBoolVariable(
144+
"pg_lake_iceberg.http_client_trace_traffic",
145+
gettext_noop("When set to true, HTTP client logging is enabled."),
146+
NULL,
147+
&HttpClientTraceTraffic,
148+
false,
149+
PGC_USERSET,
150+
GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE,
151+
NULL, NULL, NULL);
152+
153+
142154
DefineCustomStringVariable("pg_lake_iceberg.default_location_prefix",
143155
gettext_noop("Specifies the default location prefix for "
144156
"iceberg tables. This is used when the location "

0 commit comments

Comments
 (0)