feat(collectors) basic implementation of boring collectors; sender via http(s) to hard coded api route
This commit is contained in:
74
internal/agent/agent.go
Normal file
74
internal/agent/agent.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"bugs.watch/agent/internal/aggregate"
|
||||
"bugs.watch/agent/internal/collectors"
|
||||
"bugs.watch/agent/internal/collectors/cpu"
|
||||
"bugs.watch/agent/internal/collectors/disk"
|
||||
"bugs.watch/agent/internal/collectors/memory"
|
||||
"bugs.watch/agent/internal/collectors/uptime"
|
||||
"bugs.watch/agent/internal/sender"
|
||||
)
|
||||
|
||||
const (
|
||||
sampleInterval = 5 * time.Second
|
||||
windowDuration = 60 * time.Second
|
||||
)
|
||||
|
||||
func Run(ctx context.Context) {
|
||||
log.Println("bugswatch agent starting")
|
||||
|
||||
// Aggregation window
|
||||
window := aggregate.NewWindow()
|
||||
|
||||
// Register collectors
|
||||
cols := []collectors.Collector{
|
||||
uptime.New(),
|
||||
cpu.New(),
|
||||
memory.New(),
|
||||
disk.New(),
|
||||
}
|
||||
|
||||
// Sender (HTTP or logging)
|
||||
snd := sender.NewHTTPSender("http://172.17.0.1:5173/api/health")
|
||||
|
||||
ticker := time.NewTicker(sampleInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Println("bugswatch agent shutting down")
|
||||
|
||||
// Best-effort final flush with bounded timeout
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
emitSnapshot(shutdownCtx, snd, window)
|
||||
return
|
||||
|
||||
case <-ticker.C:
|
||||
for _, c := range cols {
|
||||
c.Collect(window)
|
||||
}
|
||||
|
||||
if window.Ready(windowDuration) {
|
||||
emitSnapshot(ctx, snd, window)
|
||||
window = aggregate.NewWindow()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func emitSnapshot(
|
||||
ctx context.Context,
|
||||
snd sender.Sender,
|
||||
window *aggregate.Window,
|
||||
) {
|
||||
snap := window.Snapshot()
|
||||
_ = snd.Send(ctx, snap)
|
||||
}
|
||||
Reference in New Issue
Block a user