DEV Community

Cover image for Monitor your Nodejs application with OpenTelemetry and SigNoz
Ankit Anand ✨ for SigNoz

Posted on • Edited on • Originally published at signoz.io

Monitor your Nodejs application with OpenTelemetry and SigNoz

OpenTelemetry can auto-instrument many common modules for a Javascript application. The telemetry data captured can then be sent to SigNoz for analysis and visualization.

Monitor your Nodejs applications with SigNoz
SigNoz UI

OpenTelemetry is a set of tools, APIs, and SDKs used to instrument applications to create and manage telemetry data(Logs, metrics, and traces). For any distributed system based on microservice architecture, it's an operational challenge to solve performance issues quickly.

Telemetry data helps engineering teams to troubleshoot issues across services and identify the root causes. In other words, telemetry data powers observability for your distributed applications.

Steps to get started with OpenTelemetry for a Nodejs application:

  • Installing SigNoz
  • Installing sample Nodejs app
  • Set up OpenTelemetry and send data to SigNoz

Installing SigNoz

You can get started with SigNoz using just three commands at your terminal if you have Docker installed. You can read about other deployment options from SigNoz documentation.

git clone https://github.com/SigNoz/signoz.git
cd signoz/deploy/
./install.sh
Enter fullscreen mode Exit fullscreen mode

You will have an option to choose between ClickHouse or Kafka + Druid as a storage option. Trying out SigNoz with ClickHouse database takes less than 1.5GB of memory, and for this tutorial, we will use that option.

When you are done installing SigNoz, you can access the UI at: http://localhost:3000

The application list shown in the dashboard is from a sample app called HOT R.O.D that comes bundled with the SigNoz installation package.

SigNoz dashboard
SigNoz dashboard

Install sample Nodejs application

You need to ensure that you have Node.js version 12 or newer. You can download the latest version of Node.js here. For the sample application, let's create a basic 'hello world' express.js application.

Steps to get the app set up and running:

  1. Make a directory and install express Make a directory for your sample app on your machine. Then open up the terminal, navigate to the directory path and install express with the following command:
   npm i express
Enter fullscreen mode Exit fullscreen mode
  1. Setup server.js Create a file called 'server.js' in your directory and with any text editor setup your 'Hello World' file with the code below:
   const express = require('express');

   const app = express();

   app.get('/hello', (req, res) => {
   res.status(200).send('Hello World');
   });

   app.listen(9090);
Enter fullscreen mode Exit fullscreen mode
  1. Boot up the server with the following command on the terminal:
   node server.js
Enter fullscreen mode Exit fullscreen mode

You can check if your app is working by visiting: http://localhost:9090/hello

Once you are finished checking, exit the localhost on your terminal.

Set up OpenTelemetry and send data to SigNoz

  1. In the same directory path at the terminal, install the OpenTelemetry launcher package with this command:
   npm install lightstep-opentelemetry-launcher-node
Enter fullscreen mode Exit fullscreen mode

The OpenTelemetry launcher makes getting started with OpenTelemetry easier by reducing configuration boilerplate.

  1. To use OpenTelemetry, you need to start the OpenTelemetry SDK before loading your application. By initializing OpenTelemetry first, we enable OpenTelemetry to apply available instrumentation and auto-detect packages before the application starts to run. To do that, go to your directory and create a new file named, "server_init.js". This will act as the new entry point for your app. Paste the following code in the file:
   const {
    lightstep,
    opentelemetry,
   } = require('lightstep-opentelemetry-launcher-node');

   const sdk = lightstep.configureOpenTelemetry();

   sdk.start().then(() => {
    require('./server');
   });

   function shutdown() {
    sdk.shutdown().then(
      () => console.log("SDK shut down successfully"),
      (err) => console.log("Error shutting down SDK", err),
    ).finally(() => process.exit(0))
   };

   process.on('exit', shutdown);
   process.on('SIGINT', shutdown);
   process.on('SIGTERM', shutdown);
Enter fullscreen mode Exit fullscreen mode
  1. Once the file is created, you only need to run one last command at your terminal, which passes the necessary environment variables. Here, you also set SigNoz as your backend analysis tool.
   OTEL_EXPORTER_OTLP_SPAN_ENDPOINT="http://<IP of SigNoz Backend>:55681/v1/trace" OTEL_METRICS_EXPORTER=none LS_SERVICE_NAME=<service name> node server_init.js
Enter fullscreen mode Exit fullscreen mode

Replacing the placeholders in the above command for local host:

IP of SigNoz Backend: localhost (since we are running SigNoz on our local host)

service name : sample_app (you can give whatever name that suits you)

So the final command is:

   OTEL_EXPORTER_OTLP_SPAN_ENDPOINT="http://localhost:55681/v1/trace" OTEL_METRICS_EXPORTER=none LS_SERVICE_NAME=sample_app node server_init.js
Enter fullscreen mode Exit fullscreen mode

And, congratulations! You have instrumented your sample Node.js app. You can now access the SigNoz dashboard at http://localhost:3000 to monitor your app for performance metrics.

Sample nodejs app in the applications monitored
Sample_app in the list of applications monitored

Metrics and Traces of the Nodejs application

SigNoz makes it easy to visualize metrics and traces captured through OpenTelemetry instrumentation.

SigNoz comes with out of box RED metrics charts and visualization. RED metrics stands for:

  • Rate of requests
  • Error rate of requests
  • Duration taken by requests

Sample nodejs app in the applications monitored
Measure things like application latency, requests per sec, error percentage and see your top endpoints

You can then choose a particular timestamp where latency is high to drill down to traces around that timestamp.

See traces, and apply powerful filters on trace data
View of traces at a particular timestamp

You can use flamegraphs to exactly identify the issue causing the latency.

Flamegraphs for distributed tracing
Flamegraphs showing exact duration taken by each spans - a concept of distributed tracing

Conclusion

OpenTelemetry makes it very convenient to instrument your Nodejs application. You can then use an open-source APM tool like SigNoz to analyze the performance of your app. As SigNoz offers a full-stack observability tool, you don't have to use multiple tools for your monitoring needs.

You can try out SigNoz by visiting its GitHub repo 👇

SigNoz GitHub repo

If you face any issues while trying out SigNoz, feel free to write to us at: support@signoz.io

If you want to read more about SigNoz 👇

Golang Application Performance Monitoring with SigNoz

Top comments (0)