Skip to content

Commit ac3e0ea

Browse files
PettitWesleyedsiper
authored andcommitted
out_cloudwatch_logs: Only create log group if it does not already exist to prevent throttling
Signed-off-by: Wesley Pettit <[email protected]>
1 parent 8e672f3 commit ac3e0ea

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

plugins/out_cloudwatch_logs/cloudwatch_api.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ struct log_stream *get_dynamic_log_stream(struct flb_cloudwatch *ctx,
10421042
}
10431043
new_stream->name = name;
10441044

1045-
ret = create_log_stream(ctx, new_stream);
1045+
ret = create_log_stream(ctx, new_stream, FLB_TRUE);
10461046
if (ret < 0) {
10471047
log_stream_destroy(new_stream);
10481048
return NULL;
@@ -1062,7 +1062,7 @@ struct log_stream *get_log_stream(struct flb_cloudwatch *ctx,
10621062
if (ctx->log_stream_name) {
10631063
stream = &ctx->stream;
10641064
if (ctx->stream_created == FLB_FALSE) {
1065-
ret = create_log_stream(ctx, stream);
1065+
ret = create_log_stream(ctx, stream, FLB_TRUE);
10661066
if (ret < 0) {
10671067
return NULL;
10681068
}
@@ -1236,14 +1236,16 @@ int create_log_group(struct flb_cloudwatch *ctx)
12361236
return -1;
12371237
}
12381238

1239-
int create_log_stream(struct flb_cloudwatch *ctx, struct log_stream *stream)
1239+
int create_log_stream(struct flb_cloudwatch *ctx, struct log_stream *stream,
1240+
int can_retry)
12401241
{
12411242

12421243
struct flb_http_client *c = NULL;
12431244
struct flb_aws_client *cw_client;
12441245
flb_sds_t body;
12451246
flb_sds_t tmp;
12461247
flb_sds_t error;
1248+
int ret;
12471249

12481250
flb_plg_info(ctx->ins, "Creating log stream %s in log group %s",
12491251
stream->name, ctx->log_group);
@@ -1302,6 +1304,33 @@ int create_log_stream(struct flb_cloudwatch *ctx, struct log_stream *stream)
13021304
flb_http_client_destroy(c);
13031305
return 0;
13041306
}
1307+
1308+
if (strcmp(error, ERR_CODE_NOT_FOUND) == 0) {
1309+
flb_sds_destroy(body);
1310+
flb_sds_destroy(error);
1311+
flb_http_client_destroy(c);
1312+
1313+
if (ctx->create_group == FLB_TRUE) {
1314+
flb_plg_info(ctx->ins, "Log Group %s not found. Will attempt to create it.",
1315+
ctx->log_group);
1316+
ret = create_log_group(ctx);
1317+
if (ret < 0) {
1318+
return -1;
1319+
} else {
1320+
if (can_retry == FLB_TRUE) {
1321+
/* retry stream creation */
1322+
return create_log_stream(ctx, stream, FLB_FALSE);
1323+
} else {
1324+
/* we failed to create the stream */
1325+
return -1;
1326+
}
1327+
}
1328+
} else {
1329+
flb_plg_error(ctx->ins, "Log Group %s not found and `auto_create_group` disabled.",
1330+
ctx->log_group);
1331+
}
1332+
return -1;
1333+
}
13051334
/* some other error occurred; notify user */
13061335
flb_aws_print_error(c->resp.payload, c->resp.payload_size,
13071336
"CreateLogStream", ctx->ins);

plugins/out_cloudwatch_logs/cloudwatch_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void cw_flush_destroy(struct cw_flush *buf);
4545
int process_and_send(struct flb_cloudwatch *ctx, const char *input_plugin, struct cw_flush *buf,
4646
struct log_stream *stream,
4747
const char *data, size_t bytes);
48-
int create_log_stream(struct flb_cloudwatch *ctx, struct log_stream *stream);
48+
int create_log_stream(struct flb_cloudwatch *ctx, struct log_stream *stream, int can_retry);
4949
struct log_stream *get_log_stream(struct flb_cloudwatch *ctx,
5050
const char *tag, int tag_len);
5151
int put_log_events(struct flb_cloudwatch *ctx, struct cw_flush *buf,

plugins/out_cloudwatch_logs/cloudwatch_logs.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,21 +382,13 @@ static void cb_cloudwatch_flush(struct flb_event_chunk *event_chunk,
382382
struct flb_config *config)
383383
{
384384
struct flb_cloudwatch *ctx = out_context;
385-
int ret;
386385
int event_count;
387386
struct log_stream *stream = NULL;
388387
(void) i_ins;
389388
(void) config;
390389

391390
ctx->buf->put_events_calls = 0;
392391

393-
if (ctx->create_group == FLB_TRUE && ctx->group_created == FLB_FALSE) {
394-
ret = create_log_group(ctx);
395-
if (ret < 0) {
396-
FLB_OUTPUT_RETURN(FLB_RETRY);
397-
}
398-
}
399-
400392
stream = get_log_stream(ctx,
401393
event_chunk->tag, flb_sds_len(event_chunk->tag));
402394
if (!stream) {

0 commit comments

Comments
 (0)