@@ -148,13 +148,22 @@ func newCrioContainerHandler(
148148 Namespace : CrioNamespace ,
149149 }
150150
151- // Find out if we need network metrics reported for this container.
152- // Containers that don't have their own network -- this includes
153- // containers running in Kubernetes pods that use the network of the
154- // infrastructure container -- does not need their stats to be
155- // reported. This stops metrics being reported multiple times for each
156- // container in a pod.
157- metrics := common .RemoveNetMetrics (includedMetrics , cInfo .Labels ["io.kubernetes.container.name" ] != "POD" )
151+ nonNetworkMetrics := common .RemoveNetMetrics (includedMetrics , true )
152+ // If it's a non-infra container, then we can exclude network metrics,
153+ // because in kubernetes a container always shares the network namespace
154+ // with the infra container.
155+ metrics := nonNetworkMetrics
156+ // If this is the infra container cgroup, then we only need to collect network metrics
157+ // as the rest will be discarded.
158+ if cInfo .Labels ["io.kubernetes.container.name" ] == "POD" {
159+ // Only include the non-non-network metrics. This way, we can tell if there are no network metrics
160+ metrics = includedMetrics .Difference (nonNetworkMetrics )
161+ }
162+
163+ // No metrics are being collected, no need to create a handler.
164+ if len (metrics ) == 0 {
165+ return nil , nil
166+ }
158167
159168 libcontainerHandler := containerlibcontainer .NewHandler (cgroupManager , rootFs , cInfo .Pid , metrics )
160169
0 commit comments