Documentation
¶
Overview ¶
Package stats keeps a day of usage history for each service: CPU, memory, storage and the traffic the gateway carried for it.
It is a client of the supervisor like any other, reading the service list through its public List on a timer, and it answers reads from a snapshot of its own, so drawing a page never waits on the reconcile loop and the loop never waits on a measurement.
Index ¶
Constants ¶
const ( // Buckets is how many points each series in a Usage has: a day in // quarter hours, few enough to draw as a sparkline. Buckets = 96 )
Variables ¶
var ErrUnsupported = errors.New("stats: no process probe for this platform")
ErrUnsupported is a Probe's answer on a platform it can't read. Everything else the sampler measures still works.
Functions ¶
This section is empty.
Types ¶
type Bytes ¶
type Bytes struct {
In, Out uint64
// Source names the counter the reading came from. When it changes (a
// service moved to a port that another one used before) the two readings
// count different things, and no difference is taken between them.
Source int
}
Bytes is traffic to (In) and from (Out) a service, counted since the counter started. The sampler works from the difference between readings, and a reading lower than the last is taken as a counter that started over.
type Group ¶
type Group struct {
// CPUTime is the user and system time of the group's live processes. A
// process that exits takes its time with it, so this can go down.
CPUTime time.Duration
RSS uint64 // resident bytes, summed over the group
// Leader is whether the process whose pid is the group's id is still
// among them. Without it the pid may have been reused, or the service
// has exited and left children behind.
Leader bool
}
Group is what one process group is using. The runtime starts every service as the leader of its own group, so a group is a service and whatever it has spawned. A child that moves itself into another group or session (a daemonizing helper, say) is no longer counted: there is no cheap, cgo-free way to follow it, and the supervisor can't stop it either.
type Lister ¶
type Lister interface {
List(ctx context.Context) ([]supervisor.ServiceStatus, error)
}
Lister is what the sampler needs from the supervisor.
type Options ¶
type Options struct {
Services Lister
// Probe reads CPU and memory; nil picks the one for this platform.
Probe Probe
// Traffic, when set, returns each service's cumulative gateway traffic.
Traffic func() map[string]Bytes
Now func() time.Time
Logf func(format string, v ...any)
}
Options configure a Sampler.
type Probe ¶
Probe reads the CPU and memory of every process group on the machine at once, keyed by process group id. One snapshot per tick costs the same however many services there are.
type Sampler ¶
type Sampler struct {
// contains filtered or unexported fields
}
Sampler measures every service on a timer and keeps the history.
type Usage ¶
type Usage struct {
// Up is whether the service's process is running.
Up bool
// Measured is whether CPU and RSS were read: false while the service is
// down, and always on a platform the probe can't read.
Measured bool
// CPU is in cores, averaged over the last sample interval; a page shows
// it as a percent of one core, as top does. NaN until there have been
// two readings of the same process to take a difference between.
CPU float64
RSS uint64
// Storage is the size of the service's storage directory at StorageAt.
// It is measured whether or not the service runs. StoragePartial means
// the walk stopped short, so the size is a lower bound.
Storage uint64
StorageAt time.Time
StoragePartial bool
// In24 and Out24 are the bytes the gateway carried to and from the
// service over the last day.
In24, Out24 uint64
// Each series is Buckets quarter hours, oldest first, ending with the
// current one. NaN is no data: the service was down, or the daemon
// wasn't running. CPU, RSS and Storage hold each quarter's highest
// reading; In and Out its total.
CPUSeries, RSSSeries, StorageSeries, InSeries, OutSeries []float64
// Since is the oldest sample the series draw on, so a page can say how
// much of the day it knows about.
Since time.Time
}
Usage is what a service is using now and over the last day. It is built once per sample and shared between readers: don't modify its slices.