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.
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
- Creating 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.
git clone -b main https://github.com/SigNoz/signoz.git
cd signoz/deploy/
./install.sh
For detailed instructions, you can visit our documentation.
When you are done installing SigNoz, you can access the UI at:Â http://localhost:3301
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.
Creating 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.
If you do not want to follow these steps manually, you can directly check out the GitHub repo of the sample application. You can run the app directly after cloning it. Check out the details of running the app from the last step in the set of instructions.
But, it would be better if you follow these steps to understand what's happening.
Check if node is installed on your machine by using the below command:
node -v
Steps to get the app set up and running:
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
Create index.js
Create a file calledindex.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);Check if your application is working
Run your application by using the below command at your terminal.node index.js
You can check if your app is working by visiting: http://localhost:9090/hello
Once you are finished checking, exit the application by using
Ctrl + C
on your terminal.
Set up OpenTelemetry and send data to SigNoz​
Install OpenTelemetry packages
You will need the following OpenTelemetry packages for this sample application.npm install --save @opentelemetry/api
npm install --save @opentelemetry/sdk-node
npm install --save @opentelemetry/auto-instrumentations-node
npm install --save @opentelemetry/exporter-otlp-grpcOpenTelemetry clients have two major components: the SDK and the API. The details of the packages used for the application are as follows:
opentelemetry/api
Defines data types and operations for generating and correlating tracing, metrics, and logging data. The API is what you use to instrument your code.opentelemetry/sdk-node
Provides automated instrumentation and tracing for Node.js applications.opentelemetry/auto-instrumentations-node
A meta-package from opentelemetry-js-contrib that provides a simple way to initialize multiple Node.js instrumentations.opentelemetry/exporter-otlp-grpc
Exports data via gRPC using OTLP format.
Create
tracing.js
file
Instantiate tracing by creating atracing.js
file and using the below code.// tracing.js
'use strict'
const process = require('process');
const opentelemetry = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-otlp-grpc');
// configure the SDK to export telemetry data to the console
// enable all auto-instrumentations from the meta package
const traceExporter = new OTLPTraceExporter();
const sdk = new opentelemetry.NodeSDK({
traceExporter,
instrumentations: [getNodeAutoInstrumentations()]
});
// initialize the SDK and register with the OpenTelemetry API
// this enables the API to record telemetry
sdk.start()
.then(() => console.log('Tracing initialized'))
.catch((error) => console.log('Error initializing tracing', error));
// gracefully shut down the SDK on process exit
process.on('SIGTERM', () => {
sdk.shutdown()
.then(() => console.log('Tracing terminated'))
.catch((error) => console.log('Error terminating tracing', error))
.finally(() => process.exit(0));
});Run the sample application with OpenTelemetry and send data to SigNoz
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_ENDPOINT="<IP of SigNoz>:4317" \
OTEL_RESOURCE_ATTRIBUTES=service.name=<service_name> \
node -r ./tracing.js index.jsReplacing 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
: node_app (you can give whatever name that suits you)So the final command is:
OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4317" \
OTEL_RESOURCE_ATTRIBUTES=service.name=node_app \
node -r ./tracing.js index.jsYou can check your application running at http://localhost:9090/hello. You need to generate some load in order to see data reported on SigNoz dashboard. Refresh the endpoint for 10-20 times, and wait for 2-3 mins.
And, congratulations! You have instrumented your sample Node.js app. You can now access the SigNoz dashboard at http://localhost:3301 to monitor your app for performance metrics.
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
You can then choose a particular timestamp where latency is high to drill down to traces around that timestamp.
You can use flamegraphs to exactly identify the issue causing the latency.
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 👇
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 👇