39 lines
825 B
Go
39 lines
825 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"time"
|
|
)
|
|
|
|
func (smc *SystemMetricsCollector) StartV2(ctx context.Context, storage StorageInterface, logChan chan<- LogEntry) {
|
|
ticker := time.NewTicker(time.Duration(smc.pollInterval) * time.Second)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
slog.Info("System metrics collector stopped")
|
|
return
|
|
case <-ticker.C:
|
|
metrics, err := smc.collectMetrics()
|
|
if err != nil {
|
|
slog.Error("error collecting system metrics", "error", err)
|
|
continue
|
|
}
|
|
|
|
entry := NewLogEntry("system_metrics")
|
|
entry.Service = "system-metrics"
|
|
entry.LogLevel = "Info"
|
|
entry.SystemMetrics = metrics
|
|
|
|
select {
|
|
case logChan <- entry:
|
|
case <-ctx.Done():
|
|
return
|
|
default:
|
|
slog.Warn("Log channel is full, system metrics dropped")
|
|
}
|
|
}
|
|
}
|
|
}
|