This example demonstrates how to integrate the LaunchDarkly C++ Server SDK with OpenTelemetry tracing to automatically enrich your distributed traces with feature flag evaluation data.
- Setting up OpenTelemetry with OTLP HTTP exporter
- Configuring the LaunchDarkly OpenTelemetry tracing hook
- Creating HTTP spans with Boost.Beast
- Automatic feature flag span events in traces
- Passing explicit parent span context to evaluations
- C++17 or later
- CMake 3.19 or later
- Boost 1.81 or later
- LaunchDarkly SDK key
From the repository root:
mkdir build && cd build
cmake .. -DLD_BUILD_EXAMPLES=ON -DLD_BUILD_OTEL_SUPPORT=ON
cmake --build . --target hello-cpp-server-otelEither edit main.cpp and set the SDK_KEY constant, or use an environment variable:
export LD_SDK_KEY=your-sdk-key-hereIn your LaunchDarkly dashboard, create a boolean flag named show-detailed-weather.
./build/examples/hello-cpp-server-otel/hello-cpp-server-otelYou should see:
*** SDK successfully initialized!
*** Weather server running on http://0.0.0.0:8080
*** Try: curl http://localhost:8080/weather
*** OpenTelemetry tracing enabled, sending traces to LaunchDarkly
*** LaunchDarkly integration enabled with OpenTelemetry tracing hook
curl http://localhost:8080/weatherNavigate to your LaunchDarkly project's Observability section to view traces. Each HTTP request will have:
- Root Span: "HTTP GET /weather" with HTTP attributes
- Feature Flag Event: Attached to the span with evaluation details:
feature_flag.key: "show-detailed-weather"feature_flag.provider.name: "LaunchDarkly"feature_flag.context.id: "user:weather-api-user"feature_flag.result.value: The evaluated flag value
To send traces to a different OpenTelemetry collector, set the LD_OTEL_ENDPOINT environment variable:
export LD_OTEL_ENDPOINT=http://localhost:4318/v1/traces
./build/examples/hello-cpp-server-otel/hello-cpp-server-otel