Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

out_cloudwatch: add account ID support for CloudWatch entity #4

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 17 additions & 1 deletion 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 @@ -360,7 +368,7 @@ static int init_put_payload(struct flb_cloudwatch *ctx, struct cw_flush *buf,
}
// If we are missing the service name, the entity will get rejected by the frontend anyway
swapneils marked this conversation as resolved.
Show resolved Hide resolved
// so do not emit entity unless service name is filled
if(ctx->add_entity && stream->entity != NULL && stream->entity->key_attributes != NULL && stream->entity->key_attributes->name != NULL) {
if(ctx->add_entity && stream->entity != NULL && stream->entity->key_attributes != NULL && stream->entity->key_attributes->name != NULL && stream->entity->key_attributes->account_id != NULL) {
if (!try_to_write(buf->out_buf, offset, buf->out_buf_size,
"\"entity\":{", 10)) {
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
Loading