Introduction: Why Monitoring is Critical for Swift Applications
Modern Swift applications for iOS and macOS operate in a highly competitive environment. Users expect instant response, stable operation, and no crashes. Without a monitoring system, you risk learning about a server crash or critical error only after customer complaints. In this article, we will break down how to organize effective monitoring for Swift applications: which metrics to collect, how to configure alerts, and automate processes.
1. Key Metrics for Swift Applications
Monitoring starts with choosing the right metrics. For iOS and macOS applications, it is important to track three groups of indicators.
1.1. Performance and UI
- FPS (Frames Per Second) — frame rate. A drop below 30 FPS signals problems with animation or rendering.
- Launch Time — cold and hot start. Measured from the start of loading to the first frame.
- Memory Consumption — track leaks using Instruments or built-in Xcode metrics.
import MetricKit
class PerformanceMonitor: NSObject, MXMetricManagerSubscriber { override init() { super.init() MXMetricManager.shared.add(self) } func didReceive(_ payloads: [MXMetricPayload]) { for payload in payloads { if let fpsData = payload.applicationLaunchMetrics { print("Average FPS: \\(fpsData.histogrammedApplicationResumeTime)") } } }}1.2. Network Requests and API
- Server response time (latency).
- Error codes (4xx, 5xx).
- Number of successful/unsuccessful requests.
import Foundation
class NetworkMonitor { static func trackRequest(_ url: URL, duration: TimeInterval, statusCode: Int) { let metrics: [String: Any] = [ "url": url.absoluteString, "duration_ms": duration * 1000, "status": statusCode ] // Send to analytics AnalyticsService.sendEvent("network_request", parameters: metrics) }}1.3. Crashes and Errors
Use Crashlytics (Firebase) or a custom handler. It is important to record not only fatal errors but also recoverable errors.
import Crashlytics
class CrashHandler { static func logError(_ error: Error, context: String) { Crashlytics.crashlytics().record(error: error, userInfo: ["context": context]) }}2. Tools for Collecting Metrics
The choice of tool depends on the project scale. Let's consider three popular options.
2.1. Firebase Crashlytics + Performance
A free tool from Google. Allows you to track crashes, network requests, and custom metrics. Integration via Swift Package Manager.
// Install via SPM: firebase-ios-sdkimport FirebasePerformance
class PerformanceTracer { static func startTrace(name: String) -> Trace { let trace = Performance.startTrace(name: name) return trace } static func stopTrace(_ trace: Trace) { trace.stop() }}2.2. Sentry
An advanced system with support for distributed tracing. Suitable for enterprise solutions.
import Sentry
SentrySDK.start { options in options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0" options.tracesSampleRate = 1.0 // 100% tracing}
// Manual error captureSentrySDK.capture(error: NSError(domain: "com.example", code: 404))2.3. New Relic
A powerful tool for real-time performance monitoring. Supports SwiftUI and UIKit.
// Integration via CocoaPods: pod 'NewRelicAgent'import NewRelic
NewRelic.start(w