BS
BleepingSwift
Published on
3 min read

> Swift System Metrics 1.0: Monitoring CPU, Memory, and More on the Server

If you're running Swift on the server, you've probably cobbled together your own way to track CPU usage, memory, and file descriptors. Maybe you're parsing /proc files on Linux and calling completely different APIs on macOS. The new Swift System Metrics package, which just hit 1.0, gives you a single API for all of it.

What It Collects

Swift System Metrics gathers process-level data that matters when you're running a production service: CPU utilization time, virtual and resident memory usage, the number of open file descriptors versus the maximum allowed, and the process start time. These are the numbers you actually want on a Grafana dashboard when something starts behaving strangely at 2 AM.

The package works on both Linux and macOS with the same API, and it also supports musl libc for Alpine-based Docker images.

Adding It to Your Project

Drop it into your Package.swift like any other dependency:

dependencies: [
    .package(url: "https://github.com/apple/swift-system-metrics", from: "1.0.0")
]

Then add "SystemMetrics" to your target's dependencies:

.target(
    name: "MyServer",
    dependencies: [
        .product(name: "SystemMetrics", package: "swift-system-metrics")
    ]
)

Running It with Service Lifecycle

The package is designed to slot into the Swift Service Lifecycle pattern. You create a SystemMetricsMonitor and add it to your ServiceGroup alongside your other services:

import SystemMetrics
import ServiceLifecycle

let systemMetricsMonitor = SystemMetricsMonitor(logger: logger)

let serviceGroup = ServiceGroup(
    services: [otelService, myAppService, systemMetricsMonitor],
    gracefulShutdownSignals: [.sigint],
    cancellationSignals: [.sigterm],
    logger: logger
)

try await serviceGroup.run()

The monitor collects and reports metrics on a regular interval, and it cleans up properly when the service group shuts down. No manual teardown required.

How the Pieces Fit Together

Swift System Metrics doesn't exist in a vacuum. It reports its data through Swift Metrics, which is a backend-agnostic metrics API. That means you can wire it up to Prometheus, OpenTelemetry, or any other backend that has a Swift Metrics implementation.

The flow looks like this: Swift System Metrics collects the raw numbers from the OS, pushes them through the Swift Metrics API, and your chosen backend exports them to wherever you visualize your data. The package even includes an example Grafana dashboard configuration in the repo so you can get something useful on screen quickly.

Things Worth Knowing

The 1.0 label means the public API is stable, so you can depend on it without worrying about breaking changes in minor releases. If you're already using Swift Metrics and Service Lifecycle in your server app, this is a straightforward addition that fills a gap most teams end up solving on their own.

The GitHub repository has documentation and contribution guidelines if you want to dig deeper or add support for additional metrics.

subscribe.sh

// Stay Updated

Get notified when I publish new tutorials on Swift, SwiftUI, and iOS development. No spam, unsubscribe anytime.

>

By subscribing, you agree to our Privacy Policy.