Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix metrics performance issue #15 #38

Merged
merged 2 commits into from May 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}