Skip to content

Commit 8293e93

Browse files
committed
filter_aws: send entity attributes for telemetry association
Signed-off-by: Zhihong Lin <zhiholin@amazon.com>
1 parent 9def01d commit 8293e93

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

plugins/filter_aws/aws.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,30 @@ static int get_ec2_metadata(struct flb_filter_aws *ctx)
926926
ctx->metadata_retrieved = FLB_TRUE;
927927
}
928928

929+
if (ctx->enable_entity) {
930+
if (!ctx->account_id) {
931+
ret = flb_aws_imds_request_by_key(ctx->client_imds, FLB_AWS_IMDS_ACCOUNT_ID_PATH,
932+
&ctx->account_id, &ctx->account_id_len,
933+
"accountId");
934+
935+
if (ret < 0) {
936+
flb_plg_error(ctx->ins, "Failed to get Account ID");
937+
return -1;
938+
}
939+
}
940+
941+
if (!ctx->instance_id) {
942+
ret = flb_aws_imds_request(ctx->client_imds, FLB_AWS_IMDS_INSTANCE_ID_PATH,
943+
&ctx->instance_id,
944+
&ctx->instance_id_len);
945+
if (ret < 0) {
946+
flb_plg_error(ctx->ins, "Failed to get instance ID");
947+
return -1;
948+
}
949+
}
950+
}
951+
952+
ctx->metadata_retrieved = FLB_TRUE;
929953
return 0;
930954
}
931955

@@ -1104,6 +1128,23 @@ static int cb_aws_filter(const void *data, size_t bytes,
11041128
}
11051129
}
11061130

1131+
if (ctx->enable_entity &&
1132+
ctx->instance_id &&
1133+
ctx->account_id &&
1134+
ret == FLB_EVENT_ENCODER_SUCCESS) {
1135+
ret = flb_log_event_encoder_append_body_values(
1136+
&log_encoder,
1137+
FLB_LOG_EVENT_CSTRING_VALUE(FLB_FILTER_AWS_ENTITY_INSTANCE_ID_KEY),
1138+
FLB_LOG_EVENT_STRING_VALUE(ctx->instance_id,
1139+
ctx->instance_id_len));
1140+
ret = flb_log_event_encoder_append_body_values(
1141+
&log_encoder,
1142+
FLB_LOG_EVENT_CSTRING_VALUE(FLB_FILTER_AWS_ENTITY_ACCOUNT_ID_KEY),
1143+
FLB_LOG_EVENT_STRING_VALUE(ctx->account_id,
1144+
ctx->account_id_len));
1145+
}
1146+
1147+
11071148
if (ret == FLB_EVENT_ENCODER_SUCCESS) {
11081149
ret = flb_log_event_encoder_commit_record(&log_encoder);
11091150
}
@@ -1273,6 +1314,12 @@ static struct flb_config_map config_map[] = {
12731314
0, FLB_TRUE, offsetof(struct flb_filter_aws, retry_required_interval),
12741315
"Defines minimum duration between retries for fetching metadata groups"
12751316
},
1317+
{
1318+
FLB_CONFIG_MAP_BOOL, "enable_entity", "false",
1319+
0, FLB_TRUE, offsetof(struct flb_filter_aws, enable_entity),
1320+
"Enable entity prefix for fields used for constructing entity."
1321+
"This currently only affects instance ID"
1322+
},
12761323
{0}
12771324
};
12781325

plugins/filter_aws/aws.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#define FLB_FILTER_AWS_AVAILABILITY_ZONE_KEY_LEN 2
2828
#define FLB_FILTER_AWS_INSTANCE_ID_KEY "ec2_instance_id"
2929
#define FLB_FILTER_AWS_INSTANCE_ID_KEY_LEN 15
30+
#define FLB_FILTER_AWS_ENTITY_INSTANCE_ID_KEY "aws_entity_ec2_instance_id"
31+
#define FLB_FILTER_AWS_ENTITY_INSTANCE_ID_KEY_LEN 26
3032
#define FLB_FILTER_AWS_INSTANCE_TYPE_KEY "ec2_instance_type"
3133
#define FLB_FILTER_AWS_INSTANCE_TYPE_KEY_LEN 17
3234
#define FLB_FILTER_AWS_PRIVATE_IP_KEY "private_ip"
@@ -37,6 +39,8 @@
3739
#define FLB_FILTER_AWS_AMI_ID_KEY_LEN 6
3840
#define FLB_FILTER_AWS_ACCOUNT_ID_KEY "account_id"
3941
#define FLB_FILTER_AWS_ACCOUNT_ID_KEY_LEN 10
42+
#define FLB_FILTER_AWS_ENTITY_ACCOUNT_ID_KEY "aws_entity_account_id"
43+
#define FLB_FILTER_AWS_ENTITY_ACCOUNT_ID_KEY_LEN 21
4044
#define FLB_FILTER_AWS_HOSTNAME_KEY "hostname"
4145
#define FLB_FILTER_AWS_HOSTNAME_KEY_LEN 8
4246

@@ -110,6 +114,11 @@ struct flb_filter_aws {
110114
/* tags_* fields are related to exposing EC2 tags in log labels
111115
* tags_enabled defines if EC2 tags functionality is enabled */
112116
int tags_enabled;
117+
/*
118+
* Enable entity prefix appending. This appends
119+
* 'aws_entity' to relevant keys
120+
*/
121+
int enable_entity;
113122

114123
/* tags_fetched defines if tag keys and values were fetched successfully
115124
* and might be used to inject into msgpack */

0 commit comments

Comments
 (0)