Skip to content

Commit

Permalink
Add account ID support for CloudWatch entity
Browse files Browse the repository at this point in the history
  • Loading branch information
zhihonl committed Oct 30, 2024
1 parent dca1459 commit 6097557
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion plugins/filter_aws/aws.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,14 +616,22 @@ static int cb_aws_filter(const void *data, size_t bytes,
ctx->ami_id, ctx->ami_id_len);
}

if (ctx->account_id_include) {
if (ctx->account_id_include && !ctx->enable_entity) {
msgpack_pack_str(&tmp_pck, FLB_FILTER_AWS_ACCOUNT_ID_KEY_LEN);
msgpack_pack_str_body(&tmp_pck,
FLB_FILTER_AWS_ACCOUNT_ID_KEY,
FLB_FILTER_AWS_ACCOUNT_ID_KEY_LEN);
msgpack_pack_str(&tmp_pck, ctx->account_id_len);
msgpack_pack_str_body(&tmp_pck,
ctx->account_id, ctx->account_id_len);
} else if (ctx->account_id_include && ctx->enable_entity) {
msgpack_pack_str(&tmp_pck, FLB_FILTER_AWS_ENTITY_ACCOUNT_ID_KEY_LEN);
msgpack_pack_str_body(&tmp_pck,
FLB_FILTER_AWS_ENTITY_ACCOUNT_ID_KEY,
FLB_FILTER_AWS_ENTITY_ACCOUNT_ID_KEY_LEN);
msgpack_pack_str(&tmp_pck, ctx->account_id_len);
msgpack_pack_str_body(&tmp_pck,
ctx->account_id, ctx->account_id_len);
}

if (ctx->hostname_include) {
Expand Down
2 changes: 2 additions & 0 deletions plugins/filter_aws/aws.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
#define FLB_FILTER_AWS_AMI_ID_KEY_LEN 6
#define FLB_FILTER_AWS_ACCOUNT_ID_KEY "account_id"
#define FLB_FILTER_AWS_ACCOUNT_ID_KEY_LEN 10
#define FLB_FILTER_AWS_ENTITY_ACCOUNT_ID_KEY "aws_entity_account_id"
#define FLB_FILTER_AWS_ENTITY_ACCOUNT_ID_KEY_LEN 21
#define FLB_FILTER_AWS_HOSTNAME_KEY "hostname"
#define FLB_FILTER_AWS_HOSTNAME_KEY_LEN 8

Expand Down
16 changes: 16 additions & 0 deletions plugins/out_cloudwatch_logs/cloudwatch_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ static int entity_add_key_attributes(struct flb_cloudwatch *ctx, struct cw_flush
goto error;
}
}
if(stream->entity->key_attributes->account_id != NULL && strlen(stream->entity->key_attributes->account_id) != 0) {
if (!snprintf(ts,KEY_ATTRIBUTES_MAX_LEN, ",%s%s%s","\"AwsAccountId\":\"",stream->entity->key_attributes->account_id,"\"")) {
goto error;
}
if (!try_to_write(buf->out_buf, offset, buf->out_buf_size,ts,0)) {
goto error;
}
}
if (!try_to_write(buf->out_buf, offset, buf->out_buf_size,
"},", 2)) {
goto error;
Expand Down Expand Up @@ -1109,6 +1117,14 @@ void parse_entity(struct flb_cloudwatch *ctx, entity *entity, msgpack_object map
}
entity->attributes->instance_id = flb_strndup(val.via.str.ptr, val.via.str.size);
}
if(strncmp(key.via.str.ptr, "aws_entity_account_id",key.via.str.size ) == 0 ) {
if(entity->key_attributes->account_id == NULL) {
entity->root_filter_count++;
} else {
flb_free(entity->key_attributes->account_id);
}
entity->key_attributes->account_id = flb_strndup(val.via.str.ptr, val.via.str.size);
}
}
if(entity->key_attributes->name == NULL && entity->attributes->name_source == NULL &&entity->attributes->workload != NULL) {
entity->key_attributes->name = flb_strndup(entity->attributes->workload, strlen(entity->attributes->workload));
Expand Down
1 change: 1 addition & 0 deletions plugins/out_cloudwatch_logs/cloudwatch_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ void entity_destroy(entity *entity) {
flb_free(entity->key_attributes->environment);
flb_free(entity->key_attributes->name);
flb_free(entity->key_attributes->type);
flb_free(entity->key_attributes->account_id);
flb_free(entity->key_attributes);
}
flb_free(entity);
Expand Down
1 change: 1 addition & 0 deletions plugins/out_cloudwatch_logs/cloudwatch_logs.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef struct entity_key_attributes {
char *type;
char *name;
char *environment;
char *account_id;
}entity_key_attributes;

/* Attributes used for CloudWatch Entity object
Expand Down

0 comments on commit 6097557

Please sign in to comment.