For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Sign inTry it free
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
  • SDKs
    • SDK concepts
    • SDK features
    • Client-side SDKs
    • Server-side SDKs
    • AI SDKs
    • Edge SDKs
    • OpenFeature providers
    • Observability SDKs
      • SDK features
        • Configuration for client-side observability
        • Configuration for server-side observability
        • Configuration for session replay
        • OpenTelemetry in client-side SDKs
        • Recording metrics
        • Recording traces
        • Sending errors
        • Sending logs
        • SDK data handling behavior
        • Self-hosting an OpenTelemetry Collector
        • OpenTelemetry versions and instrumentation support
      • .NET (server-side) SDK observability reference
      • .NET MAUI SDK observability reference
      • Android SDK observability reference
      • iOS SDK observability reference
      • Go SDK observability reference
      • JavaScript SDK observability reference
      • Node.js (server-side) SDK observability reference
      • Python SDK observability reference
      • Ruby SDK observability reference
      • React Native SDK observability reference
      • React Web SDK observability reference
      • Vue SDK observability reference
    • Relay Proxy
Sign inTry it free
LogoLogo
On this page
  • Overview
  • Client-side SDKs
  • iOS
  • Android
  • JavaScript
  • React Native
  • React Web
  • Vue
  • Server-side SDKs
  • .NET (server-side)
  • Go
  • Node.js (server-side)
  • Python
SDKsObservability SDKsSDK features

Recording traces

Was this page helpful?
Previous

Sending errors

Next
Built with

Overview

This topic explains how to record traces through the SDK observability plugin.

You can view all traces sent to LaunchDarkly under Traces in the LaunchDarkly user interface. To learn more, read Traces.

Details about each SDK’s configuration are available in the SDK-specific sections below:

  • Client-side SDKs
  • Server-side SDKs

Client-side SDKs

This feature is available in the observability plugin for the following client-side SDKs:

  • iOS
  • Android
  • JavaScript
  • React Native
  • React Web
  • Vue

iOS

Expand iOS code sample

To build and start a new span:

Start a new span
1let span = LDObserve.shared.startSpan(name: "exampleSpan", attributes: attributes)
2// This span ends when you call span.end()
3span.end()

The attributes argument should include the details to record with the span. To construct it, use Attributes from the @opentelemetry/api.

Android

Expand Android code sample

To start a new span:

Start a new span
1LDObserve.startSpan("exampleSpan", attributes)

The attributes argument should include the details to record with the span. To construct it, use Attributes from the @opentelemetry/api.

JavaScript

Expand JavaScript code sample

The observability plugin provides two options for starting new spans:

  • startSpan() ends the span automatically after the callback function completes, whether it returns normally or throws an error
  • startManualSpan() ends the span when you call span.end()

To start a new span:

1// This span ends automatically after the callback completes
2LDObserve.startSpan('fetchData', (span) => {
3 // Your code here
4});

To learn more, read startSpan and startManualSpan.

React Native

Expand React Native code sample

The observability plugin provides a few options for starting new spans:

  • startSpan() starts a new span without making it active
  • startActiveSpan() starts a new span, makes it active, and runs a callback function within its context
  • startWithHeaders() starts a new span with header context

Here’s an example:

Start an active span and run a callback
1// This span ends automatically after the callback completes
2LDObserve.startActiveSpan('exampleSpan', (span) => {
3 // Your callback function here
4});

To learn more, read Observe.

React Web

To start new spans with the React Web SDK, follow the example for JavaScript.

Vue

To start new spans with the React Web SDK, follow the example for JavaScript.

Server-side SDKs

This feature is available in the observability plugin for the following server-side SDKs:

  • .NET (server-side)
  • Go
  • Node.js (server-side)
  • Python

.NET (server-side)

Expand .NET (server-side) code sample

To start new spans within the observability plugin for .NET (server-side) SDK, use StartActivity. By default, it records any exceptions as error events on the span, and sets the span’s status appropriately.

Here’s how:

Example: Starting a span
1 using (var activity = Observe.StartActivity("example-span", ActivityKind.Internal,
2 new Dictionary<string, object> { { "example-attribute", "example-value" } }))
3 {
4 activity.SetTag("added-attribute", "added-attribute-value");
5 return "Hello world!";
6 }

This method requires the Name of the span. Optionally, you can pass in attributes and specify whether to record an exception or set the span’s status if there is an exception. To construct the attributes argument, use Attributes from the @opentelemetry/api.

To learn more, read StartActivity.

Go

Expand Go code sample

To start new spans with in the observability plugin for the Go SDK, use StartSpan(). By default, this records any exceptions as error events on the span and sets the span’s status appropriately.

Here’s how:

Example: Working with a span
1_, span := ldobserve.StartSpan(ctx, "example-span", []trace.SpanStartOption{})
2span.SetAttributes(attribute.String("example-attribute", "example-value"))
3span.End()

The StartSpan method requires a Go context.Context, the name of the span, and an array of the OpenTelemetry options for starting a span. You can optionally pass an array of attributes from the OpenTelemetry specification.

To learn more, read StartSpan.

Node.js (server-side)

Expand Node.js (server-side) code sample

The Node.js (server-side) SDK’s observability plugin uses the OpenTelemetry Tracing API to work with spans.

Additionally, it provides the following functions for working with spans:

  • setAttributes() sets attributes on the active span
  • startWithHeaders() starts a span with information from the request headers
  • runWithHeaders() runs a callback with information from the request headers and returns the result

Here’s an example:

Example: Starting and running a span
1app.get("/start-span-example", (req: Request, res: Response) => {
2 const {span} = LDObserve.startWithHeaders('example-span-a', req.headers);
3
4 LDObserve.setAttributes({
5 "example-attribute": "example-value",
6 });
7
8 res.send("Hello World");
9 span.end();
10});
11
12app.get("/run-span-example", async (req: Request, res: Response) => {
13 await LDObserve.runWithHeaders('example-span-b', req.headers, (span) => {
14 LDObserve.setAttributes({
15 "example-attribute": "example-value",
16 });
17
18 res.send("Hello World");
19 });
20});

To learn more, read Observe.

Python

Expand Python code sample

To start new spans with in the observability plugin for Python SDK, use start_span. This method is a context manager for creating a new span. By default, it records any exceptions as error events on the span, sets the span’s status appropriately. Exiting the context manager calls the span’s end method.

Here’s how:

Example: Starting a span
1 with observe.start_span("manual-span", attributes={"custom": "value"}) as span:
2 span.set_attribute("my-attribute", "my-value")
3 # Any user defined code I want to capture.

This method requires the name of the span. Optionally, you can pass in attributes and specify whether to record an exception or set the span’s status if there is an exception. To construct the attributes argument, use Attributes from the @opentelemetry/api.

To learn more, read start_span.