From ffdd1adaa98dba9289ad6c041cd9b81bc5ea1c83 Mon Sep 17 00:00:00 2001 From: Henrique S Matulis <69014250+hsmatulisgoogle@users.noreply.github.com> Date: Tue, 17 Nov 2020 19:41:25 -0500 Subject: [PATCH 1/4] Add option for passing in curl library when compiling --- CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 77c501d3556..207a288a053 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,10 @@ else() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FILENAME__=__FILE__") endif() +SET(GCC_LIBCURL_LINK_FLAGS "-L/usr/lib/x86_64-linux-gnu -lcurl") +SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}") + + include(GNUInstallDirs) include(ExternalProject) include(cmake/FindJournald.cmake) From e53aebdf998d64a2a9b6084d449e4a682f10e388 Mon Sep 17 00:00:00 2001 From: Henrique S Matulis <69014250+hsmatulisgoogle@users.noreply.github.com> Date: Tue, 17 Nov 2020 22:28:49 -0500 Subject: [PATCH 2/4] Try curl --- plugins/out_stackdriver/stackdriver.c | 85 ++++++++++++--------------- 1 file changed, 36 insertions(+), 49 deletions(-) diff --git a/plugins/out_stackdriver/stackdriver.c b/plugins/out_stackdriver/stackdriver.c index 21a7637e172..82a403befef 100644 --- a/plugins/out_stackdriver/stackdriver.c +++ b/plugins/out_stackdriver/stackdriver.c @@ -27,6 +27,7 @@ #include #include +#include #include "gce_metadata.h" #include "stackdriver.h" @@ -1816,6 +1817,15 @@ static void cb_stackdriver_flush(const void *data, size_t bytes, FLB_OUTPUT_RETURN(FLB_RETRY); } + /* Get or renew Token */ + token = get_google_token(ctx); + if (!token) { + flb_plg_error(ctx->ins, "cannot retrieve oauth2 token"); + flb_upstream_conn_release(u_conn); + FLB_OUTPUT_RETURN(FLB_RETRY); + } + flb_upstream_conn_release(u_conn); + /* Reformat msgpack to stackdriver JSON payload */ ret = stackdriver_format(config, i_ins, ctx, NULL, @@ -1823,66 +1833,43 @@ static void cb_stackdriver_flush(const void *data, size_t bytes, data, bytes, &out_buf, &out_size); if (ret != 0) { - flb_upstream_conn_release(u_conn); FLB_OUTPUT_RETURN(FLB_RETRY); } payload_buf = (flb_sds_t) out_buf; payload_size = out_size; - /* Get or renew Token */ - token = get_google_token(ctx); - if (!token) { - flb_plg_error(ctx->ins, "cannot retrieve oauth2 token"); - flb_upstream_conn_release(u_conn); - flb_sds_destroy(payload_buf); - FLB_OUTPUT_RETURN(FLB_RETRY); - } - - /* Compose HTTP Client request */ - c = flb_http_client(u_conn, FLB_HTTP_POST, FLB_STD_WRITE_URI, - payload_buf, payload_size, NULL, 0, NULL, 0); - - flb_http_buffer_size(c, 4192); - - flb_http_add_header(c, "User-Agent", 10, "Fluent-Bit", 10); - flb_http_add_header(c, "Content-Type", 12, "application/json", 16); - - /* Compose and append Authorization header */ - set_authorization_header(c, token); - - /* Send HTTP request */ - ret = flb_http_do(c, &b_sent); - - /* validate response */ - if (ret != 0) { - flb_plg_warn(ctx->ins, "http_do=%i", ret); + CURL *curl = curl_easy_init(); + + if (curl == NULL){ + flb_plg_error(ctx->ins, "cannot initialize curl"); + FLB_OUTPUT_RETURN(FLB_ERROR); + } + curl_easy_setopt(curl, CURLOPT_URL, FLB_STD_WRITE_URL); + curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, token); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "Fluent-Bit"); + curl_easy_setopt(curl, CURLOPT_POST, 1L); + + struct curl_slist *hs=NULL; + hs = curl_slist_append(hs, "Content-Type: application/json"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs); + + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, payload_size); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload_buf); + + CURLcode res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + + if (res == CURLE_OK){ + ret_code = FLB_OK; + } else { + /* we got an error */ + flb_plg_warn(ctx->ins, "error making http request"); ret_code = FLB_RETRY; } - else { - /* The request was issued successfully, validate the 'error' field */ - flb_plg_debug(ctx->ins, "HTTP Status=%i", c->resp.status); - if (c->resp.status == 200) { - ret_code = FLB_OK; - } - else { - if (c->resp.payload_size > 0) { - /* we got an error */ - flb_plg_warn(ctx->ins, "error\n%s", - c->resp.payload); - } - else { - flb_plg_debug(ctx->ins, "response\n%s", - c->resp.payload); - } - ret_code = FLB_RETRY; - } - } /* Cleanup */ flb_sds_destroy(payload_buf); - flb_http_client_destroy(c); - flb_upstream_conn_release(u_conn); /* Done */ FLB_OUTPUT_RETURN(ret_code); From f02e8d45f8a7a8ccb2958ffd740ee541a72dd1d9 Mon Sep 17 00:00:00 2001 From: Henrique S Matulis <69014250+hsmatulisgoogle@users.noreply.github.com> Date: Tue, 17 Nov 2020 23:26:31 -0500 Subject: [PATCH 3/4] Fix curl dep --- CMakeLists.txt | 5 +++-- plugins/out_stackdriver/CMakeLists.txt | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 207a288a053..9da722ac301 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,8 +44,9 @@ else() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FILENAME__=__FILE__") endif() -SET(GCC_LIBCURL_LINK_FLAGS "-L/usr/lib/x86_64-linux-gnu -lcurl") -SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}") +set(CURL_LIBRARY "-lcurl") +find_package(CURL REQUIRED) +include_directories(${CURL_INCLUDE_DIR}) include(GNUInstallDirs) diff --git a/plugins/out_stackdriver/CMakeLists.txt b/plugins/out_stackdriver/CMakeLists.txt index 9c80c7a2d9e..0ed58575bd0 100644 --- a/plugins/out_stackdriver/CMakeLists.txt +++ b/plugins/out_stackdriver/CMakeLists.txt @@ -9,4 +9,4 @@ set(src stackdriver_helper.c ) -FLB_PLUGIN(out_stackdriver "${src}" "") +FLB_PLUGIN(out_stackdriver "${src}" "curl") From 9eb27fffaa9fb4d209548cd67eab66baceb85617 Mon Sep 17 00:00:00 2001 From: Henrique S Matulis <69014250+hsmatulisgoogle@users.noreply.github.com> Date: Wed, 18 Nov 2020 00:35:23 -0500 Subject: [PATCH 4/4] test --- plugins/out_stackdriver/stackdriver.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/out_stackdriver/stackdriver.c b/plugins/out_stackdriver/stackdriver.c index 82a403befef..7134faedd15 100644 --- a/plugins/out_stackdriver/stackdriver.c +++ b/plugins/out_stackdriver/stackdriver.c @@ -1846,12 +1846,18 @@ static void cb_stackdriver_flush(const void *data, size_t bytes, FLB_OUTPUT_RETURN(FLB_ERROR); } curl_easy_setopt(curl, CURLOPT_URL, FLB_STD_WRITE_URL); - curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, token); curl_easy_setopt(curl, CURLOPT_USERAGENT, "Fluent-Bit"); curl_easy_setopt(curl, CURLOPT_POST, 1L); struct curl_slist *hs=NULL; + int len; + char header[512]; + + len = snprintf(header, sizeof(header) - 1, + "Authorization: Bearer %s", token); + header[len] = '\0'; hs = curl_slist_append(hs, "Content-Type: application/json"); + hs = curl_slist_append(hs, header); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, payload_size);