kubeless icon indicating copy to clipboard operation
kubeless copied to clipboard

Inject code for app monitor/analytics tools

Open diegougaldeGL opened this issue 4 years ago • 3 comments

Is this a BUG REPORT or FEATURE REQUEST?: Feature request / Question

What happened: Hello, I'm currently using Kubeless in a production environment and it has worked well. We are going to incorporate a monitor/analytics third party that needs to be executed before any other module/library (even before express is initialized). Is there a way to achieve that? Running a piece of code before express is executed.

What you expected to happen: We are able to execute code before express runs.

diegougaldeGL avatar May 03 '20 19:05 diegougaldeGL

Hi @diegougaldeGL,

This behavior is different depending on the runtime you are using. In you case (NodeJS) every time the server receives a request, the function code gets loaded an executed. This is done this way to ensure isolation between requests.

Because of that, you cannot achieve what you are trying in the function code, you would need to fork the runtime code and include your custom modifications. FWIW, this is the runtime code:

https://github.com/kubeless/runtimes/blob/master/stable/nodejs/kubeless.js

andresmgot avatar May 04 '20 09:05 andresmgot

@andresmgot Thank you for your quick reply. What you said was my understanding about serverless, however I have been able to cache data between requests and that confuses me. My guess would be that if a function imports a class, that class will only be imported/initialized once and re-used everytime the function is called right (Node's module-cache system)?

I'll fork the project and add the snippet at the beginning of that file, thank you very much once again.

diegougaldeGL avatar May 04 '20 12:05 diegougaldeGL

I have been able to cache data between requests and that confuses me

Are you able to cache data with the NodeJS runtime? Maybe I am missing something, in theory, for every request, it uses the Module pkg to create a new env:

https://github.com/kubeless/runtimes/blob/4a8afccf2421cee7d5ffb63454a49b1d686cd562/stable/nodejs/kubeless.js#L150

But maybe that only affects to env variables and things like that. A global variable might be shared.

I'll fork the project and add the snippet at the beginning of that file, thank you very much once again.

Feel free to submit a PR with your fork! If you implement it in a generic way, others can benefit from that :)

andresmgot avatar May 05 '20 08:05 andresmgot