IN-DEPTH TECH

IN-DEPTH TECH

clock icon

4 min read

Blog thumbnail
Published on 05/23/2023
Last updated on 02/03/2025

Unleashing the Power of OpenTelemetry with Auto-Instrumentation in JavaScript

Share

OpenTelemetry, Auto-instrumentation, Node.JS, Javascript

TL;DR: Learn how to effortlessly instrument your JavaScript applications with OpenTelemetry using the groundbreaking auto-instrumentation feature.


Navigating the Complexity of Micro-Services

In the era of micro-services, debugging complex application architectures has become a formidable challenge. With multiple independent services working together, understanding how they interact and impact each other is no easy task.

To address this, the OpenTelemetry observability framework steps in as a game-changer.

OpenTelemetry empowers developers to collect, process, and export telemetry data from distributed systems effectively. A critical aspect of OpenTelemetry is instrumentation, which involves integrating telemetry collection code into an application to transmit valuable data to an observability backend or collector. Traditionally, this required manual coding, but with the release of auto-instrumentation, the process has been revolutionized. Auto-instrumentation allows you to effortlessly gather telemetry data without modifying a single line of code.

Harnessing the Power of Zero-Code Instrumentation

Zero-code instrumentation, a cutting-edge technique, enables automatic telemetry data collection without any code modifications. This approach offers numerous advantages over manual instrumentation, including faster time-to-value, reduced risk of human errors, and unparalleled ease of use. With zero-code instrumentation, developers can promptly initiate telemetry data collection without diving into the intricacies of the underlying telemetry framework or API. This saves precious time and effort while ensuring the consistency and accuracy of telemetry data across applications and teams, making it an indispensable asset for organizations.

Getting Started with Auto-Instrumentation

Let's kick off our auto-instrumentation journey by creating a simple app.js application that sends a GET request:

const http = require('http');

const options = {
    hostname: 'example.com',
    port: 80,
    path: '/',
    method: 'GET'
};

const req = http.request(options);

req.end();

Before we proceed, we need to install the following packages to leverage the OpenTelemetry auto-instrumentation:

npm install --save @opentelemetry/api
npm install --save @opentelemetry/auto-instrumentations-node

For effective visualization of the collected traces, we will be utilizing the open-source Jaeger as our backend.

To run Jaeger locally, execute the following Docker command:

docker run -d --name jaeger \
  -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
  -e COLLECTOR_OTLP_ENABLED=true \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 5778:5778 \
  -p 16686:16686 \
  -p 4317:4317 \
  -p 4318:4318 \
  -p 14250:14250 \
  -p 14268:14268 \
  -p 14269:14269 \
  -p 9411:9411 \
  jaegertracing/all-in-one:latest

To send the generated traces to Jaeger, we must configure the auto-instrumentation exporter by setting the following environment variable:

export OTEL_TRACES_EXPORTER="jaeger"

Note: To see the full range of configuration options, see the Auto-Instrumentation GitHub repository.

Next, we'll run our application with auto-instrumentation enabled using the --require flag:

node --require '@opentelemetry/auto-instrumentations-node/register' app.js

If your Node.JS application is encapsulated within a complex run script, you can set the auto-instrumentation via an environment variable before executing Node.JS:

env NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"

The auto-instrumentation module offers extensive configurability through environment variables. You can fine-tune various aspects of its behavior to suit your specific requirements, including resource detectors, exporter configuration, trace context propagation headers, and more.

Note: Instrumentation configuration via environment variables is not yet supported. If you require instrumentation configuration, you must initialize OpenTelemetry programmatically.

Considerations when Using Auto-Instrumentation

While auto-instrumentation simplifies the process of instrumenting your applications, it's essential to be aware of certain considerations. This approach may introduce some performance overhead, potentially impacting your application's behavior. Additionally, auto-instrumentation may not offer the same level of configurability as programmatically using the OpenTelemetry SDK. Therefore, it's crucial to review its compatibility with your needs before fully adopting it. For more details, consult the OpenTelemetry JS Documentation.

In Summary

The groundbreaking auto-instrumentation feature of OpenTelemetry enables developers to effortlessly collect telemetry data without manual coding. By leveraging auto-instrumentation, JavaScript applications can seamlessly gather telemetry data without requiring any code changes. While this approach simplifies the instrumentation process, it's important to consider the potential performance overhead and compatibility with specific frameworks or libraries. As OpenTelemetry continues to evolve, developers can look forward to expanded auto-instrumentation capabilities, supporting a broader range of languages and frameworks in the future.

With this powerful auto-instrumentation technique at your disposal, you can unlock unparalleled observability and gain invaluable insights into your distributed systems.

Remember, embracing auto-instrumentation is a leap towards a more streamlined and effective observability experience.


Sami Musallam is a Software Engineer and open-source advocate in Cisco’s Emerging Technology & Incubation organization. You can follow him on LinkedIn @samimusallam.

Subscribe card background
Subscribe
Subscribe to
the Shift!

Get emerging insights on emerging technology straight to your inbox.

Welcome to the future of agentic AI: The Internet of Agents

Outshift is leading the way in building an open, interoperable, agent-first, quantum-safe infrastructure for the future of artificial intelligence.

thumbnail

* No email required

Subscribe
Subscribe
 to
The Shift
!
Get
emerging insights
on innovative technology straight to your inbox.

The Shift is Outshift’s exclusive newsletter.

Get the latest news and updates on generative AI, quantum computing, and other groundbreaking innovations shaping the future of technology.

Outshift Background