Skip to main content
Sumo Logic

JavaScript OpenTelemetry auto-instrumentation

Thanks to Opentelemetry-JS it is very easy to start sending telemetry data. The automatic instrumentation of NodeJS applications is simple. The OpenTelemetry-JS community supports all active versions of NodeJS. See a list of supported runtimes

Instruction below applies to OpenTelemetry JavaScript Instrumentation in version 1.0.3/0.27.0.

How to instrument your NodeJS application?

There are a few simple steps to instrument your application and obtain telemetry data.

1. Packages installation

The installation of the packages listed below is required to apply the instrumentation and export telemetry data.

$ npm install --save @opentelemetry/sdk-trace-node@1.0.1
$ npm install --save @opentelemetry/sdk-trace-base@1.0.1
$ npm install --save @opentelemetry/resources@1.0.1
$ npm install --save @opentelemetry/api@1.0.3

The packages above are mandatory. The next step is to install OpenTelemetry plugins and chosen exporters. They will automatically instrument used node modules. The list of available plugins can be found here. If the application is an HTTP Client/Server then their corresponding plugins must be installed like in the example below:

$ npm install --save @opentelemetry/instrumentation-http@0.27.0

2. Instrumentation with OpenTelemetry HTTP exporter

Install OpenTelemetry HTTP exporter package:

$ npm install --save @opentelemetry/exporter-trace-otlp-proto@0.27.0

To enable instrumentation in your application add the code below to your project. The best way is to save it and name it tracer.js - this file will contain everything that is needed to configure tracing. The first step is to provide YOUR_SERVICE_NAME which should define a logical service name as a string value that represents its business logic. This will appear as a tracing service name in the Sumo Logic web interface. Similar to service name YOUR_APPLICATION_NAME should represent its business logic. The next step is to create new NodeTracerProvider() to automatically instrument NodeJS applications. It is important to initialize NodeTracerProvider before any other module of the application.

Next, configure the OpenTelemetry Exporter, exporterOptions. This is where url is configured. The url sets the collection receiver endpoint from a Sumo Logic Source. The example below points to the default Sumologic Kubernetes Collector.

After the exporter configuration, the instrumentation has to be registered. In our example, HttpInstrumentation() is registered. This will enable auto-instrumentation for the HTTP package. If there are other libraries to instrument it is enough to add them to the instrumentations list. All available instrumentation packages can be found in the repositories OpenTelemetry JS and OpenTelemetry JS Contrib.

'use strict';

const opentelemetry = require('@opentelemetry/api');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { BatchSpanProcessor } = require("@opentelemetry/sdk-trace-base");
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { Resource } = require('@opentelemetry/resources');


module.exports = () => {
 const resources = new Resource({
    'service.name': 'YOUR_SERVICE_NAME',
    'application': 'YOUR_APPLICATION_NAME',
    //'ANY_OTHER_ATTRIBUTE_KEY': 'ANY_OTHER_ATTRIBUTE_VALUE',
 }); 

 const provider = new NodeTracerProvider({ resource: resources });
 
 const exporterOptions = {
  url: 'http://collection-sumologic-otelcol.sumologic:55681/v1/traces',
 }


 
 const exporter = new OTLPTraceExporter(exporterOptions);
 provider.addSpanProcessor(new BatchSpanProcessor(exporter));
 provider.register();

 registerInstrumentations({
  instrumentations: [
    new HttpInstrumentation(),
  ],
 })
 return opentelemetry.trace.getTracer("instrumentation-example");
}

The last step is to execute tracer in your application code. Add the following line in the code below in your application on the top of the code.

const tracer = require('./tracer')()