history

package
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package history stores a rolling window of container resource metrics for charting. It has two interchangeable backends selected at runtime:

  • in-memory ring (default, zero-config, lost on restart)
  • Redis (set DC_REDIS_ADDR) — survives restarts and can be shared

The backend is chosen by Config; everything else in the app talks to the Store interface only.

Index

Constants

View Source
const (
	MetricCPU      = "cpu"
	MetricMem      = "mem"      // percent
	MetricMemBytes = "membytes" // bytes
	// Network series are CUMULATIVE byte counters, not rates. A rate depends on
	// the sampling interval, so storing one would bake that interval into the
	// history and make every stored point wrong the day the interval changes.
	// Readers derive rates from consecutive points instead — and can see a
	// counter reset for what it is, which a stored rate would have hidden.
	MetricNetRx = "netrx" // cumulative bytes received
	MetricNetTx = "nettx" // cumulative bytes sent
	// Drops and errors are stored as RX+TX combined rather than as four series.
	// They are near-zero almost always, so four series would double the write
	// volume to store zeros; and what an operator acts on is "this container
	// started losing packets", not the direction. The live view keeps the split.
	MetricNetDrops  = "netdrops"  // cumulative dropped packets, rx+tx
	MetricNetErrors = "neterrors" // cumulative interface errors, rx+tx
)

Metrics that can be queried.

Variables

This section is empty.

Functions

func AllMetrics added in v1.6.0

func AllMetrics() []string

AllMetrics reports the series recorded for every sample. Exported so callers that gate on metric names can be checked against what is actually stored, rather than drifting from it silently.

Types

type Config

type Config struct {
	RedisAddr     string // empty → in-memory
	RedisPassword string
	RedisDB       int
	Retention     time.Duration // how long to keep points
}

Config selects and configures the backend.

type Point

type Point struct {
	T int64   `json:"t"` // unix millis
	V float64 `json:"v"`
}

Point is a single (timestamp-ms, value) datapoint in a series.

type Sample

type Sample struct {
	ContainerID string
	// HostID is the Docker host the container runs on. Recorded so a series can
	// be authorised against the caller's host scope: a container id is the only
	// key here, and knowing one is otherwise enough to read its CPU/memory
	// history from a host the caller was scoped away from.
	HostID     int64
	Time       time.Time
	CPU        float64 // percent
	MemPercent float64 // percent of limit
	MemBytes   float64 // bytes
	NetRx      float64 // cumulative bytes received, summed across interfaces
	NetTx      float64 // cumulative bytes sent
	NetDrops   float64 // cumulative dropped packets, rx+tx
	NetErrors  float64 // cumulative interface errors, rx+tx
}

Sample is one moment's resource reading for a container.

type Store

type Store interface {
	Record(ctx context.Context, samples []Sample) error
	Query(ctx context.Context, containerID, metric string, since time.Time) ([]Point, error)
	// HostFor reports which host a container's samples were recorded from.
	// ok=false means nothing has ever been recorded for that id, which callers
	// must treat as "unknown", not as the local daemon.
	HostFor(ctx context.Context, containerID string) (hostID int64, ok bool, err error)
	Close() error
}

Store persists and queries metric history.

func Open

func Open(ctx context.Context, cfg Config) Store

Open builds the configured store. If Redis is requested but unreachable it logs a warning and falls back to in-memory, so the app always starts.

Jump to

Keyboard shortcuts

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