swift-system-metrics

1.2.2

Report process-level system metrics (memory, CPU, file descriptors) to Swift Metrics
apple/swift-system-metrics

What's New

1.2.2

2026-05-19T10:54:29Z

What's Changed

SemVer Patch

  • Fix Linux open file descriptor count being inflated by 2 due to counting . and .. directory entries by @Ronitsabhaya75 in #112
  • Fix integer truncation in process start time to preserve sub-second precision by @Ronitsabhaya75 in #115
  • Fix process_max_fds to use soft limit (rlim_cur) on Linux by @Ronitsabhaya75 in #116
  • Add process page fault metrics (minor/major) using getrusage on Linux and Darwin by @Ronitsabhaya75 in #118
  • Refactor readFull() to use iterative while loop instead of recursion by @Ronitsabhaya75 in #119

Other Changes

New Contributors

Full Changelog: 1.2.1...1.2.2

Swift System Metrics

A Swift library for reporting process-level system metrics (memory, CPU, file descriptors) to Swift Metrics.

Overview

Swift System Metrics provides a type that periodically collects process-level system metrics and reports them to the Swift Metrics factory.

This package is designed to be run in tools and applications directly and is not expected to be used from libraries. If you'd like to report additional metrics from your library, use Swift Metrics directly.

Quick start

Add the dependency to your Package.swift:

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

Add the library dependency to your target:

.product(name: "SystemMetrics", package: "swift-system-metrics")

Import and use in your code:

import SystemMetrics
import ServiceLifecycle
import Metrics
import Logging

@main
struct Application {
  static func main() async throws {
    // Create a logger, or use one of the existing loggers
    let logger = Logger(label: "Application")
    let metrics = MyMetricsBackendImplementation()
    MetricsSystem.bootstrap(metrics)

    let service = FooService()
    // Create the monitor
    let systemMetricsMonitor = SystemMetricsMonitor(logger: logger)
    
    // Create the service
    let serviceGroup = ServiceGroup(
      services: [service, systemMetricsMonitor],
      gracefulShutdownSignals: [.sigint],
      cancellationSignals: [.sigterm],
      logger: logger
    )

    try await serviceGroup.run()
  }
}

Collected metrics

The monitor collects and reports the following metrics as gauges:

  • Virtual Memory: Total virtual memory, in bytes, that the process allocates.
    • Metric name: process_virtual_memory_bytes
  • Resident Memory: Physical memory, in bytes, that the process currently uses.
    • Metric name: process_resident_memory_bytes
  • Start Time: Process start time, in seconds, since UNIX epoch.
    • Metric name: process_start_time_seconds
  • CPU Time: Cumulative CPU time the process consumes, in seconds.
    • Metric name: process_cpu_seconds_total
  • Max File Descriptors: The maximum number of file descriptors the process can open.
    • Metric name: process_max_fds
  • Open File Descriptors: The number of file descriptors the process currently has open.
    • Metric name: process_open_fds
  • Thread Count: The number of threads in the process.
    • Metric name: process_thread_count
  • Minor Page Faults: The number of minor page faults (no disk I/O required).
    • Metric name: process_page_faults_minor_total
  • Major Page Faults: The number of major page faults (required disk I/O).
    • Metric name: process_page_faults_major_total

Renaming metric labels

If your metrics backend expects different label names, use MappingMetricsFactory from Swift Metrics to rename labels without modifying the library itself. See the Getting Started guide for a complete example.

Supported platforms and minimum versions

The library is supported on macOS and Linux.

Component macOS Linux
SystemMetrics ✅ 13+ ✅

Documentation

Comprehensive documentation is hosted on the Swift Package Index.

Description

  • Swift Tools 6.1.0
View More Packages from this Author

Dependencies

Last updated: Tue Jul 21 2026 21:21:33 GMT-0900 (Hawaii-Aleutian Daylight Time)