-
Notifications
You must be signed in to change notification settings - Fork 32
/
delaystats.go
53 lines (44 loc) · 1.46 KB
/
delaystats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package procstats
import (
"os"
"time"
"github.com/segmentio/stats"
)
// DelayMetrics is a metric collector that reports resource delays on processes.
type DelayMetrics struct {
engine *stats.Engine
pid int
CPUDelay time.Duration `metric:"cpu.delay.seconds" type:"counter"`
BlockIODelay time.Duration `metric:"blockio.delay.seconds" type:"counter"`
SwapInDelay time.Duration `metric:"swapin.delay.seconds" type:"counter"`
FreePagesDelay time.Duration `metric:"freepages.delay.seconds" type:"counter"`
}
// NewDelayStats collects metrics on the current process and reports them to
// the default stats engine.
func NewDelayMetrics() *DelayMetrics {
return NewDelayMetricsWith(stats.DefaultEngine, os.Getpid())
}
// NewDelayStatsWith collects metrics on the process identified by pid and
// reports them to eng.
func NewDelayMetricsWith(eng *stats.Engine, pid int) *DelayMetrics {
return &DelayMetrics{engine: eng, pid: pid}
}
// Collect satisfies the Collector interface.
func (d *DelayMetrics) Collect() {
if info, err := CollectDelayInfo(d.pid); err == nil {
d.CPUDelay = info.CPUDelay
d.BlockIODelay = info.BlockIODelay
d.SwapInDelay = info.SwapInDelay
d.FreePagesDelay = info.FreePagesDelay
d.engine.Report(d)
}
}
type DelayInfo struct {
CPUDelay time.Duration
BlockIODelay time.Duration
SwapInDelay time.Duration
FreePagesDelay time.Duration
}
func CollectDelayInfo(pid int) (DelayInfo, error) {
return collectDelayInfo(pid)
}