dockerstats

package
v0.4.89 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package dockerstats turns what Docker reports about a container into the series the stats store keeps. This file is the pure part: no clock, no I/O.

Index

Constants

View Source
const MaxGapFactor = 3

MaxGapFactor: a previous snapshot older than this many sampling intervals is not a baseline (Docker or the service was away).

Variables

View Source
var (
	ErrUnknownItem = errors.New("unknown item")
	ErrUnavailable = errors.New("stats unavailable")
)

Functions

func HealthOf

func HealthOf(status string) (float64, bool)

HealthOf reads the health probe out of Docker's status text: 1 healthy, 0 unhealthy, ok=false when there is no probe or it is still starting.

func StateOf

func StateOf(c container.Summary) string

StateOf maps Docker's state to the contract's states. An exit with a non-zero code counts as a crash.

Types

type Allocation

type Allocation struct {
	CPULimit float64 // cores; 0 = none declared
	MemLimit uint64  // bytes; 0 = none declared
}

Allocation is what the container was declared to be allowed to use.

func AllocationOf

func AllocationOf(h *container.HostConfig) Allocation

AllocationOf reads the declared limits of a container.

func (Allocation) CPUCores

func (a Allocation) CPUCores(s *container.StatsResponse) float64

CPUCores is the number of cores the container can use: its limit, else the machine's online cores as Docker reports them.

type Config

type Config struct {
	Dir      string // /var/lib/casaos/stats/app_management
	Source   Source
	Clock    stats.Clock
	Interval time.Duration // 5s in production
	// Emit must not block; minutes = the rule's window. Called from the sampler
	// goroutine or a route goroutine.
	Emit func(e stats.Event, who Identity, minutes int)
}

type DockerSource

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

DockerSource reads through one client shared for the life of the process.

func NewDockerSource

func NewDockerSource() (*DockerSource, error)

func (*DockerSource) Close

func (d *DockerSource) Close() error

Close releases the client; calling it again does nothing.

func (*DockerSource) Inspect

func (*DockerSource) List

func (d *DockerSource) List(ctx context.Context) ([]container.Summary, error)

func (*DockerSource) Stats

func (d *DockerSource) Stats(ctx context.Context, ids []string) map[string]*container.StatsResponse

type Identity

type Identity struct{ ID, Name, Kind, Group, App string }

Identity is the stable identity of a container: it survives re-creation. Kind: "app-container" | "container".

func Identify

func Identify(labels map[string]string, names []string) (Identity, bool)

Identify names a container from its labels and Docker names. A compose container (project and service labels, not a one-off run) is identified by app, service and number; anything else by its name. false when nothing usable is available.

func ParseID

func ParseID(id string) (Identity, bool)

ParseID rebuilds an Identity from an id this service made (items known only from disk). false for any other id.

type ItemInfo

type ItemInfo struct {
	Identity
	State       string   // running|stopped|paused|crashed|unknown
	CPUPercent  *float64 // latest user+system, nil if no sample in the last 3 intervals
	MemoryUsed  *uint64  // working set
	MemoryLimit *uint64  // declared limit only
	Alerts      int
	StartedAt   *time.Time // inspect State.StartedAt, nil when unknown (container known only from disk)
	Detail      string     // image name, at most 200 characters, "" when unknown
}

type Sampler

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

func New

func New(cfg Config) (*Sampler, error)

New opens the store and the evaluator, wires the evaluator to the store and rebuilds the alert counters of every item on disk.

func (*Sampler) Alerts

func (s *Sampler) Alerts(id string) ([]stats.Rule, []stats.Event, error)

Alerts returns the rules and the firing alerts of id.

func (*Sampler) Close

func (s *Sampler) Close() error

Close waits for a tick in flight, then closes the store; later calls return nil.

func (*Sampler) Items

func (s *Sampler) Items() ([]ItemInfo, error)

Items lists the containers Docker knows and those with history on disk.

func (*Sampler) Notes

func (s *Sampler) Notes() []string

Notes: stats_capacity during the hour after the item cap was hit.

func (*Sampler) Run

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

Run ticks now, then every Interval, until ctx ends. A slow tick delays the next one; ticks never overlap.

func (*Sampler) Series

func (s *Sampler) Series(id string, r stats.Range) (SeriesView, error)

Series returns the history of id over r with its scale and notes.

func (*Sampler) SetRules

func (s *Sampler) SetRules(id string, rules []stats.Rule) error

SetRules updates the rules sent for id; the Evaluator's *stats.RuleError on a bad value. Only an item with history takes rules: Prune drops the rules of any other within the hour. nil means the rules are in alerts.json; an error after they were applied means they are only in memory until the next good write.

func (*Sampler) Tick

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

Tick is one pass: list, inspect, measure, record, forget the gone ones.

type SeriesView

type SeriesView struct {
	Result        stats.Result // alert/* series already removed (Common's Query never returns them)
	CPUAllocation *float64     // cores
	MemoryLimit   *uint64
	Notes         []string
}

type Snapshot

type Snapshot struct {
	At time.Time
	// contains filtered or unexported fields
}

Snapshot is what a sample leaves behind for the next one: when it was read and the cumulative counters rates are computed from.

func Measure

func Measure(prev *Snapshot, s *container.StatsResponse, now time.Time, a Allocation, maxGap time.Duration) (stats.Sample, Snapshot)

Measure turns one Docker answer into a Sample. A rate needs prev to be at most maxGap older, and each counter is judged alone: one that went backwards (container recreated) drops its own series, not the others.

type Source

type Source interface {
	List(ctx context.Context) ([]container.Summary, error)
	Inspect(ctx context.Context, id string) (container.InspectResponse, error)
	// Stats measures the given containers; an id that could not be measured is absent.
	Stats(ctx context.Context, ids []string) map[string]*container.StatsResponse
}

Source is where the sampler reads the containers from.

Jump to

Keyboard shortcuts

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