OpenTelemetry in server-side SDKs
OpenTelemetry in server-side SDKs
OpenTelemetry in server-side SDKs
This topic explains how to enable OpenTelemetry in server-side SDKs.
For information on using OpenTelemetry in LaunchDarkly’s client-side SDKs, read OpenTelemetry in client-side SDKs.
OpenTelemetry (OTel) is an open source observability framework and toolkit designed to create and manage telemetry data such as traces, metrics, and logs. You can use LaunchDarkly’s OpenTelemetry protocol (OTLP) endpoint to send OpenTelemetry traces to LaunchDarkly.
We recommend enabling OpenTelemetry in LaunchDarkly server-side SDKs if you use LaunchDarkly’s Observability, Experimentation, or Guarded rollouts features, or if you use third-party observability tools that support the OpenTelemetry framework.
Because OpenTelemetry is vendor- and tool-agnostic, you can reuse OpenTelemetry instrumentation that already exists in your code to create LaunchDarkly metrics without changing application code.
If you want to implement OpenTelemetry features in new code, we recommend using the LaunchDarkly observability plugins to automate the process of adding flag evaluation metadata and forwarding telemetry data directly to LaunchDarkly endpoints. LaunchDarkly provides observability plugins for many client and server SDKs. To learn more, read Observability SDKs.
In the LaunchDarkly UI, OpenTelemetry metrics, logs, and traces are available in the Observability feature.
LaunchDarkly automatically generates metrics from certain OpenTelemetry traces, and these metrics are available in the Experimentation and Guarded rollouts features. Specifically, when LaunchDarkly receives OpenTelemetry trace data, it processes and converts this data into events that LaunchDarkly metrics track over time. When trace data contains spans that carry both metrics and feature flag evaluation events, LaunchDarkly correlates these and uses the result to power guarded rollouts. To learn more, read OpenTelemetry autogenerated metrics.
To configure and send OpenTelemetry data to LaunchDarkly when you are using LaunchDarkly server-side SDKs:
https://otel.observability.app.launchdarkly.com:4318 or https://otel.observability.app.launchdarkly.com:443https://otel.observability.app.launchdarkly.com:4317.If you use LaunchDarkly SDKs, resource attributes are set automatically. You do not need set the resource attributes yourself, and you can skip this section.
If you use only OpenTelemetry SDKs, you must include an active LaunchDarkly SDK key as a resource attribute in your telemetry data, so that the data is correctly associated with your LaunchDarkly project and environment. You have several options for how to set the resource attribute.
To set the resource attribute using an environment variable, use the OTEL_RESOURCE_ATTRIBUTES environment variable:
To set the resource attribute in your OpenTelemetry collector configuration, use the resource processor:
For a complete example of a collector configuration, read Configuring the collector, below.
To set the resource attribute programmatically in your application code, use the OpenTelemetry resource available for your language. Here are a few examples:
To configure a pipeline to send trace data to LaunchDarkly, modify the collector’s config file, otel-collector-config.yaml, as follows:
OpenTelemetry semantic conventions define standard attribute names for common concepts like HTTP requests, database operations, and messaging systems. These conventions evolve over time, and attribute names are occasionally renamed or restructured. For example, http.method was renamed to http.request.method and http.status_code was renamed to http.response.status_code. For more details on how attribute names change across OpenTelemetry versions, see the semantic conventions changelog.
LaunchDarkly automatically normalizes deprecated OpenTelemetry semantic convention attributes to their current equivalents when it ingests your telemetry data. This means you can send data using older attribute names, and LaunchDarkly will create the corresponding current attribute names for you. This normalization applies to all OpenTelemetry data LaunchDarkly ingests, whether from LaunchDarkly SDKs with tracing hooks enabled or from external OTLP endpoints.
This automatic normalization provides several benefits. LaunchDarkly ensures consistent attribute names across your telemetry data, even when services use different OpenTelemetry versions. You can search and filter using the current attribute names regardless of which version your services emit. You can also upgrade your instrumentation gradually without losing the ability to correlate data across services.
When viewing trace attributes in LaunchDarkly, the UI displays indicators showing which attributes have been remapped from deprecated conventions.
LaunchDarkly processes metrics, logs, and traces from your OpenTelemetry data:
A feature flag span event is defined as any span event that contains a feature_flag.context.key attribute. LaunchDarkly ignores traces that do not include span events with this attribute.
In most server-side SDKs, the OTel traces are specific to the LaunchDarkly project and environment that you specify in the collector configuration, described above. In the .NET (server-side) and Node.js (server-side) SDKs, you can provide the LaunchDarkly client-side ID in the tracing hook. This enables you to send traces for multiple projects and environments using one collector.
The following sections describe, for each supported SDK, how to ensure your spans have compatible feature flag events.
The OpenTelemetry tracing hook automatically attaches feature flag event data to your OTel traces for you. Feature flag event data, including the feature_flag.context.key attribute, is only generated when your application uses the LaunchDarkly SDK to evaluate a feature flag.
This feature is available in the following SDKs:
We recommend you use the .NET (server-side) SDK observability plugin for new application development or OpenTelemetry instrumentation. The observability plugin provides additional configuration options and automatically adds LaunchDarkly environment and flag attributes to OpenTelemetry spans.
The instructions that follow are provided for reference, for OpenTelemetry applications that were developed before the observability plugin was available.
To add flag evaluation information to OpenTelemetry spans, first add the Telemetry package from NuGet:
Next, import the LaunchDarkly.Sdk.Server.Hooks and LaunchDarkly.Sdk.Server.Telemetry namespaces. Then you can create a new tracing hook and reference it in the configuration options when you initialize the SDK client.
Here’s how:
When this configuration is set, the SDK adds span events for each call to a *Variation* method to your active span. The span event information contains details of each flag evaluation, including the flag key and the context key. You can collect this information in Guarded rollouts or other observability tools.
Optionally, you can configure the tracing hook to:
Here’s how:
Starting with v1.1 of the LaunchDarkly.Sdk.Server.Hooks package, which requires v8.7+ of the .NET (server-side) SDK, LaunchDarkly automatically decorates any OpenTelemetry spans with project and environment information.
When you create the hook, you can optionally pass in a client-side ID to specify the environment. Use the builder’s .EnvironmentId() method and pass in the client-side ID. To learn more about client-side IDs, read Keys.
Because LaunchDarkly automatically decorates the OpenTelemetry spans, you should not need to call this method explicitly. The exception is if you are using persistent stores, in which case you must either call .EnvironmentId() or use the single-environment OpenTelemetry Collector configuration to ensure that your OpenTelemetry spans have correct project and environment information.
Here’s how:
In the .NET (server-side) SDK, the tracing hook is implemented using Microsoft’s System.Diagnostics APIs. Therefore, some of the terminology it uses differs from the OpenTelemetry standard. For example, the tracing hook in the .NET (server-side) SDK uses the method CreateActivities(), rather than CreateSpans(), to enable child spans.
To learn more, read TracingHookBuilder and ConfigurationBuilder.
We recommend you use the Go SDK observability plugin for new application development or OpenTelemetry instrumentation. The observability plugin provides additional configuration options and automatically adds LaunchDarkly environment and flag attributes to OpenTelemetry spans.
The instructions that follow are provided for reference, for OpenTelemetry applications that were developed before the observability plugin was available.
To add flag evaluation information to OpenTelemetry spans, import the ldhooks package. Then, create a new tracing hook and reference it in the configuration options when you initialize the SDK client.
Here’s how:
When this configuration is set, the SDK adds span events for each call to a *Variation*Ctx method to your active span.
The span event information contains details of each flag evaluation, including the flag key and the context key. You can collect this information in Guarded rollouts or other observability tools.
The *Variation*Ctx methods are the same as the *Variation* methods, except that they also require a Go context. For example, BoolVariationCtx and BoolVariationDetailCtx are the same as the BoolVariation and BoolVariationDetail methods, except that they also require a Go context. This Go context is used in the hook implementation. If you are using hooks, you must update your variation calls to use the Ctx methods. If you are working with the LDScopedClient, it also includes *Variation*Ctx methods. These require a Go context and, like other LDScopedClient methods, do not require a LaunchDarkly context.
LDScopedClient is in beta. It is still undergoing testing and active development. Its functionality may change without notice, including becoming backwards incompatible.
Optionally, you can configure the tracing hook to:
Here’s how:
To learn more, read Config.
To add flag evaluation information to OpenTelemetry spans, import the LaunchDarkly Java Otel Hook package. Then, create a new tracing hook and reference it in the configuration options when you initialize the SDK client.
Here’s how:
When this configuration is set, the SDK adds span events for each call to a *Variation* method to your active span. The span event information contains details of each flag evaluation, including the flag key and the context key. You can collect this information in Guarded rollouts or other observability tools.
Optionally, you can configure the tracing hook to:
Here’s how:
To learn more, read LDConfig.Builder.
We recommend you use the Node.js (server-side) SDK observability plugin for new application development or OpenTelemetry instrumentation. The observability plugin provides additional configuration options and automatically adds LaunchDarkly environment and flag attributes to OpenTelemetry spans.
The instructions that follow are provided for reference, for OpenTelemetry applications that were developed before the observability plugin was available.
To add flag evaluation information to OpenTelemetry spans, import the TracingHook package. Then, create a new tracing hook and reference it in the configuration options when you initialize the SDK client.
Here’s how:
When this configuration is set, the SDK adds span events for each call to a *Variation* method to your active span. The span event information contains details of each flag evaluation, including the flag key and the context key. You can collect this information in Guarded rollouts or other observability tools.
Optionally, you can configure the tracing hook to:
Here’s how:
Starting with v1.2 of the @LaunchDarkly/node-server-sdk-otel package, which requires v9.9+ of the Node.js (server-side) SDK, LaunchDarkly automatically decorates any OpenTelemetry spans with project and environment information.
When you create the hook, you can optionally pass in a client-side ID to specify the environment. Use the environmentId option when you create the new TracingHook. To learn more about client-side IDs, read Keys.
Because LaunchDarkly automatically decorates the OpenTelemetry spans, you should not need to set this option explicitly. The exception is if you are using persistent stores, in which case you must either set the environmentId or use the single-environment OpenTelemetry Collector configuration to ensure that your OpenTelemetry spans have correct project and environment information.
Here’s how:
To learn more, read LDOptions.
To add flag evaluation information to OpenTelemetry spans, first install the package:
The package depends on the OpenTelemetry API. You also need a runtime OpenTelemetry SDK implementation, such as open-telemetry/sdk, and at least one exporter (for example, open-telemetry/exporter-otlp for OTLP/HTTP).
Next, import the TracingHook class. Then create a new tracing hook and reference it in the configuration options when you initialize the SDK client.
Here’s how:
When this configuration is set, the SDK adds a feature_flag span event to your active OpenTelemetry span for each call to variation or variationDetail. The span event information contains details of each flag evaluation, including the flag key and the context key. You can collect this information in Guarded rollouts or other observability tools.
If no OpenTelemetry span is active when the variation call runs, the hook is a no-op.
Optionally, you can configure the tracing hook to:
Here’s how:
When the LaunchDarkly PHP SDK fetches flags directly from LaunchDarkly, it captures the environment ID from the polling response and the tracing hook automatically adds it to the feature_flag.set.id attribute on each span event. You should not need to set this explicitly. The exception is if you are using daemon mode or another persistent store configuration, where flags are read from a shared store rather than fetched from LaunchDarkly directly. In those cases, the polling-response metadata is not available, so you must either set the environmentId option on TracingHookOptions or use the single-environment OpenTelemetry Collector configuration to ensure that your OpenTelemetry spans have correct project and environment information.
When you create the hook, you can optionally pass in a client-side ID to specify the environment. Use the environmentId option on TracingHookOptions and pass in the client-side ID. The configured value takes precedence over the value the SDK derives from the polling response. To learn more about client-side IDs, read Keys.
Here’s how:
Under PHP-FPM and similar request-response SAPIs, register a shutdown function to flush your tracer provider before the request ends, or use a SimpleSpanProcessor to export spans synchronously. To learn more, read the launchdarkly/server-sdk-otel README.
To learn more, read the SDK API documentation.
To add flag evaluation information to OpenTelemetry spans, first install the package:
Next, import Hook and HookOptions from the package. Then you can create a new tracing hook and reference it in the configuration options when you initialize the SDK client.
Here’s how:
When this configuration is set, the SDK adds span events for each call to a *variation* method to your active span. The span event information contains details of each flag evaluation, including the flag key and the context key. You can collect this information in Guarded rollouts or other observability tools.
Optionally, you can configure the tracing hook to:
Here’s how:
To learn more, read Config.
To add flag evaluation information to OpenTelemetry spans, first install the gem:
Then, you can create a new tracing hook and reference it in the configuration options when you initialize the SDK client.
Here’s how:
When this configuration is set, the SDK adds span events for each call to a *Variation* method to your active span. The span event information contains details of each flag evaluation, including the flag key and the context key. You can collect this information in Guarded rollouts or other observability tools.
Optionally, you can configure the tracing hook to:
Here’s how:
To learn more, read Config.