Skip to content

Commit d686e2d

Browse files
authored
Merge pull request #1 from yedidyas/errors-handling-correlation-id
Error handling - Correlation ID
2 parents 3e117cb + ddc713c commit d686e2d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Diff for: README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Read in a different language: [![CN](/assets/flags/CN.png)**CN**](/README.chines
4949
## Table of Contents
5050

5151
1. [Project Structure Practices (5)](#1-project-structure-practices)
52-
2. [Error Handling Practices (11) ](#2-error-handling-practices)
52+
2. [Error Handling Practices (13) ](#2-error-handling-practices)
5353
3. [Code Style Practices (12) ](#3-code-style-practices)
5454
4. [Testing And Overall Quality Practices (13) ](#4-testing-and-overall-quality-practices)
5555
5. [Going To Production Practices (19) ](#5-going-to-production-practices)
@@ -237,6 +237,16 @@ especially if the cause of the abnormal behavior is inside of the missing functi
237237

238238
🔗 [**Read More: returning promises**](/sections/errorhandling/returningpromises.md)
239239

240+
<br/><br/>
241+
242+
## ![] 2.13 Give your error's log a context by adding a correlation ID
243+
244+
**TL;DR:** Correlation ID lets you linking log records, even if they belong to different services. 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.
245+
246+
**Otherwise:** Once an error will occure, you might read the logs without any context of understanding what caused the unexpected input and which logs of other services are related to your investigated transaction.
247+
248+
🔗 [**Read More: Correlation ID: help your logs tell you a story and give your error a context**](/sections/errorhandling/correlationid.md)
249+
240250
<br/><br/><br/>
241251

242252
<p align="right"><a href="#table-of-contents">⬆ Return to top</a></p>

Diff for: sections/errorhandling/correlationid.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Correlation ID: help your logs tell you a story and give your error a context
2+
3+
### One Paragraph Explainer
4+
5+
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.
6+
7+
<br/>
8+
9+
### Code Example: passing the correlation ID between services on the requets http context
10+
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:
11+
12+
```javascript
13+
const httpContext = require('express-http-context');
14+
15+
app.use((req, res, next) => {
16+
// Extract the correlation ID from the previous request, or creating it if this is the first request in the transaction
17+
const correlationId = req.get('X-Correlation-ID') || uuid.v4();
18+
19+
// Set the correaltion ID on the http context
20+
httpContext.set('correlationId', correlationId);
21+
22+
// Set the correaltion ID on the response
23+
res.set('X-Correlation-ID', correlationId);
24+
25+
next();
26+
});
27+
```

0 commit comments

Comments
 (0)