Skip to main content
The Honeycomb OpenTelemetry Swift SDK is Honeycomb’s distribution of OpenTelemetry Swift. It simplifies adding instrumentation to your iOS applications and sending telemetry to Honeycomb. This page briefly covers usage of the SDK. If you just want to see some code, check out the examples on GitHub.

Before You Begin

Before you can add instrumentation to your iOS application, you will need to do a few things.

Get Your Honeycomb API Key

To send data to Honeycomb, you need to:
  1. Sign up for a Honeycomb account. To sign up, decide whether you would like Honeycomb to store your data in a US-based or EU-based location, then create a Honeycomb account in the US or create a Honeycomb account in the EU.
  2. Create a Honeycomb Ingest API Key. To get started, you can create a key that you expect to swap out when you deploy to production. Name it something helpful, perhaps noting that it’s a Getting Started key. Make note of your API key; for security reasons, you will not be able to see the key again, and you will need it later!
For setup, make sure you select the “Can create datasets” checkbox so that your data will show up in Honeycomb. Later, when you replace this key with a permanent one, you can uncheck that box.

Install the Honeycomb Swift SDK

Add Honeycomb OpenTelemetry Swift to your application’s dependencies. The Honeycomb Swift SDK is compatible with applications targeting iOS 13+.
If you manage dependencies in Xcode:
  1. In Xcode, select File > Add Package Dependencies…
  2. Enter https://github.com/honeycombio/honeycomb-opentelemetry-swift as the repository URL.
  3. Get the version number for the latest release.
  4. Add the Honeycomb package to your application’s target dependencies.

Configuration

Enable Auto-Instrumentation

Automatic instrumentation packages are enabled or disabled in your configuration.

Add Resource Attributes

Resource attributes are available on every span your instrumentation emits. Adding custom, application-specific attributes makes it easier to correlate your data to important business information. You can add extra resource attributes during SDK configuration with the .setResourceAttributes() method.

Enable Sampling

The Honeycomb Swift SDK includes optional deterministic head sampling. To enable sampling, call .setSampleRate() with your desired sample rate as an Int value. The sample rate is 1 by default, meaning every trace is exported. The example below sets a sampleRate of 40, meaning 1 in 40 traces will be exported.

Custom Instrumentation

Automatic instrumentation is a fast way to instrument your code, but you get more insight into your application by adding custom, or manual, instrumentation. To add your own custom instrumentation, include the OpenTelemetryApi as a dependency in your application.

Add Attributes to an Active Span

You can retrieve the currently active span in a trace and add attributes to it. This lets you add more context to traces and gives you more ways to group or filter traces in your queries:
In the above example, we add an app.cart.discount_code attribute to the current span. This lets us use the app.cart.discount_code field in WHERE or GROUP BY clauses in the Honeycomb query builder.

Acquire a Tracer

For manual tracing, you need to acquire a tracer:

Create Spans

Create custom spans to get a clear view of the critical parts in your application.

Custom Span Processing

Span processors provide hooks for when a span starts and when it ends. This lets you mutate spans after they have been created by automatic or manual instrumentation. Here’s a basic example of a span processor that adds an attribute to spans when they start:
Add the span processor to your SDK configuration to enable it:

Manual Instrumentation Utilities

The Honeycomb Swift SDK provides utilities for manually instrumenting SwiftUI views, SwiftUI navigation, and logging errors or exceptions.

SwiftUI View

Trace render timings of your views by wrapping them with HoneycombInstrumentedView(name: String).

SwiftUI Navigation

The SDK provides a view modifier for manually tracing a NavigationStack when you are managing navigation state externally. The instrumentNavigation(path: String) view modifier creates spans on path changes (NavigationTo, NavigationFrom) with attributes for the full navigation path and what triggered the navigation.
For other navigation components, such as TabView or NavigationSplitView, you can use the Honeycomb.setCurrentScreen(path: Any) function to trace navigation.

Log Errors and Exceptions

The Honeycomb.log() method records any Error, NSError, or NSException as a log record. You can use Honeycomb.log() for logging exceptions you catch in your own code that are not logged by the SDK.

Trace Header Propagation

If you are connecting your app to a backend service that you wish to view as a unified trace with your app, you will need to manually add headers to all your outgoing requests. You must also create a span and set it as the active span. The span’s context will be used to generate the headers needed for trace propagation.

Troubleshooting

To explore common issues when sending data, visit Common Issues with Sending Data in Honeycomb.