This example demonstrates how to integrate SystemMetricsMonitor with swift-service-lifecycle to automatically collect and report system metrics for your service.
The example shows:
- How to use
SystemMetricsMonitoras aServicewithin aServiceGroup - How to integrate it with the global
MetricsSystemusingMetricsSystem.bootstrap()
Instead of using the global MetricsSystem.bootstrap(), you can inject a custom MetricsFactory directly:
let customMetrics = MyMetricsBackendImplementation()
let systemMetricsMonitor = SystemMetricsMonitor(
metricsFactory: customMetrics,
logger: logger
)This approach decouples the monitor from global state and allows you to use different metrics backends for different components.
From the swift-system-metrics package root, run:
docker-compose -f Examples/ServiceIntegration/docker-compose.yaml up --buildThe command builds and runs 2 containers: grafana and systemmetricsmonitor.
This example includes a preconfigured Grafana dashboard for the collected metrics.
Once the example is running, access Grafana at http://localhost:3000 and navigate to the "Process System Metrics" dashboard.
The dashboard provides four visualizations that map to the metrics collected by SystemMetricsMonitor:
-
Service Uptime Timeline based on the reported
process_start_time_seconds. -
CPU Usage % calculated as
rate(process_cpu_seconds_total). -
Resident Memory (
process_resident_memory_bytes) and Virtual Memory (process_virtual_memory_bytes) consumption. -
Open File Descriptors (
process_open_fds) and Max File Descriptors (process_max_fds)