Documentation
¶
Overview ¶
Package container collects container metrics and events from the Docker Engine API (unix socket or Windows named pipe) and, on Linux, from cgroup v2 files for containerd and podman.
Index ¶
- Constants
- func DefaultHost() string
- func HasLabels(have, want map[string]string) bool
- func LabelFilter(labels map[string]string) []string
- func ParseCPUStat(s string) (uint64, bool)
- func ParseMemoryStat(s, key string) uint64
- func PromName(name string) string
- func Run(ctx context.Context, raw json.RawMessage, out sink.Sink) error
- type CG
- type Config
- type Docker
- type Identity
- type Info
- type LabelTarget
- type Poller
- type ServiceMap
- type Stats
Constants ¶
const DefaultCgroupRoot = "/sys/fs/cgroup"
DefaultCgroupRoot is where cgroup v2 is mounted.
Variables ¶
This section is empty.
Functions ¶
func DefaultHost ¶
func DefaultHost() string
DefaultHost is the engine endpoint when the local config names none.
func LabelFilter ¶
LabelFilter is the Engine API `label` filter for labels: sorted "k=v" values.
func ParseCPUStat ¶
ParseCPUStat returns usage_usec of a cgroup v2 cpu.stat file, in nanoseconds.
func ParseMemoryStat ¶
ParseMemoryStat returns the value of key in a cgroup v2 flat-keyed file such as memory.stat.
Types ¶
type CG ¶
CG is one container's cgroup reading.
func ScanCgroups ¶
ScanCgroups walks root (at most 8 levels, at most maxContainers hits) for container scopes and reads their CPU time and working-set memory (memory.current minus inactive_file).
type Config ¶
type Config struct {
Enabled *bool `json:"enabled"`
Interval string `json:"interval"` // default 30s, at least 5s
DockerHost string `json:"docker_host"` // default: the platform's engine socket
CgroupRoot string `json:"cgroup_root"` // default /sys/fs/cgroup on Linux; "off" disables
Events *bool `json:"events"` // default true
// Labels limit the engine's containers and events to those carrying every label (sent as a
// server-side filter). Set, they also turn the cgroup scan off: it cannot see labels.
Labels map[string]string `json:"labels"`
// RequireLabels makes empty Labels collect nothing instead of every container (shared hosts).
RequireLabels bool `json:"require_labels"`
// Services maps a compose service name to the service, environment and version its
// containers report when they carry no label or OTEL env of their own. Local file only.
Services map[string]ServiceMap `json:"services"`
}
Config is the container module's config. Only Enabled and Interval can be set remotely; DockerHost and CgroupRoot are local-file only.
type Docker ¶
Docker is a minimal Engine API client. The endpoint comes only from the agent's local file. Labels, when set, are sent as a server-side filter on list and events, so the engine never returns containers without every one of them (and List re-checks them).
func (*Docker) Events ¶
Events streams container events as log records until ctx ends or the stream breaks.
type Identity ¶
type Identity struct{ Service, Environment, Version string }
Identity is the application a container runs, as the product shows it.
func ResolveIdentity ¶
func ResolveIdentity(env []string, labels map[string]string, byCompose map[string]ServiceMap, name string) Identity
ResolveIdentity picks service.name, per W62, from the first non-empty of: the container's OTEL_SERVICE_NAME / OTEL_RESOURCE_ATTRIBUTES service.name env, a com.watchtower.service label, the agent.yaml compose-service map, the compose service name, then the container name. The environment and version resolve likewise (env, label, map). name is the last-resort service so a scraped container never inherits the agent's own service.name (wt-agent); pass "" for none.
type Info ¶
type Info struct {
ID string
Name string
Image string
// State is docker's state word (running, paused, restarting, exited, ...).
State string
// Labels are the container labels (watchtower.dev/* drive autodiscovery, see labels.go).
Labels map[string]string
// IP is the lowest network address; Ports the distinct private ports, ascending.
IP string
Ports []int
}
Info is one listed container.
type LabelTarget ¶
LabelTarget is a container that opted in to scraping with the same keys as the pod annotations (watchtower.dev/scrape, port, path, integration), set as Docker labels.
func LabelTargets ¶
func LabelTargets(cs []Info) []LabelTarget
LabelTargets returns the running containers whose labels opt in. Labels are untrusted input from whoever can start containers, so they only choose a port and path on the container's own IP.
type Poller ¶
type Poller struct {
// Services maps compose service names to identities (Config.Services).
Services map[string]ServiceMap
// contains filtered or unexported fields
}
Poller turns successive samples into cores-used gauges.
func (*Poller) Collect ¶
Collect samples every listed container and writes container.cpu.usage (cores in use, from the change since the last pass; none on the first) and container.memory.usage (bytes).
func (*Poller) CollectCgroups ¶
CollectCgroups reports containers the Engine API did not list (known holds ids already reported) and adds the ids it reports to known. Without a name or image, the name is the short id.
type ServiceMap ¶
type ServiceMap struct {
Service string `json:"service"`
Environment string `json:"environment"`
Version string `json:"version"`
}
ServiceMap names the service a compose service stands for (agent.yaml, modules.container.services).