Nodejs is a popular Javascript runtime environment that executes Javascript code outside of a web browser. Express is the most popular web frameworks that sits on top of Nodejs and adds functionalities like middleware, routing, etc. to Nodejs.
You can monitor your express application using OpenTelemetry and a tracing backend of your choice. OpenTelemetry is the leading open-source standard under the Cloud Native Computing Foundation that aims to standardize the process of instrumentation across multiple languages.
In this article, we will be using SigNoz to store and visualize the telemetry data collected by OpenTelemetry from a sample Expressjs application.
Running an Express application with OpenTelemetry​
OpenTelemetry is a set of tools, APIs, and SDKs used to instrument applications to create and manage telemetry data(logs, metrics, and traces).
Install 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 a sample express application​
You need to ensure that you have Node.js version 12 or newer.
Download latest version of Nodejs.For the sample application, let's create a basic 'hello world' express.js application.
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
Setup index.js
Create a file calledindex.js
in your directory and with any text editor setup yourHello 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.
Instrumenting the express application with OpenTelemetry​
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));
});Pass the necessary environment variable
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
: express_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=express_app \
node -r ./tracing.js index.js
And congratulations! You have now instrumented your express application with OpenTelemetry.
Below you can find your express_app
 in the list of applications being monitored. You might have to hit the endpoint at http://localhost:9090/hello multiple times to generate some load before you see your app on the SigNoz dashboard.
SigNoz is open-source, and a full-stack APM. It comes with charts of RED metrics and a seamless transition from metrics to traces.
Open-source tool to visualize telemetry data​
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.
You can also build custom metrics dashboard for your infrastructure.
Conclusion​
OpenTelemetry makes it very convenient to instrument your Express 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 are someone who understands more from video, then you can watch the below video tutorial on the same with SigNoz.
Â
Â
If you have any questions or need any help in setting things up, join our slack community and ping us in #support
channel.
If you want to read more about SigNoz 👇