Skip to content

Commit 39f93dc

Browse files
committed
crio: only collect required metrics for infra container
as we'll only use the network metrics from it Signed-off-by: Peter Hunt <[email protected]>
1 parent c15f44e commit 39f93dc

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

container/crio/handler.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -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

container/crio/handler_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestHandler(t *testing.T) {
5454
nil,
5555
false,
5656
nil,
57-
nil,
57+
container.AllMetrics,
5858

5959
true,
6060
"no client returned",
@@ -70,7 +70,7 @@ func TestHandler(t *testing.T) {
7070
nil,
7171
false,
7272
nil,
73-
nil,
73+
container.AllMetrics,
7474

7575
true,
7676
"no container with id 81e5c2990803c383229c9680ce964738d5e566d97f5bd436ac34808d2ec75d5f",
@@ -90,7 +90,7 @@ func TestHandler(t *testing.T) {
9090
nil,
9191
false,
9292
nil,
93-
nil,
93+
container.AllMetrics,
9494

9595
false,
9696
"",

0 commit comments

Comments
 (0)