github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/docker/container_stats.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 package docker 5 6 import ( 7 "fmt" 8 "strings" 9 "time" 10 ) 11 12 // ContainerNetworkStats represents networking stats for a container. 13 // 14 // See https://docs.docker.com/config/containers/runmetrics/#network-metrics 15 type ContainerNetworkStats struct { 16 RxBytes int `json:"rx_bytes"` 17 RxDropped int `json:"rx_dropped"` 18 RxErrors int `json:"rx_errors"` 19 RxPackets int `json:"rx_packets"` 20 TxBytes int `json:"tx_bytes"` 21 TxDropped int `json:"tx_dropped"` 22 TxErrors int `json:"tx_errors"` 23 TxPackets int `json:"tx_packets"` 24 } 25 26 // MemoryStats represents the cgroups memory stats. 27 // 28 // See https://docs.docker.com/config/containers/runmetrics/#metrics-from-cgroups-memory-cpu-block-io 29 type MemoryStats struct { 30 ActiveAnon int `json:"active_anon"` 31 ActiveFile int `json:"active_file"` 32 InactiveAnon int `json:"inactive_anon"` 33 InactiveFile int `json:"inactive_file"` 34 TotalRss int `json:"total_rss"` 35 TotalCache int `json:"total_cache"` 36 } 37 38 // ContainerMemoryStats represents the memory usage stats for a container. 39 // 40 // See https://docs.docker.com/config/containers/runmetrics/#metrics-from-cgroups-memory-cpu-block-io 41 type ContainerMemoryStats struct { 42 Stats MemoryStats `json:"stats"` 43 MaxUsage int `json:"max_usage"` 44 Usage int `json:"usage"` 45 Limit int `json:"limit"` 46 } 47 48 type blockIOOp uint8 49 50 func (biop *blockIOOp) UnmarshalJSON(data []byte) error { 51 switch strings.ToLower(string(data)) { 52 case `"read"`: 53 *biop = BlockIOReadOp 54 case `"write"`: 55 *biop = BlockIOWriteOp 56 default: 57 return fmt.Errorf("unexpected block i/o op %s", string(data)) 58 } 59 60 return nil 61 } 62 63 func (biop blockIOOp) MarshalJSON() ([]byte, error) { 64 switch biop { 65 case BlockIOReadOp: 66 return []byte(`"read"`), nil 67 case BlockIOWriteOp: 68 return []byte(`"write"`), nil 69 default: 70 return []byte(`"unknown"`), nil 71 } 72 } 73 74 // Valid block I/O operations 75 const ( 76 BlockIOReadOp = iota + 1 77 BlockIOWriteOp 78 ) 79 80 // BlockIOOpStats represents cgroups stats for a block I/O operation type. The zero-value does not represent 81 // a valid value until the Operation is specified. 82 type BlockIOOpStats struct { 83 Operation blockIOOp `json:"op"` 84 Value int `json:"value"` 85 } 86 87 // ContainerBlockIOStats represents the block I/O usage stats for a container. 88 // 89 // See https://docs.docker.com/config/containers/runmetrics/#metrics-from-cgroups-memory-cpu-block-io 90 type ContainerBlockIOStats struct { 91 ServiceBytes []BlockIOOpStats `json:"io_service_bytes_recursive"` 92 } 93 94 // CPUUsageStats represents the cgroups CPU usage stats. 95 // 96 // See https://docs.docker.com/config/containers/runmetrics/#metrics-from-cgroups-memory-cpu-block-io 97 type CPUUsageStats struct { 98 Total int `json:"total_usage"` 99 Kernel int `json:"usage_in_kernelmode"` 100 User int `json:"usage_in_usermode"` 101 } 102 103 // CPUThrottlingStats represents the cgroupd CPU throttling stats. 104 // 105 // See https://docs.docker.com/config/containers/runmetrics/#metrics-from-cgroups-memory-cpu-block-io 106 type CPUThrottlingStats struct { 107 Periods int `json:"periods"` 108 Time int `json:"throttled_time"` 109 } 110 111 // ContainerCPUStats represents the CPU usage stats for a container. 112 // 113 // See https://docs.docker.com/config/containers/runmetrics/#metrics-from-cgroups-memory-cpu-block-io 114 type ContainerCPUStats struct { 115 Usage CPUUsageStats `json:"cpu_usage"` 116 Throttling CPUThrottlingStats `json:"throttling_data"` 117 System int `json:"system_cpu_usage"` 118 OnlineCPUs int `json:"online_cpus"` 119 } 120 121 // ContainerStats represents the container resource usage stats as returned by Docker Engine. 122 // 123 // See https://docs.docker.com/engine/api/v1.30/#operation/ContainerExport 124 type ContainerStats struct { 125 ReadAt time.Time `json:"read"` 126 Networks map[string]ContainerNetworkStats `json:"networks"` 127 Memory ContainerMemoryStats `json:"memory_stats"` 128 BlockIO ContainerBlockIOStats `json:"blkio_stats"` 129 CPU ContainerCPUStats `json:"cpu_stats"` 130 }