@@ -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 );
0 commit comments