Skip to content

Commit

Permalink
filter: aws tags fetch retries at >5s interval
Browse files Browse the repository at this point in the history
retry interval is configurable with 'tags_retry_interval_s' option which
accepts integer, which allows the user to override the default 5s

Signed-off-by: Mateusz Warzyński <mateusz@warzynski.pro>
  • Loading branch information
mwarzynski authored and PettitWesley committed May 3, 2024
1 parent 16087f1 commit be44ed2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
56 changes: 46 additions & 10 deletions plugins/filter_aws/aws.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ static int get_ec2_metadata_base(struct flb_filter_aws *ctx)
{
int ret;

ctx->metadata_groups[FLB_FILTER_AWS_METADATA_GROUP_BASE].last_execution = time(NULL);

/* TODO: check this during review, I added this line -- I think it makes it more correct. */
/* it also might be fine to just delete new_keys as it's not really used anywhere */
ctx->new_keys = 0;
Expand Down Expand Up @@ -718,6 +720,8 @@ static int get_ec2_metadata_tags(struct flb_filter_aws *ctx)
int ret;
int i;

ctx->metadata_groups[FLB_FILTER_AWS_METADATA_GROUP_TAGS].last_execution = time(NULL);

if (ctx->tags_enabled && !ctx->tags_fetched) {
ret = get_ec2_tags(ctx);
if (ret < 0) {
Expand All @@ -734,13 +738,37 @@ static int get_ec2_metadata_tags(struct flb_filter_aws *ctx)
return 0;
}

static int ec2_metadata_group_should_fetch(struct flb_filter_aws *ctx, int group)
{
time_t now, required_interval, interval;

required_interval = ctx->metadata_groups[group].retry_required_interval;
if (required_interval == 0) {
return FLB_TRUE;
}

now = time(NULL);

interval = now - ctx->metadata_groups[group].last_execution;

if (interval < required_interval) {
return FLB_FALSE;
}
return FLB_TRUE;
}

/*
* Fetches all metadata values, including tags, from IMDS.
* Function handles retries as configured for each metadata group.
*
* Returns 0 on success, negative values on failures.
* Returns FLB_FILTER_AWS_CONFIGURATION_ERROR in case of configuration error.
*/
static int get_ec2_metadata(struct flb_filter_aws *ctx)
{
int ret;
int failures = 0;
int fetches_skipped = 0;

if (!ctx->metadata_groups[FLB_FILTER_AWS_METADATA_GROUP_BASE].done) {
ret = get_ec2_metadata_base(ctx);
Expand All @@ -751,20 +779,22 @@ static int get_ec2_metadata(struct flb_filter_aws *ctx)
}

if (!ctx->metadata_groups[FLB_FILTER_AWS_METADATA_GROUP_TAGS].done) {
/* TODO: retries with Fixed Interval Rate */
/* at the moment it will retry with every flush */
ret = get_ec2_metadata_tags(ctx);
if (ret == FLB_FILTER_AWS_CONFIGURATION_ERROR) {
return ret;
}
if (ret == 0) {
ctx->metadata_groups[FLB_FILTER_AWS_METADATA_GROUP_TAGS].done = FLB_TRUE;
if (!ec2_metadata_group_should_fetch(ctx, FLB_FILTER_AWS_METADATA_GROUP_TAGS)) {
fetches_skipped++;
} else {
failures++;
ret = get_ec2_metadata_tags(ctx);
if (ret == FLB_FILTER_AWS_CONFIGURATION_ERROR) {
return ret;
}
if (ret == 0) {
ctx->metadata_groups[FLB_FILTER_AWS_METADATA_GROUP_TAGS].done = FLB_TRUE;
} else {
failures++;
}
}
}

if (failures == 0) {
if (failures == 0 && fetches_skipped == 0) {
ctx->metadata_retrieved = FLB_TRUE;
}

Expand Down Expand Up @@ -1101,6 +1131,12 @@ static struct flb_config_map config_map[] = {
"if both tags_include and tags_exclude are specified, configuration is invalid"
" and plugin fails"
},
{
FLB_CONFIG_MAP_INT, "tags_retry_interval_s", "5",
0, FLB_TRUE, offsetof(struct flb_filter_aws,
metadata_groups[FLB_FILTER_AWS_METADATA_GROUP_TAGS].retry_required_interval),
"Defines minimum duration between retries for fetching EC2 instance tags"
},
{0}
};

Expand Down
7 changes: 6 additions & 1 deletion plugins/filter_aws/aws.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ struct flb_filter_aws_metadata_group {
/* defines if information was already exposed in the filter for envs */
int exposed;

// TODO: possibly it will need new_keys or something related to injecting into the msgpack
/* defines a timestamp of last execution of fetch method related to the group */
/* unit: seconds */
time_t last_execution;
/* defines a minimal interval before consecutive retries */
/* unit: seconds */
time_t retry_required_interval;
};


Expand Down

0 comments on commit be44ed2

Please sign in to comment.