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:- 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.
- 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!
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+.- Xcode
- Package.swift
If you manage dependencies in Xcode:
- In Xcode, select File > Add Package Dependencies…
- Enter
https://github.com/honeycombio/honeycomb-opentelemetry-swiftas the repository URL. - Get the version number for the latest release.
- Add the
Honeycombpackage 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: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: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 withHoneycombInstrumentedView(name: String).
SwiftUI Navigation
The SDK provides a view modifier for manually tracing a NavigationStack when you are managing navigation state externally. TheinstrumentNavigation(path: String) view modifier creates spans on path changes (NavigationTo, NavigationFrom) with attributes for the full navigation path and what triggered the navigation.
TabView or NavigationSplitView, you can use the Honeycomb.setCurrentScreen(path: Any) function to trace navigation.
Log Errors and Exceptions
TheHoneycomb.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.