From 62a1fdbbe460603a7cf0378ef8ac0c04c0040d74 Mon Sep 17 00:00:00 2001 From: oleksii_khanin Date: Mon, 14 Feb 2022 15:36:35 +0200 Subject: [PATCH 1/2] Fix metrics performance issue #15 Details see https://github.com/Mirantis/cri-dockerd/issues/15 --- src/core/stats.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/core/stats.go b/src/core/stats.go index 0d4ecb13e..4e8a9bc49 100644 --- a/src/core/stats.go +++ b/src/core/stats.go @@ -18,6 +18,7 @@ package core import ( "context" + "sync" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" ) @@ -53,16 +54,21 @@ func (ds *dockerService) ListContainerStats( return nil, err } - var stats []*runtimeapi.ContainerStats + var mtx sync.Mutex + var wg sync.WaitGroup + var stats = make([]*runtimeapi.ContainerStats, 0, len(listResp.Containers)) for _, container := range listResp.Containers { - containerStats, err := ds.getContainerStats(container.Id) - if err != nil { - return nil, err - } - if containerStats != nil { - stats = append(stats, containerStats) - } + go func() { + wg.Add(1) + defer wg.Done() + if containerStats, err := ds.getContainerStats(container.Id); err == nil && containerStats != nil { + mtx.Lock() + stats = append(stats, containerStats) + mtx.Unlock() + } + }() } + wg.Wait() return &runtimeapi.ListContainerStatsResponse{Stats: stats}, nil } From 10824fd2ff88548e8451ad07075ef4d0014f76b5 Mon Sep 17 00:00:00 2001 From: oleksii_khanin Date: Tue, 15 Feb 2022 16:06:10 +0200 Subject: [PATCH 2/2] Add error handling when get error from some container --- src/core/stats.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/stats.go b/src/core/stats.go index 4e8a9bc49..7a5c3e957 100644 --- a/src/core/stats.go +++ b/src/core/stats.go @@ -18,6 +18,7 @@ package core import ( "context" + "github.com/sirupsen/logrus" "sync" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" @@ -65,6 +66,8 @@ func (ds *dockerService) ListContainerStats( mtx.Lock() stats = append(stats, containerStats) mtx.Unlock() + } else if err != nil { + logrus.Error(err, "Failed to get stats from container "+container.Id) } }() }