Monitoring your Express application using OpenTelemetry
Tags: node, javascript, monitoring, devops
Published: 2021-11-19 13:00:38

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](https://signoz.io/?utm_source=blog&utm_medium=monitor_express)** 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 if you have Docker installed. You can install Docker from its [official website](https://www.docker.com/get-started).

```jsx
git clone https://github.com/SigNoz/signoz.git
cd 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.

[![Deployment Docs](https:/signoz.io/img/blog/common/deploy_docker_documentation.webp)](https://signoz.io/docs/deployment/docker/?utm_source=blog&utm_medium=dd_vs_prometheus)

When you are done installing SigNoz, you can access the UI at: [http://localhost:3000](http://localhost:3000/application)

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 home](https:/signoz.io/img/blog/common/signoz_dashboard_homepage.webp)<figcaption>List of applications shown as an example on SigNoz dashboard</figcaption>

### Creating a sample express application
You need to ensure that you have **Node.js version 12 or newer**.

[Download latest version of Nodejs](https://nodejs.org/en/download/).

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**<br></br>

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:
   ```jsx
   npm i express
   ```
**Setup server.js**<br></br>
Create a file called `server.js` in your directory and with any text editor setup your `Hello World` file with the code below:
   ```jsx
   const express = require('express');

   const app = express();

   app.get('/hello', (req, res) => {

   res.status(200).send('Hello World');

   });

   app.listen(9090);
   ```
**Boot up the server with the following command on the terminal:**<br></br>
   ```jsx
  node server.js
   ```
You can check if your app is working by visiting: [http://localhost:9090/hello](http://localhost:9090/hello)

![Express App](https:/signoz.io/img/blog/2021/11/express_app.webp)<figcaption>Express app running on local host</figcaption>

You can then stop the server from running using `Ctrl + C`.

### Instrumenting the express application with OpenTelemetry
**Install OpenTelemetry launcher package**<br></br>
In the same directory path at the terminal, install the OpenTelemetry launcher package with this command:
   ```jsx
   npm install lightstep-opentelemetry-launcher-node
   ```
   The OpenTelemetry launcher makes getting started with OpenTelemetry easier by reducing configuration boilerplate.

**Using OpenTelemetry SDK**<br></br>
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:

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

**Pass the necessary environment variable**<br></br>
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.

   ```jsx
   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` : express_app (you can give whatever name that suits you)

  So the final command is:
  ```jsx
  OTEL_EXPORTER_OTLP_SPAN_ENDPOINT="http://localhost:55681/v1/trace" OTEL_METRICS_EXPORTER=none LS_SERVICE_NAME=express_app node server_init.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](http://localhost:9090/hello) multiple times to generate some load before you see your app on the SigNoz dashboard.


![Express app in the list of applications](https:/signoz.io/img/blog/2021/11/express_list_apps.webp)<figcaption>Express app in the list of applications</figcaption>


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

![SigNoz charts and metrics](https:/signoz.io/img/blog/common/signoz_charts_application_metrics.webp)<figcaption>Measure things like application latency, requests per sec, error percentage and see your top endpoints with SigNoz.</figcaption>

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

![List of traces on SigNoz dashboard](https:/signoz.io/img/blog/common/signoz_list_of_traces_hc.webp)<figcaption>View of traces at a particular timestamp</figcaption>

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

![Flamegraphs used to visualize spans of distributed tracing in SigNoz UI](https:/signoz.io/img/blog/common/signoz_flamegraphs.webp)<figcaption>View of traces at a particular timestamp</figcaption>

You can also build custom metrics dashboard for your infrastructure.

![Custom metrics dashboard](https:/signoz.io/img/blog/common/signoz_custom_dashboard-min.webp)<figcaption>You can also build a custom metrics dashboard for your infrastructure</figcaption>

## 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 👇

[![SigNoz GitHub repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yroh39ngk6sz6tv3z9y.png)](https://bit.ly/2WkkmL4)

If you have any questions or need any help in setting things up, join our slack community and ping us in `#help` channel.

[![SigNoz Slack community](https:/signoz.io/img/blog/common/join_slack_cta.png)](https://bit.ly/signoz-slack)

---

If you want to read more about SigNoz 👇

[Golang Aplication Monitoring with OpenTelemetry and SigNoz](https://signoz.io/opentelemetry/go/)

[OpenTelemetry collector - complete guide](https://signoz.io/blog/opentelemetry-collector-complete-guide/)