stats

package
v0.9.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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
)
View Source
const RecentSlots = 60

RecentSlots is how many of the latest 30-second samples Recent* hold: half an hour.

Variables

View Source
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)
	// Logger supersedes Logf, logging at levels and with fields.
	Logger *log.Logger
}

Options configure a Sampler.

type Probe

type Probe interface {
	Snapshot(ctx context.Context) (map[int]Group, error)
}

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.

func ProbeFor

func ProbeFor(goos string) Probe

ProbeFor returns the Probe for a GOOS value. Every implementation is built on every platform, as internal/autostart does it, so each can be tested from either.

type Sampler

type Sampler struct {
	// contains filtered or unexported fields
}

Sampler measures every service on a timer and keeps the history.

func New

func New(opts Options) *Sampler

New builds a Sampler. Run starts it.

func (*Sampler) All added in v0.8.0

func (s *Sampler) All() map[string]Usage

All is the latest snapshot of every service, keyed by name. The map is the caller's; the Usages in it share their slices with other readers.

func (*Sampler) Run

func (s *Sampler) Run(ctx context.Context)

Run samples every 30 seconds, and walks storage every 10 minutes, until ctx ends. It returns only once both have stopped, so the caller can close what List reads from after it.

func (*Sampler) Usage

func (s *Sampler) Usage(name string) (Usage, bool)

Usage is the latest snapshot for a service, and false for one the sampler hasn't seen.

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

	// Each Recent series is the last RecentSlots 30-second samples as taken,
	// oldest first, ending with the current one, with gaps as in the
	// quarter-hour series.
	RecentCPU, RecentRSS, RecentIn, RecentOut []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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL