Skip to main content
OpenTelemetry provides a unified approach to collecting telemetry data from your Node.js and TypeScript apps. This guide demonstrates how to configure OpenTelemetry in a Node.js app to send telemetry data to Axiom using OpenTelemetry SDK.

Prerequisites

To configure OpenTelemetry in a Node.js app for sending telemetry data to Axiom, certain prerequisites are necessary. These include:

Core Application (app.ts)

app.ts is the core of the app. It uses Express.js to create a simple web server. The server has an endpoint /rolldice that returns a random number, simulating a basic API. It also demonstrates the usage of span links to establish relationships between spans across different traces.

Exporter (instrumentation.ts)

instrumentation.ts sets up the OpenTelemetry instrumentation. It configures the OTLP (OpenTelemetry Protocol) exporters for traces and initializes the Node SDK with automatic instrumentation capabilities.

Installing the Dependencies

Navigate to the root directory of your project and run the following command to install the required dependencies:
This command will install all the necessary packages listed in your package.json below

Setting Up TypeScript Development Environment

To run the TypeScript app, you need to set up a TypeScript development environment. This includes adding a package.json file to manage your project’s dependencies and scripts, and a tsconfig.json file to manage TypeScript compiler options.

Add package.json

Create a package.json file in the root of your project with the following content:

Add tsconfig.json

Create a tsconfig.json file in the root of your project with the following content:
This configuration file specifies how the TypeScript compiler should transpile TypeScript files into JavaScript.

Running the Instrumented Application

To run your Node.js app with OpenTelemetry instrumentation, make sure your API token, and dataset is set in the instrumentation.ts file.

In Development Mode

For development purposes, especially when you need automatic restarts upon file changes, use:
This command will start the OpenTelemetry instrumentation in development mode using ts-node-dev. It sets up the exporter for tracing and restarts the server automatically whenever you make changes to the files.

In Production Mode

To run the app in production mode, you need to first build the TypeScript files into JavaScript. Run the following command to build your application:
This command compiles the TypeScript files to JavaScript based on the settings specified in tsconfig.json. Once the build process is complete, you can start your app in production mode with:
The server will start on the specified port, and you can interact with it by sending requests to the /rolldice endpoint.

Observe the telemetry data in Axiom

As you interact with your app, traces will be collected and exported to Axiom, where you can monitor and analyze your app’s performance and behavior.
Observing the telemetry data in Axiom

Observing the telemetry data in Axiom

Dynamic OpenTelemetry traces dashboard

This data can then be further viewed and analyzed in Axiom’s dashboard, providing insights into the performance and behaviour of your app.
Dynamic OpenTelemetry traces dashboard

Dynamic OpenTelemetry traces dashboard

Send data from an existing Node project

Manual Instrumentation

Manual instrumentation in Node.js requires adding code to create and manage spans around the code blocks you want to trace.
  1. Initialize Tracer:
Import and configure a tracer in your Node.js app. Use the tracer configured in your instrumentation setup (instrumentation.ts).
  1. Create Spans:
Wrap the code blocks that you want to trace with spans. Start and end these spans within your code.
  1. Annotate Spans:
Add metadata and logs to your spans for the trace data.

Automatic Instrumentation

Automatic instrumentation in Node.js simplifies adding telemetry data to your app. It uses pre-built libraries to automatically instrument common frameworks and libraries.
  1. Install Instrumentation Libraries:
Use OpenTelemetry packages that automatically instrument common Node.js frameworks and libraries.
  1. Instrument Application: Configure your app to use these libraries, which will automatically generate spans for standard operations.
After you set them up, these libraries automatically trace relevant operations without additional code changes in your app.

Reference

List of OpenTelemetry trace fields

List of imported libraries

The instrumentation.ts file imports the following libraries:

@opentelemetry/sdk-node

This package is the core SDK for OpenTelemetry in Node.js. It provides the primary interface for configuring and initializing OpenTelemetry in a Node.js app. It includes functionalities for managing traces and context propagation. The SDK is designed to be extensible, allowing for custom configurations and integration with different telemetry backends like Axiom.

@opentelemetry/auto-instrumentations-node

This package offers automatic instrumentation for Node.js apps. It simplifies the process of instrumenting various common Node.js libraries and frameworks. By using this package, developers can automatically collect telemetry data (such as traces) from their apps without needing to manually instrument each library or API call. This is important for apps with complex dependencies, as it ensures comprehensive and consistent telemetry collection across the app.

@opentelemetry/exporter-trace-otlp-proto

The @opentelemetry/exporter-trace-otlp-proto package provides an exporter that sends trace data using the OpenTelemetry Protocol (OTLP). OTLP is the standard protocol for transmitting telemetry data in the OpenTelemetry ecosystem. This exporter allows Node.js apps to send their collected traces to any backend that supports OTLP, such as Axiom. The use of OTLP ensures broad compatibility and a standardized way of transmitting telemetry data.

@opentelemetry/sdk-trace-base

Contained within this package is the BatchSpanProcessor, among other foundational elements for tracing in OpenTelemetry. The BatchSpanProcessor is a component that collects and processes spans (individual units of trace data). As the name suggests, it batches these spans before sending them to the configured exporter (in this case, the OTLPTraceExporter). This batching mechanism is efficient as it reduces the number of outbound requests by aggregating multiple spans into fewer batches. It helps in the performance and scalability of trace data export in an OpenTelemetry-instrumented app.