From e155de371cc474339aaefd56fa2f2560114b628e Mon Sep 17 00:00:00 2001 From: TommyDew42 Date: Sat, 3 Sep 2022 17:47:40 +0800 Subject: [PATCH 1/6] document how error is received from worker transport --- docs/transports.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/transports.md b/docs/transports.md index 9d6e04bb3..c511e6e03 100644 --- a/docs/transports.md +++ b/docs/transports.md @@ -887,3 +887,29 @@ $ node app.js | pino-websocket -a my-websocket-server.example.com -p 3004 For full documentation of command line switches read the [README](https://github.com/abeai/pino-websocket#readme). [pino-pretty]: https://github.com/pinojs/pino-pretty + + +## Communication between pino and transport +Here we discuss some technical details of how pino communicates with its worker threads. + +pino uses [`thread-stream`](https://github.com/pinojs/thread-stream) to create a stream for transports. +When we create a stream with `thread-stream`, `thread-stream` spawns a [worker](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L50-L60) (an independent JS execution thread). + + +### Error message +How is error message propagated from a transport worker to pino? +Say we have a transport with an error listener: +```js +// index.js +const transport = pino.transport({ + target: './transport.js' +}) + +transport.on('error', err => { + console.error('error caught', err) +}) + +const log = pino(transport) +``` +When our worker emits an error event, the worker has listeners for it: [error](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L59-L70) and [unhandledRejection](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L135-L141). These listeners send the error message to the main thread where pino is in. +When pino receives the error message, it further [emit](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L349) the error message. Finally the error message arrives at our index.js and caught by our error listener. From 94dc201d780304ce0f6130aeaa9bae78ea24d837 Mon Sep 17 00:00:00 2001 From: TommyDew42 Date: Sun, 4 Sep 2022 16:01:31 +0800 Subject: [PATCH 2/6] add a link to nodejs worker thread doc --- docs/transports.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/transports.md b/docs/transports.md index c511e6e03..aff2296c9 100644 --- a/docs/transports.md +++ b/docs/transports.md @@ -890,7 +890,7 @@ For full documentation of command line switches read the [README](https://github ## Communication between pino and transport -Here we discuss some technical details of how pino communicates with its worker threads. +Here we discuss some technical details of how pino communicates with its [worker threads](https://nodejs.org/api/worker_threads.html). pino uses [`thread-stream`](https://github.com/pinojs/thread-stream) to create a stream for transports. When we create a stream with `thread-stream`, `thread-stream` spawns a [worker](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L50-L60) (an independent JS execution thread). From 87e67320b030b346a419a990bcfbac71c078fef8 Mon Sep 17 00:00:00 2001 From: TommyDew42 Date: Mon, 5 Sep 2022 23:28:01 +0800 Subject: [PATCH 3/6] improve doc based on review --- docs/transports.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/transports.md b/docs/transports.md index aff2296c9..5ca9382f9 100644 --- a/docs/transports.md +++ b/docs/transports.md @@ -889,16 +889,16 @@ For full documentation of command line switches read the [README](https://github [pino-pretty]: https://github.com/pinojs/pino-pretty -## Communication between pino and transport -Here we discuss some technical details of how pino communicates with its [worker threads](https://nodejs.org/api/worker_threads.html). +## Communication between Pino and Transports +Here we discuss some technical details of how Pino communicates with its [worker threads](https://nodejs.org/api/worker_threads.html). -pino uses [`thread-stream`](https://github.com/pinojs/thread-stream) to create a stream for transports. -When we create a stream with `thread-stream`, `thread-stream` spawns a [worker](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L50-L60) (an independent JS execution thread). +Pino uses [`thread-stream`](https://github.com/pinojs/thread-stream) to create a stream for transports. +When we create a stream with `thread-stream`, `thread-stream` spawns a [worker](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L50-L60) (an independent JavaScript execution thread). -### Error message -How is error message propagated from a transport worker to pino? -Say we have a transport with an error listener: +### Error messages +How are error messages propagated from a transport worker to Pino? +Let's assume we have a transport with an error listener: ```js // index.js const transport = pino.transport({ @@ -911,5 +911,5 @@ transport.on('error', err => { const log = pino(transport) ``` -When our worker emits an error event, the worker has listeners for it: [error](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L59-L70) and [unhandledRejection](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L135-L141). These listeners send the error message to the main thread where pino is in. -When pino receives the error message, it further [emit](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L349) the error message. Finally the error message arrives at our index.js and caught by our error listener. +When our worker emits an error event, the worker has listeners for it: [error](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L59-L70) and [unhandledRejection](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L135-L141). These listeners send the error message to the main thread where pino is present. +When Pino receives the error message, it further [emits](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L349) the error message. Finally the error message arrives at our `index.js` and is caught by our error listener. From 2ba6eb383f31f6f486077e3399ab9d0e1683db55 Mon Sep 17 00:00:00 2001 From: TommyDew42 Date: Mon, 5 Sep 2022 23:30:14 +0800 Subject: [PATCH 4/6] ops, missed a few places --- docs/transports.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/transports.md b/docs/transports.md index 5ca9382f9..e9f197d07 100644 --- a/docs/transports.md +++ b/docs/transports.md @@ -911,5 +911,5 @@ transport.on('error', err => { const log = pino(transport) ``` -When our worker emits an error event, the worker has listeners for it: [error](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L59-L70) and [unhandledRejection](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L135-L141). These listeners send the error message to the main thread where pino is present. -When Pino receives the error message, it further [emits](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L349) the error message. Finally the error message arrives at our `index.js` and is caught by our error listener. +When our worker emits an error event, the worker has listeners for it: [error](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L59-L70) and [unhandledRejection](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L135-L141). These listeners send the error message to the main thread where Pino is present. +When Pino receives the error message, it further [emits](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L349) the error message. Finally, the error message arrives at our `index.js` and is caught by our error listener. From 763e0ea8f0b293516679cbd1a1dc364f78349cbb Mon Sep 17 00:00:00 2001 From: Tommy Dew Date: Tue, 6 Sep 2022 01:47:54 +0800 Subject: [PATCH 5/6] Update docs/transports.md Co-authored-by: James Sumners --- docs/transports.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/transports.md b/docs/transports.md index e9f197d07..23190aa35 100644 --- a/docs/transports.md +++ b/docs/transports.md @@ -898,6 +898,7 @@ When we create a stream with `thread-stream`, `thread-stream` spawns a [worker]( ### Error messages How are error messages propagated from a transport worker to Pino? + Let's assume we have a transport with an error listener: ```js // index.js From 72fec9a749190fd1897f098a945f87a30d92fb4b Mon Sep 17 00:00:00 2001 From: TommyDew42 Date: Tue, 6 Sep 2022 01:48:54 +0800 Subject: [PATCH 6/6] add a line between sentences --- docs/transports.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/transports.md b/docs/transports.md index e9f197d07..6e5c90c0c 100644 --- a/docs/transports.md +++ b/docs/transports.md @@ -911,5 +911,7 @@ transport.on('error', err => { const log = pino(transport) ``` + When our worker emits an error event, the worker has listeners for it: [error](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L59-L70) and [unhandledRejection](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/lib/worker.js#L135-L141). These listeners send the error message to the main thread where Pino is present. + When Pino receives the error message, it further [emits](https://github.com/pinojs/thread-stream/blob/f19ac8dbd602837d2851e17fbc7dfc5bbc51083f/index.js#L349) the error message. Finally, the error message arrives at our `index.js` and is caught by our error listener.