Skip to content

Commit

Permalink
Document how error is received from worker transport (#1548)
Browse files Browse the repository at this point in the history
* document how error is received from worker transport

* add a link to nodejs worker thread doc

* improve doc based on review

* ops, missed a few places

* Update docs/transports.md

Co-authored-by: James Sumners <james@sumners.email>

* add a line between sentences

Co-authored-by: James Sumners <james@sumners.email>
  • Loading branch information
TommyDew42 and jsumners committed Sep 5, 2022
1 parent 376a8c4 commit dc4e854
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/transports.md
Expand Up @@ -887,3 +887,32 @@ $ 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
<a id="communication-between-pino-and-transport"></a>
## 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 JavaScript execution thread).
### 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({
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 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.

0 comments on commit dc4e854

Please sign in to comment.