in Sources/SystemMetrics/SystemMetrics.swift [56:79]
init(config: SystemMetrics.Configuration) {
self.timeInterval = config.interval
self.dataProvider = config.dataProvider
self.labels = config.labels
self.timer = DispatchSource.makeTimerSource(queue: self.queue)
self.timer.setEventHandler(handler: DispatchWorkItem(block: { [weak self] in
guard let self = self, let metrics = self.dataProvider() else { return }
Gauge(label: self.labels.label(for: \.virtualMemoryBytes)).record(metrics.virtualMemoryBytes)
Gauge(label: self.labels.label(for: \.residentMemoryBytes)).record(metrics.residentMemoryBytes)
Gauge(label: self.labels.label(for: \.startTimeSeconds)).record(metrics.startTimeSeconds)
Gauge(label: self.labels.label(for: \.cpuSecondsTotal)).record(metrics.cpuSeconds)
Gauge(label: self.labels.label(for: \.maxFileDescriptors)).record(metrics.maxFileDescriptors)
Gauge(label: self.labels.label(for: \.openFileDescriptors)).record(metrics.openFileDescriptors)
}))
self.timer.schedule(deadline: .now() + self.timeInterval, repeating: self.timeInterval)
if #available(OSX 10.12, *) {
self.timer.activate()
} else {
self.timer.resume()
}
}