-
Notifications
You must be signed in to change notification settings - Fork 754
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
feat: improved cross platform metric collection #2834
base: master
Are you sure you want to change the base?
Conversation
…eral and a containerised flag to Configuration
… containerised enviroment
hey all, thanks for agreeing to take a look. I havent done much OSS before so im looking forward to hearing your thoughts :). Assuming it all looks good to you, when might it be incorperated into a crawlee release? At work we have a project that hinged on using crawlee in k8 so the autoscaling issues in containers is causing a fairly significant issue for us. When you come to review it, id be happy to hop on a discord call and discuss it. :) Thanks for everything you do! |
Hi @NathanSavageKaimai and thanks for your willingness to contribute! In the issue that this aims to close, you mentioned the possibility of using the |
hi @janbuchar, ps-list would serve the same purpose as the new Let me know if you would like me to explore these options. |
with ps-list, you are relying on a third party binary which doesnt provide its source code as far as i can tell. It could be a potential supply chain risk. |
I will +1 that worry, I'm not a fan of using a dependency that embeds a binary whose source code isn't directly open source / one we could build ourselves and embed |
Um, as far as I can tell, |
@janbuchar Ah yep, i hadnt found the cpp repo. still, being externally tracked, theres no automatic method to verify the authenticity of the binary beyond downloading from both sources and checking the hashes. |
The fact the binary is just embedded in instead of precompiled (like impit) or built at install time is a worry imo |
You both make a good point. I'd still consider exploring |
cool will do. :) |
@janbuchar ive had a play around with |
Sounds reasonable, thanks! I will try to review the code in depth this week. |
f7d7518
to
173630c
Compare
hey @janbuchar have you been able to have a look yet? No worries ethier way. if you like, we can sit down for a call later. Just shoot me a message on Discord - crafty5064. Im free until 2pm UTC or all day Saturday. :) |
Hi all, I hope you had a good weekend! Have you had a chance to review these changes? I was hoping they might be included in a release soon as these issues are blocking my company from deploying our product fully to k8. If you would like a chat, im free untill 1pm utc tomorrow. :) |
Hi, I'll look into it tomorrow. Sorry for the delay! |
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.
Good job on this one! I have a bunch of readability/code structure comments. More importantly though, the tests here are very narrowly scoped and use mocking heavily. Do you think you could add an E2E test that would verify that the system information detection works as expected? Feel free to suggest any other way to test this as a whole.
@@ -43,6 +43,35 @@ export async function isDocker(forceReset?: boolean): Promise<boolean> { | |||
return isDockerPromiseCache; | |||
} | |||
|
|||
/** | |||
* Returns a `Promise` that resolves to true if the code is running in a containerised environment. | |||
* Returns true if the CRAWLEE_CONTAINERISED environment variable is set. |
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.
Who is supposed to set the CRAWLEE_CONTAINERISED
environment variable?
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.
that is meant to be a manual way to run the containerised resource checks in case the other heuristics dont catch it.
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.
Got it. Could you add it to the documentation?
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.
would you mind showing me where? im a little bit lost on that side of it, ta.
Im free for a call right now if you would like a chat. :)
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 understand that 😁 https://github.com/apify/crawlee/blob/master/docs/guides/configuration.mdx this is the place
* @param includeRoot - Optional flag. When true, include the process with the given PID if found. | ||
* Defaults to false. | ||
*/ | ||
export async function psTree(pid: number | string, includeRoot: boolean = false): Promise<ProcessInfo[]> { |
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.
This is a very long and complex function. Possibly the main reason why it's hard to read is that it combines the implementation for UNIX and Windows in a single function. Could it be broken down into multiple smaller functions?
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.
it was pretty much a copy paste from apify/pstree with the WMIC changes. I can reformat it. :)
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.
Please do that, apify/pstree is super dated and I'm sure that if we don't refactor it now, we won't get back to it, ever.
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.
cool will do :)
hi @janbuchar thanks for your insight! Most of the points were just me trying to follow conventions set out in the prexisting memory-info.ts file but i will definitely work on clarifying it. :) |
@NathanSavageKaimai please look into refactoring of Also, any thoughts regarding this?
|
its a difficult one given that its so close to the metal, an e2e test would be dependant on the current state of the test runner unless i mocked the exec call and readline interface but at that point it may as well be a unit test. Also, personally I am only set up to run tests on windows or linux through wsl so i cant verify Macos compatability beyond "its a copy paste from a solution that persumably worked". What i will do is reimplement the unit tests in apify/pstree |
Cool, that will at least give us some certainty that I guess we could make a script that would show the current CPU and memory usage ratio and compare it with the old implementation. Then we could at least test-drive this on several machines with different OS and see if it behaves reasonably. What do you think? |
…_CONTAINERIZED env var and tidied up @crawlee/utils api surface
This PR improves CPU and RAM metric collection across multiple environments. The CPU metrics are now fully cGroup aware report properly in containerised environments with cpu quota limits. The memory profile method on windows has been updated from using legacy WMIC to powershell. Finally two new utility methods have been added to
@crawlee/utils
,general.ts
to determine if the scraper is containerised (instead of just running in docker) as well as if cGroup is enabled.Adds
@crawlee/utils
general.ts
isContainerised()
an extention ofisDocker()
that also checks for the presence of aKUBERNETES_SERVICE_HOST
environment variable for k8 and aCRAWLEE_CONTAINERISED
environment variable for manual control.getCgroupsVersion()
a method to determine the cGroup version in a cGroup controlled environment. It does this by checking for a file at/sys/fs/cgroup/memory/
. If it is present, the cGroup verison is 1, else version 2.cpu-info.ts
Collects cpu infomation in a similar manner to
memory-info.ts
getCurrentCpuTicks()
The existing solution. Used in AWS lambda, containerised environments without a cGroup cpu limit and on bare metal.getCpuQuota()
Gets the cpu quota in cGroup controlled environments.getCpuPeriod()
Gets the cpu quota period in cGroup controlled environments.getContainerCpuUsage()
Gets the containers cpu usage.getSystemCpuUsage()
Gets the systems cpu usage.getCpuInfo()
The main method for collecting cpu load metrics. Determines the enviroment and calls the other functions accordingly.Removes
@apify/ps-tree
A legacy package that checked memory usage using
WMIC.exe
(depreciated) on windows orps
on *nix. Replaced by@crawlee\packages\utils\src\internals\psTree.ts
which calculates the memory usage in a similar manner but usingpowershell
andGet-CimInstance Win32_Process
. Also adds type safety.Fixes
Fixes: #2771