Node.js (server-side) SDK observability reference

This topic documents how to get started with the LaunchDarkly observability plugin for the Node.js (server-side) SDK.

The Node.js (server-side) SDK supports the observability plugin for error monitoring, logging, and tracing.

SDK quick links

LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:

ResourceLocation
SDK API documentationObservability plugin API docs
GitHub repository@launchdarkly/observability-node
Sample applicationExample Express app
Published modulenpm
For use in server-side applications only

The observability-node plugin is intended for use in multi-user Node.js server applications. If you want to set up LaunchDarkly in JavaScript in a browser environment, read the JavaScript SDK reference and JavaScript SDK observability reference.

To learn more about LaunchDarkly’s different SDK types, read Choosing an SDK type.

Prerequisites and dependencies

This reference guide assumes that you are somewhat familiar with the LaunchDarkly Node.js (server-side) SDK.

The observability plugin is compatible with the Node.js (server-side) SDK, version 9.10.0 and later.

Get started

Follow these steps to get started:

Install the plugin

LaunchDarkly uses a plugin to the Node.js (server-side) SDK to provide observability.

The first step is to make both the SDK and the observability plugins available as dependencies.

Here’s how:

$npm install @launchdarkly/node-server-sdk
$npm install @launchdarkly/observability-node

Then, import the plugin into your code:

Import, Node.js SDK v9.10+
1import { init } from '@launchdarkly/node-server-sdk'
2import { Observability } from "@launchdarkly/observability-node";

Initialize the client

Next, initialize the SDK and the plugin.

To initialize, you need your LaunchDarkly environment’s SDK key. This authorizes your application to connect to a particular environment within LaunchDarkly. To learn more, read Initialize the client in the Node.js (server-side) SDK reference guide.

Here’s how to initialize the SDK and plugin:

Initialize, Node.js SDK v9.10+
1const client = init(
2 'YOUR_SDK_KEY',
3 {
4 plugins: [
5 new Observability(),
6 ],
7 },
8)

Import the plugin before other libraries

The observability plugin instruments supported libraries, such as Express and database drivers, by hooking into the Node.js CommonJS module loader. Importing @launchdarkly/observability-node installs those hooks. The plugin instruments only the libraries that your application loads after the hooks are in place. If your application loads Express before it imports the plugin, the plugin does not capture Express routes or middleware.

Your application does not need to wait for the LaunchDarkly client to finish initializing before it loads other libraries.

To control the order, initialize the SDK in a dedicated module, then load that module before your application loads anything else. How you load it depends on whether your application uses CommonJS or ECMAScript modules (ESM).

Automatic instrumentation is limited in ESM applications

The hooks only intercept CommonJS module loading. The plugin does not instrument libraries that your application loads with an ESM import, which means that ESM applications do not receive spans for Express routes or middleware.

Most frameworks load the Node.js http module internally through CommonJS. As a result, ESM applications still receive spans for incoming and outgoing HTTP requests, along with the metrics that LaunchDarkly generates from those spans. If your application does not load http this way, the plugin captures no automatic spans.

In either case, you can record spans, metrics, logs, and errors manually with LDObserve. To learn more, read Tracing.

Here is the dedicated module:

launchdarkly.js
1const { init } = require('@launchdarkly/node-server-sdk')
2const { Observability } = require('@launchdarkly/observability-node')
3
4module.exports = init(
5 'YOUR_SDK_KEY',
6 {
7 plugins: [
8 new Observability(),
9 ],
10 },
11)

Then load that module from the first line of your application’s entry point, before you require any other library:

server.js
1const client = require('./launchdarkly')
2const express = require('express')
3
4const app = express()

Alternatively, preload the module with the Node.js --require option. This keeps the ordering requirement out of your application code:

Preload the module
$node --require ./launchdarkly.js server.js

If you write your application in TypeScript and compile it to CommonJS, the same ordering applies to the compiled output. Put the initialization in its own source file, such as launchdarkly.ts, and import that file first.

In an ESM application, Node.js evaluates every import statement in a file before it runs any of that file’s own code. Initializing the SDK in the body of your entry point is too late, because your application has already loaded Express and your other libraries by that point. Put the initialization in its own module instead.

Ordering determines whether an ESM application produces any telemetry at all. If the plugin loads first, it captures HTTP request spans through the imports your framework makes internally. If it loads later, it captures nothing.

Here is the dedicated module:

instrumentation.js
1import { init } from '@launchdarkly/node-server-sdk'
2import { Observability } from '@launchdarkly/observability-node'
3
4export const client = init(
5 'YOUR_SDK_KEY',
6 {
7 plugins: [
8 new Observability(),
9 ],
10 },
11)

Then preload the module with the Node.js --import option:

Preload the module
$node --import ./instrumentation.js server.js

You can also load the module as the first import in your entry point. We recommend the --import option instead, because bundlers and automatic import sorting can reorder import statements:

server.js
1import './instrumentation.js'
2import express from 'express'
3
4const app = express()

Configure the plugin options

You can configure options for the observability plugin when you initialize the SDK. The plugin constructor takes an optional object with the configuration details.

Here is an example:

Plugin options, Node.js SDK v9.10+
1const client = init(
2 'YOUR_SDK_KEY',
3 {
4 plugins: [
5 new Observability({
6 serviceName: 'example-service',
7 // we recommend setting serviceVersion to the latest deployed git SHA
8 serviceVersion: 'example-sha'
9 }),
10 ],
11 },
12)

For more information on plugin options, as well as how they interact with environment variables and existing OpenTelemetry configuration, read Configuration for server-side observability.

Explore supported features

The observability plugin supports the following features. After the SDK and plugins are initialized, you can access these from within your application:

Review observability data in LaunchDarkly

After you initialize the SDK and observability plugin, your application automatically starts sending observability data back to LaunchDarkly in the form of custom events. You can review this information in the LaunchDarkly user interface. To learn how, read Observability.

Specifically, the observability data includes events that LaunchDarkly uses to automatically create the following metrics:

  • User HTTP error rate (OpenTelemetry)
  • User HTTP 5XX response rate (OpenTelemetry)
  • User non-HTTP exception rate (OpenTelemetry)
  • Average, P95, and P99 request latency (OpenTelemetry)

To learn more, read OpenTelemetry autogenerated metrics.