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
| Option | Description |
|---|---|
| APIKey | StringYour Honeycomb Ingest API Key. Required if sending telemetry directly to Honeycomb. |
| tracesAPIKey | StringDedicated Ingest API Key to use when sending traces. Overrides APIKey for traces. |
| metricsAPIKey | StringDedicated Ingest API Key to use when sending metrics. Overrides APIKey for metrics. |
| logsAPIKey | StringDedicated Ingest API Key to use when sending logs. Overrides APIKey for logs. |
| dataset | StringName of the dataset to send telemetry data to. Required if using Honeycomb Classic. |
| metricsDataset | StringName of the dataset to send metrics to. Overrides dataset for metrics. |
| APIEndpoint | StringTelemetry is sent to this URL. For Honeycomb EU instances set this as https://api.eu1.honeycomb.io:443. If you’re using an OpenTelemetry Collector, provide your collector URL instead.Default: https://api.honeycomb.io:443 (US instance) |
| tracesEndpoint | StringAPI endpoint to send traces to. |
| metricsEndpoint | StringAPI endpoint to send metrics to. |
| logsEndpoint | StringAPI endpoint to send logs to. |
| sampleRate | IntSample rate to apply. For example, a sampleRate of 40 means 1 in 40 traces will be exported. Default: 1 |
| sessionTimeout | TimeIntervalMaximum length of time, in seconds, for a single user session. Used to generate session.id span attribute.Default: TimeInterval(60 * 60 * 4) (4 hours) |
| serviceName | StringThe name of your application. Used as the value for service.name resource attribute. |
| serviceVersion | StringCurrent version of your application. Used as the value for service.version resource attribute. |
| resourceAttributes | Dictionary<String, String>Attributes to attach to outgoing resources. |
| headers | Dictionary<String, String>Headers to add to exported telemetry data. |
| tracesHeaders | Dictionary<String, String>Headers to add to exported trace data. |
| metricsHeaders | Dictionary<String, String>Headers to add to exported metrics data. |
| logsHeaders | Dictionary<String, String>Headers to add to exported logs data. |
| timeout | TimeIntervalTimeout used by exporter when sending data. |
| tracesTimeout | TimeIntervalTimeout used by traces exporter. Overrides timeout for trace data. |
| metricsTimeout | TimeIntervalTimeout used by metrics exporter. Overrides timeout for metrics data. |
| logsTimeout | TimeIntervalTimeout used by logs exporter. Overrides timeout for logs data. |
| protocol | enum HoneycombOptions.OTLPProtocolProtocol to use when sending data. Default: .httpProtobuf |
| tracesProtocol | enum HoneycombOptions.OTLPProtocolOverrides protocol for trace data. |
| metricsProtocol | enum HoneycombOptions.OTLPProtocolOverrides protocol for metrics data. |
| logsProtocol | enum HoneycombOptions.OTLPProtocolOverrides protocol for logs data. |
| spanProcessor | OpenTelemetryApi.SpanProcessorAdditional span processor to use. |
| metricKitInstrumentationEnabled | BoolEnable MetricKit instrumentation. Default: true |
| urlSessionInstrumentationEnabled | BoolEnable URLSession instrumentation. Default: true |
| uiKitInstrumentationEnabled | BoolEnable UIKit view instrumentation. Default: true |
| touchInstrumentationEnabled | BoolEnable UIKit touch instrumentation. Default: false |
| unhandledExceptionInstrumentationEnabled | BoolEnable unhandled exception instrumentation. Default: true |
| offlineCachingEnabled | BoolEnable offline caching for telemetry. When offline caching is enabled, telemetry is cached during network failures. The SDK will retry exporting telemetry for up to 18 hours. Offline caching also adds a minimum delay of 5 seconds to telemetry exports. Offline caching is an alpha feature and may be unstable. Default: false |
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.