JavaScript SDK observability reference

The LaunchDarkly observability features are available for early access

Observability features in the LaunchDarkly UI are publicly available in early access.

The SDK observability plugins are designed for use with the in-app observability features. They are currently in available in Early Access and APIs are subject to change until a 1.x version is released.

If you are interested participating in the Early Access Program for upcoming observability plugins for additional SDKs, sign up here.

Overview

This topic documents how to get started with the LaunchDarkly observability plugins for the client-side JavaScript SDK.

The JavaScript SDK supports the following observability plugins:

  • An Observability plugin for error monitoring, logging, and tracing.
  • A Session replay plugin that provides a way to record and replay end-user sessions from your application.
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
Session replay API docs
GitHub repository@launchdarkly/observability
Published modulenpm
For use in client applications only

These observability and session replay plugins are for the LaunchDarkly client-side JavaScript-based SDKs.

To learn more about LaunchDarkly’s different SDK types, read Client-side, server-side, and edge SDKs.

The observability plugin replaces the JavaScript telemetry integration

We strongly recommend that any customers who are using LaunchDarkly’s browser-telemetry integration now use this observability plugin instead.

As we develop additional telemetry-related functionality, we will only be adding it to this observability plugin.

Prerequisites and dependencies

This reference guide assumes that you are somewhat familiar with the LaunchDarkly JavaScript SDK.

The observability plugin is compatible with the JavaScript SDK, version 3.7.0 and later.

Do you need information about Angular, Remix, Svelte, or other frameworks?

LaunchDarkly does not offer SDKs for all languages or frameworks. If you’re using another framework, such as Angular, Remix, or Svelte, you may be able to use the JavaScript SDK instead. Install the observability plugins and initialize them when you initialize the client for the JavaScript SDK.

To request support for a specific language or framework, start a Support ticket.

Get started

Follow these steps to get started:

Install the plugins

LaunchDarkly uses plugins to the JavaScript SDK to provide observability. Most customers use both the observability and session replay plugins. However, there is no dependency between them, and you can use only one or the other if you like.

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

Here’s how:

$npm install launchdarkly-js-client-sdk
>npm install @launchdarkly/observability
>npm install @launchdarkly/session-replay

Then, import the plugin into your code:

Import, JS SDK v3.7+
1import { initialize } from "launchdarkly-js-client-sdk";
2import { Observability, LDObserve } from "@launchdarkly/observability";
3import { SessionReplay, LDRecord } from "@launchdarkly/session-replay";

Initialize the client

Next, initialize the SDK and the plugins.

To initialize, you need your LaunchDarkly environment’s client-side ID. This authorizes your application to connect to a particular environment within LaunchDarkly. To learn more, read Initialize the client in the JavaScript SDK reference guide.

You can initialize the observability plugins either at the same time you initialize the SDK, or afterwards.

Initialize SDK client and plugins together

Here’s how to initialize the SDK and plugins:

Initialize, JS SDK v3.7+
1const context = {
2 kind: 'user',
3 key: 'context-key-123abc'
4};
5
6const client = initialize('client-side-id-123abc', context, {
7 plugins: [ new Observability(), new SessionReplay() ]
8});

Initialize the plugins after the SDK client

You can initialize the observability and session replay plugins manually, after the SDK client is initialized.

This approach supports feature-flagged rollouts or dynamic initialization after end user consent. Both plugins use a manualStart option combined with .start() calls.

First, configure the plugins with manualStart: true:

Manual start configuration, JS SDK v3.7+
1const context = {
2 kind: 'user',
3 key: 'context-key-123abc'
4};
5
6const client = initialize('client-side-id-123abc', context, {
7 plugins: [
8 new Observability({ manualStart: true }),
9 new SessionReplay({ manualStart: true })
10 ]
11});

Then, start the plugins when appropriate, such as after receiving end user consent or when a feature flag enables observability.

Here’s an example starting the observability plugin:

1// Start observability after user consent or feature flag check
2if (userConsentReceived || featureFlagEnabled) {
3 LDObserve.start();
4}

Here’s an example with the session replay plugin:

1LDRecord.start({
2 forceNew: true, //start a new recording session
3 silent: false // if true, console.warn messages created in this method are skipped
4});

This approach allows you to:

  • Feature-flag the rollout of observability to a subset of end users
  • Wait for end user consent before starting data collection
  • Dynamically enable observability based on runtime conditions
  • Maintain compliance with privacy regulations

Configure the plugin options

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

Here is an example:

Plugin options, JS SDK v3.7+
1const context = {
2 kind: 'user',
3 key: 'context-key-123abc'
4};
5
6const client = initialize('client-side-id-123abc', context, {
7 plugins: [
8 new Observability({
9 tracingOrigins: true, // attribute frontend requests to backend domains
10 networkRecording: {
11 enabled: true,
12 recordHeadersAndBody: true
13 }
14 }),
15 new SessionReplay({
16 privacySetting: 'none',
17 // or 'default' to redact text matching common regex for PII
18 // or 'strict' to redact all text and images
19 })
20 ]
21});

For more information on plugin options, read Configuration for client-side observability and Configuration for session replay.

Set Context-Security-Policy (CSP)

If your application runs in an environment that enforces content security policies, you must set the Content-Security-Policy (CSP) in your application to tell the browser how your page can interact with third-party scripts.

Here are the policies you need to set to use the observability plugin:

  • connect-src: https://pub.observability.app.launchdarkly.com https://otel.observability.app.launchdarkly.com: This policy allows connecting with LaunchDarkly servers to send recorded observability data.
  • worker-src: data: blob:: This policy allows creating an inlined web worker initialized by the npm package for this plugin.

Your CSP definition may look something like this:

Example CSP definition
1<meta
2 http-equiv="Content-Security-Policy"
3 content="connect-src: https://pub.observability.app.launchdarkly.com https://otel.observability.app.launchdarkly.com; worker-src data: blob:;"
4/>

Alternatively, you can set the CSP in the HTML document response header Content-Security-Policy. Check your initial app HTML document load for the header to make sure you are setting it to the desired value.

Explore supported features

The observability plugins 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 plugins, 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:

  • Average, P95, and P99 Cumulative Layout Shift (CLS) per context (LaunchDarkly)
  • Average, P95, and P99 Document Load Latency per context (LaunchDarkly)
  • Percentage of users with errors (LaunchDarkly)
  • Average, P95, and P99 First Contentful Paint (FCP) per context (LaunchDarkly)
  • Average, P95, and P99 First Input Delay (FID) per context (LaunchDarkly)
  • Average, P95, and P99 Interaction to Next Paint (INP) per context (LaunchDarkly)
  • Average, P95, and P99 Largest Contentful Paint (LCP) (LaunchDarkly)
  • Average, P95, and P99 Time to First Byte (TTFB) per context (LaunchDarkly)

To learn more, read Metrics autogenerated from observability events.