feat: implement new generic parser and improve production readyness

This commit is contained in:
Patryk Hegenberg 2026-01-18 12:37:57 +01:00
parent 8364218234
commit 0830b403e0
34 changed files with 1715 additions and 2114 deletions

View file

@ -12,9 +12,8 @@ import (
"strings"
"syscall"
"time"
"tixel_watch/models"
"watch-tool/models"
"github.com/elastic/go-elasticsearch/v8"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
@ -31,24 +30,24 @@ type SystemMetricsCollector struct {
lastNetworkStats map[string]models.NetworkStat
lastDiskStats map[string]models.DiskIOStat
lastMeasureTime time.Time
hostname string
}
func NewSystemMetricsCollector(config SystemMetrics, pollInterval int) *SystemMetricsCollector {
func NewSystemMetricsCollector(config SystemMetrics, pollInterval int, hostname string) *SystemMetricsCollector {
return &SystemMetricsCollector{
config: config,
pollInterval: pollInterval,
lastNetworkStats: make(map[string]models.NetworkStat),
lastDiskStats: make(map[string]models.DiskIOStat),
lastMeasureTime: time.Now(),
hostname: hostname,
}
}
func (smc *SystemMetricsCollector) Start(ctx context.Context, es *elasticsearch.Client, baseIndex string) {
func (smc *SystemMetricsCollector) Start(ctx context.Context, storage StorageInterface, logChan chan<- models.LogMessage) {
ticker := time.NewTicker(time.Duration(smc.pollInterval) * time.Second)
defer ticker.Stop()
sender := NewElasticsearchSender(es)
for {
select {
case <-ctx.Done():
@ -61,15 +60,23 @@ func (smc *SystemMetricsCollector) Start(ctx context.Context, es *elasticsearch.
continue
}
if err := sender.SendSystemMetrics(baseIndex, metrics); err != nil {
slog.Error("error sending system metrics", "error", err)
entry := models.NewLogMessage("system_metrics", smc.hostname)
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")
}
}
}
}
func (smc *SystemMetricsCollector) collectMetrics() (models.SystemResources, error) {
result := models.NewSystemResources(hostname)
result := models.NewSystemResources(smc.hostname)
var err error