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.

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/node
$ npm install --save @opentelemetry/tracing
$ npm install --save @opentelemetry/api
$ npm install --save @opentelemetry/exporter-jaeger

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

$ npm install --save @opentelemetry/plugin-http
$ npm install --save @opentelemetry/plugin-https

 

2. Instrumentation with Jaeger exporter

To enable instrumentation in your application add the code below to your project. The best way is to save it and name it as tracer.js - this file will contain everything that is needed to configure tracing. The creation of the new NodeTracerProvider() is the first step to automatically instrument NodeJS applications.

It is important to initialize NodeTracerProvider before any other module of the application. Next, configure the JaegerExporter.

In the example below endpoint points to the default Sumologic Kubernetes Collector.

'use strict';

const opentelemetry = require('@opentelemetry/api');
const { NodeTracerProvider } = require('@opentelemetry/node');
const { BatchSpanProcessor } = require("@opentelemetry/tracing");
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');

module.exports = () => {
 const provider = new NodeTracerProvider();
 const options = {
  serviceName: "my-service-name",
  endpoint: 'http://collection-sumologic-otelcol.sumologic:14268/api/traces',
}

const exporter = new JaegerExporter(options)
provider.addSpanProcessor(new BatchSpanProcessor(exporter));
provider.register();

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)()