container

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 22 Imported by: 1 module, 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 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

View Source
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 HasLabels

func HasLabels(have, want map[string]string) bool

HasLabels reports whether have carries every pair of want.

func LabelFilter

func LabelFilter(labels map[string]string) []string

LabelFilter is the Engine API `label` filter for labels: sorted "k=v" values.

func ParseCPUStat

func ParseCPUStat(s string) (uint64, bool)

ParseCPUStat returns usage_usec of a cgroup v2 cpu.stat file, in nanoseconds.

func ParseMemoryStat

func ParseMemoryStat(s, key string) uint64

ParseMemoryStat returns the value of key in a cgroup v2 flat-keyed file such as memory.stat.

func PromName

func PromName(name string) string

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

func Run(ctx context.Context, raw json.RawMessage, out sink.Sink) error

Run polls until ctx ends. A missing engine is not an error: the module keeps trying and the cgroup scan still reports.

Types

type CG

type CG struct {
	ID         string
	CPUUsageNS uint64
	MemBytes   uint64
}

CG is one container's cgroup reading.

func ScanCgroups

func ScanCgroups(root string) []CG

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

type Docker struct {
	HTTP   *http.Client
	Labels map[string]string
}

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 NewDocker

func NewDocker(endpoint string) (*Docker, error)

NewDocker accepts unix:///path and npipe:////./pipe/name endpoints.

func (*Docker) Env

func (d *Docker) Env(ctx context.Context, id string) ([]string, error)

Env returns the container's environment (NAME=value) from the engine.

func (*Docker) Events

func (d *Docker) Events(ctx context.Context, since time.Time, out sink.Sink) error

Events streams container events as log records until ctx ends or the stream breaks.

func (*Docker) List

func (d *Docker) List(ctx context.Context) ([]Info, error)

List returns the running containers (at most maxContainers).

func (*Docker) Stats

func (d *Docker) Stats(ctx context.Context, id string) (Stats, error)

Stats takes one sample (no streaming, no server-side wait).

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.

func (Identity) Resource

func (i Identity) Resource() map[string]string

Resource returns the OTLP resource attributes (unprefixed) for the identity; nil when unknown. wt.internal=false overrides the agent's own marker (the agent resource carries wt.internal=true).

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

type LabelTarget struct {
	ID, Name, IP, Path, Integration string
	Port                            int
}

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

func (p *Poller) Collect(ctx context.Context, d *Docker, out sink.Sink) ([]Info, error)

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

func (p *Poller) CollectCgroups(root string, known map[string]bool, out sink.Sink)

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.

func (*Poller) Prune

func (p *Poller) Prune(seen map[string]bool)

Prune forgets containers that are no longer reported.

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).

type Stats

type Stats struct {
	CPUTotalNS uint64 // cumulative CPU time
	MemUsage   uint64 // working set: usage minus reclaimable page cache
	At         time.Time
}

Stats is the part of /containers/{id}/stats that the agent uses.

Jump to

Keyboard shortcuts

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