This commit is contained in:
Yedidya
2021-02-05 00:11:58 +02:00
parent 102e45e37a
commit b6ce4640dd

View File

@ -25,18 +25,21 @@ app.get('/getUserData/{id}', async (req, res, next) => {
// At any point in the app after cls-rtracer middleware was initialized, even when 'req' object doesn't exist, the TransactionId is reachable // At any point in the app after cls-rtracer middleware was initialized, even when 'req' object doesn't exist, the TransactionId is reachable
const transactionId = rTracer.id(); const transactionId = rTracer.id();
logger.info(`user ${user.id} data was fetched successfully`, { transactionId }); logger.info(`user ${user.id} data was fetched successfully`, { transactionId });
res.json(user); res.json(user);
} catch (err) { } catch (err) {
// As next step I'd recommand using rTracer.id() from inside the logger component, to prevent from using it all over the code // As next step I'd recommand using rTracer.id() from inside the logger component, to prevent from using it all over the code
logger.error(`error while fetching user id ${req.params.id} data`, { transactionId: rTracer.id(), error: err }); logger.error(`error while fetching user id ${req.params.id} data`, { transactionId: rTracer.id(), error: err });
next(err); next(err);
} }
}) })
``` ```
<br/>
### Code example: sharing TransactionId among microservices ### Code example: sharing TransactionId among microservices
```javascript ```javascript
// cls-tracer has the ability to store the TransactionId on your service outgoing requests headers, and extract the TransactionId from incoming requests headers, just by overriding the default middleware config // cls-tracer has the ability to store the TransactionId on your service outgoing requests headers, and extract the TransactionId from incoming requests headers, just by overriding the default middleware config
@ -50,14 +53,14 @@ app.use(rTracer.expressMiddleware({
})); }));
``` ```
<br/>
### Good: assign 'TransactionId' to your logs ### Good: assign 'TransactionId' to your logs
image TBD image TBD
### Bad: write your logs without any common flow indication ### Bad: logs without any flow correaltion
image TBD image TBD
<br/>
### Blog Quote: "The notion of a Correlation ID is simple. Its a value that is common to all requests, messages and responses in a given transaction. With this simplicity you get a lot of power." ### Blog Quote: "The notion of a Correlation ID is simple. Its a value that is common to all requests, messages and responses in a given transaction. With this simplicity you get a lot of power."