metrics

package
v0.0.0-...-dca2ff9 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const NanoToUserHZDivisor float64 = 1e9 / 100

NanoToUserHZDivisor holds the divisor to convert cpu.usage to the same unit as cpu.system (USER_HZ = 1/100) TODO: get USER_HZ from gopsutil? Needs to patch it

Variables

View Source
var (

	// ErrMissingTarget is an error set when a cgroup target is missing.
	ErrMissingTarget = errors.New("Missing cgroup target")
)

Functions

func ContainerIDForPID

func ContainerIDForPID(pid int) (string, error)

ContainerIDForPID is a lighter version of CgroupsForPids to only retrieve the container ID for origin detection. Returns container id as a string, empty if the PID is not in a container.

Matching is tested for docker on known cgroup variations, and containerd / cri-o default Kubernetes cgroups

func GetFileDescriptorLen

func GetFileDescriptorLen(pid int) (int, error)

GetFileDescriptorLen gets the number of open file descriptors for a given pid

func ReadCgroupsForPath

func ReadCgroupsForPath(pidCgroupPath, prefix string) (string, map[string]string, error)

ReadCgroupsForPath reads the cgroups from a /proc/$pid/cgroup path.

func ScrapeAllCgroups

func ScrapeAllCgroups() (map[string]*ContainerCgroup, error)

ScrapeAllCgroups returns ContainerCgroup for every container that's in a Cgroup. This version iterates on /{host/}proc to retrieve processes out of the namespace. We return as a map[containerID]Cgroup for easy look-up.

Types

type CgroupIOStat

type CgroupIOStat struct {
	ContainerID      string
	ReadBytes        uint64
	WriteBytes       uint64
	DeviceReadBytes  map[string]uint64
	DeviceWriteBytes map[string]uint64
}

CgroupIOStat store I/O statistics about a cgroup. Sums are stored in ReadBytes and WriteBytes

type CgroupMemStat

type CgroupMemStat struct {
	ContainerID             string
	Cache                   uint64
	Swap                    uint64 // See SwapPresent to make sure it's a real zero
	SwapPresent             bool
	RSS                     uint64
	RSSHuge                 uint64
	MappedFile              uint64
	Pgpgin                  uint64
	Pgpgout                 uint64
	Pgfault                 uint64
	Pgmajfault              uint64
	InactiveAnon            uint64
	ActiveAnon              uint64
	InactiveFile            uint64
	ActiveFile              uint64
	Unevictable             uint64
	HierarchicalMemoryLimit uint64
	HierarchicalMemSWLimit  uint64 // One can safely assume 0 == absent
	TotalCache              uint64
	TotalRSS                uint64
	TotalRSSHuge            uint64
	TotalMappedFile         uint64
	TotalPgpgIn             uint64
	TotalPgpgOut            uint64
	TotalPgFault            uint64
	TotalPgMajFault         uint64
	TotalInactiveAnon       uint64
	TotalActiveAnon         uint64
	TotalInactiveFile       uint64
	TotalActiveFile         uint64
	TotalUnevictable        uint64
	MemUsageInBytes         uint64
}

CgroupMemStat stores memory statistics about a cgroup.

type CgroupTimesStat

type CgroupTimesStat struct {
	ContainerID string
	System      uint64
	User        uint64
	UsageTotal  float64
	SystemUsage uint64
	Shares      uint64
}

CgroupTimesStat stores CPU times for a cgroup. Unit is userspace scheduling unit (USER_HZ, usually 1/100)

type ContainerCgroup

type ContainerCgroup struct {
	ContainerID string
	Pids        []int32
	Paths       map[string]string
	Mounts      map[string]string
}

ContainerCgroup is a structure that stores paths and mounts for a cgroup. It provides several methods for collecting stats about the cgroup using the paths and mounts metadata.

func (ContainerCgroup) CPU

CPU returns the CPU status for this cgroup instance If the cgroup file does not exist then we just log debug return nothing.

func (ContainerCgroup) CPULimit

func (c ContainerCgroup) CPULimit() (float64, error)

CPULimit would show CPU limit for this cgroup. It does so by checking the cpu period and cpu quota config if a user does this:

docker run --cpus='0.5' ubuntu:latest

we should return 50% for that container If the limits files aren't available (on older version) then we'll return the default value of 100.

func (ContainerCgroup) CPUNrThrottled

func (c ContainerCgroup) CPUNrThrottled() (uint64, error)

CPUNrThrottled returns the number of times the cgroup has been throttle/limited because of CPU quota / limit If the cgroup file does not exist then we just log debug and return 0.

func (ContainerCgroup) ContainerStartTime

func (c ContainerCgroup) ContainerStartTime() (int64, error)

ContainerStartTime gets the stat for cgroup directory and use the mtime for that dir to determine the start time for the container this should work because the cgroup dir for the container would be created only when it's started

func (ContainerCgroup) FailedMemoryCount

func (c ContainerCgroup) FailedMemoryCount() (uint64, error)

FailedMemoryCount returns the number of times this cgroup reached its memory limit, if it exists. If the file does not exist or there is no limit, then this will default to 0

func (ContainerCgroup) IO

func (c ContainerCgroup) IO() (*CgroupIOStat, error)

IO returns the disk read and write bytes stats for this cgroup. tested in DiskMappingTestSuite.TestContainerCgroupIO Format:

8:0 Read 49225728 8:0 Write 9850880 8:0 Sync 0 8:0 Async 59076608 8:0 Total 59076608 252:0 Read 49094656 252:0 Write 9850880 252:0 Sync 0 252:0 Async 58945536 252:0 Total 58945536

func (ContainerCgroup) KernelMemoryUsage

func (c ContainerCgroup) KernelMemoryUsage() (uint64, error)

KernelMemoryUsage returns the number of bytes of kernel memory used by this cgroup, if it exists. If the file does not exist or there is an error, then this will default to 0

func (ContainerCgroup) Mem

func (c ContainerCgroup) Mem() (*CgroupMemStat, error)

Mem returns the memory statistics for a Cgroup. If the cgroup file is not available then we return an empty stats file.

func (ContainerCgroup) MemLimit

func (c ContainerCgroup) MemLimit() (uint64, error)

MemLimit returns the memory limit of the cgroup, if it exists. If the file does not exist or there is no limit then this will default to 0.

func (ContainerCgroup) ParseSingleStat

func (c ContainerCgroup) ParseSingleStat(target, file string) (uint64, error)

ParseSingleStat reads and converts a single-value cgroup stat file content to uint64.

func (ContainerCgroup) SoftMemLimit

func (c ContainerCgroup) SoftMemLimit() (uint64, error)

SoftMemLimit returns the soft memory limit of the cgroup, if it exists. If the file does not exist or there is no limit then this will default to 0.

func (ContainerCgroup) ThreadCount

func (c ContainerCgroup) ThreadCount() (uint64, error)

ThreadCount returns the number of threads in the pid cgroup linked to the container. ref: https://www.kernel.org/doc/Documentation/cgroup-v1/pids.txt

Although the metric is called `pid.current`, it also tracks threads, and not only task-group-pids

func (ContainerCgroup) ThreadLimit

func (c ContainerCgroup) ThreadLimit() (uint64, error)

ThreadLimit returns the thread count limit in the pid cgroup linked to the container. ref: https://www.kernel.org/doc/Documentation/cgroup-v1/pids.txt

If `max` is found, the method returns 0 as-in "no limit"

type ContainerNetStats

type ContainerNetStats []*InterfaceNetStats

ContainerNetStats stores network statistics about a Docker container per interface

func CollectNetworkStats

func CollectNetworkStats(pid int, networks map[string]string) (ContainerNetStats, error)

CollectNetworkStats retrieves the network statistics for a given pid. The networks map allows to optionnaly map interface name to user-friendly network names. If not found in the map, the interface name is used.

func (ContainerNetStats) SumInterfaces

func (ns ContainerNetStats) SumInterfaces() *InterfaceNetStats

SumInterfaces sums stats from all interfaces into a single InterfaceNetStats

type InterfaceNetStats

type InterfaceNetStats struct {
	NetworkName string
	BytesSent   uint64
	BytesRcvd   uint64
	PacketsSent uint64
	PacketsRcvd uint64
}

InterfaceNetStats stores network statistics about a Docker network interface

type NetworkDestination

type NetworkDestination struct {
	Interface string
	Subnet    uint64
	Mask      uint64
}

NetworkDestination holds one network destination subnet and it's linked interface name

func DetectNetworkDestinations

func DetectNetworkDestinations(pid int) ([]NetworkDestination, error)

DetectNetworkDestinations lists all the networks available to a given PID and parses them in NetworkInterface objects

Jump to

Keyboard shortcuts

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