Home » How to Use OpenTelementry to Hint Node.js Applications

How to Use OpenTelementry to Hint Node.js Applications

by Icecream
0 comment

Observability refers to our capacity to “see” and perceive what’s taking place inside a system by its exterior indicators (like logs, metrics, and traces).

Observability entails accumulating and analyzing knowledge from sources inside a system to observe its efficiency and deal with issues successfully.

Why is Observability Useful?

  1. Detecting and Troubleshooting Problems: Observability performs a job in figuring out and diagnosing points inside a system. When one thing goes flawed, accessing knowledge helps pinpoint the trigger and resolve issues extra shortly.

  2. Optimizing Performance: Through monitoring metrics and efficiency indicators, observability helps in optimizing the efficiency of your system. This consists of figuring out bottlenecks, bettering useful resource utilization, and guaranteeing operation.

  3. Planning for Future Capacity: Understanding how your system behaves over time is important for planning capability necessities. Observability knowledge can reveal traits, peak utilization durations, and useful resource wants, serving to your choices relating to scaling.

  4. Enhancing User Experience: By observing consumer interactions together with your system by way of logs and metrics, you possibly can enhance the consumer expertise. It assists in recognizing patterns, preferences, and potential areas that may be enhanced for consumer satisfaction.

Why Should I Use OpenTelementary?

Observability is crucial for guaranteeing the reliability and availability of your Node.js purposes. But manually instrumenting your code to gather and export telemetry knowledge, comparable to traces, metrics, and logs, can turn out to be very tense.

Manual instrumentation could be very tedious, error-prone, and inconsistent. It may introduce further overhead and complexity to your utility logic.

In this information, you’ll discover ways to use OpenTelemetry’s auto-instrumentation that will help you obtain easy Node.js monitoring.


Before you undergo this information, be sure you have the next:

  • A Node.js utility

  • A Datadog account and an API key. If you do not have one, you possibly can enroll right here to get one.

  • A Backend service. You can use a backend service like Zepkin or Jaeger to retailer and analyze hint knowledge. For this information, we’ll be utilizing Jaeger.

  • Some fundamental information of Linux instructions. You needs to be aware of utilizing the command line and modifying configuration recordsdata.

Prepare Your Application

In this information, you can be utilizing a Node.js utility that has two providers that switch knowledge between themselves. You will use OpenTelemetry’s Node.js shopper library to ship hint knowledge to an OpenTelementay collector.

Firstly, clone the Repo Locally:

$ git clone https://github.com/<github-account>/nodejs-example.git

Then run the appliance:

npm set up

Go to the listing of the primary service utilizing this command:

$ cd <ServiceA>

And begin the primary service.

$ node index.js

Then go to the listing of the second service

$ cd <ServiceB>

And begin the second service.

$ node index.js

Open Service A, on this case port 5555, and enter some info. Then repeat the identical for Service B.


How to Set Up OpenTelementary

After beginning the providers, it is time to set up the OpenTelementary modules you will want for auto-instrumentation.

Here are what we have to set up:

$ npm set up --save @opentelemetry/api

$ npm set up --save @opentelemetry/instrumentation

$ npm set up --save @opentelemetry/tracing

$ npm set up --save @opentelemetry/exporter-trace-otlp-http

$ npm set up --save @opentelemetry/assets

$ npm set up --save @opentelemetry/semantic-conventions

$ npm set up --save @opentelemetry/auto-instrumentations-node

$ npm set up --save @opentelemetry/sdk-node

$ npm set up --save @opentelemetry/exporter-jaeger

Here’s break down of what every module does:

  • @opentelemetry/api: This module gives the OpenTelemetry API for Node.js.

  • @opentelemetry/instrumentation: The instrumentation libraries present computerized instrumentation to your Node.js utility. They mechanically seize telemetry knowledge with out requiring handbook code modifications.

  • @opentelemetry/tracing: This module incorporates the core tracing performance for OpenTelemetry in your Node.js utility. It consists of the Tracer and Span interfaces, that are necessary for capturing and representing distributed traces inside your purposes.

  • @opentelemetry/exporter-trace-otlp-http: This exporter module allows sending hint knowledge to an OpenTelemetry Protocol (OTLP) suitable backend over HTTP.

  • @opentelemetry/assets: This module gives a option to outline and handle assets related to traces.

  • @opentelemetry/semantic-conventions: This module defines a set of semantic conventions for tracing. It establishes a typical set of attribute keys and worth codecs to make sure consistency in how telemetry knowledge is represented and interpreted.

  • @opentelemetry/auto-instrumentations-node: This module simplifies the method of instrumenting your utility by mechanically making use of instrumentation to supported libraries.

  • @opentelemetry/sdk-node: The Software Development Kit (SDK) for Node.js gives the implementation of the OpenTelemetry API.

  • @opentelemetry/exporter-jaeger: This exporter module permits exporting hint knowledge to Jaeger. Jaeger gives a user-friendly interface for monitoring and analyzing hint knowledge.

Configure the Node.js Application

Next, add a Node.js SDk tracer to deal with the instantiation and shutdown of the tracing.

To add the tracer, create a file tracer.js:

$ nano tracer.js

Then add the next code to the file:

"use strict";

const {
} = require("@opentelemetry/tracing");
// Import the JaegerExporter
const { JaegerExporter } = require("@opentelemetry/exporter-jaeger");
const { Resource } = require("@opentelemetry/assets");
const {
} = require("@opentelemetry/semantic-conventions");

const opentelemetry = require("@opentelemetry/sdk-node");
const {
} = require("@opentelemetry/auto-instrumentations-node");

// Create a brand new occasion of JaegerExporter with the choices
const exporter = new JaegerExporter({
    serviceName: "YOUR-SERVICE-NAME",
    host: "localhost", // optionally available, might be set by OTEL_EXPORTER_JAEGER_AGENT_HOST
    port: 16686 // optionally available

const supplier = new PrimaryTracerSupplier({
    useful resource: new Resource({
// Add the JaegerExporter to the span processor
supplier.addSpanProcessor(new EasySpanProcessor(exporter));

const sdk = new opentelemetry.NodeSDK({
    hintExporter: exporter,
    instrumentations: [getNodeAutoInstrumentations()],

    .then(() => {
        console.log("Tracing initialized");
    .catch((error) => console.log("Error initializing tracing", error));

course of.on("SIGTERM", () => {
        .then(() => console.log("Tracing terminated"))
        .catch((error) => console.log("Error terminating tracing", error))
        .lastly(() => course of.exit(0));

Here is an easy breakdown of the code:

  • The code begins by importing the modules PrimaryTracerSupplier and EasySpanProcessor for organising tracing from the OpenTelemetry library

  • It then imports the JaegerExporter module for exporting hint knowledge to Jaeger.

  • The code creates a brand new occasion of the JaegerExporter, specifying the service title, host, and port.

  • It then creates a PrimaryTracerSupplier and provides the JaegerExporter to the span processor utilizing EasySpanProcessor.

  • The supplier is registered, setting it because the default supplier for the appliance.

  • An OpenTelemetry SDK occasion is created, configuring it with the JaegerExporter and enabling auto-instrumentations for Node.js.

  • The OpenTelemetry SDK is began, initializing tracing.

  • A handler for the SIGTERM sign is ready as much as shut down tracing when the appliance is terminated.

  • The code then configures the hint supplier with a hint exporter. To confirm the instrumentation, ConsoleSpanExporter is used to print among the tracer output to the console.

How to Set Up OpenTelemetry to Export the Traces

Next, you will want to put in writing the configurations to gather and export knowledge within the OpenTelemetry Collector.

Create a file config.yaml:


    api: # Replace together with your Datadog API key
      key: "<YOUR_DATADOG_API_KEY>"
    # Optional:
    #   - endpoint: https://app.datadoghq.eu  # For EU area


    endpoint: :1777
    endpoint: :55679

  extensions: [health_check, pprof, zpages]
      receivers: [otlp]
      processors: [batch]
      exporters: [datadog]

The configuration units up OpenTelemetry with the OTLP (OpenTelemetry Protocol) receiver and the Datadog exporter. Here’s a break down of the code:

  • receivers: Specifies the parts that obtain the telemetry knowledge. In this case, it consists of the OTLP receiver, which helps each gRPC and HTTP protocols.

  • exporters: Defines the parts accountable for exporting telemetry knowledge. Here, it configures the Datadog exporter, offering the Datadog API key. Additionally, an optionally available endpoint is offered for utilizing Datadog’s EU area.

  • processors: Specifies the info processing parts. In this case, the batch processor is used to batch and ship knowledge in bigger chunks for effectivity.

  • extensions: Defines further parts that reach the performance. Here, it consists of extensions for pprof (profiling knowledge), zpages (debugging pages), and a well being verify extension.

  • service: Configures the general service conduct, together with the extensions and pipelines. The extensions part lists the extensions for use, and the pipelines part configures the telemetry knowledge pipeline. Here, the traces pipeline consists of the OTLP receiver, the batch processor, and the Datadog exporter.

This code is configured by the collector with the Datadog exporter to ship the traces to Datadog Distributed Tracing providers. However, there are different distributed tracing providers that you should utilize like New Relic, Logzio, and Zipkin.

How to Start the Application

After accurately organising auto-instrumentation, begin the appliance once more to check and confirm the tracing configuration.

Begin by beginning the OpenTelemetry Collector:

./otelcontribcol_darwin_amd64 --config ./config.yaml

The collector will begin on port 4317.

Next, go to the listing of the primary service:

$ cd <ServiceA>

Then begin the primary service with the “–require ‘./tracer.js’” parameter to allow the appliance instrumentation.

$ node --require './tracer.js' index.js

Repeat this to begin the second service.

Using a browser like Chrome, go to the endpoints of your two purposes’ providers, add some knowledge, and ship some requests to check the tracing configuration.

Once the requests are made, these traces are picked up by the collector, which then dispatches them to the distributed tracing backend specified by the exporter configuration within the collector’s configuration file.

It’s price noting that our tracer not solely facilitates the transmission of traces to the designated backend, but in addition exports them to the console on the similar time.

This twin performance permits for real-time visibility into the traces being generated and despatched, serving to us within the monitoring and debugging processes.

Now, let’s use Jaeger UI to observe the traces:

Start Jaeger with the next command:

docker run -d --name jaeger 
  -p 5775:5775/udp 
  -p 6831:6831/udp 
  -p 6832:6832/udp 
  -p 5778:5778 
  -p 16686:16686 
  -p 14250:14250 
  -p 14268:14268 
  -p 14269:14269 
  -p 9411:9411 

Using a browser, begin Jaeger UI on the http://localhost:16686/ endpoint.


There you’ve gotten it! The initiation of the hint ranging from the inception level of 1 service, navigating by way of a sequence of operations.

This path is created because the service begins its operations, ensuing within the arrange of the opposite service to meet the unique request you initiated earlier.

The hint gives a visible narrative of what occurs between these providers, providing insights into every step of the method.

How Can You Use Observability Data?

  1. Monitoring Metrics: Keep an eye fixed on key metrics comparable to response occasions, error charges, and useful resource utilization. Sudden spikes or anomalies can point out points that require consideration.

  2. Logging: Log knowledge gives detailed details about occasions and actions inside a system. Analyzing logs helps in understanding the sequence of actions and tracing the steps resulting in a problem.

  3. Tracing: Tracing entails monitoring the movement of requests or transactions throughout completely different parts of a system. This helps in understanding the journey of a request and figuring out any bottlenecks or delays.

  4. Alerting: Set up alerts based mostly on particular circumstances or thresholds. When sure metrics exceed predefined limits, alerts can notify you in real-time, permitting for instant motion.

  5. Visualization: Use graphical representations and dashboards to visualise advanced knowledge. This makes it simpler to establish patterns, traits, and correlations within the observability knowledge.

Observability, when carried out successfully, empowers groups to proactively handle and enhance the efficiency, reliability, and consumer expertise of their techniques. It’s an important side of recent software program improvement and operations.


In this information you realized easy methods to auto-instrument Node.js purposes with little code by:

  • Installing and configuring the OpenTelemetry Node.js SDK and the auto-instrumentation package deal

  • Enabling computerized tracing and metrics assortment to your Node.js purposes and their dependencies

  • Exporting to visualise your telemetry knowledge on a backend, Jaeger.

Using OpenTelemetry’s auto-instrumentation might help you achieve invaluable insights into the efficiency and conduct of your Node.js purposes with out having to manually instrument every library or framework.

You may also like

Leave a Comment