Skip to content

Commit 1e28ae7

Browse files
chore: fallback to host memory limit
A container may not have a memory limit set. If there is no memory limit set, we want to fallback to using the memory available on the underlying host.
1 parent 87adb1d commit 1e28ae7

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

agent/proto/resourcesmonitor/fetcher.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,24 @@ func (f *fetcher) FetchMemory() (total int64, used int64, err error) {
2929
if f.isContainerized {
3030
mem, err = f.ContainerMemory(clistat.PrefixDefault)
3131
if err != nil {
32-
return 0, 0, xerrors.Errorf("failed to fetch memory: %w", err)
32+
return 0, 0, xerrors.Errorf("failed to fetch container memory: %w", err)
33+
}
34+
35+
// A container might not have a memory limit set. If this
36+
// happens we want to fallback to querying the host's memory
37+
// to know what the total memory is on the host.
38+
if mem.Total == nil {
39+
hostMem, err := f.HostMemory(clistat.PrefixDefault)
40+
if err != nil {
41+
return 0, 0, xerrors.Errorf("failed to host fetch memory: %w", err)
42+
}
43+
44+
mem.Total = hostMem.Total
3345
}
3446
} else {
3547
mem, err = f.HostMemory(clistat.PrefixDefault)
3648
if err != nil {
37-
return 0, 0, xerrors.Errorf("failed to fetch memory: %w", err)
49+
return 0, 0, xerrors.Errorf("failed to host fetch memory: %w", err)
3850
}
3951
}
4052

0 commit comments

Comments
 (0)