othiym23 / node-continuation-local-storage

implementation of https://github.com/joyent/node/issues/5243
BSD 2-Clause "Simplified" License
1.13k stars 207 forks source link

how to know current callstack finished #74

Open peaksnail opened 8 years ago

peaksnail commented 8 years ago

hi I want to know when current callstack finished, and then I will do some other things after that; I debug the function exit in the finally of namespace.bind or run, But It couldn't work Is there some good way to know when current callstack finished(include callback)?

thanks zeng

AleksMeshkov commented 7 years ago

Hi! If you have a webapp may be this approach will help you. I decided to put expressjs response object in the context and added this to the lib.

https://github.com/AleksMeshkov/node-continuation-local-storage/commit/7eee8cf67a4c3f79ca69bd7b5246c183dbdaaedd

rochdev commented 6 years ago

I am interested in this as well. This functionality could be added with some kind of event when the context exits. I don't know the performance implications of adding this however.

Our use case is for our tracing library to automatically instrument code. For cases following a request/response pattern (i.e. web frameworks) it's easy to know when to finish the span. However, for cases like message queue consumers or task workers, it can be difficult to know when to finish the span since there is no explicit callback or response. In this case, the only way to know that the span is finished right now is for the user to explicitly finish it. It would be really useful to be able to know when the context has exited to automatically finish the span.

@othiym23 I believe you have created this library for a similar use case? Any insight on this would be appreciated :)