-
Notifications
You must be signed in to change notification settings - Fork 943
feat: integrate agentAPI with resources monitoring logic #16438
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
9d42cad
work on new agent version
defelmnq 7b2d19e
improve function for resources monitoring
defelmnq 0124d60
add missing files
defelmnq 3661e8c
work on resources monitor tests
defelmnq a5a788e
apply fmt and lint
defelmnq 91d1515
work on dbauthz tests
defelmnq 0bc7632
work on dbauthz
defelmnq 3085041
work on rbac
defelmnq 120a37b
continue to iterate
defelmnq dd8ed40
continue to iterate
defelmnq 0a8941b
continue to iterate
defelmnq f3388b4
work on tests
defelmnq 523f6fd
improve testing
defelmnq 06adbf7
improve error messages
defelmnq c7b03d0
rework architecture of resources monitor
defelmnq 2c3d171
improve resourcesmonitor struct
defelmnq 18b65e0
improve resourcesmonitor struct
defelmnq c95b05a
change proto payload for get resources monitoring config
defelmnq c79b6cb
change proto payload for get resources monitoring config
defelmnq b28d4fa
rework fetcher and tests
defelmnq 7701624
fix tests
defelmnq 5fad903
fix tests
defelmnq b611ae5
fix tests
defelmnq 3c65b8a
fix logic
defelmnq 63c5869
improve testing fetcher and rename struct
defelmnq 2d3eeb5
lint
defelmnq e17aafc
work on dbauthz
defelmnq c5a4201
improve dbauthz for fetching
defelmnq 262a672
change dbauthz permissions
defelmnq dbca96e
finalise tests
defelmnq 3145eab
fix comments from github
defelmnq 3bec324
add collectedAt
defelmnq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
rework architecture of resources monitor
- Loading branch information
commit c7b03d03011c305a3bb7234e8d444c1190e15985
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 17 additions & 17 deletions
34
agent/resources_monitor_queue.go → agent/proto/resourcesmonitor/queue.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package resourcesmonitor | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"cdr.dev/slog" | ||
"github.com/coder/coder/v2/agent/proto" | ||
"github.com/coder/coder/v2/cli/clistat" | ||
"github.com/coder/quartz" | ||
) | ||
|
||
type monitor struct { | ||
logger slog.Logger | ||
clock quartz.Clock | ||
config *proto.GetResourcesMonitoringConfigurationResponse | ||
datapointsPusher datapointsPusher | ||
} | ||
|
||
//nolint:revive | ||
func NewResourcesMonitor(logger slog.Logger, clock quartz.Clock, config *proto.GetResourcesMonitoringConfigurationResponse, datapointsPusher datapointsPusher) *monitor { | ||
return &monitor{ | ||
logger: logger, | ||
clock: clock, | ||
config: config, | ||
datapointsPusher: datapointsPusher, | ||
} | ||
} | ||
|
||
type datapointsPusher interface { | ||
PushResourcesMonitoringUsage(ctx context.Context, req *proto.PushResourcesMonitoringUsageRequest) (*proto.PushResourcesMonitoringUsageResponse, error) | ||
} | ||
|
||
func (m *monitor) Start(ctx context.Context) error { | ||
if !m.config.Enabled { | ||
m.logger.Info(ctx, "resources monitoring is disabled - skipping") | ||
return nil | ||
} | ||
|
||
resourcesFetcher, err := clistat.New() | ||
if err != nil { | ||
return xerrors.Errorf("failed to create resources fetcher: %w", err) | ||
} | ||
|
||
datapointsQueue := NewQueue(int(m.config.Config.NumDatapoints)) | ||
|
||
m.clock.TickerFunc(ctx, time.Duration(m.config.Config.CollectionIntervalSeconds*int32(time.Second)), func() error { | ||
defelmnq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
memTotal, memUsed, err := m.fetchResourceMonitoredMemory(resourcesFetcher) | ||
if err != nil { | ||
m.logger.Error(ctx, "failed to fetch memory", slog.Error(err)) | ||
return nil | ||
} | ||
|
||
volumes := make([]*VolumeDatapoint, 0, len(m.config.MonitoredVolumes)) | ||
for _, volume := range m.config.MonitoredVolumes { | ||
volTotal, volUsed, err := m.fetchResourceMonitoredVolume(resourcesFetcher, volume) | ||
if err != nil { | ||
m.logger.Error(ctx, "failed to fetch volume", slog.Error(err)) | ||
continue | ||
} | ||
|
||
volumes = append(volumes, &VolumeDatapoint{ | ||
Path: volume, | ||
Total: volTotal, | ||
Used: volUsed, | ||
}) | ||
} | ||
|
||
datapointsQueue.Push(Datapoint{ | ||
Memory: &MemoryDatapoint{ | ||
Total: memTotal, | ||
Used: memUsed, | ||
}, | ||
Volumes: volumes, | ||
}) | ||
|
||
if datapointsQueue.IsFull() { | ||
_, err = m.datapointsPusher.PushResourcesMonitoringUsage(ctx, &proto.PushResourcesMonitoringUsageRequest{ | ||
Datapoints: datapointsQueue.ItemsAsProto(), | ||
}) | ||
if err != nil { | ||
m.logger.Error(ctx, "failed to push resources monitoring usage", slog.Error(err)) | ||
} | ||
} | ||
|
||
return nil | ||
}, "resources_monitor") | ||
|
||
return nil | ||
} | ||
|
||
func (m *monitor) fetchResourceMonitoredMemory(fetcher *clistat.Statter) (total int64, used int64, err error) { | ||
mem, err := fetcher.HostMemory(clistat.PrefixMebi) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
|
||
var memTotal, memUsed int64 | ||
if mem.Total == nil { | ||
return 0, 0, xerrors.New("memory total is nil - can not fetch memory") | ||
} | ||
|
||
memTotal = m.bytesToMegabytes(int64(*mem.Total)) | ||
memUsed = m.bytesToMegabytes(int64(mem.Used)) | ||
|
||
return memTotal, memUsed, nil | ||
} | ||
|
||
func (m *monitor) fetchResourceMonitoredVolume(fetcher *clistat.Statter, volume string) (total int64, used int64, err error) { | ||
vol, err := fetcher.Disk(clistat.PrefixMebi, volume) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
|
||
var volTotal, volUsed int64 | ||
if vol.Total == nil { | ||
return 0, 0, xerrors.New("volume total is nil - can not fetch volume") | ||
} | ||
|
||
volTotal = m.bytesToMegabytes(int64(*vol.Total)) | ||
volUsed = m.bytesToMegabytes(int64(vol.Used)) | ||
|
||
return volTotal, volUsed, nil | ||
} | ||
|
||
func (*monitor) bytesToMegabytes(bytes int64) int64 { | ||
return bytes / (1024 * 1024) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.