This LaunchDarkly observability plugin is currently available in Early Access, and APIs are subject to change until a 1.x version is released.
This topic documents how to get started with the LaunchDarkly observability plugin for the Android SDK.
The Android SDK supports the observability plugin for error monitoring, logging, tracing, and session replay.
LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:
This reference guide assumes that you are somewhat familiar with the LaunchDarkly Android SDK.
The observability plugin is compatible with the Android SDK, version 5.9.0 and later.
The LaunchDarkly Android SDK is compatible with Android SDK versions 21 and higher (Android 5.0, Lollipop).
Follow these steps to get started:
LaunchDarkly uses a plugin to the Android SDK to provide observability.
The first step is to make both the SDK and the observability plugin available as dependencies.
Here’s how:
Then, import the plugin into your code:
Next, initialize the SDK and the plugin.
To initialize, you need your LaunchDarkly environment’s mobile key and the context for which you want to evaluate flags. This authorizes your application to connect to a particular environment within LaunchDarkly. To learn more, read Initialize the client in the Android SDK reference guide.
The Android observability SDK uses a mobile key. Keys are specific to each project and environment. They are available on the SDK keys page under Settings. To learn more about key types, read Keys.
Mobile keys are not secret and you can expose them in your client-side code without risk. However, never embed a server-side SDK key into a client-side application.
Here’s how to initialize the SDK and plugin:
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:
For more information on plugin options, read Configuration for client-side observability.
You can customize the observability plugin with additional options:
The available ObservabilityOptions configuration options are:
INFO. Set to ObservabilityOptions.LogLevel.NONE to disable log exporting.ObservabilityOptions.TracesApi.disabled() to disable all tracing, or set includeErrors/includeSpans individually.ObservabilityOptions.MetricsApi.disabled() to disable metrics.crashReporting: If true, automatically reports uncaught exceptions as errors. Defaults to true.activityLifecycle: If true, automatically starts spans for Android Activity lifecycle events. Defaults to true.launchTime: If true, automatically measures and reports application startup time as metrics. Defaults to false.false.To enable HTTP request instrumentation and user interaction instrumentation, add the following plugin and dependencies to your top level application’s Gradle file.
The Android SDK supports session replay, which captures snapshots of your app’s UI at regular intervals. This allows you to visually review user sessions in LaunchDarkly to better understand user behavior and diagnose issues.
To enable session replay, add the SessionReplay plugin to the plugins list after the Observability plugin. Session replay depends on the Observability plugin being present and initialized first.
Here’s how:
Important notes:
SessionReplay depends on Observability. If Observability is missing or listed after SessionReplay, the plugin logs an error and stays inactive.You can initialize the session replay plugin manually, after the SDK client is initialized.
This approach supports feature-flagged rollouts or dynamic initialization after end user consent. Set enabled to false in ReplayOptions, then call LDReplay.start() when you’re ready to begin recording.
First, configure the plugin with enabled = false:
Then, start the session replay plugin when appropriate, such as after receiving end user consent or when a feature flag enables session replay.
This approach allows you to:
You can customize session replay behavior by passing a ReplayOptions object to the SessionReplay constructor:
The available ReplayOptions configuration options are:
1000 (1 second).true. Defaults to false.Note: Service configuration options like serviceName and serviceVersion are set in ObservabilityOptions, not in ReplayOptions.
The PrivacyProfile class controls how UI elements are masked during session replay. Session replay for Android uses Jetpack Compose semantics to identify and mask UI elements. By default, text inputs are masked to protect user privacy.
Here’s how to configure privacy settings:
The available privacy options are:
true, masks all text input fields including editable text and paste operations. Defaults to true.true, masks all non-input text elements in the UI. Defaults to false.true, masks sensitive views that contain password fields or text matching sensitive keywords. Defaults to false.When maskBySemanticsKeywords is enabled, the SDK automatically masks any Compose UI text or content descriptions containing predetermined keywords. Keywords you specify are not case-sensitive. For the current set of keywords, read PrivacyProfile.
For maximum privacy (recommended for production):
For debugging or development, you can turn masking off:
For selective masking, which masks inputs and sensitive data but shows regular text:
In addition to the privacy profile settings, you can explicitly mask individual UI elements by using the .ldMask() modifier. This is useful when you need to mask specific sensitive fields while allowing other content to remain visible.
For traditional Android XML-based views, you can mask any View by calling the .ldMask() extension function:
For Jetpack Compose, you can add the .ldMask() modifier to any composable to mask it in session replay recordings:
You can also explicitly unmask elements that would otherwise be masked by the privacy profile settings using the .ldUnmask() modifier:
The .ldMask() and .ldUnmask() modifiers give you fine-grained control over which UI elements are masked in session replay recordings, allowing you to balance privacy protection with useful debugging information.
For more information on session replay configuration, read Configuration for session replay.
To implement distributed tracing, all spans generated across different function calls and services for the same user request must share the same context.
While nested spans in the same synchronous scope automatically use the context of their parent span, the context is not automatically propagated when launching new coroutines or switching dispatchers. Use the OpenTelemetry Context API to capture and restore the parent span context explicitly in asynchronous code:
Alternatively, you can store the current context and restore it later in a different scope:
To propagate a span context to an entirely different service, use the OpenTelemetry W3CTraceContextPropagator to inject the context into outgoing HTTP headers. Services that receive the headers can then extract and use the context.
The above example adds header content similar to:
Services that receive the headers use the extract method from W3CTraceContextPropagator to obtain the trace context and use it as the parent context for new spans.
The observability plugin supports the following features. After the SDK and plugins are initialized, you can access these from within your application:
After you initialize the SDK and observability plugin, your application automatically starts sending observability data back to LaunchDarkly. You can review this information in the LaunchDarkly user interface. To learn how, read Observability.
The observability data collected includes:
Specifically, the observability data includes events that LaunchDarkly uses to automatically create the following metrics:
To learn more about autogenerated metrics, read Observability autogenerated metrics.