KubFly SDK for Go / api/usage/sampler
package sampler
import "kubfly.io/sdk/api/usage/sampler"
Package sampler takes usage snapshots: the io.kubfly.sdk.api.usage.sampler package of the Java SDK.
Types
type Sampler
type Sampler struct {
// contains filtered or unexported fields
}
Sampler takes usage.Usage snapshots. On Linux the host comes from /proc (stat, loadavg, meminfo) and the container from the cgroup v2 of the process (/proc/self/cgroup under /sys/fs/cgroup); elsewhere only what the Go runtime knows. The platform part always comes from the Go runtime (runtime/metrics), and the disks from statfs: the root / and the paths of KUBFLY_USAGE_DISKS. A part that cannot be read becomes unknown (NaN or -1); a snapshot never fails because of it.
A sampler keeps the previous counters to compute shares and the longest pauses over the interval, so one sampler serves one sequence of snapshots; Sample is safe for concurrent use.
func NewSampler
func NewSampler(proc string, cgroups string, linux bool, disks []string, start int64) *Sampler
NewSampler is a sampler over given roots and file systems: tests pass a prepared directory instead of /proc and /sys/fs/cgroup. Start is the start of the process, milliseconds since the epoch: the uptime is counted from it.
func System
func System(disks []string, start int64) *Sampler
System is the sampler of this machine: /proc and /sys/fs/cgroup on Linux, the Go runtime elsewhere.
func (*Sampler) Sample
func (s *Sampler) Sample() usage.Usage
Sample takes a snapshot.
func (*Sampler) Watch
func (s *Sampler) Watch(limit int64, warn func(text string))
Watch reports a pause of the garbage collector of at least limit milliseconds: the longest pause of an interval is compared with it at every snapshot (Go tells of no collection as it happens, as the JVM does) and its description goes to warn, on the goroutine of the snapshot.