How to apply distributed tracing for a Node.js app

Donald Le
28 min readNov 19, 2023

Introduction

In a microservices architecture, besides the advantages like scalability, faster time to market, and improving maintainability, it also comes with disadvantages. One of the hardest problems with microservices is how to debug errors when they happen. With dozens to hundreds of microservices communicating with each other in an application, you can easily get lost in the ocean of logs where you don’t know the root cause of the problem.

To quickly debug and find out the root cause of the problem, you need to apply distributed tracing for your application. By the end of the article, you will understand how to apply distributed tracing for Node.js services using Jaeger.

Prerequisites

To follow along with the step-by-step, terminal-based instructions in the article, you need to prepare the following prerequisites:

  1. Prepare an Ubuntu cloud server, preferably version 20.04 or above with 4 vCPUs and 16 GB Memory
  2. Install Docker Desktop latest version to deploy MongoDB for storing the data of the backend services and Jaeger to store traces of the API requests. Please note that you need Docker Compose with version 2 or above to run the Docker services in the article. Docker Compose V2 is included in the latest version of Docker Desktop.
  3. Install pm2 globally in the virtual machine to manage multiple backend services
  4. Install OpenSSL to generate the RSA key to create the JWT key for authentication in the demo app.

Demo Application

The demo application is a blog application where users can make API requests to create a new blog or read existing blogs in the database. There are 3 types of users in the demo application.

  • Anonymous users cannot read or create a new blog
  • Authenticated users with READ permission can only read the blogs
  • Authenticated users with WRITE permission can read the blogs and create a new blog.

The blog application has 4 services:

  • User service allows the administrator to create new users
  • Authen service allows user to authenticate their credentials so that they can have the token to read or create new blogs later on
  • Author service checks the user token to see…

--

--

Donald Le

A passionate automation engineer who strongly believes in “A man can do anything he wants if he puts in the work”.