Prometheus Metrics in Swift: Monitoring 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)

Prometheus is a powerful open-source monitoring and alerting system that collects metrics from target applications via HTTP. In the Swift ecosystem for iOS and macOS, there are several libraries that allow exporting metrics in Prometheus format. In this article, we will look at the SwiftPrometheus library — a lightweight tool for collecting and exporting metrics in Swift applications.



What is SwiftPrometheus?

SwiftPrometheus is a client library for Swift that implements the Prometheus specification. It allows you to create three main metric types: Counter, Gauge, and Histogram. The library is written entirely in Swift, supports iOS 13+, macOS 10.15+, watchOS 6+, and tvOS 13+.



Installation

Add the dependency to your Package.swift:

// swift-tools-version:5.5import PackageDescription

let package = Package( name: "MyApp", dependencies: [ .package(url: "https://github.com/QuickBirdEng/SwiftPrometheus.git", from: "2.0.0") ], targets: [ .target( name: "MyApp", dependencies: ["SwiftPrometheus"] ) ])


Or via CocoaPods in your Podfile:

pod 'SwiftPrometheus', '~> 2.0'


Main Metric Types

  • Counter — a monotonically increasing counter (e.g., number of requests).
  • Gauge — a value that can decrease and increase (e.g., current temperature).
  • Histogram — a distribution of values across specified intervals (e.g., response time).


Counter Usage Example

import SwiftPrometheus

let prometheus = Prometheus()let httpRequests = prometheus.createCounter(for: "http_requests_total", help: "Total number of HTTP requests", labels: ["method", "path"])

httpRequests.inc() // +1httpRequests.inc(5) // +5httpRequests.labels(["GET", "/api/v1/users"]).inc() // with labels


Gauge Usage Example

let temperature = prometheus.createGauge(for: "device_temperature_celsius",                                         help: "Current device temperature")

temperature.set(36.5) // set valuetemperature.inc(2.0) // increase by 2temperature.dec(1.0) // decrease by 1


Histogram Usage Example

let requestDuration = prometheus.createHistogram(for: "request_duration_seconds",                                                help: "Request duration in seconds",                                                buckets: [0.1, 0.5, 1.0, 2.5, 5.0])

requestDuration.observe(0.3) // record a value


Exporting Metrics in Prometheus Format

For export, create an HTTP endpoint that will serve metrics in text format. Example for Vapor (server-side):

import Vaporimport SwiftPrometheus

func routes(_ app: Application) throws { app.get("metrics") { req in let metrics = prometheus.collect() return Response(body: .init(string: metrics)) }}


For iOS/macOS applications, you can use the built-in URLSession to send metrics to a Prometheus server:

func sendMetrics() {    let metrics = prometheus.collect()    var request = URLRequest(url: URL(string: "https://pushgateway.example.org/metrics")!)    request.httpMethod = "POST"    request.httpBody = metrics.data(using: .utf8)    URLSession.shared.dataTask(with: request).resume()}


Additional Features

  • Labels

Recommendations