Skip to content

Commit 87adb1d

Browse files
chore: query container memory if available
1 parent 77fe10e commit 87adb1d

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

agent/proto/resourcesmonitor/fetcher.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,29 @@ type Fetcher interface {
1313

1414
type fetcher struct {
1515
*clistat.Statter
16+
isContainerized bool
1617
}
1718

1819
//nolint:revive
1920
func NewFetcher(f *clistat.Statter) *fetcher {
20-
return &fetcher{
21-
f,
22-
}
21+
isContainerized, _ := f.IsContainerized()
22+
23+
return &fetcher{f, isContainerized}
2324
}
2425

2526
func (f *fetcher) FetchMemory() (total int64, used int64, err error) {
26-
mem, err := f.HostMemory(clistat.PrefixDefault)
27-
if err != nil {
28-
return 0, 0, xerrors.Errorf("failed to fetch memory: %w", err)
27+
var mem *clistat.Result
28+
29+
if f.isContainerized {
30+
mem, err = f.ContainerMemory(clistat.PrefixDefault)
31+
if err != nil {
32+
return 0, 0, xerrors.Errorf("failed to fetch memory: %w", err)
33+
}
34+
} else {
35+
mem, err = f.HostMemory(clistat.PrefixDefault)
36+
if err != nil {
37+
return 0, 0, xerrors.Errorf("failed to fetch memory: %w", err)
38+
}
2939
}
3040

3141
if mem.Total == nil {

cli/clistat/container.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ const (
1616
kubernetesDefaultServiceAccountToken = "/var/run/secrets/kubernetes.io/serviceaccount/token" //nolint:gosec
1717
)
1818

19+
func (s *Statter) IsContainerized() (ok bool, err error) {
20+
return IsContainerized(s.fs)
21+
}
22+
1923
// IsContainerized returns whether the host is containerized.
2024
// This is adapted from https://github.com/elastic/go-sysinfo/tree/main/providers/linux/container.go#L31
2125
// with modifications to support Sysbox containers.

0 commit comments

Comments
 (0)