Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 27, 2021 01:12 pm GMT

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 SigNozSigNoz 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.gitcd signoz/deploy/./install.sh

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 dashboardSigNoz 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 expressMake 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
  1. Setup server.jsCreate 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);
  1. Boot up the server with the following command on the terminal:
   node server.js

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

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);
  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

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

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 monitoredSample_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 monitoredMeasure 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 dataView of traces at a particular timestamp

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

Flamegraphs for distributed tracingFlamegraphs 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: [email protected]

If you want to read more about SigNoz

Golang Application Performance Monitoring with SigNoz


Original Link: https://dev.to/signoz/monitor-your-nodejs-application-with-opentelemetry-and-signoz-21k5

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To