Prometheus Metrics in Swift: Integration for iOS/macOS Applications

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Prometheus Metrics in Swift (iOS/macOS): Monitoring Mobile and Desktop Applications



Prometheus is an open-source monitoring and alerting system that collects metrics as time series data. In the Apple ecosystem (iOS and macOS), using Prometheus allows you to track application performance, user behavior, network status, and other important indicators. In this article, we will look at how to integrate Prometheus metrics into Swift projects, what libraries exist for this purpose, and how to configure data export.



Why are Prometheus metrics needed in mobile applications?



Mobile and desktop applications often operate under conditions of limited resources and unstable connections. Collecting metrics helps:

  • Identify performance bottlenecks (UI delays, memory leaks)
  • Track the frequency and types of errors (crashes, network errors)
  • Analyze user behavior (session time, screens)
  • Configure alerts for critical values (e.g., API response time > 5 sec)


Main types of Prometheus metrics



Prometheus supports four main metric types that can be implemented in Swift:



  • Counter — a monotonically increasing value (number of requests, errors)
  • Gauge — an arbitrary value that can increase and decrease (current memory, active users)
  • Histogram — distribution of values across specified intervals (function execution time)
  • Summary — distribution with quantiles (latencies)


Libraries for Swift



Currently, the most popular library for working with Prometheus in Swift is SwiftPrometheus. It provides a simple API for creating and registering metrics, and also supports export in a format compatible with Prometheus.



Installation via Swift Package Manager:

// Package.swiftdependencies: [    .package(url: "https://github.com/reddavis/SwiftPrometheus.git", from: "1.0.0")]


Code examples in Swift



1. Creating a counter for API requests



import SwiftPrometheus

let prometheus = Prometheus()let apiRequests = prometheus.createCounter( named: "api_requests_total", help: "Total number of API requests", labels: ["endpoint"])

// Increment on each requestapiRequests.inc(["endpoint": "/users"])apiRequests.inc(["endpoint": "/posts"])


2. Gauge for tracking memory



let memoryGauge = prometheus.createGauge(    named: "app_memory_bytes",    help: "Current memory usage in bytes")

// Update when memory changesmemoryGauge.set(Double(reportMemoryUsage()))


3. Histogram of data loading time



let loadDuration = prometheus.createHistogram(    named: "data_load_duration_seconds",    help: "Histogram of data loading time",    buckets: [0.1, 0.5, 1.0, 2.0, 5.0])

let start = CFAbsoluteTimeGetCurrent()// ... data loadinglet elapsed = CFAbsoluteTimeGetCurrent() - startloadDuration.observe(elapsed)


Exporting metrics to Prometheus



For Prometheus to collect metrics, your application must provide an endpoint that returns data in plain text exposition format. For iOS/macOS, this can be done via a local HTTP server (e.g., using Network.framework or third-party libraries).



Example of a simple HTTP server for export:



import SwiftPrometheusimport Network

class MetricsServer { let prometheus: Prometheus var listener: NWListener? init(prometheus: Prometheus, port: UInt16 = 9090) { self.prometheus = prometheus startServer(port: p

Recommendations