MCPcopy Index your code
hub / github.com/coder/coder / Start

Method Start

agent/proto/resourcesmonitor/resources_monitor.go:37–93  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

35}
36
37func (m *monitor) Start(ctx context.Context) error {
38 m.clock.TickerFunc(ctx, time.Duration(m.config.Config.CollectionIntervalSeconds)*time.Second, func() error {
39 datapoint := Datapoint{
40 CollectedAt: m.clock.Now(),
41 Volumes: make([]*VolumeDatapoint, 0, len(m.config.Volumes)),
42 }
43
44 if m.config.Memory != nil && m.config.Memory.Enabled {
45 memTotal, memUsed, err := m.resourcesFetcher.FetchMemory()
46 if err != nil {
47 m.logger.Error(ctx, "failed to fetch memory", slog.Error(err))
48 } else {
49 datapoint.Memory = &MemoryDatapoint{
50 Total: memTotal,
51 Used: memUsed,
52 }
53 }
54 }
55
56 for _, volume := range m.config.Volumes {
57 if !volume.Enabled {
58 continue
59 }
60
61 volTotal, volUsed, err := m.resourcesFetcher.FetchVolume(volume.Path)
62 if err != nil {
63 m.logger.Error(ctx, "failed to fetch volume", slog.Error(err))
64 continue
65 }
66
67 datapoint.Volumes = append(datapoint.Volumes, &VolumeDatapoint{
68 Path: volume.Path,
69 Total: volTotal,
70 Used: volUsed,
71 })
72 }
73
74 m.queue.Push(datapoint)
75
76 if m.queue.IsFull() {
77 _, err := m.datapointsPusher.PushResourcesMonitoringUsage(ctx, &proto.PushResourcesMonitoringUsageRequest{
78 Datapoints: m.queue.ItemsAsProto(),
79 })
80 if err != nil {
81 // We don't want to stop the monitoring if we fail to push the datapoints
82 // to the server. We just log the error and continue.
83 // The queue will anyway remove the oldest datapoint and add the new one.
84 m.logger.Error(ctx, "failed to push resources monitoring usage", slog.Error(err))
85 return nil
86 }
87 }
88
89 return nil
90 }, "resources_monitor")
91
92 return nil
93}

Callers

nothing calls this directly

Calls 8

DurationMethod · 0.80
IsFullMethod · 0.80
ItemsAsProtoMethod · 0.80
FetchMemoryMethod · 0.65
FetchVolumeMethod · 0.65
ErrorMethod · 0.45
PushMethod · 0.45

Tested by

no test coverage detected