Skip to content

Commit

Permalink
out_cloudwatch_logs: Only create log group if it does not already exi…
Browse files Browse the repository at this point in the history
…st to prevent throttling

Signed-off-by: Wesley Pettit <[email protected]>
  • Loading branch information
PettitWesley committed Feb 15, 2022
1 parent 46ea2bd commit 632914b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
27 changes: 27 additions & 0 deletions plugins/out_cloudwatch_logs/cloudwatch_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

#define ERR_CODE_ALREADY_EXISTS "ResourceAlreadyExistsException"
#define ERR_CODE_INVALID_SEQUENCE_TOKEN "InvalidSequenceTokenException"
#define ERR_CODE_NOT_FOUND "ResourceNotFoundException"


#define AMZN_REQUEST_ID_HEADER "x-amzn-RequestId"

Expand Down Expand Up @@ -1243,6 +1245,9 @@ int create_log_stream(struct flb_cloudwatch *ctx, struct log_stream *stream)
flb_sds_t body;
flb_sds_t tmp;
flb_sds_t error;
int ret;

retry_create_stream:

flb_plg_info(ctx->ins, "Creating log stream %s in log group %s",
stream->name, ctx->log_group);
Expand Down Expand Up @@ -1301,6 +1306,28 @@ int create_log_stream(struct flb_cloudwatch *ctx, struct log_stream *stream)
flb_http_client_destroy(c);
return 0;
}

if (strcmp(error, ERR_CODE_NOT_FOUND) == 0) {
flb_sds_destroy(body);
flb_sds_destroy(error);
flb_http_client_destroy(c);

if (ctx->create_group == FLB_TRUE) {
flb_plg_info(ctx->ins, "Log Group %s not found. Will attempt to create it.",
ctx->log_group);
ret = create_log_group(ctx);
if (ret < 0) {
return -1;
} else {
/* retry stream creation */
goto retry_create_stream;
}
} else {
flb_plg_error(ctx->ins, "Log Group %s not found and `auto_create_group` disabled.",
ctx->log_group);
}
return -1;
}
/* some other error occurred; notify user */
flb_aws_print_error(c->resp.payload, c->resp.payload_size,
"CreateLogStream", ctx->ins);
Expand Down
7 changes: 0 additions & 7 deletions plugins/out_cloudwatch_logs/cloudwatch_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,6 @@ static void cb_cloudwatch_flush(const void *data, size_t bytes,

ctx->buf->put_events_calls = 0;

if (ctx->create_group == FLB_TRUE && ctx->group_created == FLB_FALSE) {
ret = create_log_group(ctx);
if (ret < 0) {
FLB_OUTPUT_RETURN(FLB_RETRY);
}
}

stream = get_log_stream(ctx, tag, tag_len);
if (!stream) {
FLB_OUTPUT_RETURN(FLB_RETRY);
Expand Down

0 comments on commit 632914b

Please sign in to comment.