Using instrumentation libraries

How to instrument libraries an app depends on

When you develop an app, you make use of third-party libraries and frameworks to accelerate your work and to not reinvent the wheel. If you now instrument your app with OpenTelemetry, you don’t want to spend additional time on manually adding traces, logs and metrics to those libraries and frameworks. Fortunately, you don’t have to reinvent the wheel for those either: libraries might come with OpenTelemetry support natively or you can use an Instrumentation Library in order to generate telemetry data for a library or framework.

If you are instrumenting an app, you can learn on this page how to make use of natively instrumented libraries and Instrumentation Libraries for your dependencies.

If you want to instrument a library, you can learn on this page what you need to do to natively instrument your own library or how you can create an Instrumentation Library for a third-party library if none is available.

Use natively instrumented libraries

If a library comes with OpenTelemetry out of the box, you get the traces, metrics and logs emitted from that library, by adding and setting up the OpenTelemetry SDK with your app.

The library may provide some additional configuration for the instrumentation. Go to the documentation of that library to learn more.

Use Instrumentation Libraries

If a library does not come with OpenTelemetry out of the box, you can use instrumentation libraries in order to generate telemetry data for a library or framework.

For example, the instrumentation library for Express will automatically create spans based on the inbound HTTP requests.

Setup

Each instrumentation library is an NPM package. For example, here’s how you can install the instrumentation-express and instrumentation-http instrumentation libraries to instrument inbound and outbound HTTP traffic:

npm install --save @opentelemetry/instrumentation-http @opentelemetry/instrumentation-express

OpenTelemetry JavaScript also defines metapackages auto-instrumentation-node and auto-instrumentation-web, that bundle all Node.js- or web-based instrumentation libraries into a single package. It’s a convenient way to add automatically-generated telemetry for all your libraries with minimal effort:

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

Note, that using those metapackages increases your dependency graph size. Use individual instrumentation packages if you know exactly which ones you need.

Registration

After installing the instrumentation libraries you need, register them with the OpenTelemetry SDK for Node.js. If you followed the Getting Started you already use the metapackages. If you followed the instructions to initialize the SDK for manual instrumentation, update your instrumentation.ts (or instrumentation.js) as follows:

/*instrumentation.ts*/
...
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';

const sdk = new NodeSDK({
  ...
  // This registers all instrumentation packages
  instrumentations: [getNodeAutoInstrumentations()]
});

sdk.start()
/*instrumentation.js*/
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');

const sdk = new NodeSDK({
  ...
  // This registers all instrumentation packages
  instrumentations: [getNodeAutoInstrumentations()]
});

To disable individual instrumentation libraries you can apply the following change:

/*instrumentation.ts*/
...
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';

const sdk = new NodeSDK({
  ...
  // This registers all instrumentation packages
  instrumentations: [
    getNodeAutoInstrumentations({
      '@opentelemetry/instrumentation-fs': {
        enabled: false,
      },
    }),
  ],
});

sdk.start()
/*instrumentation.js*/
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');

const sdk = new NodeSDK({
  ...
  // This registers all instrumentation packages
  instrumentations: [
    getNodeAutoInstrumentations({
      '@opentelemetry/instrumentation-fs': {
        enabled: false,
      },
    }),
  ],
});

To only load individual instrumentation libraries, replace [getNodeAutoInstrumentations()] with the list of those you need:

/*instrumentation.ts*/
...
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { ExpressInstrumentation } from "@opentelemetry/instrumentation-express";

const sdk = new NodeSDK({
  ...
  instrumentations: [
    // Express instrumentation expects HTTP layer to be instrumented
    new HttpInstrumentation(),
    new ExpressInstrumentation(),
  ]
});

sdk.start()
/*instrumentation.js*/
const { HttpInstrumentation } = require("@opentelemetry/instrumentation-http");
const { ExpressInstrumentation } = require("@opentelemetry/instrumentation-express");

const sdk = new NodeSDK({
  ...
  instrumentations: [
    // Express instrumentation expects HTTP layer to be instrumented
    new HttpInstrumentation(),
    new ExpressInstrumentation(),
  ]
});

Configuration

Some instrumentation libraries offer additional configuration options.

For example, Express instrumentation offers ways to ignore specified middleware or enrich spans created automatically with a request hook:

import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
  ExpressInstrumentation,
  ExpressLayerType,
} from '@opentelemetry/instrumentation-express';

const expressInstrumentation = new ExpressInstrumentation({
  requestHook: function (span: Span, info: ExpressRequestInfo) {
    if (info.layerType === ExpressLayerType.REQUEST_HANDLER) {
      span.setAttribute([SemanticAttributes.HTTP_METHOD], info.request.method);
      span.setAttribute([SemanticAttributes.HTTP_URL], info.request.baseUrl);
    }
  },
});
/*instrumentation.js*/
const { SemanticAttributes } = require('@opentelemetry/semantic-conventions');
const {
  ExpressInstrumentation,
  ExpressLayerType,
} = require('@opentelemetry/instrumentation-express');

const expressInstrumentation = new ExpressInstrumentation({
  requestHook: function (span, info) {
    if (info.layerType === ExpressLayerType.REQUEST_HANDLER) {
      span.setAttribute([SemanticAttributes.HTTP_METHOD], info.request.method);
      span.setAttribute([SemanticAttributes.HTTP_URL], info.request.baseUrl);
    }
  },
});

You’ll need to refer to each instrumentation library’s documentation for advanced configuration.

Available instrumentation libraries

You can find a list of available instrumentation in the registry.

Instrument a library natively

If you want to add native instrumentation to your library, you should review the following documentation:

  • The concept page Libraries provides you with insights on when to instrument and what to instrument
  • The manual instrumentation provides you with the required code examples to create traces, metrics and logs for your library
  • The Instrumentation Implementation Guide for Node.js and browser contains JavaScript specific best practices for creating library instrumentation.

Create an instrumentation library

While having out of the box observability for an application is the preferred way, this is not always possible or desired. In those cases, you can create an instrumentation library, which would inject instrumentation calls, using mechanisms such as wrapping interfaces, subscribing to library-specific callbacks, or translating existing telemetry into the OpenTelemetry model.

To create such a library follow the Instrumentation Implementation Guide for Node.js and browser.