mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: improve resources_monitoring for OOM & OOD monitoring (#16241)
As requested for [this issue](https://github.com/coder/internal/issues/245) we need to have a new resource `resources_monitoring` in the agent. It needs to be parsed from the provisioner and inserted into a new db table.
This commit is contained in:
@@ -1927,6 +1927,32 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
|
||||
}
|
||||
}
|
||||
|
||||
if prAgent.ResourcesMonitoring != nil {
|
||||
if prAgent.ResourcesMonitoring.Memory != nil {
|
||||
_, err = db.InsertMemoryResourceMonitor(ctx, database.InsertMemoryResourceMonitorParams{
|
||||
AgentID: agentID,
|
||||
Enabled: prAgent.ResourcesMonitoring.Memory.Enabled,
|
||||
Threshold: prAgent.ResourcesMonitoring.Memory.Threshold,
|
||||
CreatedAt: dbtime.Now(),
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("failed to insert agent memory resource monitor into db: %w", err)
|
||||
}
|
||||
}
|
||||
for _, volume := range prAgent.ResourcesMonitoring.Volumes {
|
||||
_, err = db.InsertVolumeResourceMonitor(ctx, database.InsertVolumeResourceMonitorParams{
|
||||
AgentID: agentID,
|
||||
Path: volume.Path,
|
||||
Enabled: volume.Enabled,
|
||||
Threshold: volume.Threshold,
|
||||
CreatedAt: dbtime.Now(),
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("failed to insert agent volume resource monitor into db: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logSourceIDs := make([]uuid.UUID, 0, len(prAgent.Scripts))
|
||||
logSourceDisplayNames := make([]string, 0, len(prAgent.Scripts))
|
||||
logSourceIcons := make([]string, 0, len(prAgent.Scripts))
|
||||
|
||||
@@ -1874,6 +1874,57 @@ func TestInsertWorkspaceResource(t *testing.T) {
|
||||
// that all apps are disabled.
|
||||
require.Equal(t, []database.DisplayApp{}, agent.DisplayApps)
|
||||
})
|
||||
|
||||
t.Run("ResourcesMonitoring", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
db := dbmem.New()
|
||||
job := uuid.New()
|
||||
err := insert(db, job, &sdkproto.Resource{
|
||||
Name: "something",
|
||||
Type: "aws_instance",
|
||||
Agents: []*sdkproto.Agent{{
|
||||
DisplayApps: &sdkproto.DisplayApps{},
|
||||
ResourcesMonitoring: &sdkproto.ResourcesMonitoring{
|
||||
Memory: &sdkproto.MemoryResourceMonitor{
|
||||
Enabled: true,
|
||||
Threshold: 80,
|
||||
},
|
||||
Volumes: []*sdkproto.VolumeResourceMonitor{
|
||||
{
|
||||
Path: "/volume1",
|
||||
Enabled: true,
|
||||
Threshold: 90,
|
||||
},
|
||||
{
|
||||
Path: "/volume2",
|
||||
Enabled: true,
|
||||
Threshold: 50,
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
resources, err := db.GetWorkspaceResourcesByJobID(ctx, job)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, resources, 1)
|
||||
agents, err := db.GetWorkspaceAgentsByResourceIDs(ctx, []uuid.UUID{resources[0].ID})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, agents, 1)
|
||||
|
||||
agent := agents[0]
|
||||
memMonitor, err := db.FetchMemoryResourceMonitorsByAgentID(ctx, agent.ID)
|
||||
require.NoError(t, err)
|
||||
volMonitors, err := db.FetchVolumesResourceMonitorsByAgentID(ctx, agent.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, int32(80), memMonitor.Threshold)
|
||||
require.Len(t, volMonitors, 2)
|
||||
require.Equal(t, int32(90), volMonitors[0].Threshold)
|
||||
require.Equal(t, "/volume1", volMonitors[0].Path)
|
||||
require.Equal(t, int32(50), volMonitors[1].Threshold)
|
||||
require.Equal(t, "/volume2", volMonitors[1].Path)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNotifications(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user