Open maxbrieiev opened 7 years ago
Hi,
First of all, never use when/monitor/console
in production. In fall 2015, we had a very hard-to-debug issue including memory leaks on production (like #194 - for us it crashed apps and made the GC stall at 1.5GB), and that module turned out to be the reason. I am pretty sure the issue is still there.
Other than that, it seems to me that you are trying to use Promises as a context-aware task runner. It may not be a good fit, because of two reasons:
console
instance but also something like a syslog socket fd, etc. If you extend the Promise prototype, you are effectively imposing a singleton logger on the users of the library throughout the project. This is bad.You should typically inject loggers into your logic. A handy library that can help you manage and enrich logger state is bunyan, which allows "hierarchical" loggers. One combination with when
that is particularly useful is Promise#tap, which applies a side effect without modifying the resolution value.
Thank you for your comment.
Maybe extending Promise prototype with new methods is bad idea.
My initial thoughts were as following. The chain of promises constitutes some asynchronous 'task'. Promises wait on each other as long as they are returned from functions. When the last promise is resolved the task finishes. Why not use this promise machinery to attach some context, so the group of related asynchronous operations could be treated together. The main purpose here is context aware logging. It would be useful to have some hooks provided by the library.
What I want to have is something like node domains feature ( https://nodejs.org/api/domain.html ). The API doesn't have to be the extension of Promise
. It can be for example, something like this:
var promise = db.cal('sql');
var log = getLoggingFromExecutionContext(promise);
log.debug('fetching data from db');
I have previously looked at bunyan, but I didn't find a way to group related asynchronous operations together. I will take a look once more at hierarchical logging.
Hi!
I want to bind some context to promise chain for the purpose of context aware logging. For this my idea is to extend promise API. Here is an example:
I would expect to see something like this in the log output:
My idea was to extend
Promise
along following lines:At first I thought that
when/monitor/PromiseMonitor
would be a good start. But after playing with it for some time, I am a bit confused. To me it appears that it doesn't track context properly. Here is an example:Here I expect that p2 runs within the context of p1. However, the traceback doesn't give any insights on this:
So my question is: would
PromiseMonitor
be a good example to implement context binding to chain of promises?Is it feasible at all to extend
Promise
this way?Thank you in advance for any suggestions.