From 93bb27ed7a13df3dd47ef9696d171872d358df6b Mon Sep 17 00:00:00 2001 From: Wesley Pettit Date: Thu, 9 Sep 2021 17:22:26 -0700 Subject: [PATCH 1/2] out_cloudwatch_logs: auto retry invalid requests Signed-off-by: Wesley Pettit --- plugins/out_cloudwatch_logs/cloudwatch_api.c | 39 ++++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/plugins/out_cloudwatch_logs/cloudwatch_api.c b/plugins/out_cloudwatch_logs/cloudwatch_api.c index b87831621f1..5a46770999c 100644 --- a/plugins/out_cloudwatch_logs/cloudwatch_api.c +++ b/plugins/out_cloudwatch_logs/cloudwatch_api.c @@ -1313,6 +1313,7 @@ int put_log_events(struct flb_cloudwatch *ctx, struct cw_flush *buf, flb_sds_t tmp; flb_sds_t error; int num_headers = 1; + int retry = FLB_TRUE; buf->put_events_calls++; @@ -1336,6 +1337,7 @@ int put_log_events(struct flb_cloudwatch *ctx, struct cw_flush *buf, num_headers = 2; } +retry_request: if (plugin_under_test() == FLB_TRUE) { c = mock_http_call("TEST_PUT_LOG_EVENTS_ERROR", "PutLogEvents"); } @@ -1350,6 +1352,31 @@ int put_log_events(struct flb_cloudwatch *ctx, struct cw_flush *buf, flb_plg_debug(ctx->ins, "PutLogEvents http status=%d", c->resp.status); if (c->resp.status == 200) { + if (c->resp.data == NULL || c->resp.data_len == 0 || strstr(c->resp.data, AMZN_REQUEST_ID_HEADER) == NULL) { + /* code was 200, but response is invalid, treat as failure */ + if (retry == FLB_TRUE) { + flb_plg_debug(ctx->ins, "Recieved code 200 but response was invalid, %s header not found", + AMZN_REQUEST_ID_HEADER); + } + else { + flb_plg_error(ctx->ins, "Recieved code 200 but response was invalid, %s header not found", + AMZN_REQUEST_ID_HEADER); + } + if (c->resp.data != NULL) { + flb_plg_debug(ctx->ins, "Could not find sequence token in " + "response: response body is empty: full data: `%.*s`", c->resp.data_len, c->resp.data); + } + flb_http_client_destroy(c); + + if (retry == FLB_TRUE) { + flb_plg_debug(ctx->ins, "issuing immediate retry for invalid request"); + retry = FLB_FALSE; + goto retry_request; + } + return -1; + } + + /* success */ if (c->resp.payload_size > 0) { flb_plg_debug(ctx->ins, "Sent events to %s", stream->name); @@ -1370,18 +1397,6 @@ int put_log_events(struct flb_cloudwatch *ctx, struct cw_flush *buf, } } - if (c->resp.data == NULL || c->resp.data_len == 0 || strstr(c->resp.data, AMZN_REQUEST_ID_HEADER) == NULL) { - /* code was 200, but response is invalid, treat as failure */ - flb_plg_error(ctx->ins, "Recieved code 200 but response was invalid, %s header not found", - AMZN_REQUEST_ID_HEADER); - if (c->resp.data != NULL) { - flb_plg_debug(ctx->ins, "Could not find sequence token in " - "response: response body is empty: full data: `%.*s`", c->resp.data_len, c->resp.data); - } - flb_http_client_destroy(c); - return -1; - } - flb_http_client_destroy(c); return 0; } From b776778ac18128d37ee1368ca13072177cca965c Mon Sep 17 00:00:00 2001 From: Wesley Pettit Date: Sun, 21 Nov 2021 16:09:06 -0800 Subject: [PATCH 2/2] out_cloudwatch_logs: simplify error logging for invalid responses Signed-off-by: Wesley Pettit --- plugins/out_cloudwatch_logs/cloudwatch_api.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/plugins/out_cloudwatch_logs/cloudwatch_api.c b/plugins/out_cloudwatch_logs/cloudwatch_api.c index 5a46770999c..9b8f0c06421 100644 --- a/plugins/out_cloudwatch_logs/cloudwatch_api.c +++ b/plugins/out_cloudwatch_logs/cloudwatch_api.c @@ -1354,14 +1354,6 @@ int put_log_events(struct flb_cloudwatch *ctx, struct cw_flush *buf, if (c->resp.status == 200) { if (c->resp.data == NULL || c->resp.data_len == 0 || strstr(c->resp.data, AMZN_REQUEST_ID_HEADER) == NULL) { /* code was 200, but response is invalid, treat as failure */ - if (retry == FLB_TRUE) { - flb_plg_debug(ctx->ins, "Recieved code 200 but response was invalid, %s header not found", - AMZN_REQUEST_ID_HEADER); - } - else { - flb_plg_error(ctx->ins, "Recieved code 200 but response was invalid, %s header not found", - AMZN_REQUEST_ID_HEADER); - } if (c->resp.data != NULL) { flb_plg_debug(ctx->ins, "Could not find sequence token in " "response: response body is empty: full data: `%.*s`", c->resp.data_len, c->resp.data); @@ -1369,10 +1361,12 @@ int put_log_events(struct flb_cloudwatch *ctx, struct cw_flush *buf, flb_http_client_destroy(c); if (retry == FLB_TRUE) { - flb_plg_debug(ctx->ins, "issuing immediate retry for invalid request"); + flb_plg_debug(ctx->ins, "issuing immediate retry for invalid response"); retry = FLB_FALSE; goto retry_request; } + flb_plg_error(ctx->ins, "Recieved code 200 but response was invalid, %s header not found", + AMZN_REQUEST_ID_HEADER); return -1; }