This commit is contained in:
Yedidya Schwartz
2021-02-03 07:50:53 +02:00
committed by GitHub
parent 201aa0ac64
commit a82a3782ad

View File

@ -2,18 +2,19 @@
### One Paragraph Explainer
Correlation ID is one of the best problem-solving patterns. It lets you linking log records, even if they belong to different services. If your system consumes other services and is itself a producer service, adding a correlaction ID is a must. By this pattern, your transaction logs can become into a story that tells itself by filtering your logs with a specific correlation ID, instead of try linking the cross-services transaction logs to each other by yourself. It can save your day when a process that including 20 different microservices throws an exception in one of them, and you have no idea where did the problem started across the flow.
Correlation ID is one of the best problem-solving patterns. It lets you link log records, even if they belong to different services. If your system consumes other services and is itself a producer service, adding a correlaction ID is a must. By this pattern, your transaction logs can become into a story that tells itself by filtering your logs with a specific correlation ID, instead of try linking the cross-services transaction logs to each other by yourself. It can save your day when a process including 20 different microservices throws an exception in one of them, and you have no idea where did the problem started across the flow.
<br/>
### Code Example: passing the correlation ID between services on the requet http context
Here is an example of using [express-http-context](https://www.npmjs.com/package/express-http-context) library to set the forwarded correlation ID on the http context after it was extracted from the request. If the correlation ID isn't existing on the request, since this may be the first request in the requests chain, it's being generated.
Here is an example of using [express-http-context](https://www.npmjs.com/package/express-http-context) library as part of a middleware to set the forwarded correlation ID on the http context after it was extracted from the request.
If the correlation ID doesn't exist on the request, since this may be the first request in the requests chain, it's being generated.
```javascript
const httpContext = require('express-http-context');
app.use((req, res, next) => {
// Extract the correlation ID from the previous request, or creating it if this is the first request in the transaction
// Extract the correlation ID from the previous request, or generate it if this is the first request in the transaction
const correlationId = req.get('X-Correlation-ID') || uuid.v4();
// Set the correaltion ID on the http context
@ -25,6 +26,7 @@ app.use((req, res, next) => {
next();
});
```
<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."