mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-28 03:25:55 +08:00
brs
This commit is contained in:
@ -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. It’s 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. It’s a value that is common to all requests, messages and responses in a given transaction. With this simplicity you get a lot of power."
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user