opentelemetry
Loading

Set up the APM iOS Agent

Stack Serverless Observability EDOT iOS

Learn how to set up and configure the Elastic Distribution of OpenTelemetry iOS (EDOT iOS) to instrument your application.

This project requires Swift 5.10, and is intended for use in Swift-base mobile apps.

Other platform requires:

platform version
iOS 16
macOS 13
tvOS 16
watchOS 10

Add the Elastic Distribution of OpenTelemetry iOS to your Xcode project or your Package.swift.

Here are instructions for adding a package dependency to a standard Xcode project.

Details of adding dependencies to your Package.swift can be found on Add a Dependency on Another Swift Package. The following is a helpful code snippet:

package.swift:

Package(
    dependencies:[
         .package(name: "apm-agent-ios", url: "https://github.com/elastic/apm-agent-ios.git", from: "1.2.0"),
    ],
  targets:[
    .target(
        name: "MyApp",
        dependencies: [
            .product(name: "ElasticApm", package: "apm-agent-ios")
        ]
    ),
])

After you've added the SDK as a dependency, initialize it.

If you’re using SwiftUI to build your app add the following to your App.swift:

import SwiftUI
import ElasticApm

class AppDelegate : NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        var config = AgentConfigBuilder()
            .withServerUrl(URL(string:"http://127.0.0.1:8200"))
            .withSecretToken("<SecretToken>")
            .build()

        ElasticApmAgent.start(with: config)
        return true
    }
}

@main
struct MyApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    init() {
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
  1. APM Server URL
  2. Set the secret token for APM server connection

If you’re not using SwiftUI you can alternatively add the same thing to your AppDelegate file:

AppDelegate.swift

import UIKit
import ElasticApm
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var config = AgentConfigBuilder()
                       .withServerUrl(URL(string:"http://127.0.0.1:8200"))
                       .withSecretToken("<SecretToken>")
                       .build()
        ElasticApmAgent.start(with: config)
        return true
    }
}
  1. APM Server URL
  2. Set the secret token for APM server connection