4141 metrics .ALPHA ,
4242 "" )
4343
44+ nodeSwapUsageDesc = metrics .NewDesc ("node_swap_usage_bytes" ,
45+ "Current swap usage of the node in bytes. Reported only on non-windows systems" ,
46+ nil ,
47+ nil ,
48+ metrics .ALPHA ,
49+ "" )
50+
4451 containerCPUUsageDesc = metrics .NewDesc ("container_cpu_usage_seconds_total" ,
4552 "Cumulative cpu time consumed by the container in core-seconds" ,
4653 []string {"container" , "pod" , "namespace" },
5562 metrics .ALPHA ,
5663 "" )
5764
65+ containerSwapUsageDesc = metrics .NewDesc ("container_swap_usage_bytes" ,
66+ "Current amount of the container swap usage in bytes. Reported only on non-windows systems" ,
67+ []string {"container" , "pod" , "namespace" },
68+ nil ,
69+ metrics .ALPHA ,
70+ "" )
71+
5872 podCPUUsageDesc = metrics .NewDesc ("pod_cpu_usage_seconds_total" ,
5973 "Cumulative cpu time consumed by the pod in core-seconds" ,
6074 []string {"pod" , "namespace" },
6983 metrics .ALPHA ,
7084 "" )
7185
86+ podSwapUsageDesc = metrics .NewDesc ("pod_swap_usage_bytes" ,
87+ "Current amount of the pod swap usage in bytes. Reported only on non-windows systems" ,
88+ []string {"pod" , "namespace" },
89+ nil ,
90+ metrics .ALPHA ,
91+ "" )
92+
7293 resourceScrapeResultDesc = metrics .NewDesc ("scrape_error" ,
7394 "1 if there was an error while getting container metrics, 0 otherwise" ,
7495 nil ,
@@ -104,11 +125,14 @@ var _ metrics.StableCollector = &resourceMetricsCollector{}
104125func (rc * resourceMetricsCollector ) DescribeWithStability (ch chan <- * metrics.Desc ) {
105126 ch <- nodeCPUUsageDesc
106127 ch <- nodeMemoryUsageDesc
128+ ch <- nodeSwapUsageDesc
107129 ch <- containerStartTimeDesc
108130 ch <- containerCPUUsageDesc
109131 ch <- containerMemoryUsageDesc
132+ ch <- containerSwapUsageDesc
110133 ch <- podCPUUsageDesc
111134 ch <- podMemoryUsageDesc
135+ ch <- podSwapUsageDesc
112136 ch <- resourceScrapeResultDesc
113137}
114138
@@ -131,15 +155,18 @@ func (rc *resourceMetricsCollector) CollectWithStability(ch chan<- metrics.Metri
131155
132156 rc .collectNodeCPUMetrics (ch , statsSummary .Node )
133157 rc .collectNodeMemoryMetrics (ch , statsSummary .Node )
158+ rc .collectNodeSwapMetrics (ch , statsSummary .Node )
134159
135160 for _ , pod := range statsSummary .Pods {
136161 for _ , container := range pod .Containers {
137162 rc .collectContainerStartTime (ch , pod , container )
138163 rc .collectContainerCPUMetrics (ch , pod , container )
139164 rc .collectContainerMemoryMetrics (ch , pod , container )
165+ rc .collectContainerSwapMetrics (ch , pod , container )
140166 }
141167 rc .collectPodCPUMetrics (ch , pod )
142168 rc .collectPodMemoryMetrics (ch , pod )
169+ rc .collectPodSwapMetrics (ch , pod )
143170 }
144171}
145172
@@ -161,6 +188,15 @@ func (rc *resourceMetricsCollector) collectNodeMemoryMetrics(ch chan<- metrics.M
161188 metrics .NewLazyConstMetric (nodeMemoryUsageDesc , metrics .GaugeValue , float64 (* s .Memory .WorkingSetBytes )))
162189}
163190
191+ func (rc * resourceMetricsCollector ) collectNodeSwapMetrics (ch chan <- metrics.Metric , s summary.NodeStats ) {
192+ if s .Swap == nil || s .Swap .SwapUsageBytes == nil {
193+ return
194+ }
195+
196+ ch <- metrics .NewLazyMetricWithTimestamp (s .Memory .Time .Time ,
197+ metrics .NewLazyConstMetric (nodeSwapUsageDesc , metrics .GaugeValue , float64 (* s .Swap .SwapUsageBytes )))
198+ }
199+
164200func (rc * resourceMetricsCollector ) collectContainerStartTime (ch chan <- metrics.Metric , pod summary.PodStats , s summary.ContainerStats ) {
165201 if s .StartTime .Unix () <= 0 {
166202 return
@@ -190,6 +226,16 @@ func (rc *resourceMetricsCollector) collectContainerMemoryMetrics(ch chan<- metr
190226 float64 (* s .Memory .WorkingSetBytes ), s .Name , pod .PodRef .Name , pod .PodRef .Namespace ))
191227}
192228
229+ func (rc * resourceMetricsCollector ) collectContainerSwapMetrics (ch chan <- metrics.Metric , pod summary.PodStats , s summary.ContainerStats ) {
230+ if s .Swap == nil || s .Swap .SwapUsageBytes == nil {
231+ return
232+ }
233+
234+ ch <- metrics .NewLazyMetricWithTimestamp (s .Swap .Time .Time ,
235+ metrics .NewLazyConstMetric (containerSwapUsageDesc , metrics .GaugeValue ,
236+ float64 (* s .Swap .SwapUsageBytes ), s .Name , pod .PodRef .Name , pod .PodRef .Namespace ))
237+ }
238+
193239func (rc * resourceMetricsCollector ) collectPodCPUMetrics (ch chan <- metrics.Metric , pod summary.PodStats ) {
194240 if pod .CPU == nil || pod .CPU .UsageCoreNanoSeconds == nil {
195241 return
@@ -209,3 +255,13 @@ func (rc *resourceMetricsCollector) collectPodMemoryMetrics(ch chan<- metrics.Me
209255 metrics .NewLazyConstMetric (podMemoryUsageDesc , metrics .GaugeValue ,
210256 float64 (* pod .Memory .WorkingSetBytes ), pod .PodRef .Name , pod .PodRef .Namespace ))
211257}
258+
259+ func (rc * resourceMetricsCollector ) collectPodSwapMetrics (ch chan <- metrics.Metric , pod summary.PodStats ) {
260+ if pod .Swap == nil || pod .Swap .SwapUsageBytes == nil {
261+ return
262+ }
263+
264+ ch <- metrics .NewLazyMetricWithTimestamp (pod .Swap .Time .Time ,
265+ metrics .NewLazyConstMetric (podSwapUsageDesc , metrics .GaugeValue ,
266+ float64 (* pod .Swap .SwapUsageBytes ), pod .PodRef .Name , pod .PodRef .Namespace ))
267+ }
0 commit comments