No results for ""
EXPAND ALL
  • Home
  • API docs

GIVE DOCS FEEDBACK

OpenTelemetry

Read time: 6 minutes
Last edited: Apr 26, 2024

Overview

This topic explains how to enable OpenTelemetry in server-side SDKs. OpenTelemetry (OTel) is an open source observability framework.

LaunchDarkly's SDKs support instrumentation for OpenTelemetry traces. Traces provide an overview of how your application handles requests. For example, traces may show that a particular feature flag was evaluated for a particular context as part of a given HTTP request.

To add flag evaluation information to OpenTelemetry spans, create a new tracing hook and reference it in the configuration options when you initialize the SDK client. When this configuration is set, the SDK adds span events to your active span.

We recommend enabling OpenTelemetry in the SDK if you use LaunchDarkly's Experimentation or Release guardian features to monitor metrics on flag releases, or if you use third-party observability tools that support the OpenTelemetry framework.

To learn how to send these traces back to LaunchDarkly, read Sending OpenTelemetry traces to LaunchDarkly.

Details about each SDK's configuration are available in the SDK-specific sections below.

  • Server-side SDKs

Server-side SDKs

This feature is available in the following SDKs:

.NET (server-side)

Expand .NET (server-side) code sample

To add flag evaluation information to OpenTelemetry spans, first add the Telemetry package from NuGet:

dotnet add package LaunchDarkly.ServerSdk.Telemetry

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:

using LaunchDarkly.Sdk.Server.Hooks;
using LaunchDarkly.Sdk.Server.Telemetry;
var config = Configuration.Builder("sdk-key-123abc")
.Hooks(Components.Hooks()
.Add(TracingHook.Default())
).Build();
var client = new LdClient(config);

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. This information is available in Release guardian, and accessible in your observability tools.

Optionally, you can configure the tracing hook to:

  • create new spans that contain span events with flag evaluation information, rather than adding the events to the active span. The new spans are nested in the current active span, and are not active.
  • include the flag's evaluated flag value in the span event.

Here's how:

using LaunchDarkly.Sdk.Server.Hooks;
using LaunchDarkly.Sdk.Server.OpenTelemetry;
var config = Configuration.Builder("sdk-key-123abc")
.Hooks(Components.Hooks()
.Add(TracingHook.Builder()
.CreateActivities()
.IncludeVariant()
.Build()
)
).Build();
var client = new LdClient(config);

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.

Go

Expand Go code sample

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:

import (
ld "github.com/launchdarkly/go-server-sdk/v7"
"github.com/launchdarkly/go-server-sdk/v7/ldhooks"
"github.com/launchdarkly/go-server-sdk/ldotel"
)
var config ld.Config
client, _ = ld.MakeCustomClient("sdk-key-123abc",
ld.Config{
Hooks: []ldhooks.Hook{ldotel.NewTracingHook()},
}, 5*time.Second)

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. This information is available in Release guardian, and accessible in your 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.

Optionally, you can configure the tracing hook to:

  • create new spans that contain span events with flag evaluation information, rather than adding the events to the active span. The new spans are nested in the current active span, and are not active.
  • include the flag's evaluated flag value in the span event.

Here's how:

import (
ld "github.com/launchdarkly/go-server-sdk/v7"
"github.com/launchdarkly/go-server-sdk/v7/ldhooks"
"github.com/launchdarkly/go-server-sdk/ldotel"
)
var config ld.Config
client, _ = ld.MakeCustomClient("sdk-key-123abc",
ld.Config{
Hooks: []ldhooks.Hook{ldotel.NewTracingHook(ldotel.WithSpans(), ldotel.WithVariant())},
}, 5*time.Second)

To learn more, read Config.

Java

Expand Java code sample

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:

import com.launchdarkly.integrations.TracingHook;
TracingHook tracingHook = new TracingHook.Builder().build();
LDConfig config = new LDConfig.Builder()
.hooks(
Components.hooks().setHooks(Collections.singletonList(tracingHook)))
.build();
LDClient client = new LDClient("sdk-key-123abc", config);

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. This information is available in Release guardian, and accessible in your observability tools.

Optionally, you can configure the tracing hook to:

  • create new spans that contain span events with flag evaluation information, rather than adding the events to the active span. The new spans are nested in the current active span, and are not active.
  • include the flag's evaluated flag value in the span event.

Here's how:

import com.launchdarkly.integrations.TracingHook;
TracingHook tracingHook = new TracingHook.Builder().withSpans().withVariant().build())
LDConfig config = new LDConfig.Builder()
.hooks(
Components.hooks().setHooks(Collections.singletonList(tracingHook)))
.build();
LDClient client = new LDClient("sdk-key-123abc", config);

To learn more, read LDConfig.Builder.

Node.js (server-side)

Expand Node.js (server-side) code sample

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:

import * as ld from '@launchdarkly/node-server-sdk';
import { TracingHook } from '@LaunchDarkly/node-server-sdk-otel';
const options: ld.LDOptions = {
hooks: [new TracingHook()]
}
const client = ld.init('sdk-key-123abc', options);

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. This information is available in Release guardian, and accessible in your observability tools.

Optionally, you can configure the tracing hook to:

  • create new spans that contain span events with flag evaluation information, rather than adding the events to the active span. The new spans are nested in the current active span, and are not active.
  • include the flag's evaluated flag value in the span event.

Here's how:

import * as ld from '@launchdarkly/node-server-sdk';
import { TracingHook } from '@LaunchDarkly/node-server-sdk-otel';
const options: ld.LDOptions = {
hooks: [new TracingHook({spans: true, includeVariant: true})]
}
const client = ld.init('sdk-key-123abc', options);

To learn more, read LDOptions.

Ruby

Expand Ruby code sample

To add flag evaluation information to OpenTelemetry spans, create a new tracing hook and reference it in the configuration options when you initialize the SDK client.

Here's how:

require 'ldclient-otel'
config = LaunchDarkly::Config.new(hooks: [LaunchDarkly::Otel::TracingHook.new])
client = LaunchDarkly::LDClient.new("sdk-key-123abc", config);

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. This information is available in Release guardian, and accessible in your observability tools.

Optionally, you can configure the tracing hook to:

  • create new spans. The new spans are nested in the current active span, and are not active. The span events with flag evaluation information are still added to the parent span.
  • include the flag's evaluated flag value in the span event.

Here's how:

require 'ldclient-otel'
tracing_hook_options = LaunchDarkly::Otel::TracingHookOptions.new(add_spans: true, include_variant: true)
hook = LaunchDarkly::Otel::TracingHook.new(tracing_hook_options)
config = LaunchDarkly::Config.new(hooks: [hook])
client = LaunchDarkly::LDClient.new("sdk-key-123abc", config);

To learn more, read Config.