From b9aea88a766a1a5f17dcff49c36f932d45c5dc66 Mon Sep 17 00:00:00 2001
From: Yedidya Schwartz <36074789+yedidyas@users.noreply.github.com>
Date: Wed, 27 Jan 2021 20:01:14 +0200
Subject: [PATCH 1/7] Create correlationid.md
---
sections/errorhandling/correlationid.md | 15 +++++++++++++++
1 file changed, 15 insertions(+)
create mode 100644 sections/errorhandling/correlationid.md
diff --git a/sections/errorhandling/correlationid.md b/sections/errorhandling/correlationid.md
new file mode 100644
index 000000000..faf662791
--- /dev/null
+++ b/sections/errorhandling/correlationid.md
@@ -0,0 +1,15 @@
+# Correlation ID: help your logs tell you a story
+
+### 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-server transaction logs to each other by yourself. Can save your day when a process including 20 microservices throw an exception in one of them, and you have no idea where did the problem started in the flow.
+
+### Blog Quote: "We have a problem with promises"
+
+ From the blog, pouchdb.com ranked 11 for the keywords “Node Promises”
+
+ > … We recommend you to watch these signals for all of your services: Error Rate: Because errors are user facing and immediately affect your customers.
+Response time: Because the latency directly affects your customers and business.
+Throughput: The traffic helps you to understand the context of increased error rates and the latency too.
+Saturation: It tells how “full” your service is. If the CPU usage is 90%, can your system handle more traffic?
+…
From f23a4e37fd2dbd814de31e521316159b58ee8430 Mon Sep 17 00:00:00 2001
From: Yedidya Schwartz <36074789+yedidyas@users.noreply.github.com>
Date: Wed, 27 Jan 2021 20:15:23 +0200
Subject: [PATCH 2/7] code example was added
---
sections/errorhandling/correlationid.md | 26 ++++++++++++++++++-------
1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/sections/errorhandling/correlationid.md b/sections/errorhandling/correlationid.md
index faf662791..38ead4dbc 100644
--- a/sections/errorhandling/correlationid.md
+++ b/sections/errorhandling/correlationid.md
@@ -4,12 +4,24 @@
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-server transaction logs to each other by yourself. Can save your day when a process including 20 microservices throw an exception in one of them, and you have no idea where did the problem started in the flow.
-### Blog Quote: "We have a problem with promises"
+
- From the blog, pouchdb.com ranked 11 for the keywords “Node Promises”
+### Code Example: passing the correlation ID between services on the requets 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:
- > … We recommend you to watch these signals for all of your services: Error Rate: Because errors are user facing and immediately affect your customers.
-Response time: Because the latency directly affects your customers and business.
-Throughput: The traffic helps you to understand the context of increased error rates and the latency too.
-Saturation: It tells how “full” your service is. If the CPU usage is 90%, can your system handle more traffic?
-…
+```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
+ const correlationId = req.get('X-Correlation-ID') || uuid.v4();
+
+ // Set the correaltion ID on the http context
+ httpContext.set('correlationId', correlationId);
+
+ // Set the correaltion ID on the response
+ res.set('X-Correlation-ID', correlationId);
+
+ next();
+});
+```
From 4ca0c3a8c0a7efc7b570095e2df2af706891adaf Mon Sep 17 00:00:00 2001
From: Yedidya Schwartz <36074789+yedidyas@users.noreply.github.com>
Date: Wed, 27 Jan 2021 20:20:21 +0200
Subject: [PATCH 3/7] remove line break and change server to service
---
sections/errorhandling/correlationid.md | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/sections/errorhandling/correlationid.md b/sections/errorhandling/correlationid.md
index 38ead4dbc..6b05d1d3b 100644
--- a/sections/errorhandling/correlationid.md
+++ b/sections/errorhandling/correlationid.md
@@ -1,10 +1,10 @@
-# Correlation ID: help your logs tell you a story
+# Correlation ID: help your logs tell you a story and give you error a context
### 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-server transaction logs to each other by yourself. Can save your day when a process including 20 microservices throw an exception in one of them, and you have no idea where did the problem started in the flow.
+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.
-
+
### Code Example: passing the correlation ID between services on the requets 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:
From f44e477c15a604de51101c64fe5852daab636d0a Mon Sep 17 00:00:00 2001
From: Yedidya Schwartz <36074789+yedidyas@users.noreply.github.com>
Date: Wed, 27 Jan 2021 20:29:54 +0200
Subject: [PATCH 4/7] correlation id added
---
README.md | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 16b0e5e09..e029147ef 100644
--- a/README.md
+++ b/README.md
@@ -49,7 +49,7 @@ Read in a different language: [**CN**](/README.chines
## Table of Contents
1. [Project Structure Practices (5)](#1-project-structure-practices)
-2. [Error Handling Practices (11) ](#2-error-handling-practices)
+2. [Error Handling Practices (13) ](#2-error-handling-practices)
3. [Code Style Practices (12) ](#3-code-style-practices)
4. [Testing And Overall Quality Practices (13) ](#4-testing-and-overall-quality-practices)
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
🔗 [**Read More: returning promises**](/sections/errorhandling/returningpromises.md)
+
+
+## ![✔] 2.13 Give you error's log a context by adding a correlation ID
+
+**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.
+
+**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.
+
+🔗 [**Read More: Correlation ID: help your logs tell you a story and give you error a context**](/sections/errorhandling/correlationid.md)
+