From 4a8b277057b1682769aebc47f589468bda6f7a18 Mon Sep 17 00:00:00 2001
From: "Cardy.Tang" <zuniorone@gmail.com>
Date: Fri, 19 May 2023 09:49:58 +0800
Subject: [PATCH] bugfix: add nil pointer check for cgroup v1 mem usage

Signed-off-by: Cardy.Tang <zuniorone@gmail.com>
---
 metrics/cgroups/memory.go | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/metrics/cgroups/memory.go b/metrics/cgroups/memory.go
index e90295fb3..e8e9f093c 100644
--- a/metrics/cgroups/memory.go
+++ b/metrics/cgroups/memory.go
@@ -543,7 +543,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Total,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Usage == nil {
 				return nil
 			}
 			return []value{
@@ -559,7 +559,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Usage == nil {
 				return nil
 			}
 			return []value{
@@ -575,7 +575,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Usage == nil {
 				return nil
 			}
 			return []value{
@@ -591,7 +591,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Usage == nil {
 				return nil
 			}
 			return []value{
@@ -607,7 +607,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Total,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Swap == nil {
 				return nil
 			}
 			return []value{
@@ -623,7 +623,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Swap == nil {
 				return nil
 			}
 			return []value{
@@ -639,7 +639,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Swap == nil {
 				return nil
 			}
 			return []value{
@@ -655,7 +655,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Swap == nil {
 				return nil
 			}
 			return []value{
@@ -671,7 +671,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Total,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Kernel == nil {
 				return nil
 			}
 			return []value{
@@ -687,7 +687,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Kernel == nil {
 				return nil
 			}
 			return []value{
@@ -703,7 +703,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Kernel == nil {
 				return nil
 			}
 			return []value{
@@ -719,7 +719,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.Kernel == nil {
 				return nil
 			}
 			return []value{
@@ -735,7 +735,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Total,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.KernelTCP == nil {
 				return nil
 			}
 			return []value{
@@ -751,7 +751,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.KernelTCP == nil {
 				return nil
 			}
 			return []value{
@@ -767,7 +767,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.KernelTCP == nil {
 				return nil
 			}
 			return []value{
@@ -783,7 +783,7 @@ var memoryMetrics = []*metric{
 		unit: metrics.Bytes,
 		vt:   prometheus.GaugeValue,
 		getValues: func(stats *cgroups.Metrics) []value {
-			if stats.Memory == nil {
+			if stats.Memory == nil || stats.Memory.KernelTCP == nil {
 				return nil
 			}
 			return []value{
-- 
2.33.0

