Skip to content

Commit

Permalink
Merge pull request #38 from OleksiiKhanin/metricsPerformanceIssue
Browse files Browse the repository at this point in the history
Fix metrics performance issue #15
  • Loading branch information
evol262 committed May 24, 2022
2 parents 5b999e4 + 10824fd commit 2d56669
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/core/stats.go
Expand Up @@ -18,6 +18,8 @@ package core

import (
"context"
"github.com/sirupsen/logrus"
"sync"

runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)
Expand Down Expand Up @@ -53,16 +55,23 @@ 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()
} else if err != nil {
logrus.Error(err, "Failed to get stats from container "+container.Id)
}
}()
}
wg.Wait()

return &runtimeapi.ListContainerStatsResponse{Stats: stats}, nil
}

0 comments on commit 2d56669

Please sign in to comment.