56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package exporters
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
"time"
|
|
|
|
"git.govekar.net/LovroG05/nvllama-watchdog/core"
|
|
)
|
|
|
|
type DevExporter struct {
|
|
jsonFile string // optional: path to dump JSON, empty = stdout only
|
|
}
|
|
|
|
func NewDevExporter(jsonFile string) *DevExporter {
|
|
return &DevExporter{jsonFile: jsonFile}
|
|
}
|
|
|
|
func (e *DevExporter) Name() string { return "dev" }
|
|
|
|
func (e *DevExporter) Write(_ context.Context, ms []core.Metric) error {
|
|
// Sort for stable output
|
|
sort.Slice(ms, func(i, j int) bool {
|
|
if ms[i].Name != ms[j].Name {
|
|
return ms[i].Name < ms[j].Name
|
|
}
|
|
return fmt.Sprint(ms[i].Labels) < fmt.Sprint(ms[j].Labels)
|
|
})
|
|
|
|
// Human-readable to stdout
|
|
fmt.Printf("\n── %s ── (%d metrics)\n", time.Now().Format("15:04:05"), len(ms))
|
|
for _, m := range ms {
|
|
fmt.Printf(" %-40s %12.4f %v\n", m.Name, m.Value, m.Labels)
|
|
}
|
|
|
|
// Optional JSON dump (overwrite each cycle, for `jq` inspection)
|
|
if e.jsonFile != "" {
|
|
payload := struct {
|
|
Timestamp string `json:"timestamp"`
|
|
Count int `json:"count"`
|
|
Metrics []core.Metric `json:"metrics"`
|
|
}{
|
|
Timestamp: time.Now().UTC().Format(time.RFC3339),
|
|
Count: len(ms),
|
|
Metrics: ms,
|
|
}
|
|
data, _ := json.MarshalIndent(payload, "", " ")
|
|
_ = os.WriteFile(e.jsonFile, data, 0644)
|
|
}
|
|
|
|
return nil
|
|
}
|