-
Notifications
You must be signed in to change notification settings - Fork 42
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
[Asset Inventory] Run fetchers periodically #2902
Conversation
This pull request does not have a backport label. Could you fix it @kubasobon? 🙏
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, has it never worked? 😬
internal/inventory/inventory.go
Outdated
go func(fetcher AssetFetcher) { | ||
fetcher.Fetch(ctx, a.assetCh) | ||
}(fetcher) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we remove the the fetcher loop on top of this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, no. I added this so all fetchers run every fetcherPeriod
tick. But I think canceled context takes precedence. If it's canceled for whatever reason, let's not run any more fetchers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the extra one on top is so that it starts immediately, on second 0. Otherwise it would take until the first fetcherPeriod
tick to run first fetchers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm true, just checked the Ticker and we can't make it run straight away.
Nitpick: Maybe move this code to a function? Just so we have a nice wrapped function, it prevents if in the future we need to fix something and we do in one place only
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A note: in manager (used in fetching of cspm mostly), we use a timer, first with a 0
value and then reset with the period value, to do the trick of "trigger now and then per period".
cloudbeat/internal/resources/fetching/manager/manager.go
Lines 72 to 87 in b493e0c
timer := time.NewTimer(0) | |
defer timer.Stop() | |
for { | |
select { | |
case <-ctx.Done(): | |
m.log.Info("Fetchers manager canceled") | |
return | |
case <-timer.C: | |
// update the interval | |
timer.Reset(m.interval) | |
// this is blocking so the stop will not be called until all the fetchers are finished | |
// in case there is a blocking fetcher it will halt (til the m.timeout) | |
go m.fetchIteration(ctx) | |
} | |
} |
I'm not saying we should do the same with asset inventory since I think the ticker is better for what we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Yeah, I know the trick, but I always find calling timer.Reset()
every time more messy. Nothing against the implementation, just personal taste I guess.
Yes, unfortunately it seems like it always ran just once and that's it. See #2887 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a suggestion, but looks good otherwise
internal/inventory/inventory.go
Outdated
go func(fetcher AssetFetcher) { | ||
fetcher.Fetch(ctx, a.assetCh) | ||
}(fetcher) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm true, just checked the Ticker and we can't make it run straight away.
Nitpick: Maybe move this code to a function? Just so we have a nice wrapped function, it prevents if in the future we need to fix something and we do in one place only
I've extracted duplicate code to |
const indexTemplate = "logs-cloud_asset_inventory.asset_inventory-%s_%s_%s_%s-default" | ||
const ( | ||
indexTemplate = "logs-cloud_asset_inventory.asset_inventory-%s_%s_%s_%s-default" | ||
minimalPeriod = 30 * time.Second |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can omit this const?
locally we can tweak
period: 30s |
and users have a hard-coded value, for example - https://github.com/elastic/integrations/blob/56d0473a2ed4900b7b24b6a254795b9bc82cb713/packages/cloud_asset_inventory/data_stream/asset_inventory/agent/stream/gcp.yml.hbs#L1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather keep it. It's not a default value, but a guardian value. If the customer sets value too low, this will start to spawn many goroutines that hit API. I can lower it if you think it makes sense, but we definitely need to make sure there is a reasonable minimum threshold. Obviously the worst-case scenario is period: 0s
, where we get an infinite loop of new goroutines, but even 5s
or 10s
is too small IMHO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
np with keeping it imo. but this value isn't configurable by the users, it's hardcoded in the integration package.
(cherry picked from commit bb7a3e9)
(cherry picked from commit bb7a3e9)
(cherry picked from commit bb7a3e9)
) [Asset Inventory] Run fetchers periodically (#2902) (cherry picked from commit bb7a3e9) Co-authored-by: Kuba Soboń <[email protected]>
…2907) [Asset Inventory] Run fetchers periodically (#2902) (cherry picked from commit bb7a3e9) Co-authored-by: Kuba Soboń <[email protected]>
…2906) [Asset Inventory] Run fetchers periodically (#2902) (cherry picked from commit bb7a3e9) Co-authored-by: Kuba Soboń <[email protected]>
Summary of your changes
Fixes an issue where fetchers ran only once.
Related Issues
See #2887
Checklist