Skip to content

Commit

Permalink
out_cloudwatch_logs: auto retry invalid requests
Browse files Browse the repository at this point in the history
Signed-off-by: Wesley Pettit <[email protected]>
  • Loading branch information
PettitWesley authored and nokute78 committed Nov 22, 2021
1 parent 766c63f commit 07baa55
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions plugins/out_cloudwatch_logs/cloudwatch_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;

Expand All @@ -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");
}
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down

0 comments on commit 07baa55

Please sign in to comment.