InitMonitors fetches resource monitors from the database and caches them. This must be called once after creating a ResourcesMonitoringAPI, the context should be the agent per-RPC connection context. If fetching fails with a real error (not sql.ErrNoRows), the connection should be torn down.
(ctx context.Context)
| 44 | // the agent per-RPC connection context. If fetching fails with a real error (not sql.ErrNoRows), the |
| 45 | // connection should be torn down. |
| 46 | func (a *ResourcesMonitoringAPI) InitMonitors(ctx context.Context) error { |
| 47 | memMon, err := a.Database.FetchMemoryResourceMonitorsByAgentID(ctx, a.AgentID) |
| 48 | if err != nil && !errors.Is(err, sql.ErrNoRows) { |
| 49 | return xerrors.Errorf("fetch memory resource monitor: %w", err) |
| 50 | } |
| 51 | // If sql.ErrNoRows, memoryMonitor stays as zero value (CreatedAt.IsZero() = true). |
| 52 | // Otherwise, store the fetched monitor. |
| 53 | if err == nil { |
| 54 | a.memoryMonitor = memMon |
| 55 | } |
| 56 | |
| 57 | volMons, err := a.Database.FetchVolumesResourceMonitorsByAgentID(ctx, a.AgentID) |
| 58 | if err != nil { |
| 59 | return xerrors.Errorf("fetch volume resource monitors: %w", err) |
| 60 | } |
| 61 | // 0 length is valid, indicating none configured, since the volume monitors in the DB can be many. |
| 62 | a.volumeMonitors = volMons |
| 63 | |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | func (a *ResourcesMonitoringAPI) GetResourcesMonitoringConfiguration(_ context.Context, _ *proto.GetResourcesMonitoringConfigurationRequest) (*proto.GetResourcesMonitoringConfigurationResponse, error) { |
| 68 | return &proto.GetResourcesMonitoringConfigurationResponse{ |