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 CgroupsAvailable() bool
- func DefaultHost() string
- func HasLabels(have, want map[string]string) bool
- func ImageIntegration(image string) string
- func LabelFilter(labels map[string]string) []string
- func MatchedSummary(cs []Info) string
- func ParseCPUStat(s string) (uint64, bool)
- func ParseIOStat(s string) (read, write uint64)
- func ParseMemoryStat(s, key string) uint64
- func PromName(name string) string
- func Run(ctx context.Context, raw json.RawMessage, out sink.Sink) error
- func RunCgroups(ctx context.Context, raw json.RawMessage, out sink.Sink) error
- type CG
- type CgroupsConfig
- type Config
- type Docker
- type Identity
- type Info
- type LabelTarget
- type Poller
- type ServiceMap
- type Stats
Constants ¶
const ComposeProjectLabel = "com.docker.compose.project"
ComposeProjectLabel is the label docker compose puts on every container of a project.
const DefaultCgroupRoot = "/sys/fs/cgroup"
DefaultCgroupRoot is where cgroup v2 is mounted.
Variables ¶
This section is empty.
Functions ¶
func CgroupsAvailable ¶ added in v0.3.4
func CgroupsAvailable() bool
CgroupsAvailable is true when no Docker socket exists but the cgroup root is readable: the agent then starts the cgroups module on its own.
func DefaultHost ¶
func DefaultHost() string
DefaultHost is the engine endpoint when the local config names none.
func ImageIntegration ¶ added in v0.3.4
ImageIntegration maps an image reference (registry/name:tag@digest) to an integration name by its last path segment, or "".
func LabelFilter ¶
LabelFilter is the Engine API `label` filter for labels: sorted "k=v" values.
func MatchedSummary ¶ added in v0.3.4
MatchedSummary lists "name(id12)" of cs, sorted, for the startup and change log.
func ParseCPUStat ¶
ParseCPUStat returns usage_usec of a cgroup v2 cpu.stat file, in nanoseconds.
func ParseIOStat ¶ added in v0.3.4
ParseIOStat sums rbytes and wbytes over the devices of a cgroup v2 io.stat file.
func ParseMemoryStat ¶
ParseMemoryStat returns the value of key in a cgroup v2 flat-keyed file such as memory.stat.
func PromName ¶
PromName converts an OTLP metric name to a PromQL-safe one: every character outside [a-zA-Z0-9_:] becomes an underscore (container.cpu.usage -> container_cpu_usage).
func Run ¶
Run polls until ctx ends. A missing engine is not an error: the module keeps trying and the cgroup scan still reports.
func RunCgroups ¶ added in v0.3.4
RunCgroups reports containers from a (read-only) cgroup v2 mount, with no Docker socket.
Types ¶
type CG ¶
type CG struct {
ID string
CPUUsageNS uint64
MemBytes uint64
// MemLimit is memory.max (0: unlimited); the rest are plain readings (Has* false when the file is absent).
MemLimit, MemAnon, MemFile uint64
IORead, IOWrite uint64
Pids uint64
HasPids, HasIO bool
}
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 CgroupsConfig ¶ added in v0.3.4
type CgroupsConfig struct {
Enabled *bool `json:"enabled"`
Interval string `json:"interval"` // default 30s, at least 5s
CgroupRoot string `json:"cgroup_root"` // default WT_AGENT_CGROUP_ROOT or /sys/fs/cgroup
}
CgroupsConfig is the `cgroups` module's config; the root is local-file only.
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 IntegrationTargets ¶ added in v0.3.4
func IntegrationTargets(cs []Info, byImage bool) []LabelTarget
IntegrationTargets returns running containers that name an integration with the label watchtower.integration (optionally watchtower.port), or, with byImage, are recognised by image. Credentials are never read from here: the agent's own file holds the base config (see modules_autodiscover.go). Containers that already opt in with watchtower.dev/scrape are left to LabelTargets.
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).